Skip to content

Commit e2b66ac

Browse files
committed
Load cached trouble sheet data
1 parent 9ac5ab7 commit e2b66ac

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import * as libsqlClient from '@libsql/client';
2020
import cookieSession from 'cookie-session';
2121
import {initRoutes} from './routes';
2222
import {ensureCachedSheetDataTableExists} from './init-dependencies/google/ensure-cached-sheet-data-table-exists';
23-
import {loadCachedSheetData} from './load-cached-sheet-data';
23+
import {
24+
loadCachedSheetData,
25+
loadCachedTroubleTicketData,
26+
} from './load-cached-sheet-data';
2427
import {timeAsync} from './util';
2528

2629
// Dependencies and Config
@@ -131,6 +134,31 @@ void (async () => {
131134
);
132135
}
133136

137+
await timeAsync(elapsedNs =>
138+
deps.logger.info(
139+
'Loaded cached trouble ticket events in %sms',
140+
elapsedNs / (1000 * 1000)
141+
)
142+
)(
143+
Promise.all([
144+
pipe(
145+
loadCachedTroubleTicketData(
146+
deps.getCachedTroubleTicketData,
147+
deps.sharedReadModel.updateState
148+
),
149+
TE.match(
150+
failure => {
151+
deps.logger.warn(
152+
'Failed to load cached trouble ticket data - continuing anyway: %s',
153+
failure
154+
);
155+
},
156+
_ => {}
157+
)
158+
)(),
159+
])
160+
);
161+
134162
server.listen(conf.PORT, () => {
135163
deps.logger.info({port: conf.PORT}, 'Server listening');
136164
if (conf.PUBLIC_URL.includes('localhost')) {

src/load-cached-sheet-data.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import {Logger} from 'pino';
22
import * as E from 'fp-ts/Either';
33
import * as O from 'fp-ts/Option';
4+
import * as TE from 'fp-ts/TaskEither';
5+
import * as RA from 'fp-ts/ReadonlyArray';
46
import {Dependencies} from './dependencies';
57
import {Equipment} from './read-models/shared-state/return-types';
8+
import {pipe} from 'fp-ts/lib/function';
9+
import {TROUBLE_TICKET_RESPONSES_SHEET} from './read-models/shared-state/async-apply-external-event-sources';
10+
import {failureWithStatus} from './types/failure-with-status';
11+
import {StatusCodes} from 'http-status-codes';
12+
import {formatValidationErrors} from 'io-ts-reporters';
613

714
export const loadCachedSheetData =
815
(
@@ -73,3 +80,42 @@ export const loadCachedSheetData =
7380
}
7481
}
7582
};
83+
84+
export const loadCachedTroubleTicketData = (
85+
getCachedTroubleTicketData: Dependencies['getCachedTroubleTicketData'],
86+
updateState: Dependencies['sharedReadModel']['updateState']
87+
) =>
88+
pipe(
89+
getCachedTroubleTicketData(TROUBLE_TICKET_RESPONSES_SHEET),
90+
TE.flatMap(cacheData =>
91+
pipe(
92+
cacheData,
93+
O.match(
94+
() =>
95+
TE.left(
96+
failureWithStatus(
97+
'No cached trouble data found',
98+
StatusCodes.NOT_FOUND
99+
)
100+
),
101+
dataValidation =>
102+
pipe(
103+
dataValidation.cached_data,
104+
E.match(
105+
errs =>
106+
TE.left(
107+
failureWithStatus(
108+
`Cached trouble data is malformed: ${formatValidationErrors(errs).join(',')}`,
109+
StatusCodes.BAD_REQUEST
110+
)
111+
),
112+
data => TE.right(data)
113+
)
114+
)
115+
)
116+
)
117+
),
118+
TE.map(loadedData => {
119+
pipe(loadedData, RA.map(updateState));
120+
})
121+
);

src/read-models/shared-state/async-apply-external-event-sources.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {extractTimestamp} from '../../google/util';
3535
const ROW_BATCH_SIZE = 200;
3636
const EXPECTED_TROUBLE_TICKET_RESPONSE_SHEET_NAME = 'Form Responses 1';
3737
const TROUBLE_TICKET_SYNC_INTERVAL = Duration.fromMillis(1000 * 60 * 20);
38-
const TROUBLE_TICKET_RESPONSES_SHEET =
38+
export const TROUBLE_TICKET_RESPONSES_SHEET =
3939
'1ZSQoCOyw4ss9JuriySQX04gISfFnb4MadNpPFkEYW84'; // FIXME - Make this configurable.
4040

4141
const pullNewEquipmentQuizResultsForSheet = async (

0 commit comments

Comments
 (0)