Skip to content

Commit

Permalink
start dashboard cache
Browse files Browse the repository at this point in the history
  • Loading branch information
epipav committed Jan 8, 2024
1 parent f909317 commit 0e5f4f9
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
84 changes: 84 additions & 0 deletions backend/src/bin/scripts/refresh-dashboard-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import commandLineArgs from 'command-line-args'
import commandLineUsage from 'command-line-usage'
import { QueryTypes } from 'sequelize'
import * as fs from 'fs'
import path from 'path'
import SequelizeRepository from '../../database/repositories/sequelizeRepository'
import TenantService from '@/services/tenantService'
import OrganizationService from '@/services/organizationService'
import getUserContext from '@/database/utils/getUserContext'
import { IRepositoryOptions } from '@/database/repositories/IRepositoryOptions'
import {
MergeActionState,
MergeActionType,
MergeActionsRepository,
} from '@/database/repositories/mergeActionsRepository'
import { CubeJsService } from '@crowd/cubejs'

/* eslint-disable no-console */

const banner = fs.readFileSync(path.join(__dirname, 'banner.txt'), 'utf8')

const options = [
{
name: 'tenant',
alias: 't',
type: String,
description: 'The unique ID of tenant',
},
{
name: 'allTenants',
alias: 'a',
type: Boolean,
defaultValue: false,
description: 'Set this flag to merge similar organizations for all tenants.',
},
{
name: 'help',
alias: 'h',
type: Boolean,
description: 'Print this usage guide.',
},
]
const sections = [
{
content: banner,
raw: true,
},
{
header: 'Merge organizations with similarity higher than given threshold.',
content: 'Merge organizations with similarity higher than given threshold.',
},
{
header: 'Options',
optionList: options,
},
]

const usage = commandLineUsage(sections)
const parameters = commandLineArgs(options)

if (parameters.help || (!parameters.tenant && !parameters.allTenants)) {
console.log(usage)
} else {
setImmediate(async () => {
const options = await SequelizeRepository.getDefaultIRepositoryOptions()

let tenantIds

if (parameters.allTenants) {
tenantIds = (await TenantService._findAndCountAllForEveryUser({})).rows.map((t) => t.id)
} else if (parameters.tenant) {
tenantIds = parameters.tenant.split(',')
} else {
tenantIds = []
}

for (const tenantId of tenantIds) {
const cjs = new CubeJsService()
await cjs.init(tenantId, [])
}

process.exit(0)
})
}
3 changes: 2 additions & 1 deletion services/libs/cubejs/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM cubejs/cube:v0.33.55

COPY ./ /cube/conf/
COPY ./ /cube/conf/
COPY ./src /cube/conf/
8 changes: 8 additions & 0 deletions services/libs/cubejs/src/dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ export default class CubeDimensions {

static readonly IS_TEAM_MEMBER: string = 'Members.isTeamMember'

static readonly IS_BOT: string = 'Members.isBot'

static readonly IS_ORGANIZATION: string = 'Members.isOrganization'

static readonly ACTIVITY_DATE: string = 'Activities.date'

static readonly ACTIVITY_PLATFORM: string = 'Activities.platform'

static readonly CONVERSATION_CREATED_AT: string = 'Conversations.createdat'

static readonly CONVERSATION_FIRST_ACTIVITY_TIME: string = 'Conversations.firstActivityTime'

static readonly ORGANIZATIONS_JOINED_AT: string = 'Organizations.joinedAt'

static readonly SEGMENTS_ID: string = 'Segments.id'
}
10 changes: 10 additions & 0 deletions services/libs/cubejs/src/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export enum CubeGranularity {
SECOND = 'second',
MINUTE = 'minute',
HOUR = 'hour',
DAY = 'day',
WEEK = 'week',
MONTH = 'month',
QUARTER = 'quarter',
YEAR = 'year',
}
75 changes: 75 additions & 0 deletions services/libs/cubejs/src/metrics/activeMembersTimeseries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import moment from 'moment'

import { CubeJsService } from '../service'
import CubeDimensions from '../dimensions'
import CubeMeasures from '../measures'
import { ICubeFilter } from 'types'
import { CubeGranularity } from 'enums'

/**
* Gets `active members` timeseries data for a given date range in given granularity.
* Members are active when they have an activity in given date range.
* @param cjs cubejs service instance
* @param startDate
* @param endDate
* @returns
*/
export default async (
cjs: CubeJsService,
startDate: moment.Moment,
endDate: moment.Moment,
granularity: CubeGranularity = CubeGranularity.DAY,
platform: string = undefined,
segment: string = undefined,
) => {
const filters: ICubeFilter[] = [
{
member: CubeDimensions.IS_TEAM_MEMBER,
operator: 'equals',
values: ['false'],
},
{
member: CubeDimensions.IS_BOT,
operator: 'equals',
values: ['false'],
},
{
member: CubeDimensions.IS_ORGANIZATION,
operator: 'equals',
values: ['false'],
},
]

if (platform) {
filters.push({
member: CubeDimensions.ACTIVITY_PLATFORM,
operator: 'equals',
values: [platform],
})
}

if (segment) {
filters.push({
member: CubeDimensions.SEGMENTS_ID,
operator: 'equals',
values: [segment],
})
}

const activeMembersTimeseries = await cjs.load({
measures: [CubeMeasures.MEMBER_COUNT],
timeDimensions: [
{
dimension: CubeDimensions.ACTIVITY_DATE,
dateRange: [startDate.format('YYYY-MM-DD'), endDate.format('YYYY-MM-DD')],
granularity,
},
],
// order: { [CubeDimensions.MEMBER_JOINED_AT]: 'asc' },
filters,
}) // [0][CubeMeasures.MEMBER_COUNT] ?? 0

console.log(activeMembersTimeseries)

// return parseInt(activeMembers, 10)
}
7 changes: 7 additions & 0 deletions services/libs/cubejs/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import CubeDimensions from 'dimensions'

export interface ICubeFilter {
member: CubeDimensions
operator: string
values: string[]
}

0 comments on commit 0e5f4f9

Please sign in to comment.