Skip to content

Commit 88a3718

Browse files
authored
Improve cubejs (#1391)
1 parent 7e02326 commit 88a3718

20 files changed

+277
-975
lines changed

backend/src/bin/job-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ for (const job of jobs) {
1010
async () => {
1111
log.info({ job: job.name }, 'Triggering job.')
1212
try {
13-
await job.onTrigger()
13+
await job.onTrigger(log)
1414
} catch (err) {
1515
log.error(err, { job: job.name }, 'Error while executing a job!')
1616
}

backend/src/bin/jobs/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import weeklyAnalyticsEmailsCoordinator from './weeklyAnalyticsEmailsCoordinator
44
import memberScoreCoordinator from './memberScoreCoordinator'
55
import checkSqsQueues from './checkSqsQueues'
66
import refreshMaterializedViews from './refreshMaterializedViews'
7+
import refreshMaterializedViewsForCube from './refreshMaterializedViewsForCube'
78
import downgradeExpiredPlans from './downgradeExpiredPlans'
89
import eagleEyeEmailDigestTicks from './eagleEyeEmailDigestTicks'
910
import integrationDataChecker from './integrationDataChecker'
@@ -21,6 +22,7 @@ const jobs: CrowdJob[] = [
2122
memberScoreCoordinator,
2223
checkSqsQueues,
2324
refreshMaterializedViews,
25+
refreshMaterializedViewsForCube,
2426
downgradeExpiredPlans,
2527
eagleEyeEmailDigestTicks,
2628
integrationDataChecker,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Logger, logExecutionTimeV2 } from '@crowd/logging'
2+
import SequelizeRepository from '../../database/repositories/sequelizeRepository'
3+
import { CrowdJob } from '../../types/jobTypes'
4+
5+
let processing = false
6+
7+
const job: CrowdJob = {
8+
name: 'Refresh Materialized View For Cube',
9+
cronTime: '1,31 * * * *',
10+
onTrigger: async (log: Logger) => {
11+
if (!processing) {
12+
processing = true
13+
} else {
14+
return
15+
}
16+
const dbOptions = await SequelizeRepository.getDefaultIRepositoryOptions()
17+
18+
const materializedViews = [
19+
'mv_members_cube',
20+
'mv_activities_cube',
21+
'mv_organizations_cube',
22+
'mv_segments_cube',
23+
]
24+
25+
for (const view of materializedViews) {
26+
await logExecutionTimeV2(
27+
() =>
28+
dbOptions.database.sequelize.query(`REFRESH MATERIALIZED VIEW CONCURRENTLY "${view}"`),
29+
log,
30+
`Refresh Materialized View ${view}`,
31+
)
32+
}
33+
34+
processing = false
35+
},
36+
}
37+
38+
export default job

backend/src/cubejs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.cubecloud
Lines changed: 26 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,218 +1,61 @@
1-
cube(`Activities`, {
2-
sql: `SELECT * FROM public.activities`,
3-
4-
preAggregations: {
5-
Activities: {
6-
measures: [Activities.count],
7-
dimensions: [
8-
Activities.platform,
9-
Activities.type,
10-
Members.score,
11-
Members.location,
12-
Members.tenantId,
13-
Members.isTeamMember,
14-
Members.isBot,
15-
Members.isOrganization,
16-
Activities.tenantId,
17-
Segments.id,
18-
],
19-
timeDimension: Activities.date,
20-
granularity: `day`,
21-
refreshKey: {
22-
every: `10 minute`,
23-
},
24-
},
25-
ActivitiesCumulative: {
26-
measures: [Activities.cumulativeCount],
27-
dimensions: [
28-
Activities.platform,
29-
Activities.type,
30-
Members.score,
31-
Members.location,
32-
Members.tenantId,
33-
Members.isTeamMember,
34-
Members.isBot,
35-
Members.isOrganization,
36-
Activities.tenantId,
37-
Segments.id,
38-
],
39-
timeDimension: Activities.date,
40-
granularity: `day`,
41-
refreshKey: {
42-
every: `10 minute`,
43-
},
44-
},
45-
ActivitiesPTD: {
46-
measures: [Activities.count],
47-
dimensions: [
48-
Activities.platform,
49-
Activities.type,
50-
Members.score,
51-
Members.location,
52-
Members.tenantId,
53-
Members.isTeamMember,
54-
Members.isBot,
55-
Members.isOrganization,
56-
Activities.tenantId,
57-
Segments.id,
58-
],
59-
timeDimension: Activities.date,
60-
granularity: `day`,
61-
partition_granularity: `month`,
62-
refreshKey: {
63-
sql: `SELECT MAX("updatedAt") FROM public.activities WHERE ${FILTER_PARAMS.Activities.date.filter(
64-
'timestamp',
65-
)}`,
66-
every: `30 minute`,
67-
},
68-
},
69-
ActivitiesCumulativePTD: {
70-
measures: [Activities.cumulativeCount],
71-
dimensions: [
72-
Activities.platform,
73-
Activities.type,
74-
Members.score,
75-
Members.location,
76-
Members.tenantId,
77-
Members.isTeamMember,
78-
Members.isBot,
79-
Members.isOrganization,
80-
Activities.tenantId,
81-
Segments.id,
82-
],
83-
timeDimension: Activities.date,
84-
granularity: `day`,
85-
partition_granularity: `month`,
86-
refreshKey: {
87-
sql: `SELECT MAX("updatedAt") FROM public.activities WHERE ${FILTER_PARAMS.Activities.date.filter(
88-
'timestamp',
89-
)}`,
90-
every: `30 minute`,
91-
},
92-
},
93-
},
1+
cube('Activities', {
2+
sql_table: 'mv_activities_cube',
943

954
measures: {
965
count: {
97-
type: `count`,
98-
drillMembers: [
99-
memberId,
100-
sourceid,
101-
tenantId,
102-
id,
103-
updatedbyid,
104-
parentid,
105-
createdbyid,
106-
createdat,
107-
updatedat,
108-
date,
109-
],
6+
sql: `${CUBE}.id`,
7+
type: 'count_distinct',
8+
drillMembers: [tenantId, date],
1109
},
11110
cumulativeCount: {
112-
type: `count`,
11+
type: 'count',
11312
rollingWindow: {
114-
trailing: `unbounded`,
13+
trailing: 'unbounded',
11514
},
11615
},
11716
},
11817

11918
dimensions: {
120-
memberId: {
121-
sql: `${CUBE}."memberId"`,
122-
type: `string`,
123-
shown: false,
19+
id: {
20+
sql: `${CUBE}.id`,
21+
type: 'string',
22+
primaryKey: true,
12423
},
12524

126-
sentimentMood: {
127-
case: {
128-
when: [
129-
{ sql: `${CUBE}.sentiment->>'sentiment' is null`, label: `no data` },
130-
{ sql: `(${CUBE}.sentiment->>'sentiment')::integer < 34`, label: `negative` },
131-
{ sql: `(${CUBE}.sentiment->>'sentiment')::integer > 66`, label: `positive` },
132-
],
133-
else: { label: `neutral` },
134-
},
135-
type: `string`,
25+
iscontribution: {
26+
sql: `${CUBE}."isContribution"`,
27+
type: 'string',
13628
},
13729

138-
sourceid: {
139-
sql: `${CUBE}."sourceId"`,
140-
type: `string`,
141-
shown: false,
30+
sentimentMood: {
31+
sql: `${CUBE}."sentimentMood"`,
32+
type: 'string',
14233
},
14334

14435
platform: {
145-
sql: `platform`,
146-
type: `string`,
36+
sql: `${CUBE}.platform`,
37+
type: 'string',
14738
},
14839

14940
channel: {
150-
sql: `channel`,
151-
type: `string`,
41+
sql: `${CUBE}.channel`,
42+
type: 'string',
15243
},
15344

15445
tenantId: {
15546
sql: `${CUBE}."tenantId"`,
156-
type: `string`,
47+
type: 'string',
15748
shown: false,
15849
},
15950

160-
id: {
161-
sql: `id`,
162-
type: `string`,
163-
primaryKey: true,
164-
},
165-
16651
type: {
167-
sql: `type`,
168-
type: `string`,
169-
},
170-
171-
updatedbyid: {
172-
sql: `${CUBE}."updatedById"`,
173-
type: `string`,
174-
shown: false,
175-
},
176-
177-
iscontribution: {
178-
sql: `${CUBE}."isContribution"`,
179-
type: `string`,
180-
shown: true,
181-
},
182-
183-
parentid: {
184-
sql: `${CUBE}."parentId"`,
185-
type: `string`,
186-
shown: false,
187-
},
188-
189-
createdbyid: {
190-
sql: `${CUBE}."createdById"`,
191-
type: `string`,
192-
shown: false,
193-
},
194-
195-
createdat: {
196-
sql: `${CUBE}."createdAt"`,
197-
type: `time`,
198-
shown: false,
199-
},
200-
201-
updatedat: {
202-
sql: `${CUBE}."updatedAt"`,
203-
type: `time`,
204-
shown: false,
52+
sql: `${CUBE}.type`,
53+
type: 'string',
20554
},
20655

20756
date: {
208-
sql: `timestamp`,
209-
type: `time`,
210-
},
211-
212-
deletedat: {
213-
sql: `${CUBE}."deletedAt"`,
214-
type: `time`,
215-
shown: false,
57+
sql: `${CUBE}.timestamp`,
58+
type: 'time',
21659
},
21760
},
21861
})

0 commit comments

Comments
 (0)