wip
This commit is contained in:
@@ -299,3 +299,11 @@ const ROLLUP_DATE_PREFIX = '1970-01-01';
|
||||
export function isClickhouseDefaultMinDate(date: string): boolean {
|
||||
return date.startsWith(ROLLUP_DATE_PREFIX) || date.startsWith('1969-12-31');
|
||||
}
|
||||
export function toNullIfDefaultMinDate(date?: string | null): Date | null {
|
||||
if (!date) {
|
||||
return null;
|
||||
}
|
||||
return isClickhouseDefaultMinDate(date)
|
||||
? null
|
||||
: convertClickhouseDateToJs(date);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
import type {
|
||||
IAliasPayload as AliasPayload,
|
||||
IAssignGroupPayload as AssignGroupPayload,
|
||||
IDecrementPayload as DecrementPayload,
|
||||
IGroupPayload as GroupPayload,
|
||||
IIdentifyPayload as IdentifyPayload,
|
||||
@@ -13,6 +14,7 @@ import { Api } from './api';
|
||||
|
||||
export type {
|
||||
AliasPayload,
|
||||
AssignGroupPayload,
|
||||
DecrementPayload,
|
||||
GroupPayload,
|
||||
IdentifyPayload,
|
||||
@@ -27,7 +29,7 @@ export interface TrackProperties {
|
||||
groups?: string[];
|
||||
}
|
||||
|
||||
export type GroupMetadata = Omit<GroupPayload, 'id'>;
|
||||
export type UpsertGroupPayload = GroupPayload;
|
||||
|
||||
export interface OpenPanelOptions {
|
||||
clientId: string;
|
||||
@@ -187,26 +189,38 @@ export class OpenPanel {
|
||||
}
|
||||
}
|
||||
|
||||
setGroups(groupIds: string[]) {
|
||||
this.log('set groups', groupIds);
|
||||
this.groups = groupIds;
|
||||
upsertGroup(payload: UpsertGroupPayload) {
|
||||
this.log('upsert group', payload);
|
||||
return this.send({
|
||||
type: 'group',
|
||||
payload,
|
||||
});
|
||||
}
|
||||
|
||||
setGroup(groupId: string, metadata?: GroupMetadata) {
|
||||
this.log('set group', groupId, metadata);
|
||||
setGroup(groupId: string) {
|
||||
this.log('set group', groupId);
|
||||
if (!this.groups.includes(groupId)) {
|
||||
this.groups = [...this.groups, groupId];
|
||||
}
|
||||
if (metadata) {
|
||||
return this.send({
|
||||
type: 'group',
|
||||
payload: {
|
||||
id: groupId,
|
||||
...metadata,
|
||||
profileId: this.profileId,
|
||||
},
|
||||
});
|
||||
}
|
||||
return this.send({
|
||||
type: 'assign_group',
|
||||
payload: {
|
||||
groupIds: [groupId],
|
||||
profileId: this.profileId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
setGroups(groupIds: string[]) {
|
||||
this.log('set groups', groupIds);
|
||||
this.groups = [...new Set([...this.groups, ...groupIds])];
|
||||
return this.send({
|
||||
type: 'assign_group',
|
||||
payload: {
|
||||
groupIds,
|
||||
profileId: this.profileId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -291,7 +305,7 @@ export class OpenPanel {
|
||||
profileId: item.payload.profileId ?? this.profileId,
|
||||
} as TrackHandlerPayload['payload'];
|
||||
}
|
||||
if (item.type === 'group') {
|
||||
if (item.type === 'assign_group') {
|
||||
return {
|
||||
...item.payload,
|
||||
profileId: item.payload.profileId ?? this.profileId,
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
getGroupsByIds,
|
||||
getGroupTypes,
|
||||
TABLE_NAMES,
|
||||
toNullIfDefaultMinDate,
|
||||
updateGroup,
|
||||
} from '@openpanel/db';
|
||||
import sqlstring from 'sqlstring';
|
||||
@@ -49,7 +50,7 @@ export const groupRouter = createTRPCRouter({
|
||||
|
||||
byId: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
.query(({ input: { id, projectId } }) => {
|
||||
return getGroupById(id, projectId);
|
||||
}),
|
||||
|
||||
@@ -63,7 +64,7 @@ export const groupRouter = createTRPCRouter({
|
||||
properties: z.record(z.string()).default({}),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
.mutation(({ input }) => {
|
||||
return createGroup(input);
|
||||
}),
|
||||
|
||||
@@ -77,26 +78,26 @@ export const groupRouter = createTRPCRouter({
|
||||
properties: z.record(z.string()).optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input: { id, projectId, ...data } }) => {
|
||||
.mutation(({ input: { id, projectId, ...data } }) => {
|
||||
return updateGroup(id, projectId, data);
|
||||
}),
|
||||
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.mutation(async ({ input: { id, projectId } }) => {
|
||||
.mutation(({ input: { id, projectId } }) => {
|
||||
return deleteGroup(id, projectId);
|
||||
}),
|
||||
|
||||
types: protectedProcedure
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ input: { projectId } }) => {
|
||||
.query(({ input: { projectId } }) => {
|
||||
return getGroupTypes(projectId);
|
||||
}),
|
||||
|
||||
metrics: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
return chQuery<{
|
||||
const data = await chQuery<{
|
||||
totalEvents: number;
|
||||
uniqueProfiles: number;
|
||||
firstSeen: string;
|
||||
@@ -111,11 +112,18 @@ export const groupRouter = createTRPCRouter({
|
||||
WHERE project_id = ${sqlstring.escape(projectId)}
|
||||
AND has(groups, ${sqlstring.escape(id)})
|
||||
`);
|
||||
|
||||
return {
|
||||
totalEvents: data[0]?.totalEvents ?? 0,
|
||||
uniqueProfiles: data[0]?.uniqueProfiles ?? 0,
|
||||
firstSeen: toNullIfDefaultMinDate(data[0]?.firstSeen),
|
||||
lastSeen: toNullIfDefaultMinDate(data[0]?.lastSeen),
|
||||
};
|
||||
}),
|
||||
|
||||
activity: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
.query(({ input: { id, projectId } }) => {
|
||||
return chQuery<{ count: number; date: string }>(`
|
||||
SELECT count() AS count, toStartOfDay(created_at) AS date
|
||||
FROM ${TABLE_NAMES.events}
|
||||
@@ -174,7 +182,7 @@ export const groupRouter = createTRPCRouter({
|
||||
|
||||
mostEvents: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
.query(({ input: { id, projectId } }) => {
|
||||
return chQuery<{ count: number; name: string }>(`
|
||||
SELECT count() as count, name
|
||||
FROM ${TABLE_NAMES.events}
|
||||
@@ -189,7 +197,7 @@ export const groupRouter = createTRPCRouter({
|
||||
|
||||
popularRoutes: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
.query(({ input: { id, projectId } }) => {
|
||||
return chQuery<{ count: number; path: string }>(`
|
||||
SELECT count() as count, path
|
||||
FROM ${TABLE_NAMES.events}
|
||||
@@ -204,7 +212,7 @@ export const groupRouter = createTRPCRouter({
|
||||
|
||||
memberGrowth: protectedProcedure
|
||||
.input(z.object({ id: z.string(), projectId: z.string() }))
|
||||
.query(async ({ input: { id, projectId } }) => {
|
||||
.query(({ input: { id, projectId } }) => {
|
||||
return chQuery<{ date: string; count: number }>(`
|
||||
SELECT
|
||||
toDate(toStartOfDay(min_date)) AS date,
|
||||
@@ -228,13 +236,13 @@ export const groupRouter = createTRPCRouter({
|
||||
|
||||
properties: protectedProcedure
|
||||
.input(z.object({ projectId: z.string() }))
|
||||
.query(async ({ input: { projectId } }) => {
|
||||
.query(({ input: { projectId } }) => {
|
||||
return getGroupPropertyKeys(projectId);
|
||||
}),
|
||||
|
||||
listByIds: protectedProcedure
|
||||
.input(z.object({ projectId: z.string(), ids: z.array(z.string()) }))
|
||||
.query(async ({ input: { projectId, ids } }) => {
|
||||
.query(({ input: { projectId, ids } }) => {
|
||||
return getGroupsByIds(projectId, ids);
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -7,6 +7,10 @@ export const zGroupPayload = z.object({
|
||||
type: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
properties: z.record(z.unknown()).optional(),
|
||||
});
|
||||
|
||||
export const zAssignGroupPayload = z.object({
|
||||
groupIds: z.array(z.string().min(1)),
|
||||
profileId: z.union([z.string().min(1), z.number()]).optional(),
|
||||
});
|
||||
|
||||
@@ -110,6 +114,10 @@ export const zTrackHandlerPayload = z.discriminatedUnion('type', [
|
||||
type: z.literal('group'),
|
||||
payload: zGroupPayload,
|
||||
}),
|
||||
z.object({
|
||||
type: z.literal('assign_group'),
|
||||
payload: zAssignGroupPayload,
|
||||
}),
|
||||
]);
|
||||
|
||||
export type ITrackPayload = z.infer<typeof zTrackPayload>;
|
||||
@@ -119,6 +127,7 @@ export type IDecrementPayload = z.infer<typeof zDecrementPayload>;
|
||||
export type IAliasPayload = z.infer<typeof zAliasPayload>;
|
||||
export type IReplayPayload = z.infer<typeof zReplayPayload>;
|
||||
export type IGroupPayload = z.infer<typeof zGroupPayload>;
|
||||
export type IAssignGroupPayload = z.infer<typeof zAssignGroupPayload>;
|
||||
export type ITrackHandlerPayload = z.infer<typeof zTrackHandlerPayload>;
|
||||
|
||||
// Deprecated types for beta version of the SDKs
|
||||
|
||||
Reference in New Issue
Block a user