Skip to content

Commit

Permalink
Use exclusive bound on events produced from rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan2u committed Sep 20, 2024
1 parent 34c8e88 commit 8a1ff0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
14 changes: 4 additions & 10 deletions src/read-models/shared-state/async-apply-external-event-sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,19 @@ export const pullNewEquipmentQuizResults = (
return async () => [] as ReadonlyArray<QzEvent>;
}
const trainingSheetId = equipment.trainingSheetId.value;
logger.info(
`Scanning training sheet ${trainingSheetId}. Pulling google sheet data...`
);
logger = logger.child({trainingSheetId});
logger.info('Scanning training sheet. Pulling google sheet data...');
return pipe(
pullGoogleSheetData(logger, trainingSheetId),
TE.map(
extractGoogleSheetData(
logger.child({trainingSheetId: trainingSheetId}),
trainingSheetId,
equipment.lastQuizResult,
)
extractGoogleSheetData(logger, trainingSheetId, equipment.lastQuizResult)
),
TE.map(RA.flatten),
// eslint-disable-next-line @typescript-eslint/require-await
TE.getOrElse(err => async () => {
logger.error(
'Failed to receive data from google sheets for equipment %s training sheet %o: %s',
'Failed to receive data from google sheets for equipment %s: %s',
equipment.name,
equipment.trainingSheetId,
err.message
);
return [] as ReadonlyArray<QzEvent>;
Expand Down
21 changes: 14 additions & 7 deletions src/training-sheets/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import {UUID} from 'io-ts-types';
import {DateTime} from 'luxon';
import {QzEvent} from '../types/qz-event';
import {sheets_v4} from '@googleapis/sheets';
import { Equipment } from '../read-models/shared-state/return-types';

// Bounds to prevent clearly broken parsing.
const MIN_RECOGNISED_MEMBER_NUMBER = 0;
const MAX_RECOGNISED_MEMBER_NUMBER = 1_000_000;
const MAX_RECOGNISED_MEMBER_NUMBER = 10_000;

const MIN_VALID_TIMESTAMP_EPOCH_S = 1262304000; // Year 2010
const MAX_VALID_TIMESTAMP_EPOCH_S = 4102444800; // Year 2100
Expand Down Expand Up @@ -254,7 +253,12 @@ const extractFromRow =
};

export const extractGoogleSheetData =
(logger: Logger, trainingSheetId: string) =>
(
logger: Logger,
trainingSheetId: string,
equipmentId: UUID,
eventsFromExclusive: O.Option<DateTime>
) =>
(
spreadsheet: sheets_v4.Schema$Spreadsheet
): ReadonlyArray<ReadonlyArray<QzEvent>> =>
Expand Down Expand Up @@ -299,9 +303,7 @@ export const extractGoogleSheetData =
extractQuizSheetInformation(logger),
O.match(
() => {
logger.warn(
`Failed to extract sheet info '${trainingSheetId}' for equipment '${equipmentId}'`
);
logger.warn('Failed to extract sheet info');
return [];
},
sheetInfo =>
Expand All @@ -316,7 +318,12 @@ export const extractGoogleSheetData =
timezone
)
),
RA.filterMap(e => e)
RA.filterMap(e => e),
RA.filter(
e =>
O.isNone(eventsFromExclusive) ||
e.timestampEpochS > eventsFromExclusive.value.toSeconds()
)
)
)
);
Expand Down

0 comments on commit 8a1ff0f

Please sign in to comment.