Skip to content

Commit

Permalink
add banner
Browse files Browse the repository at this point in the history
  • Loading branch information
emily-shen committed Nov 20, 2024
1 parent 4799bba commit 56c4cfa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/wrangler/src/metrics/metrics-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ export interface MetricsConfigFile {
enabled: boolean;
/** The date that this permission was set. */
date: Date;
/** Version number the banner was last shown - only show on version update */
bannerLastShown?: string;
};
c3permission?: {
/** True if c3 should send metrics to Cloudflare. */
Expand Down
48 changes: 47 additions & 1 deletion packages/wrangler/src/metrics/metrics-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from "chalk";
import { fetch } from "undici";
import { logger } from "../logger";
import {
Expand All @@ -6,7 +7,11 @@ import {
getPlatform,
getWranglerVersion,
} from "./helpers";
import { getMetricsConfig, readMetricsConfig } from "./metrics-config";
import {
getMetricsConfig,
readMetricsConfig,
writeMetricsConfig,
} from "./metrics-config";
import type { MetricsConfigOptions } from "./metrics-config";
import type { CommonEventProperties, Events } from "./send-event";

Expand Down Expand Up @@ -43,13 +48,22 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
});
},

/**
* Dispatches `wrangler command started / completed / errored` events
*
* This happens on every command execution, and will (hopefully) replace sendEvent soon.
* However to prevent disruption, we're adding under `sendNewEvent` for now.
*/
async sendNewEvent<EventName extends Events["name"]>(
name: EventName,
properties: Omit<
Extract<Events, { name: EventName }>["properties"],
keyof CommonEventProperties
>
): Promise<void> {
if (name === "wrangler command started") {
printMetricsBanner();
}
const commonEventProperties: CommonEventProperties = {
amplitude_session_id,
amplitude_event_id: amplitude_event_id++,
Expand Down Expand Up @@ -119,6 +133,38 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) {
);
});
}

function printMetricsBanner() {
const metricsConfig = readMetricsConfig();
const lastShown = metricsConfig.permission?.bannerLastShown;
if (
metricsConfig.permission?.enabled &&
(!lastShown || isNewVersion(lastShown, wranglerVersion))
) {
logger.log(
chalk.gray(
`\nCloudflare collects anonymous telemetry about your usage of Wrangler. Learn more at https://github.com/cloudflare/workers-sdk/tree/main/telemetry.md`
)
);
metricsConfig.permission.bannerLastShown = wranglerVersion;
writeMetricsConfig(metricsConfig);
}
}
}

export type Properties = Record<string, unknown>;

const isNewVersion = (stored: string, current: string) => {
const storedVersion = stored.split(".");
const currentVersion = current.split(".");
for (let i = 0; i < storedVersion.length; i++) {
const storedSegment = parseInt(storedVersion[i]);
const currentSegment = parseInt(currentVersion[i]);
if (currentSegment > storedSegment) {
return true;
} else if (currentSegment < storedSegment) {
return false;
}
}
return false;
};

0 comments on commit 56c4cfa

Please sign in to comment.