Skip to content

Commit

Permalink
Fix dashboard cache building (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
sausage-todd authored Oct 25, 2024
1 parent 99bb419 commit e714ce1
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 711 deletions.
8 changes: 0 additions & 8 deletions services/apps/cache_worker/src/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import {
} from './activities/computeAggs/organization'
import {
findNewActivityPlatforms,
getActiveMembersNumber,
getActiveMembersTimeseries,
getActiveOrganizationsNumber,
getActiveOrganizationsTimeseries,
getActivePlatforms,
getActivitiesBySentiment,
Expand All @@ -17,9 +15,7 @@ import {
getActivitiesTimeseries,
getDashboardCacheLastRefreshedAt,
getDefaultSegment,
getNewMembersNumber,
getNewMembersTimeseries,
getNewOrganizationsNumber,
getNewOrganizationsTimeseries,
saveToCache,
updateMemberMergeSuggestionsLastGeneratedAt,
Expand All @@ -39,13 +35,9 @@ export {
getProjectGroupLeafSegments,
getDashboardCacheLastRefreshedAt,
getDefaultSegment,
getNewMembersNumber,
getNewMembersTimeseries,
getActiveMembersNumber,
getActiveMembersTimeseries,
getNewOrganizationsNumber,
getNewOrganizationsTimeseries,
getActiveOrganizationsNumber,
getActiveOrganizationsTimeseries,
getActivitiesNumber,
getActivitiesTimeseries,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {
IActiveMembersTimeseriesResult,
IActivityBySentimentMoodResult,
IActivityByTypeAndPlatformResult,
IActivityTimeseriesResult,
activitiesBySentiment,
activitiesByTypeAndPlatform,
activitiesTimeseries,
countMembersWithActivities,
countOrganizationsWithActivities,
getNumberOfNewMembers,
getTimeseriesOfActiveMembers,
getTimeseriesOfNewMembers,
queryActivities,
} from '@crowd/data-access-layer'
import { DbStore } from '@crowd/data-access-layer/src/database'
Expand All @@ -18,21 +12,21 @@ import IntegrationRepository from '@crowd/data-access-layer/src/old/apps/cache_w
import SegmentRepository from '@crowd/data-access-layer/src/old/apps/cache_worker/segment.repo'
import { ISegment } from '@crowd/data-access-layer/src/old/apps/cache_worker/types'
import {
IActiveOrganizationsTimeseriesResult,
getNumberOfActiveOrganizations,
getNumberOfNewOrganizations,
getTimeseriesOfActiveOrganizations,
getTimeseriesOfNewOrganizations,
} from '@crowd/data-access-layer/src/organizations'
import { dbStoreQx } from '@crowd/data-access-layer/src/queryExecutor'
import { RedisCache } from '@crowd/redis'
import { DashboardTimeframe } from '@crowd/types'

import { svc } from '../../main'
import {
DashboardTimeframe,
IActivityBySentimentMoodResult,
IActivityByTypeAndPlatformResult,
IDashboardData,
IGraphQueryParams,
INewMembersTimeseriesResult,
INewOrganizationsTimeseriesResult,
} from '../../types'
IQueryTimeseriesParams,
ITimeseriesDatapoint,
} from '@crowd/types'

import { svc } from '../../main'

const qdb = new DbStore(svc.log, svc.questdbSQL)

Expand Down Expand Up @@ -66,188 +60,31 @@ export async function updateMemberMergeSuggestionsLastGeneratedAt(
await segmentRepo.updateDashboardCacheLastRefreshedAt(segmentId)
}

export async function getNewMembersNumber(params: IGraphQueryParams): Promise<number> {
let result = 0
try {
result = await getNumberOfNewMembers(svc.postgres.reader, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
after: params.startDate,
before: params.endDate,
platform: params.platform,
})
} catch (err) {
throw new Error(err)
}

return result
}

export async function getNewMembersTimeseries(
params: IGraphQueryParams,
): Promise<INewMembersTimeseriesResult[]> {
let result: INewMembersTimeseriesResult[]

try {
const rows = await countMembersWithActivities(svc.questdbSQL, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
timestampFrom: params.startDate,
timestampTo: params.endDate,
platform: params.platform,
groupBy: 'day',
})

const mapped: Record<string, INewMembersTimeseriesResult> = {}
rows.forEach((row) => {
if (!mapped[row.date]) {
mapped[row.date] = {
date: row.date,
count: Number(row.count),
}
} else {
mapped[row.date]['count'] = Number(mapped[row.date]['count']) + Number(row.count)
}
})

result = Object.values(mapped)
} catch (err) {
throw new Error(err)
}

return result
}

export async function getActiveMembersNumber(params: IGraphQueryParams): Promise<number> {
let result = 0
try {
const rows = await countMembersWithActivities(svc.questdbSQL, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
timestampFrom: params.startDate,
timestampTo: params.endDate,
platform: params.platform,
groupBy: 'day',
})

rows.forEach((row) => {
result += Number(row.count)
})
} catch (err) {
throw new Error(err)
}

return result
params: IQueryTimeseriesParams,
): Promise<ITimeseriesDatapoint[]> {
return getTimeseriesOfNewMembers(dbStoreQx(svc.postgres.reader), params)
}

export async function getActiveMembersTimeseries(
params: IGraphQueryParams,
): Promise<IActiveMembersTimeseriesResult[]> {
let result: IActiveMembersTimeseriesResult[]
try {
result = await getTimeseriesOfActiveMembers(qdb, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
after: params.startDate,
before: params.endDate,
platform: params.platform,
})
} catch (err) {
throw new Error(err)
}

return result
}

export async function getNewOrganizationsNumber(params: IGraphQueryParams): Promise<number> {
let result = 0
try {
result = await getNumberOfNewOrganizations(svc.postgres.reader, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
after: params.startDate,
before: params.endDate,
platform: params.platform,
})
} catch (err) {
throw new Error(err)
}

return result
params: IQueryTimeseriesParams,
): Promise<ITimeseriesDatapoint[]> {
return getTimeseriesOfActiveMembers(dbStoreQx(qdb), params)
}

export async function getNewOrganizationsTimeseries(
params: IGraphQueryParams,
): Promise<INewOrganizationsTimeseriesResult[]> {
let result: INewOrganizationsTimeseriesResult[]

try {
const rows = await countOrganizationsWithActivities(svc.questdbSQL, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
timestampFrom: params.startDate,
timestampTo: params.endDate,
platform: params.platform,
groupBy: 'day',
})

const mapped: Record<string, INewOrganizationsTimeseriesResult> = {}
rows.forEach((row) => {
if (!mapped[row.date]) {
mapped[row.date] = {
date: row.date,
count: Number(row.count),
}
} else {
mapped[row.date]['count'] = Number(mapped[row.date]['count']) + Number(row.count)
}
})

result = Object.values(mapped)
} catch (err) {
throw new Error(err)
}

return result
}

export async function getActiveOrganizationsNumber(params: IGraphQueryParams): Promise<number> {
let result = 0
try {
result = await getNumberOfActiveOrganizations(qdb, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
after: params.startDate,
before: params.endDate,
platform: params.platform,
})
} catch (err) {
throw new Error(err)
}

return result
params: IQueryTimeseriesParams,
): Promise<ITimeseriesDatapoint[]> {
return getTimeseriesOfNewOrganizations(dbStoreQx(svc.postgres.reader), params)
}

export async function getActiveOrganizationsTimeseries(
params: IGraphQueryParams,
): Promise<IActiveOrganizationsTimeseriesResult[]> {
let result: IActiveOrganizationsTimeseriesResult[]
try {
result = await getTimeseriesOfActiveOrganizations(qdb, {
tenantId: params.tenantId,
segmentIds: params.segmentIds,
after: params.startDate,
before: params.endDate,
platform: params.platform,
})
} catch (err) {
throw new Error(err)
}

return result
params: IQueryTimeseriesParams,
): Promise<ITimeseriesDatapoint[]> {
return getTimeseriesOfActiveOrganizations(dbStoreQx(qdb), params)
}

export async function getActivitiesNumber(params: IGraphQueryParams): Promise<number> {
export async function getActivitiesNumber(params: IQueryTimeseriesParams): Promise<number> {
let result = 0
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -290,9 +127,9 @@ export async function getActivitiesNumber(params: IGraphQueryParams): Promise<nu
}

export async function getActivitiesTimeseries(
params: IGraphQueryParams,
): Promise<IActivityTimeseriesResult[]> {
let result: IActivityTimeseriesResult[]
params: IQueryTimeseriesParams,
): Promise<ITimeseriesDatapoint[]> {
let result: ITimeseriesDatapoint[]

try {
result = await activitiesTimeseries(svc.questdbSQL, {
Expand All @@ -309,7 +146,7 @@ export async function getActivitiesTimeseries(
return result
}
export async function getActivitiesBySentiment(
params: IGraphQueryParams,
params: IQueryTimeseriesParams,
): Promise<IActivityBySentimentMoodResult[]> {
let result: IActivityBySentimentMoodResult[]

Expand All @@ -328,7 +165,7 @@ export async function getActivitiesBySentiment(
return result
}
export async function getActivitiesByType(
params: IGraphQueryParams,
params: IQueryTimeseriesParams,
): Promise<IActivityByTypeAndPlatformResult[]> {
let result: IActivityByTypeAndPlatformResult[]

Expand Down
68 changes: 0 additions & 68 deletions services/apps/cache_worker/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,3 @@
import {
IActivityBySentimentMoodResult,
IActivityByTypeAndPlatformResult,
IActivityTimeseriesResult,
} from '@crowd/data-access-layer'
import { IActiveOrganizationsTimeseriesResult } from '@crowd/data-access-layer/src/organizations'

export interface IActiveMembersTimeseriesResult {
date: string
count: number
}

export interface INewMembersTimeseriesResult {
date: string
count: number
}

export interface INewOrganizationsTimeseriesResult {
date: string
count: number
}

export interface IDashboardData {
activeMembers: {
total: number
previousPeriodTotal: number
timeseries: IActiveMembersTimeseriesResult[]
}
newMembers: {
total: number
previousPeriodTotal: number
timeseries: INewMembersTimeseriesResult[]
}
newOrganizations: {
total: number
previousPeriodTotal: number
timeseries: INewOrganizationsTimeseriesResult[]
}
activeOrganizations: {
total: number
previousPeriodTotal: number
timeseries: IActiveOrganizationsTimeseriesResult[]
}
activity: {
total: number
previousPeriodTotal: number
timeseries: IActivityTimeseriesResult[]
bySentimentMood: IActivityBySentimentMoodResult[]
byTypeAndPlatform: IActivityByTypeAndPlatformResult[]
}
}

export interface ITimeframe {
startDate: Date
endDate: Date
previousPeriodStartDate: Date
previousPeriodEndDate: Date
}

export interface IGraphQueryParams {
tenantId: string
segmentIds: string[]
startDate: Date
endDate: Date
platform?: string
groupBy?: string
}

export interface IProcessComputeOrgAggs {
organizationId: string
}
Loading

0 comments on commit e714ce1

Please sign in to comment.