Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/new_tracker_events_endpo…
Browse files Browse the repository at this point in the history
…int' into feature/new_tracker_tei_endpoint

# Conflicts:
#	src/data/events/EventsD2ApiRepository.ts
  • Loading branch information
xurxodev committed Jan 8, 2025
2 parents f82c371 + be67412 commit 3ae9fed
Showing 1 changed file with 32 additions and 13 deletions.
45 changes: 32 additions & 13 deletions src/data/events/EventsD2ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { D2Api } from "../../types/d2-api";
import { promiseMap } from "../../utils/common";
import { getD2APiFromInstance } from "../../utils/d2-utils";
import mime from "mime-types";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import { D2TrackerEvent, TrackerEventsResponse } from "@eyeseetea/d2-api/api/trackerEvents";
import { TrackerPostParams, TrackerPostRequest, TrackerPostResponse } from "@eyeseetea/d2-api/api/tracker";

export class EventsD2ApiRepository implements EventsRepository {
Expand Down Expand Up @@ -97,16 +97,19 @@ export class EventsD2ApiRepository implements EventsRepository {
};

const result = await promiseMap(orgUnits, async (orgUnit): Promise<D2TrackerEvent[]> => {
const { instances, total = 0, pageSize } = await fetchApi(orgUnit, 1);
const firstResponse = await fetchApi(orgUnit, 1);

const pageCount = Math.ceil(total / pageSize);
const pageCount = Math.ceil((firstResponse.total || 0) / firstResponse.pageSize);

const paginatedEvents = await promiseMap(_.range(2, pageCount + 1), async page => {
const { instances } = await fetchApi(orgUnit, page);
return instances;
const response = await fetchApi(orgUnit, page);

return this.extractEvents(response);
});

return [...instances, ..._.flatten(paginatedEvents)];
const events = this.extractEvents(firstResponse);

return [...events, ..._.flatten(paginatedEvents)];
});

return _(result)
Expand Down Expand Up @@ -150,16 +153,21 @@ export class EventsD2ApiRepository implements EventsRepository {

const result = await promiseMap(programStageIds, async programStage => {
const filteredEvents = await promiseMap(orgUnits, async orgUnit => {
const { instances, total = 0, pageSize } = await fetchApi(programStage, orgUnit, 1);
const firstResponse = await fetchApi(programStage, orgUnit, 1);

const pageCount = Math.ceil(total / pageSize);
const pageCount = Math.ceil(firstResponse.total || 0 / firstResponse.pageSize);

const paginatedEvents = await promiseMap(_.range(2, pageCount + 1), async page => {
const { instances } = await fetchApi(programStage, orgUnit, page);
return instances;
const response = await fetchApi(programStage, orgUnit, page);

const events = this.extractEvents(response);

return events;
});

return [...instances, ..._.flatten(paginatedEvents)];
const events = this.extractEvents(firstResponse);

return [...events, ..._.flatten(paginatedEvents)];
});

return _.flatten(filteredEvents);
Expand Down Expand Up @@ -191,15 +199,18 @@ export class EventsD2ApiRepository implements EventsRepository {

for (const programStage of programStageIds) {
for (const ids of _.chunk(filter, 300)) {
const { instances } = await this.api.tracker.events
const response = await this.api.tracker.events
.get({
programStage,
event: ids.join(";"),
fields: { $all: true },
skipPaging: true,
})
.getData();
result.push(...instances);

const events = this.extractEvents(response);

result.push(...events);
}
}

Expand Down Expand Up @@ -324,4 +335,12 @@ export class EventsD2ApiRepository implements EventsRepository {

return new File([blob], fileName, { type: fileResource.contentType });
}

extractEvents(response: TrackerEventsResponse): D2TrackerEvent[] {
return response.instances || (hasEventsProperty(response) ? response.events : []);
}
}

function hasEventsProperty(obj: any): obj is { events: D2TrackerEvent[] } {
return obj && Array.isArray(obj.events);
}

0 comments on commit 3ae9fed

Please sign in to comment.