move tracker to telemetry extension

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen committed 2020-10-16 10:56:25 +03:00
1 parent a00d26149c
commit 2a12bb74a9
19 files changed
+477 -113

No files matched your search

+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);
+51
View File
@@ -0,0 +1,51 @@
export interface Listener<T> {
(event: T): void;
}
export interface Disposable {
dispose(): void;
}
export class EventBus<T> {
private listeners: Listener<T>[] = [];
private listenersOnce: Listener<T>[] = [];
on(listener: Listener<T>): Disposable {
this.listeners.push(listener);
return {
dispose: () => this.off(listener)
};
}
once(listener: Listener<T>): void {
this.listenersOnce.push(listener);
}
off(listener: Listener<T>) {
const callbackIndex = this.listeners.indexOf(listener);
if (callbackIndex > -1) this.listeners.splice(callbackIndex, 1);
}
emit(event: T) {
this.listeners.forEach((listener) => listener(event));
// clear the once queue
if (this.listenersOnce.length > 0) {
const toCall = this.listenersOnce;
this.listenersOnce = [];
toCall.forEach((listener) => listener(event));
}
}
pipe = (te: EventBus<T>): Disposable => {
return this.on((e) => te.emit(e));
}
}
export type AppEvent = {
name: string;
action: string;
params?: object;
}
export const appEventBus = new EventBus<AppEvent>()
-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();
}
+2
View File
@@ -4,3 +4,5 @@ export type { LensExtensionRuntimeEnv } from "./lens-runtime";
// APIs
export * from "./lens-main-extension"
export { BaseStore } from "../common/base-store"
export { appEventBus, AppEvent } from "../common/event-bus"
export { Singleton } from "../common/utils"
+2
View File
@@ -6,6 +6,8 @@ export * from "./lens-extension"
export * from "./lens-renderer-extension"
export { DynamicPageType, PageRegistry } from "./page-registry"
export { AppPreferenceRegistry } from "./app-preference-registry"
export { appEventBus, AppEvent } from "../common/event-bus"
export { Singleton } from "../common/utils"
// TODO: add more common re-usable UI components + refactor interfaces (Props -> ComponentProps)
export * from "../renderer/components/icon"
+3 -2
View File
@@ -16,7 +16,7 @@ import { registerFileProtocol } from "../common/register-protocol";
import { clusterStore } from "../common/cluster-store"
import { userStore } from "../common/user-store";
import { workspaceStore } from "../common/workspace-store";
import { tracker } from "../common/tracker";
import { appEventBus } from "../common/event-bus"
import * as LensExtensions from "../extensions/extension-api";
import { extensionManager } from "../extensions/extension-manager";
import { extensionLoader } from "../extensions/extension-loader";
@@ -48,7 +48,6 @@ async function main() {
await shellSync();
logger.info(`🚀 Starting Lens from "${workingDir}"`)
tracker.event("app", "start");
const updater = new AppUpdater()
updater.start();
@@ -89,6 +88,8 @@ async function main() {
extensionLoader.loadOnMain(getLensRuntime)
extensionLoader.extensions.replace(await extensionManager.load())
extensionLoader.broadcastExtensions()
appEventBus.emit({name: "app", action: "start"})
}
app.on("ready", main);
+2 -2
View File
@@ -6,7 +6,7 @@ import * as k8s from "@kubernetes/client-node"
import { KubeConfig } from "@kubernetes/client-node"
import { Cluster } from "./cluster"
import logger from "./logger";
import { tracker } from "../common/tracker";
import { appEventBus } from "../common/event-bus"
export class NodeShellSession extends ShellSession {
protected nodeName: string;
@@ -44,7 +44,7 @@ export class NodeShellSession extends ShellSession {
this.closeWebsocketOnProcessExit()
this.exitProcessOnWebsocketClose()
tracker.event("node-shell", "open")
appEventBus.emit({name: "node-shell", action: "open"})
}
protected exit(code = 1000) {
+2 -2
View File
@@ -6,7 +6,7 @@ import * as yaml from "js-yaml";
import path from "path";
import * as tempy from "tempy";
import logger from "./logger"
import { tracker } from "../common/tracker";
import { appEventBus } from "../common/event-bus"
import { cloneJsonObject } from "../common/utils";
export class ResourceApplier {
@@ -15,7 +15,7 @@ export class ResourceApplier {
async apply(resource: KubernetesObject | any): Promise<string> {
resource = this.sanitizeObject(resource);
tracker.event("resource", "apply")
appEventBus.emit({name: "resource", action: "apply"})
return await this.kubectlApply(yaml.safeDump(resource));
}
+2 -2
View File
@@ -9,7 +9,7 @@ import { Cluster } from "./cluster"
import { ClusterPreferences } from "../common/cluster-store";
import { helmCli } from "./helm/helm-cli"
import { isWindows } from "../common/vars";
import { tracker } from "../common/tracker";
import { appEventBus } from "../common/event-bus"
import { userStore } from "../common/user-store";
export class ShellSession extends EventEmitter {
@@ -58,7 +58,7 @@ export class ShellSession extends EventEmitter {
this.closeWebsocketOnProcessExit()
this.exitProcessOnWebsocketClose()
tracker.event("shell", "open")
appEventBus.emit({name: "shell", action: "open"})
}
protected cwd(): string {