From 3c4edce7c85117a19ad62cfcdb00c7316a650d6c Mon Sep 17 00:00:00 2001 From: Louis Murerwa <35416595+louismurerwa@users.noreply.github.com> Date: Mon, 10 Jun 2024 16:16:47 -0400 Subject: [PATCH] Fix Type Error in Ocular Telemeter (#124) --- packages/ocular-telemetry/src/store.ts | 75 +++---- packages/ocular-telemetry/src/telemeter.ts | 183 +++++++++--------- .../src/util/telemetry-dispatcher.ts | 22 +-- 3 files changed, 139 insertions(+), 141 deletions(-) diff --git a/packages/ocular-telemetry/src/store.ts b/packages/ocular-telemetry/src/store.ts index 6ae5da4d..195a6824 100644 --- a/packages/ocular-telemetry/src/store.ts +++ b/packages/ocular-telemetry/src/store.ts @@ -1,72 +1,73 @@ -import Configstore from "configstore" -import path from "path" +import Configstore from "configstore"; +import path from "path"; -import InMemoryConfigStore from "./util/in-memory-config" -import isTruthy from "./util/is-truthy" -import OutboxStore from "./util/outbox-store" +import InMemoryConfigStore from "./util/in-memory-config"; +import isTruthy from "./util/is-truthy"; +import OutboxStore from "./util/outbox-store"; class Store { + private config_: typeof InMemoryConfigStore; + private outbox_: OutboxStore; + private disabled_: boolean; - private config_: typeof InMemoryConfigStore - private outbox_: OutboxStore - private disabled_: boolean - - private static instance:Store + private static instance: Store; constructor() { try { - this.config_ = new Configstore(`ocular`, {}, { globalConfigPath: true }) + this.config_ = new Configstore(`ocular`, {}, { globalConfigPath: true }); } catch (e) { - console.error(e) + console.error(e); } - const baseDir = path.dirname(this.config_.path) - this.outbox_ = new OutboxStore(baseDir) + const baseDir = path.dirname(this.config_.path); + this.outbox_ = new OutboxStore(baseDir); - this.disabled_ = isTruthy(process.env.OCULAR_DISABLE_TELEMETRY) + this.disabled_ = isTruthy(process.env.OCULAR_DISABLE_TELEMETRY); } - static getInstance():Store{ - if(!Store.instance){ - Store.instance = new Store() + static getInstance(): Store { + if (!Store.instance) { + Store.instance = new Store(); } - return Store.instance + return Store.instance; } - public getQueueSize() :number{ - return this.outbox_.getSize() + public getQueueSize(): number { + return this.outbox_.getSize(); } - public getQueueCount():number { - return this.outbox_.getCount() + public getQueueCount(): number { + return this.outbox_.getCount(); } - public addEvent(event):string|null { + public addEvent(event): string | null { if (this.disabled_) { - return + return; } - const eventString = JSON.stringify(event) - return this.outbox_.appendToBuffer(eventString + `\n`) + const eventString = JSON.stringify(event); + return this.outbox_.appendToBuffer(eventString + `\n`); } - public async flushEvents(handler: (events: any[]) => Promise): Promise { - return await this.outbox_.startFlushEvents(async (eventData:string) => { + public async flushEvents( + handler: (events: any[]) => Promise + ): Promise { + return await this.outbox_.startFlushEvents(async (eventData: string) => { const events = eventData .split(`\n`) .filter((e) => e && e.length > 2) - .map((e) => JSON.parse(e)) + .map((e) => JSON.parse(e)); - return await handler(events) - }) + return await handler(events); + }); } - public getConfig(path:string) :any{ - return this.config_.get(path) + public getConfig(path: string): any { + return this.config_.get(path); } - public setConfig(path:string, val:any) :void{ - return this.config_.set(path, val) + public setConfig(path: string, val: any): void { + return this.config_.set(path, val); } } -export default Store.getInstance() \ No newline at end of file +export default Store; diff --git a/packages/ocular-telemetry/src/telemeter.ts b/packages/ocular-telemetry/src/telemeter.ts index ca75982f..743c1f74 100644 --- a/packages/ocular-telemetry/src/telemeter.ts +++ b/packages/ocular-telemetry/src/telemeter.ts @@ -1,22 +1,22 @@ -import fs from "fs" -import isDocker from "is-docker" -import os from "os" -import { join, sep } from "path" -import { v4 as uuidv4 } from "uuid" - -import Store from "./store" -import createFlush from "./util/create-flush" -import getTermProgram from "./util/get-term-program" -import { getCIName, isCI } from "./util/is-ci" -import isTruthy from "./util/is-truthy" -import showAnalyticsNotification from "./util/show-notification" - -const OCULAR_TELEMETRY_VERBOSE = process.env.OCULAR_TELEMETRY_VERBOSE || false - -interface Options{ - flushAt: number - maxQueueSize: number - flushInterval: number +import fs from "fs"; +import isDocker from "is-docker"; +import os from "os"; +import { join, sep } from "path"; +import { v4 as uuidv4 } from "uuid"; + +import Store from "./store"; +import createFlush from "./util/create-flush"; +import getTermProgram from "./util/get-term-program"; +import { getCIName, isCI } from "./util/is-ci"; +import isTruthy from "./util/is-truthy"; +import showAnalyticsNotification from "./util/show-notification"; + +const OCULAR_TELEMETRY_VERBOSE = process.env.OCULAR_TELEMETRY_VERBOSE || false; + +interface Options { + flushAt: number; + maxQueueSize: number; + flushInterval: number; } interface OsInfo { @@ -32,66 +32,64 @@ interface OsInfo { } class Telemeter { - - private store_: typeof Store - private flushAt: number - private maxQueueSize: number - private flushInterval: number - private flushed: boolean - private queueSize_: number - private queueCount_:number - private machineId: string|unknown - private trackingEnabled: boolean|unknown - private featureFlags_:Set - private osInfo:OsInfo - private apps_:Array - private plugins_:Array - private timer:NodeJS.Timeout|undefined - - - constructor(options :Options) { - - this.flushAt = Math.max(options.flushAt, 1) - this.maxQueueSize = options.maxQueueSize - this.flushInterval = options.flushInterval - this.flushed = false - - this.queueSize_ = this.store_.getQueueSize() - this.queueCount_ = this.store_.getQueueCount() - - this.featureFlags_ = new Set() - this.apps_ = [] - this.plugins_ = [] + private store_: Store; + private flushAt: number; + private maxQueueSize: number; + private flushInterval: number; + private flushed: boolean; + private queueSize_: number; + private queueCount_: number; + private machineId: string | unknown; + private trackingEnabled: boolean | unknown; + private featureFlags_: Set; + private osInfo: OsInfo; + private apps_: Array; + private plugins_: Array; + private timer: NodeJS.Timeout | undefined; + + constructor(options: Options) { + this.store_ = new Store(); + this.flushAt = Math.max(options.flushAt, 1); + this.maxQueueSize = options.maxQueueSize; + this.flushInterval = options.flushInterval; + this.flushed = false; + + this.queueSize_ = this.store_.getQueueSize(); + this.queueCount_ = this.store_.getQueueCount(); + + this.featureFlags_ = new Set(); + this.apps_ = []; + this.plugins_ = []; } - public getMachineId() :string|unknown{ + public getMachineId(): string | unknown { if (this.machineId) { - return this.machineId + return this.machineId; } - let machineId = this.store_.getConfig(`telemetry.machine_id`) + let machineId = this.store_.getConfig(`telemetry.machine_id`); if (typeof machineId !== `string`) { - machineId = uuidv4() - this.store_.setConfig(`telemetry.machine_id`, machineId) + machineId = uuidv4(); + this.store_.setConfig(`telemetry.machine_id`, machineId); } - this.machineId = machineId - return machineId + this.machineId = machineId; + return machineId; } - public isTrackingEnabled() :string|unknown{ + public isTrackingEnabled(): string | unknown { // Cache the result if (this.trackingEnabled !== undefined) { - return this.trackingEnabled + return this.trackingEnabled; } - let enabled = this.store_.getConfig(`telemetry.enabled`) + let enabled = this.store_.getConfig(`telemetry.enabled`); if (enabled === undefined || enabled === null) { if (!isCI()) { // showAnalyticsNotification() } - enabled = true - this.store_.setConfig(`telemetry.enabled`, enabled) + enabled = true; + this.store_.setConfig(`telemetry.enabled`, enabled); } - this.trackingEnabled = enabled - return enabled + this.trackingEnabled = enabled; + return enabled; } public getOsInfo(): OsInfo { @@ -114,33 +112,33 @@ class Telemeter { return osInfo; } - public getOcularVersion() :string{ + public getOcularVersion(): string { try { - const packageJson = require.resolve(`@ocular/ocular/package.json`) - const { version } = JSON.parse(fs.readFileSync(packageJson, `utf-8`)) - return version + const packageJson = require.resolve(`@ocular/ocular/package.json`); + const { version } = JSON.parse(fs.readFileSync(packageJson, `utf-8`)); + return version; } catch (e) { if (isTruthy(OCULAR_TELEMETRY_VERBOSE)) { - console.error("failed to get ocular version", e) + console.error("failed to get ocular version", e); } } - return `-0.0.0` + return `-0.0.0`; } - public setTelemetryEnabled(enabled:boolean):void { - this.trackingEnabled = enabled - this.store_.setConfig(`telemetry.enabled`, enabled) + public setTelemetryEnabled(enabled: boolean): void { + this.trackingEnabled = enabled; + this.store_.setConfig(`telemetry.enabled`, enabled); } - public track(event:string, data:object):void { - return this.enqueue_(event, data) + public track(event: string, data: object): void { + return this.enqueue_(event, data); } - public enqueue_(type:string, data:object):void { + public enqueue_(type: string, data: object): void { const event = { - event:type, + event: type, distinct_id: this.getMachineId(), - properties:{ + properties: { ...data, machine_id: this.getMachineId(), os_info: this.getOsInfo(), @@ -149,40 +147,39 @@ class Telemeter { plugins: this.plugins_, }, timestamp: new Date(), - } - this.store_.addEvent(event) + }; + this.store_.addEvent(event); - this.queueCount_ += 1 - this.queueSize_ += JSON.stringify(event).length + this.queueCount_ += 1; + this.queueSize_ += JSON.stringify(event).length; - const hasReachedFlushAt = this.queueCount_ >= this.flushAt - const hasReachedQueueSize = this.queueSize_ >= this.maxQueueSize + const hasReachedFlushAt = this.queueCount_ >= this.flushAt; + const hasReachedQueueSize = this.queueSize_ >= this.maxQueueSize; if (hasReachedQueueSize || hasReachedFlushAt) { - const flush = createFlush(this.isTrackingEnabled()) - flush && flush() + const flush = createFlush(this.isTrackingEnabled()); + flush && flush(); } if (this.flushInterval && !this.timer) { - const flush = createFlush(this.isTrackingEnabled()) + const flush = createFlush(this.isTrackingEnabled()); if (flush) { - this.timer = setTimeout(flush, this.flushInterval) + this.timer = setTimeout(flush, this.flushInterval); } } } - - public trackApp(app):void { + public trackApp(app): void { if (app) { - this.apps_.push(app) + this.apps_.push(app); } } - public trackPlugin(plugin) :void{ + public trackPlugin(plugin): void { if (plugin) { - this.plugins_.push(plugin) + this.plugins_.push(plugin); } } } -export default Telemeter \ No newline at end of file +export default Telemeter; diff --git a/packages/ocular-telemetry/src/util/telemetry-dispatcher.ts b/packages/ocular-telemetry/src/util/telemetry-dispatcher.ts index fafd667f..4d211db2 100644 --- a/packages/ocular-telemetry/src/util/telemetry-dispatcher.ts +++ b/packages/ocular-telemetry/src/util/telemetry-dispatcher.ts @@ -8,28 +8,28 @@ import isTruthy from "./is-truthy"; const OCULAR_TELEMETRY_VERBOSE = process.env.OCULAR_TELEMETRY_VERBOSE || false; -interface Options{ +interface Options { host: string; public_key: string; - axiosInstance:AxiosInstance|AxiosStatic; - timeout:boolean + axiosInstance: AxiosInstance | AxiosStatic; + timeout: boolean; } class TelemetryDispatcher { - private store_: typeof Store; + private store_: Store; private host: string; private public_key: string; - private axiosInstance: AxiosInstance|AxiosStatic; // Use AxiosInstance type + private axiosInstance: AxiosInstance | AxiosStatic; // Use AxiosInstance type private timeout: boolean; private flushed: boolean; private trackingEnabled: boolean; constructor(options: Options) { + this.store_ = new Store(); this.host = removeSlash(options.host || "https://us.i.posthog.com/batch/"); this.public_key = - options.public_key || - "phc_fExEtFcrzPBqQ17LW0UbP4umSAnRHpPfOVIScJKvj0B"; - + options.public_key || "phc_fExEtFcrzPBqQ17LW0UbP4umSAnRHpPfOVIScJKvj0B"; + let axiosInstance: AxiosStatic | AxiosInstance; if (options.axiosInstance) { axiosInstance = options.axiosInstance; @@ -37,10 +37,10 @@ class TelemetryDispatcher { axiosInstance = axios.create(); } this.axiosInstance = axiosInstance; - + this.timeout = options.timeout || false; this.flushed = false; - + // need to resolve this //@ts-ignore axiosRetry(this.axiosInstance, { @@ -48,7 +48,7 @@ class TelemetryDispatcher { retryDelay: axiosRetry.exponentialDelay, retryCondition: this.isErrorRetryable_, }); - } + } isTrackingEnabled() { // Cache the result