feat(agent): add anonymous event: Connected. (#349)

release-0.0
Zhiming Ma 2023-08-10 17:14:45 +08:00 committed by GitHub
parent 61a885ddd8
commit b6ce85733f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -15,6 +15,8 @@ export class AnonymousUsageLogger {
? undefined
: `${process.version} ${process.platform} ${require("os").arch()} ${require("os").release()}`,
};
private properties: { [key: string]: any } = {};
private emittedUniqueEvent: string[] = [];
private dataStore: DataStore | null = null;
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) {
return;
}
if (unique && this.emittedUniqueEvent.indexOf(event) >= 0) {
return;
}
await this.anonymousUsageTrackingApi.api
.usage({
distinctId: this.anonymousId,
event,
properties: {
...this.systemData,
...this.properties,
...data,
},
})
.then(() => {
if (unique) {
this.emittedUniqueEvent.push(event);
}
})
.catch((error) => {
this.logger.error({ error }, "Error when sending anonymous usage data");
});

View File

@ -86,6 +86,9 @@ export class TabbyAgent extends EventEmitter implements Agent {
const event: AgentEvent = { event: "statusChanged", status };
this.logger.debug({ event }, "Status changed");
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
// `pino.Logger.setBindings` is not present in the browser
allLoggers.forEach((logger) => logger.setBindings?.({ client: options.client }));
this.anonymousUsageLogger.addProperties({ client: options.client });
}
if (userAgentConfig) {
await userAgentConfig.load();
@ -164,9 +168,7 @@ export class TabbyAgent extends EventEmitter implements Agent {
const event: AgentEvent = { event: "authRequired", server: this.config.server };
super.emit("authRequired", event);
}
await this.anonymousUsageLogger.event("AgentInitialized", {
client: options.client,
});
await this.anonymousUsageLogger.uniqueEvent("AgentInitialized");
this.logger.debug({ options }, "Initialized");
return this.status !== "notInitialized";
}