(
sql: string,
options: GetEventsOptions = {}
)
| 309 | export const getEventMetasCached = cacheable(getEventMetas, 60 * 5); |
| 310 | |
| 311 | export async function getEvents( |
| 312 | sql: string, |
| 313 | options: GetEventsOptions = {} |
| 314 | ): Promise<IServiceEvent[]> { |
| 315 | const events = await chQuery<IClickhouseEvent>(sql); |
| 316 | const projectId = events[0]?.project_id; |
| 317 | if (options.profile && projectId) { |
| 318 | const ids = events |
| 319 | .filter((e) => e.device_id !== e.profile_id) |
| 320 | .map((e) => e.profile_id); |
| 321 | const profiles = await getProfilesCached(ids, projectId); |
| 322 | |
| 323 | const map = new Map<string, IServiceProfile>(); |
| 324 | for (const profile of profiles) { |
| 325 | map.set(profile.id, profile); |
| 326 | } |
| 327 | |
| 328 | for (const event of events) { |
| 329 | event.profile = map.get(event.profile_id) ?? { |
| 330 | id: event.profile_id, |
| 331 | email: '', |
| 332 | avatar: '', |
| 333 | firstName: '', |
| 334 | lastName: '', |
| 335 | createdAt: new Date(), |
| 336 | lastSeenAt: new Date(), |
| 337 | projectId, |
| 338 | isExternal: false, |
| 339 | properties: {}, |
| 340 | groups: [], |
| 341 | }; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (options.meta && projectId) { |
| 346 | const metas = await getEventMetasCached(projectId); |
| 347 | const map = new Map<string, EventMeta>(); |
| 348 | for (const meta of metas) { |
| 349 | map.set(meta.name, meta); |
| 350 | } |
| 351 | for (const event of events) { |
| 352 | event.meta = map.get(event.name); |
| 353 | } |
| 354 | } |
| 355 | return events.map(transformEvent); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Persist an event to ClickHouse (via the buffer) and upsert the profile |
no test coverage detected