Skip to content

Commit

Permalink
fix(telemetry): ⬇️ Reduce event syncs (#1296)
Browse files Browse the repository at this point in the history
* fix(telemetry): ⬇️ Reduce event syncs

Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>

* fix(telemetry): 🧪 Fix tests

Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>

---------

Signed-off-by: Nishant Arora <1895906+whizzzkid@users.noreply.github.com>
  • Loading branch information
whizzzkid committed Sep 25, 2023
1 parent 9f852ef commit 6326cdb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion add-on/src/lib/trackers/requestTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ 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
private readonly log: debug.Debugger & { error?: debug.Debugger }
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`)
Expand Down
22 changes: 11 additions & 11 deletions test/functional/lib/trackers/requestTrackers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand All @@ -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)
})
Expand All @@ -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
}
Expand All @@ -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,
Expand All @@ -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)
})
Expand Down

0 comments on commit 6326cdb

Please sign in to comment.