Skip to content

Commit

Permalink
update engagement performance to use filtering (#4366)
Browse files Browse the repository at this point in the history
* update engagement performance to use filtering

* remove useless await
  • Loading branch information
lctrt authored Sep 4, 2024
1 parent 0bf4d50 commit 3ff1b40
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
69 changes: 37 additions & 32 deletions apps/asap-cli/src/scripts/algolia/performance/engagement.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { SearchIndex } from 'algoliasearch';

import { timeRanges } from '@asap-hub/model';

import {
deletePreviousObjects,
getAllHits,
Expand All @@ -19,37 +21,40 @@ export const processEngagementPerformance = async (index: SearchIndex) => {

await deletePreviousObjects(index, type);

const getPaginatedHits = (page: number) =>
index.search<EngagementHit>('', {
filters: `__meta.type:"${type}"`,
attributesToRetrieve: [
'eventCount',
'totalSpeakerCount',
'uniqueAllRolesCount',
'uniqueKeyPersonnelCount',
],
page,
hitsPerPage: 50,
});

const hits = await getAllHits<EngagementHit>(getPaginatedHits);

await index.saveObject(
{
events: getPerformanceMetrics(hits.map((hit) => hit.eventCount)),
totalSpeakers: getPerformanceMetrics(
hits.map((hit) => hit.totalSpeakerCount),
),
uniqueAllRoles: getPerformanceMetrics(
hits.map((hit) => hit.uniqueAllRolesCount),
),
uniqueKeyPersonnel: getPerformanceMetrics(
hits.map((hit) => hit.uniqueKeyPersonnelCount),
),
__meta: {
type: `${type}-performance`,
timeRanges.forEach(async (range) => {
const getPaginatedHits = (page: number) =>
index.search<EngagementHit>('', {
filters: `__meta.range:"${range}" AND __meta.type:"${type}"`,
attributesToRetrieve: [
'eventCount',
'totalSpeakerCount',
'uniqueAllRolesCount',
'uniqueKeyPersonnelCount',
],
page,
hitsPerPage: 50,
});

const hits = await getAllHits<EngagementHit>(getPaginatedHits);

await index.saveObject(
{
events: getPerformanceMetrics(hits.map((hit) => hit.eventCount)),
totalSpeakers: getPerformanceMetrics(
hits.map((hit) => hit.totalSpeakerCount),
),
uniqueAllRoles: getPerformanceMetrics(
hits.map((hit) => hit.uniqueAllRolesCount),
),
uniqueKeyPersonnel: getPerformanceMetrics(
hits.map((hit) => hit.uniqueKeyPersonnelCount),
),
__meta: {
range,
type: `${type}-performance`,
},
},
},
{ autoGenerateObjectIDIfNotExist: true },
);
{ autoGenerateObjectIDIfNotExist: true },
);
});
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { timeRanges } from '@asap-hub/model';
import { SearchIndex } from 'algoliasearch';
import { processEngagementPerformance } from '../engagement';

Expand Down Expand Up @@ -48,22 +49,27 @@ describe('processEngagementPerformance', () => {
'old-performance-3',
]);

expect(mockIndex.search).toHaveBeenCalledWith('', {
filters: `__meta.type:"engagement"`,
attributesToRetrieve: [
'eventCount',
'totalSpeakerCount',
'uniqueAllRolesCount',
'uniqueKeyPersonnelCount',
],
page: expect.any(Number),
hitsPerPage: 50,
timeRanges.forEach((range) => {
expect(mockIndex.search).toHaveBeenCalledWith('', {
filters: `__meta.range:"${range}" AND __meta.type:"engagement"`,
attributesToRetrieve: [
'eventCount',
'totalSpeakerCount',
'uniqueAllRolesCount',
'uniqueKeyPersonnelCount',
],
page: expect.any(Number),
hitsPerPage: 50,
});
});

// one for each time range
expect(mockIndex.saveObject).toHaveBeenCalledTimes(5);
expect(mockIndex.saveObject).toHaveBeenCalledWith(
{
__meta: {
type: 'engagement-performance',
range: 'all',
},
events: {
aboveAverageMax: -1,
Expand Down

0 comments on commit 3ff1b40

Please sign in to comment.