|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { KNOWN_CLIENT_ID, CLIENT_INFO_STORAGE } from "./constants"; |
| 6 | +import UUIDMetricType from "metrics/types/uuid"; |
| 7 | +import DatetimeMetricType from "metrics/types/datetime"; |
| 8 | +import StringMetricType from "metrics/types/string"; |
| 9 | +import { createMetric } from "metrics/utils"; |
| 10 | +import TimeUnit from "metrics/time_unit"; |
| 11 | +import { Lifetime } from "metrics"; |
| 12 | +import Glean from "glean"; |
| 13 | +import PlatformInfo from "platform_info"; |
| 14 | + |
| 15 | +export class CoreMetrics { |
| 16 | + readonly clientId: UUIDMetricType; |
| 17 | + readonly firstRunDate: DatetimeMetricType; |
| 18 | + readonly os: StringMetricType; |
| 19 | + readonly osVersion: StringMetricType; |
| 20 | + readonly architecture: StringMetricType; |
| 21 | + readonly locale: StringMetricType; |
| 22 | + // Provided by the user |
| 23 | + readonly appBuild: StringMetricType; |
| 24 | + readonly appDisplayVersion: StringMetricType; |
| 25 | + |
| 26 | + constructor() { |
| 27 | + this.clientId = new UUIDMetricType({ |
| 28 | + name: "client_id", |
| 29 | + category: "", |
| 30 | + sendInPings: ["glean_client_info"], |
| 31 | + lifetime: Lifetime.User, |
| 32 | + disabled: false, |
| 33 | + }); |
| 34 | + |
| 35 | + this.firstRunDate = new DatetimeMetricType({ |
| 36 | + name: "first_run_date", |
| 37 | + category: "", |
| 38 | + sendInPings: ["glean_client_info"], |
| 39 | + lifetime: Lifetime.User, |
| 40 | + disabled: false, |
| 41 | + }, TimeUnit.Day); |
| 42 | + |
| 43 | + this.os = new StringMetricType({ |
| 44 | + name: "os", |
| 45 | + category: "", |
| 46 | + sendInPings: ["glean_client_info"], |
| 47 | + lifetime: Lifetime.Application, |
| 48 | + disabled: false, |
| 49 | + }); |
| 50 | + |
| 51 | + this.osVersion = new StringMetricType({ |
| 52 | + name: "os_version", |
| 53 | + category: "", |
| 54 | + sendInPings: ["glean_client_info"], |
| 55 | + lifetime: Lifetime.Application, |
| 56 | + disabled: false, |
| 57 | + }); |
| 58 | + |
| 59 | + this.architecture = new StringMetricType({ |
| 60 | + name: "architecture", |
| 61 | + category: "", |
| 62 | + sendInPings: ["glean_client_info"], |
| 63 | + lifetime: Lifetime.Application, |
| 64 | + disabled: false, |
| 65 | + }); |
| 66 | + |
| 67 | + this.locale = new StringMetricType({ |
| 68 | + name: "locale", |
| 69 | + category: "", |
| 70 | + sendInPings: ["glean_client_info"], |
| 71 | + lifetime: Lifetime.Application, |
| 72 | + disabled: false, |
| 73 | + }); |
| 74 | + |
| 75 | + this.appBuild = new StringMetricType({ |
| 76 | + name: "app_build", |
| 77 | + category: "", |
| 78 | + sendInPings: ["glean_client_info"], |
| 79 | + lifetime: Lifetime.Application, |
| 80 | + disabled: false, |
| 81 | + }); |
| 82 | + |
| 83 | + this.appDisplayVersion = new StringMetricType({ |
| 84 | + name: "app_display_version", |
| 85 | + category: "", |
| 86 | + sendInPings: ["glean_client_info"], |
| 87 | + lifetime: Lifetime.Application, |
| 88 | + disabled: false, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + async initialize(appBuild?: string, appDisplayVersion?: string): Promise<void> { |
| 93 | + await this.initializeClientId(); |
| 94 | + await this.initializeFirstRunDate(); |
| 95 | + await this.initializeOs(); |
| 96 | + await this.initializeOsVersion(); |
| 97 | + await this.initializeArchitecture(); |
| 98 | + await this.initializeLocale(); |
| 99 | + await this.appBuild.set(appBuild || "Unknown"); |
| 100 | + await this.appDisplayVersion.set(appDisplayVersion || "Unkown"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Generates and sets the client_id if it is not set, |
| 105 | + * or if the current value is currepted. |
| 106 | + */ |
| 107 | + private async initializeClientId(): Promise<void> { |
| 108 | + let needNewClientId = false; |
| 109 | + const clientIdData = await Glean.metricsDatabase.getMetric(CLIENT_INFO_STORAGE, this.clientId); |
| 110 | + if (clientIdData) { |
| 111 | + try { |
| 112 | + const currentClientId = createMetric("uuid", clientIdData); |
| 113 | + if (currentClientId.payload() === KNOWN_CLIENT_ID) { |
| 114 | + needNewClientId = true; |
| 115 | + } |
| 116 | + } catch { |
| 117 | + console.warn("Unexpected value found for Glean clientId. Ignoring."); |
| 118 | + needNewClientId = true; |
| 119 | + } |
| 120 | + } else { |
| 121 | + needNewClientId = true; |
| 122 | + } |
| 123 | + |
| 124 | + if (needNewClientId) { |
| 125 | + await this.clientId.generateAndSet(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Generates and sets the first_run_date if it is not set. |
| 131 | + */ |
| 132 | + private async initializeFirstRunDate(): Promise<void> { |
| 133 | + const firstRunDate = await Glean.metricsDatabase.getMetric( |
| 134 | + CLIENT_INFO_STORAGE, |
| 135 | + this.firstRunDate |
| 136 | + ); |
| 137 | + |
| 138 | + if (!firstRunDate) { |
| 139 | + await this.firstRunDate.set(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets and sets the os. |
| 145 | + */ |
| 146 | + async initializeOs(): Promise<void> { |
| 147 | + await this.os.set(await PlatformInfo.os()); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Gets and sets the os. |
| 152 | + */ |
| 153 | + async initializeOsVersion(): Promise<void> { |
| 154 | + await this.osVersion.set(await PlatformInfo.osVersion()); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Gets and sets the system architecture. |
| 159 | + */ |
| 160 | + async initializeArchitecture(): Promise<void> { |
| 161 | + await this.architecture.set(await PlatformInfo.arch()); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Gets and sets the system / browser locale. |
| 166 | + */ |
| 167 | + async initializeLocale(): Promise<void> { |
| 168 | + await this.locale.set(await PlatformInfo.locale()); |
| 169 | + } |
| 170 | +} |
0 commit comments