feat(agent): add anonymous event: Connected. (#349)
parent
61a885ddd8
commit
b6ce85733f
|
|
@ -15,6 +15,8 @@ export class AnonymousUsageLogger {
|
||||||
? undefined
|
? undefined
|
||||||
: `${process.version} ${process.platform} ${require("os").arch()} ${require("os").release()}`,
|
: `${process.version} ${process.platform} ${require("os").arch()} ${require("os").release()}`,
|
||||||
};
|
};
|
||||||
|
private properties: { [key: string]: any } = {};
|
||||||
|
private emittedUniqueEvent: string[] = [];
|
||||||
private dataStore: DataStore | null = null;
|
private dataStore: DataStore | null = null;
|
||||||
private anonymousId: string;
|
private anonymousId: string;
|
||||||
|
|
||||||
|
|
@ -52,19 +54,37 @@ export class AnonymousUsageLogger {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async event(event: string, data: any) {
|
addProperties(properties: { [key: string]: any }) {
|
||||||
|
// not a deep merge
|
||||||
|
this.properties = { ...this.properties, ...properties };
|
||||||
|
}
|
||||||
|
|
||||||
|
async uniqueEvent(event: string, data: { [key: string]: any } = {}) {
|
||||||
|
await this.event(event, data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async event(event: string, data: { [key: string]: any } = {}, unique = false) {
|
||||||
if (this.disabled) {
|
if (this.disabled) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (unique && this.emittedUniqueEvent.indexOf(event) >= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await this.anonymousUsageTrackingApi.api
|
await this.anonymousUsageTrackingApi.api
|
||||||
.usage({
|
.usage({
|
||||||
distinctId: this.anonymousId,
|
distinctId: this.anonymousId,
|
||||||
event,
|
event,
|
||||||
properties: {
|
properties: {
|
||||||
...this.systemData,
|
...this.systemData,
|
||||||
|
...this.properties,
|
||||||
...data,
|
...data,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
.then(() => {
|
||||||
|
if (unique) {
|
||||||
|
this.emittedUniqueEvent.push(event);
|
||||||
|
}
|
||||||
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
this.logger.error({ error }, "Error when sending anonymous usage data");
|
this.logger.error({ error }, "Error when sending anonymous usage data");
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,9 @@ export class TabbyAgent extends EventEmitter implements Agent {
|
||||||
const event: AgentEvent = { event: "statusChanged", status };
|
const event: AgentEvent = { event: "statusChanged", status };
|
||||||
this.logger.debug({ event }, "Status changed");
|
this.logger.debug({ event }, "Status changed");
|
||||||
super.emit("statusChanged", event);
|
super.emit("statusChanged", event);
|
||||||
|
if (this.status == "ready") {
|
||||||
|
this.anonymousUsageLogger.uniqueEvent("Connected");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,6 +149,7 @@ export class TabbyAgent extends EventEmitter implements Agent {
|
||||||
// Client info is only used in logging for now
|
// Client info is only used in logging for now
|
||||||
// `pino.Logger.setBindings` is not present in the browser
|
// `pino.Logger.setBindings` is not present in the browser
|
||||||
allLoggers.forEach((logger) => logger.setBindings?.({ client: options.client }));
|
allLoggers.forEach((logger) => logger.setBindings?.({ client: options.client }));
|
||||||
|
this.anonymousUsageLogger.addProperties({ client: options.client });
|
||||||
}
|
}
|
||||||
if (userAgentConfig) {
|
if (userAgentConfig) {
|
||||||
await userAgentConfig.load();
|
await userAgentConfig.load();
|
||||||
|
|
@ -164,9 +168,7 @@ export class TabbyAgent extends EventEmitter implements Agent {
|
||||||
const event: AgentEvent = { event: "authRequired", server: this.config.server };
|
const event: AgentEvent = { event: "authRequired", server: this.config.server };
|
||||||
super.emit("authRequired", event);
|
super.emit("authRequired", event);
|
||||||
}
|
}
|
||||||
await this.anonymousUsageLogger.event("AgentInitialized", {
|
await this.anonymousUsageLogger.uniqueEvent("AgentInitialized");
|
||||||
client: options.client,
|
|
||||||
});
|
|
||||||
this.logger.debug({ options }, "Initialized");
|
this.logger.debug({ options }, "Initialized");
|
||||||
return this.status !== "notInitialized";
|
return this.status !== "notInitialized";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue