Skip to content

Commit

Permalink
fix: prevent processing the current hour
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Dec 16, 2024
1 parent 9059a86 commit 746b4ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/lambda-analytic-cloudfront/src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { basename } from 'path';

import { byDay, getYesterday } from './date.js';
import { Elastic } from './elastic.js';
import { FileProcess } from './log.reader.js';
import { FileProcess, toFullDate } from './log.reader.js';
import { LogStats } from './log.stats.js';

const gzipPromise = promisify(gzip);
Expand Down Expand Up @@ -58,10 +58,14 @@ export async function main(req: LambdaRequest): Promise<void> {
for (let hour = 23; hour >= 0; hour--) {
processedCount++;
if (processedCount > MaxToProcess) break;

const hourOfDay = String(hour).padStart(2, '0');
const prefix = `${prefixByDay}-${hourOfDay}`;

const targetDate = new Date(toFullDate(prefixByDay + 'T' + hourOfDay));
const dateDiff = Date.now() - targetDate.getTime();
// Do not process anything within a hour of the current time as some logs take a while to propagate into the bucket
if (dateDiff < 60 * 60 * 1000) continue;

// Create a folder structure of /YYYY/MM/
const cacheFolderParts = prefix.slice(0, 7).replace('-', '/');

Expand Down
2 changes: 1 addition & 1 deletion packages/lambda-analytic-cloudfront/src/log.reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function hideApiKey(str: string): string {
}

const empty: Record<string, number> = { webp: 0, png: 0, jpeg: 0 };
function toFullDate(x: string): string {
export function toFullDate(x: string): string {
if (x.length === IsoDateMonth) return `${x}-01T00:00:00.000Z`;
if (x.length === IsoDateDay) return `${x}T00:00:00.000Z`;
if (x.length === IsoDateHour) return `${x}:00:00.000Z`;
Expand Down

0 comments on commit 746b4ee

Please sign in to comment.