From 56c4cfaeff287e56121b0994b0a1720b21704718 Mon Sep 17 00:00:00 2001 From: emily-shen <69125074+emily-shen@users.noreply.github.com> Date: Wed, 20 Nov 2024 13:22:08 +0000 Subject: [PATCH] add banner --- .../wrangler/src/metrics/metrics-config.ts | 2 + .../src/metrics/metrics-dispatcher.ts | 48 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/wrangler/src/metrics/metrics-config.ts b/packages/wrangler/src/metrics/metrics-config.ts index 8dd6d496c001..306ebaa5b4f5 100644 --- a/packages/wrangler/src/metrics/metrics-config.ts +++ b/packages/wrangler/src/metrics/metrics-config.ts @@ -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. */ diff --git a/packages/wrangler/src/metrics/metrics-dispatcher.ts b/packages/wrangler/src/metrics/metrics-dispatcher.ts index 9a66966c12a9..8429d8eebf5d 100644 --- a/packages/wrangler/src/metrics/metrics-dispatcher.ts +++ b/packages/wrangler/src/metrics/metrics-dispatcher.ts @@ -1,3 +1,4 @@ +import chalk from "chalk"; import { fetch } from "undici"; import { logger } from "../logger"; import { @@ -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"; @@ -43,6 +48,12 @@ 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( name: EventName, properties: Omit< @@ -50,6 +61,9 @@ export function getMetricsDispatcher(options: MetricsConfigOptions) { keyof CommonEventProperties > ): Promise { + if (name === "wrangler command started") { + printMetricsBanner(); + } const commonEventProperties: CommonEventProperties = { amplitude_session_id, amplitude_event_id: amplitude_event_id++, @@ -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; + +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; +};