Move tracker to telemetry extension (#1092)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2020-10-19 11:56:08 +03:00
1 parent a478143423
commit c566c85351
27 files changed
+472 -117

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
import { appEventBus, AppEvent } from "../event-bus"
describe("event bus tests", () => {
describe("emit", () => {
it("emits an event", () => {
let event: AppEvent = null
appEventBus.addListener((data) => {
event = data
})
appEventBus.emit({name: "foo", action: "bar"})
expect(event.name).toBe("foo")
})
})
})
+5 -5
View File
@@ -1,7 +1,7 @@
import { createIpcChannel } from "./ipc";
import { ClusterId, clusterStore } from "./cluster-store";
import { extensionLoader } from "../extensions/extension-loader"
import { tracker } from "./tracker";
import { appEventBus } from "./event-bus"
export const clusterIpc = {
activate: createIpcChannel({
@@ -37,7 +37,7 @@ export const clusterIpc = {
disconnect: createIpcChannel({
channel: "cluster:disconnect",
handle: (clusterId: ClusterId) => {
tracker.event("cluster", "stop");
appEventBus.emit({name: "cluster", action: "stop"});
return clusterStore.getById(clusterId)?.disconnect();
},
}),
@@ -45,7 +45,7 @@ export const clusterIpc = {
installFeature: createIpcChannel({
channel: "cluster:install-feature",
handle: async (clusterId: ClusterId, feature: string, config?: any) => {
tracker.event("cluster", "install", feature);
appEventBus.emit({name: "cluster", action: "install", params: { feature: feature}})
const cluster = clusterStore.getById(clusterId);
if (cluster) {
await cluster.installFeature(feature, config)
@@ -58,7 +58,7 @@ export const clusterIpc = {
uninstallFeature: createIpcChannel({
channel: "cluster:uninstall-feature",
handle: (clusterId: ClusterId, feature: string) => {
tracker.event("cluster", "uninstall", feature);
appEventBus.emit({name: "cluster", action: "uninstall", params: { feature: feature}})
return clusterStore.getById(clusterId)?.uninstallFeature(feature)
}
}),
@@ -66,7 +66,7 @@ export const clusterIpc = {
upgradeFeature: createIpcChannel({
channel: "cluster:upgrade-feature",
handle: (clusterId: ClusterId, feature: string, config?: any) => {
tracker.event("cluster", "upgrade", feature);
appEventBus.emit({name: "cluster", action: "upgrade", params: { feature: feature}})
return clusterStore.getById(clusterId)?.upgradeFeature(feature, config)
}
}),
+3 -3
View File
@@ -7,7 +7,7 @@ import { BaseStore } from "./base-store";
import { Cluster, ClusterState } from "../main/cluster";
import migrations from "../migrations/cluster-store"
import logger from "../main/logger";
import { tracker } from "./tracker";
import { appEventBus } from "./event-bus"
import { dumpConfigYaml } from "./kube-helpers";
import { saveToAppFiles } from "./utils/saveToAppFiles";
import { KubeConfig } from "@kubernetes/client-node";
@@ -142,7 +142,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
@action
addCluster(...models: ClusterModel[]) {
models.forEach(model => {
tracker.event("cluster", "add");
appEventBus.emit({name: "cluster", action: "add"})
const cluster = new Cluster(model);
this.clusters.set(model.id, cluster);
})
@@ -150,7 +150,7 @@ export class ClusterStore extends BaseStore<ClusterStoreModel> {
@action
async removeById(clusterId: ClusterId) {
tracker.event("cluster", "remove");
appEventBus.emit({name: "cluster", action: "remove"})
const cluster = this.getById(clusterId);
if (cluster) {
this.clusters.delete(clusterId);
+9
View File
@@ -0,0 +1,9 @@
import { EventEmitter } from "./event-emitter"
export type AppEvent = {
name: string;
action: string;
params?: object;
}
export const appEventBus = new EventEmitter<[AppEvent]>()
+40
View File
@@ -0,0 +1,40 @@
// Custom event emitter
interface Options {
once?: boolean; // call once and remove
prepend?: boolean; // put listener to the beginning
}
type Callback<D extends [...any[]]> = (...data: D) => void | boolean;
export class EventEmitter<D extends [...any[]]> {
protected listeners = new Map<Callback<D>, Options>();
addListener(callback: Callback<D>, options: Options = {}) {
if (options.prepend) {
const listeners = [...this.listeners];
listeners.unshift([callback, options]);
this.listeners = new Map(listeners);
}
else {
this.listeners.set(callback, options);
}
}
removeListener(callback: Callback<D>) {
this.listeners.delete(callback);
}
removeAllListeners() {
this.listeners.clear();
}
emit(...data: D) {
[...this.listeners].every(([callback, options]) => {
if (options.once) this.removeListener(callback);
const result = callback(...data);
if (result === false) return; // break cycle
return true;
})
}
}
-49
View File
@@ -1,49 +0,0 @@
import { app, App, remote } from "electron"
import ua from "universal-analytics"
import { machineIdSync } from "node-machine-id"
import Singleton from "./utils/singleton";
import { userStore } from "./user-store"
import logger from "../main/logger";
export class Tracker extends Singleton {
static readonly GA_ID = "UA-159377374-1"
protected visitor: ua.Visitor
protected machineId: string = null;
protected ip: string = null;
protected appVersion: string;
protected locale: string;
protected electronUA: string;
private constructor(app: App) {
super();
try {
this.visitor = ua(Tracker.GA_ID, machineIdSync(), { strictCidFormat: false })
} catch (error) {
this.visitor = ua(Tracker.GA_ID)
}
this.visitor.set("dl", "https://telemetry.k8slens.dev")
}
protected async isTelemetryAllowed(): Promise<boolean> {
return userStore.preferences.allowTelemetry;
}
async event(eventCategory: string, eventAction: string, otherParams = {}) {
try {
const allowed = await this.isTelemetryAllowed();
if (!allowed) {
return;
}
this.visitor.event({
ec: eventCategory,
ea: eventAction,
...otherParams,
}).send()
} catch (err) {
logger.error(`Failed to track "${eventCategory}:${eventAction}"`, err)
}
}
}
export const tracker = Tracker.getInstance<Tracker>(app || remote.app);
+3 -3
View File
@@ -7,7 +7,7 @@ import { BaseStore } from "./base-store";
import migrations from "../migrations/user-store"
import { getAppVersion } from "./utils/app-version";
import { kubeConfigDefaultPath, loadConfig } from "./kube-helpers";
import { tracker } from "./tracker";
import { appEventBus } from "./event-bus"
import logger from "../main/logger";
import path from 'path';
@@ -40,7 +40,7 @@ export class UserStore extends BaseStore<UserStoreModel> {
// track telemetry availability
reaction(() => this.preferences.allowTelemetry, allowed => {
tracker.event("telemetry", allowed ? "enabled" : "disabled");
appEventBus.emit({name: "telemetry", action: allowed ? "enabled" : "disabled"})
});
// refresh new contexts
@@ -77,7 +77,7 @@ export class UserStore extends BaseStore<UserStoreModel> {
@action
saveLastSeenAppVersion() {
tracker.event("app", "whats-new-seen")
appEventBus.emit({name: "app", action: "whats-new-seen"})
this.lastSeenAppVersion = getAppVersion();
}