Skip to content

Commit

Permalink
chore: tweak user events (#323)
Browse files Browse the repository at this point in the history
Only send events with email to analytics. Added a couple of sdk events
to track document runs.
  • Loading branch information
geclos authored Oct 2, 2024
1 parent 17641c5 commit 19f98bb
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 20 deletions.
11 changes: 11 additions & 0 deletions apps/web/src/actions/evaluations/runBatch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use server'

import { readMetadata } from '@latitude-data/compiler'
import { publisher } from '@latitude-data/core/events/publisher'
import {
DatasetsRepository,
EvaluationsRepository,
Expand Down Expand Up @@ -91,6 +92,16 @@ export const runBatchEvaluationAction = withDataset
})
})
.handler(async ({ input, ctx }) => {
publisher.publishLater({
type: 'batchEvaluationRunRequested',
data: {
evaluationIds: input.evaluationIds,
documentUuid: input.documentUuid,
workspaceId: ctx.workspace.id,
userEmail: ctx.user.email,
},
})

const evaluationsRepo = new EvaluationsRepository(ctx.workspace.id)
const evaluations = await evaluationsRepo
.filterById(input.evaluationIds)
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/actions/sdk/addMessagesAction.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
'use server'

import { StreamEventTypes } from '@latitude-data/core/browser'
import { publisher } from '@latitude-data/core/events/publisher'
import {
type ChainEventDto,
type Message,
type StreamChainResponse,
} from '@latitude-data/sdk'
import { createSdk } from '$/app/(private)/_lib/createSdk'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { createStreamableValue, StreamableValue } from 'ai/rsc'

type AddMessagesActionProps = {
Expand All @@ -25,6 +27,18 @@ export async function addMessagesAction({
documentLogUuid,
messages,
}: AddMessagesActionProps) {
const { workspace, user } = await getCurrentUser()

publisher.publishLater({
type: 'chatMessageRequested',
data: {
documentLogUuid,
messages,
workspaceId: workspace.id,
userEmail: user.email,
},
})

const sdk = await createSdk().then((r) => r.unwrap())
const stream = createStreamableValue<
{ event: StreamEventTypes; data: ChainEventDto },
Expand Down
16 changes: 16 additions & 0 deletions apps/web/src/actions/sdk/runDocumentAction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use server'

import { StreamEventTypes } from '@latitude-data/core/browser'
import { publisher } from '@latitude-data/core/events/publisher'
import { Latitude, type ChainEventDto } from '@latitude-data/sdk'
import { createSdk } from '$/app/(private)/_lib/createSdk'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { createStreamableValue, StreamableValue } from 'ai/rsc'

type RunDocumentActionProps = {
Expand All @@ -25,6 +27,20 @@ export async function runDocumentAction({
commitUuid,
parameters,
}: RunDocumentActionProps) {
const { workspace, user } = await getCurrentUser()

publisher.publishLater({
type: 'documentRunRequested',
data: {
projectId,
commitUuid,
documentPath,
parameters,
workspaceId: workspace.id,
userEmail: user.email,
},
})

const sdk = await createSdk({ projectId }).then((r) => r.unwrap())
const stream = createStreamableValue<
{ event: StreamEventTypes; data: ChainEventDto },
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/events/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ export type EvaluationResultCreatedEvent = LatitudeEventGeneric<
}
>

export type DocumentRunRequestedEvent = LatitudeEventGeneric<
'documentRunRequested',
{
projectId: number
commitUuid: string
documentPath: string
parameters: Record<string, unknown>
workspaceId: number
userEmail: string
}
>

export type ChatMessageRequestedEvent = LatitudeEventGeneric<
'chatMessageRequested',
{
documentLogUuid: string
messages: Message[]
workspaceId: number
userEmail: string
}
>

export type BatchEvaluationRunRequestedEvent = LatitudeEventGeneric<
'batchEvaluationRunRequested',
{
evaluationIds: number[]
documentUuid: string
workspaceId: number
userEmail: string
}
>

export type LatitudeEvent =
| MembershipCreatedEvent
| UserCreatedEvent
Expand All @@ -265,6 +297,9 @@ export type LatitudeEvent =
| BatchEvaluationRunEvent
| DocumentCreatedEvent
| EvaluationResultCreatedEvent
| DocumentRunRequestedEvent
| ChatMessageRequestedEvent
| BatchEvaluationRunRequestedEvent

export interface IEventsHandlers {
magicLinkTokenCreated: EventHandler<MagicLinkTokenCreated>[]
Expand All @@ -289,6 +324,9 @@ export interface IEventsHandlers {
batchEvaluationRun: EventHandler<BatchEvaluationRunEvent>[]
documentCreated: EventHandler<DocumentCreatedEvent>[]
evaluationResultCreated: EventHandler<EvaluationResultCreatedEvent>[]
documentRunRequested: EventHandler<DocumentRunRequestedEvent>[]
chatMessageRequested: EventHandler<ChatMessageRequestedEvent>[]
batchEvaluationRunRequested: EventHandler<BatchEvaluationRunRequestedEvent>[]
}

export const EventHandlers: IEventsHandlers = {
Expand Down Expand Up @@ -317,4 +355,7 @@ export const EventHandlers: IEventsHandlers = {
userCreated: [],
userInvited: [],
workspaceCreated: [],
documentRunRequested: [],
chatMessageRequested: [],
batchEvaluationRunRequested: [],
} as const
37 changes: 17 additions & 20 deletions packages/jobs/src/job-definitions/events/publishToAnalyticsJob.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LatitudeEvent } from '@latitude-data/core/events/handlers/index'
import { PostHogClient } from '@latitude-data/core/services/posthog'
import { env } from '@latitude-data/env'
import { Job } from 'bullmq'

export const publishToAnalyticsJob = async (job: Job<LatitudeEvent>) => {
Expand All @@ -12,28 +13,24 @@ export const publishToAnalyticsJob = async (job: Job<LatitudeEvent>) => {
workspaceId = event.data.workspaceId
}

const client = PostHogClient()
if (!userEmail) return

if (env.NODE_ENV !== 'production') {
console.log('Analytics event captured:', event.type)

if (!userEmail && !workspaceId) return
if (!userEmail) {
client.capture({
distinctId: `workspace:${workspaceId}`,
event: event.type,
properties: {
data: event.data,
workspaceId,
},
})
} else {
client.capture({
distinctId: userEmail,
event: event.type,
properties: {
data: event.data,
workspaceId,
},
})
return
}

const client = PostHogClient()

client.capture({
distinctId: userEmail,
event: event.type,
properties: {
data: event.data,
workspaceId,
},
})

await client.shutdown()
}

0 comments on commit 19f98bb

Please sign in to comment.