Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

chore(guardoni): replaced moment with date-fns #355

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion YCAI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"io-ts-types": "^0.5.16",
"lodash": "^4.17.21",
"marked": "^2.1.3",
"moment": "^2.29.1",
"monocle-ts": "^2.3.11",
"nacl": "^0.1.3",
"newtype-ts": "^0.3.4",
Expand Down
1 change: 0 additions & 1 deletion guardoni/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"io-ts": "^2.2.16",
"io-ts-types": "^0.5.16",
"lodash": "^4.17.21",
"moment": "^2.29.1",
"nconf": "^0.11.3",
"node-fetch": "^2.6.6",
"puppeteer-core": "^13.1.2",
Expand Down
21 changes: 21 additions & 0 deletions guardoni/src/guardoni/__tests__/domainSpecific.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import subSeconds from 'date-fns/subSeconds';
import {
getMaybeScreenshotFilename,
getScreenshotName,
} from '../domainSpecific';

describe('Domain specific API', () => {
describe('Screenshot path', () => {
it('succeeds with undefined when last screenshot < 5s ago', () => {
const lastScreenTime = subSeconds(new Date(), 3);
expect(getMaybeScreenshotFilename(lastScreenTime)).toBe(null);
});

it('succeeds with screenshot file name', () => {
const lastScreenTime = subSeconds(new Date(), 6);
const screenshotName = getScreenshotName('null');
const maybeScreenshotName = getMaybeScreenshotFilename(lastScreenTime);
expect(maybeScreenshotName?.startsWith(screenshotName)).toBe(true);
});
});
});
36 changes: 18 additions & 18 deletions guardoni/src/guardoni/domainSpecific.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import D from 'debug';
import * as fs from 'fs';
import _ from 'lodash';
import moment from 'moment';
import nconf from 'nconf';
import path from 'path';
import * as puppeteer from 'puppeteer-core';
import url from 'url';
import { GuardoniProfile } from './types';
import subSeconds from 'date-fns/subSeconds';
import differenceInSeconds from 'date-fns/differenceInSeconds';
import format from 'date-fns/format';

const debug = D('guardoni:youtube');
const logreqst = D('guardoni:requests');
Expand All @@ -19,39 +21,38 @@ const SCREENSHOT_MARKER = 'SCREENSHOTMARKER';
const scrnshtrgxp = new RegExp(SCREENSHOT_MARKER);

interface GlobalConfig {
lastScreenTime: moment.Moment;
lastScreenTime: Date;
currentURLlabel: string | null;
screenshotPrefix: string | null;
interval: NodeJS.Timer | null;
publicKeySpot: string | null;
}

const globalConfig: GlobalConfig = {
lastScreenTime: moment().subtract(4, 'seconds'),
lastScreenTime: subSeconds(new Date(), 4),
currentURLlabel: null,
screenshotPrefix: null,
interval: null,
publicKeySpot: null,
};

function getScreenshotFilename(): string | null {
export function getScreenshotName(prefix: string): string {
return `${prefix}-${format(new Date(), 'yyyy-MM-dd-KK:mm')}.png`;
}

export function getMaybeScreenshotFilename(
lastScreenTime: Date
): string | null {
/* this function return null if no screenshot has to be taken,
* and the criteria is to take max one screen every 5 seconds */
const now = moment();
if (
moment
.duration((now as any) - (globalConfig.lastScreenTime as any))
.asSeconds() < 5
)
return null;
const now = new Date();
if (differenceInSeconds(now, lastScreenTime) < 5) return null;

globalConfig.lastScreenTime = now;
/* screenshotPrefix would exist as a directory */
return path.join(
globalConfig.screenshotPrefix as any,
`${
globalConfig.currentURLlabel
}-${globalConfig.lastScreenTime.toISOString()}.jpeg`
globalConfig.screenshotPrefix ?? '',
getScreenshotName(globalConfig.currentURLlabel as any)
);
}

Expand All @@ -68,7 +69,7 @@ async function consoleLogParser(
globalConfig.publicKeySpot = material.response.publicKey;
}
if (consoleline.match(scrnshtrgxp)) {
const fdestname = getScreenshotFilename();
const fdestname = getMaybeScreenshotFilename(globalConfig.lastScreenTime);
// if the screenshot are less than 5 seconds close, the function
// above would return null, so we don't take it.
if (fdestname) {
Expand Down Expand Up @@ -236,8 +237,7 @@ async function afterWait(page: puppeteer.Page, directive: any): Promise<void> {
}

if (directive.screenshot) {
const screendumpf =
moment().format('YYYYMMDD-HHmm') + '-' + directive.name + '.png';
const screendumpf = getScreenshotName(directive.name);
const fullpath = path.join(directive.profile, screendumpf);
debug('afterWait: collecting screenshot in %s', fullpath);

Expand Down
Loading