Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Fix Type Error in Ocular Telemeter #124

Merged
merged 1 commit into from
Jun 10, 2024
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
75 changes: 38 additions & 37 deletions packages/ocular-telemetry/src/store.ts
Original file line number Diff line number Diff line change
@@ -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<boolean>): Promise<boolean> {
return await this.outbox_.startFlushEvents(async (eventData:string) => {
public async flushEvents(
handler: (events: any[]) => Promise<boolean>
): Promise<boolean> {
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()
export default Store;
183 changes: 90 additions & 93 deletions packages/ocular-telemetry/src/telemeter.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<string>
private osInfo:OsInfo
private apps_:Array<any>
private plugins_:Array<any>
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<string>;
private osInfo: OsInfo;
private apps_: Array<any>;
private plugins_: Array<any>;
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 {
Expand All @@ -114,33 +112,33 @@ class Telemeter {
return osInfo;
Vivek-Lahole marked this conversation as resolved.
Show resolved Hide resolved
}

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(),
Expand All @@ -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
export default Telemeter;
Loading