From 6326cdb7db91ea98e3c061ff49c1b48ccaeb2724 Mon Sep 17 00:00:00 2001 From: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:41:45 -0600 Subject: [PATCH] fix(telemetry): :arrow_down: Reduce event syncs (#1296) * fix(telemetry): :arrow_down: Reduce event syncs Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> * fix(telemetry): :test_tube: Fix tests Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --------- Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com> --- add-on/src/lib/trackers/requestTracker.ts | 4 +++- .../lib/trackers/requestTrackers.test.ts | 22 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/add-on/src/lib/trackers/requestTracker.ts b/add-on/src/lib/trackers/requestTracker.ts index 4f3a97009..bd4ba9e8b 100644 --- a/add-on/src/lib/trackers/requestTracker.ts +++ b/add-on/src/lib/trackers/requestTracker.ts @@ -2,6 +2,8 @@ import debug from 'debug' import type browser from 'webextension-polyfill' import { trackEvent } from '../telemetry.js' +export const DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL = 1000 * 60 * 60 + export class RequestTracker { private readonly eventKey: 'url-observed' | 'url-resolved' private readonly flushInterval: number @@ -9,7 +11,7 @@ export class RequestTracker { private lastSync: number = Date.now() private requestTypeStore: { [key in browser.WebRequest.ResourceType]?: number } = {} - constructor (eventKey: 'url-observed' | 'url-resolved', flushInterval = 1000 * 60 * 5) { + constructor (eventKey: 'url-observed' | 'url-resolved', flushInterval = DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) { this.eventKey = eventKey this.log = debug(`ipfs-companion:request-tracker:${eventKey}`) this.log.error = debug(`ipfs-companion:request-tracker:${eventKey}:error`) diff --git a/test/functional/lib/trackers/requestTrackers.test.ts b/test/functional/lib/trackers/requestTrackers.test.ts index 0e0b936c1..8f3d52cff 100644 --- a/test/functional/lib/trackers/requestTrackers.test.ts +++ b/test/functional/lib/trackers/requestTrackers.test.ts @@ -2,7 +2,7 @@ import { expect } from 'chai'; import sinon from 'sinon'; import browser from 'sinon-chrome'; import PatchedCountly from 'countly-sdk-web' -import { RequestTracker } from './../../../../add-on/src/lib/trackers/requestTracker.js' +import { DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL, RequestTracker } from './../../../../add-on/src/lib/trackers/requestTracker.js' const sinonSandBox = sinon.createSandbox() describe('lib/trackers/requestTracker', () => { @@ -32,11 +32,11 @@ describe('lib/trackers/requestTracker', () => { it('should track a request', async () => { await requestTracker.track({ type: 'main_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.calledWith(countlySDKStub.add_event, { key: 'url-observed', count: 1, - dur: 300000, + dur: 3600000, segmentation: { main_frame: 1 } @@ -47,11 +47,11 @@ describe('lib/trackers/requestTracker', () => { await requestTracker.track({ type: 'main_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) await requestTracker.track({ type: 'sub_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) await requestTracker.track({ type: 'xmlHTTPRequest' } as browser.WebRequest.OnBeforeRequestDetailsType) - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.calledWith(countlySDKStub.add_event, { key: 'url-observed', count: 3, - dur: 300000, + dur: 3600000, segmentation: { main_frame: 1, sub_frame: 1, @@ -61,7 +61,7 @@ describe('lib/trackers/requestTracker', () => { }) it('should not send event if count is 0', async () => { - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.notCalled(countlySDKStub.add_event) }) @@ -79,11 +79,11 @@ describe('lib/trackers/requestTracker', () => { it('should track a request', async () => { await requestTracker.track({ type: 'main_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.calledWith(countlySDKStub.add_event, { key: 'url-resolved', count: 1, - dur: 300000, + dur: 3600000, segmentation: { main_frame: 1 } @@ -94,11 +94,11 @@ describe('lib/trackers/requestTracker', () => { await requestTracker.track({ type: 'main_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) await requestTracker.track({ type: 'sub_frame' } as browser.WebRequest.OnBeforeRequestDetailsType) await requestTracker.track({ type: 'xmlHTTPRequest' } as browser.WebRequest.OnBeforeRequestDetailsType) - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.calledWith(countlySDKStub.add_event, { key: 'url-resolved', count: 3, - dur: 300000, + dur: 3600000, segmentation: { main_frame: 1, sub_frame: 1, @@ -108,7 +108,7 @@ describe('lib/trackers/requestTracker', () => { }) it('should not send event if count is 0', async () => { - clock.tick(1000 * 60 * 6) + clock.tick(DEFAULT_REQUEST_TRACKER_FLUSH_INTERVAL) sinon.assert.notCalled(countlySDKStub.add_event) })