feature(dashboard): add new retention chart type
This commit is contained in:
committed by
Carl-Gerhard Lindesvärd
parent
e2065da16e
commit
f977c5454a
58
packages/db/migrations/20241007210706_retention_mv.sql
Normal file
58
packages/db/migrations/20241007210706_retention_mv.sql
Normal file
@@ -0,0 +1,58 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE MATERIALIZED VIEW cohort_events_mv ENGINE = AggregatingMergeTree()
|
||||
ORDER BY (project_id, name, created_at, profile_id) POPULATE AS
|
||||
SELECT project_id,
|
||||
name,
|
||||
toDate(created_at) AS created_at,
|
||||
profile_id,
|
||||
COUNT() AS event_count
|
||||
FROM events_v2
|
||||
WHERE profile_id != device_id
|
||||
GROUP BY project_id,
|
||||
name,
|
||||
created_at,
|
||||
profile_id;
|
||||
-- +goose StatementEnd
|
||||
-- +goose StatementBegin
|
||||
CREATE MATERIALIZED VIEW distinct_event_names_mv ENGINE = AggregatingMergeTree()
|
||||
ORDER BY (project_id, name, created_at) POPULATE AS
|
||||
SELECT project_id,
|
||||
name,
|
||||
max(created_at) AS created_at,
|
||||
count() AS event_count
|
||||
FROM events_v2
|
||||
GROUP BY project_id,
|
||||
name;
|
||||
-- +goose StatementEnd
|
||||
-- +goose StatementBegin
|
||||
CREATE MATERIALIZED VIEW event_property_values_mv ENGINE = AggregatingMergeTree()
|
||||
ORDER BY (project_id, name, property_key, property_value) POPULATE AS
|
||||
select project_id,
|
||||
name,
|
||||
key_value.keys as property_key,
|
||||
key_value.values as property_value,
|
||||
created_at
|
||||
from (
|
||||
SELECT project_id,
|
||||
name,
|
||||
untuple(arrayJoin(properties)) as key_value,
|
||||
max(created_at) as created_at
|
||||
from events_v2
|
||||
group by project_id,
|
||||
name,
|
||||
key_value
|
||||
)
|
||||
where property_value != ''
|
||||
and property_key != ''
|
||||
and property_key NOT IN ('__duration_from', '__properties_from')
|
||||
group by project_id,
|
||||
name,
|
||||
property_key,
|
||||
property_value,
|
||||
created_at;
|
||||
-- +goose StatementEnd
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
SELECT 'down SQL query';
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterEnum
|
||||
ALTER TYPE "ChartType" ADD VALUE 'retention';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterEnum
|
||||
ALTER TYPE "Interval" ADD VALUE 'week';
|
||||
@@ -0,0 +1,2 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE "reports" ADD COLUMN "criteria" TEXT;
|
||||
@@ -192,6 +192,7 @@ enum Interval {
|
||||
day
|
||||
month
|
||||
minute
|
||||
week
|
||||
}
|
||||
|
||||
enum ChartType {
|
||||
@@ -203,6 +204,7 @@ enum ChartType {
|
||||
area
|
||||
map
|
||||
funnel
|
||||
retention
|
||||
}
|
||||
|
||||
model Dashboard {
|
||||
@@ -243,6 +245,7 @@ model Report {
|
||||
projectId String
|
||||
project Project @relation(fields: [projectId], references: [id])
|
||||
previous Boolean @default(false)
|
||||
criteria String?
|
||||
|
||||
dashboardId String
|
||||
dashboard Dashboard @relation(fields: [dashboardId], references: [id])
|
||||
|
||||
@@ -14,6 +14,9 @@ export const TABLE_NAMES = {
|
||||
self_hosting: 'self_hosting',
|
||||
events_bots: 'events_bots',
|
||||
dau_mv: 'dau_mv',
|
||||
event_names_mv: 'distinct_event_names_mv',
|
||||
event_property_values_mv: 'event_property_values_mv',
|
||||
cohort_events_mv: 'cohort_events_mv',
|
||||
};
|
||||
|
||||
export const originalCh = createClient({
|
||||
@@ -129,6 +132,10 @@ export function formatClickhouseDate(
|
||||
_date: Date | string,
|
||||
skipTime = false,
|
||||
): string {
|
||||
if (typeof _date === 'string') {
|
||||
return _date.slice(0, 19).replace('T', ' ');
|
||||
}
|
||||
|
||||
const date = typeof _date === 'string' ? new Date(_date) : _date;
|
||||
if (skipTime) {
|
||||
return date.toISOString().split('T')[0]!;
|
||||
|
||||
@@ -81,6 +81,10 @@ export function getChartSql({
|
||||
sb.select.date = `toStartOfDay(toTimeZone(created_at, '${getTimezoneFromDateString(startDate)}')) as date`;
|
||||
break;
|
||||
}
|
||||
case 'week': {
|
||||
sb.select.date = `toStartOfWeek(toTimeZone(created_at, '${getTimezoneFromDateString(startDate)}')) as date`;
|
||||
break;
|
||||
}
|
||||
case 'month': {
|
||||
sb.select.date = `toStartOfMonth(toTimeZone(created_at, '${getTimezoneFromDateString(startDate)}')) as date`;
|
||||
break;
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
IChartLineType,
|
||||
IChartProps,
|
||||
IChartRange,
|
||||
ICriteria,
|
||||
} from '@openpanel/validation';
|
||||
|
||||
import { db } from '../prisma-client';
|
||||
@@ -64,6 +65,7 @@ export function transformReport(
|
||||
formula: report.formula ?? undefined,
|
||||
metric: report.metric ?? 'sum',
|
||||
unit: report.unit ?? undefined,
|
||||
criteria: (report.criteria as ICriteria) ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user