Skip to content

Commit f163ff0

Browse files
committed
refactor: validate errors are written into s3
1 parent 517dd7b commit f163ff0

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

packages/lambda-analytic-cloudfront/src/__test__/analytics.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe('analytic lambda', () => {
2222
fsa.register('mem://', memory);
2323
memory.files.clear();
2424

25-
Elastic.indexDelay = 1;
25+
Elastic.indexDelay = 1; // do not wait between requests
26+
Elastic.minRequestCount = 1; // index everything
2627
Elastic._client = undefined;
2728
});
2829

@@ -111,9 +112,9 @@ describe('analytic lambda', () => {
111112
const files = [...memory.files.keys()];
112113
assert.equal(files.length, 2); // two files one input one output
113114

114-
assert.ok(
115-
files[1].startsWith(`mem://cache/errors-${new Date().toISOString().slice(0, 12)}`),
116-
// ${shortDate.slice(0, 4)}/${shortDate.slice(5, 7)}/${shortDate}.ndjson.gz`,
117-
);
115+
assert.ok(files[1].startsWith(`mem://cache/errors-${new Date().toISOString().slice(0, 12)}`));
116+
117+
const data = await fsa.read(new URL(files[1]));
118+
assert.ok(data.toString().includes(JSON.stringify('Hello')));
118119
});
119120
});

packages/lambda-analytic-cloudfront/src/elastic.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export class ElasticClient {
88
/** Between index requests delay this amount */
99
indexDelay: number = 200;
1010

11+
/**
12+
* Do not index analytics for buckets that contain less than this number of total requests
13+
*
14+
* @example
15+
* `1` - drop all requests where total requests <= 1
16+
*
17+
*/
18+
minRequestCount: number = 1;
19+
1120
get client(): Client {
1221
if (this._client != null) return this._client;
1322

@@ -44,7 +53,7 @@ export class ElasticClient {
4453
const ret = await client.bulk({ operations });
4554

4655
if (ret.errors) {
47-
errors.push(ret);
56+
errors.push({ prefix, errors: ret.errors });
4857
throw new Error('Failed to index: ' + prefix);
4958
}
5059
// Give it a little bit of time to index
@@ -53,7 +62,8 @@ export class ElasticClient {
5362
}
5463

5564
for (const rec of combined) {
56-
if (rec.total < 1) {
65+
// skip over roll ups that are less than
66+
if (rec.total <= this.minRequestCount) {
5767
skipHits++;
5868
continue;
5969
}

packages/lambda-analytic-cloudfront/src/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export async function handler(): Promise<void> {
138138
if (Elastic.errors.length > 0) {
139139
const errorLocation = new URL(`./errors-${new Date().toISOString()}.json`, CacheLocation);
140140
logger.fatal({ errorLocation: errorLocation.href }, 'log:index:failed');
141-
await fsa.write(errorLocation, JSON.stringify(rets));
141+
await fsa.write(errorLocation, JSON.stringify(Elastic.errors));
142142
}
143143

144144
let failed = false;

0 commit comments

Comments
 (0)