Skip to content

Commit

Permalink
fix: missing implementation for order by on /events
Browse files Browse the repository at this point in the history
  • Loading branch information
christianmat committed Nov 22, 2024
1 parent 2306864 commit 1f6e811
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
38 changes: 27 additions & 11 deletions apps/trench/src/events/events.dao.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'
import { BadRequestException, Injectable } from '@nestjs/common'
import { ClickHouseService } from '../services/data/click-house/click-house.service'
import { escapeString, formatToClickhouseDate } from '../services/data/click-house/click-house.util'
import { Event, EventDTO, EventsQuery, PaginatedEventResponse } from './events.interface'
Expand All @@ -8,6 +8,7 @@ import { v4 as uuidv4 } from 'uuid'
import { mapRowToEvent } from './events.util'
import { Workspace } from '../workspaces/workspaces.interface'
import { getKafkaTopicFromWorkspace } from '../services/data/kafka/kafka.util'
import { isReadOnlyQuery } from '../queries/queries.util'

@Injectable()
export class EventsDao {
Expand Down Expand Up @@ -41,6 +42,8 @@ export class EventsDao {
endDate,
limit,
offset,
orderByField,
orderByDirection,
} = query

const maxRecords = Math.min(limit ?? 1000, 1000)
Expand Down Expand Up @@ -78,28 +81,41 @@ export class EventsDao {
}
}
if (startDate) {
conditions.push(`timestamp >= '${formatToClickhouseDate(new Date(startDate))}'`)
conditions.push(`timestamp >= '${escapeString(formatToClickhouseDate(new Date(startDate)))}'`)
}
if (endDate) {
conditions.push(`timestamp <= '${formatToClickhouseDate(new Date(endDate))}'`)
conditions.push(`timestamp <= '${escapeString(formatToClickhouseDate(new Date(endDate)))}'`)
}
if (uuid) {
conditions.push(`uuid = '${escapeString(uuid)}'`)
}

const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''

const orderByClause =
orderByField && orderByDirection
? `ORDER BY ${escapeString(orderByField)} ${escapeString(orderByDirection)}`
: 'ORDER BY timestamp DESC'
const limitClause = `LIMIT ${maxRecords}`
const offsetClause = offset ? `OFFSET ${offset}` : ''

const clickhouseQuery = `SELECT * FROM events ${whereClause} ${limitClause} ${offsetClause}`
const result = await this.clickhouse.queryResults(clickhouseQuery, workspace.databaseName)
const results = result.map((row: any) => mapRowToEvent(row))
const clickhouseQuery = `SELECT * FROM events ${whereClause} ${orderByClause} ${limitClause} ${offsetClause}`
if (!isReadOnlyQuery(clickhouseQuery)) {
throw new BadRequestException('The provided query is not read-only')
}

try {
const result = await this.clickhouse.queryResults(clickhouseQuery, workspace.databaseName)
const results = result.map((row: any) => mapRowToEvent(row))

return {
results: results,
limit: maxRecords,
offset: +offset || 0,
total: null,
return {
results: results,
limit: maxRecords,
offset: +offset || 0,
total: null,
}
} catch (error) {
throw new BadRequestException(`Error querying events: ${error.message}`)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/analytics-plugin-trench/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type TrenchConfig = {
};

export interface BaseEvent {
uuid?: string;
anonymousId?: string;
context?: {
active?: boolean;
Expand Down

0 comments on commit 1f6e811

Please sign in to comment.