|
| 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 { v4 as UUIDv4, validate as UUIDvalidate } from "uuid"; |
| 6 | + |
| 7 | +import Metric, { CommonMetricData } from "metrics"; |
| 8 | +import Glean from "glean"; |
| 9 | +import { isString } from "utils"; |
| 10 | + |
| 11 | +export type UUIDMetricPayload = string; |
| 12 | + |
| 13 | +/** |
| 14 | + * Checks whether or not `v` is a valid UUID metric payload. |
| 15 | + * |
| 16 | + * @param v The value to verify. |
| 17 | + * |
| 18 | + * @returns A special Typescript value (which compiles down to a boolean) |
| 19 | + * stating whether `v` is a valid boolean metric payload. |
| 20 | + */ |
| 21 | +export function isUUIDMetricPayload(v: unknown): v is UUIDMetricPayload { |
| 22 | + if (!isString(v)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + return UUIDvalidate(v); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * An UUID metric. |
| 31 | + * |
| 32 | + * Stores UUID v4 (randomly generated) values. |
| 33 | + */ |
| 34 | +class UUIDMetric extends Metric { |
| 35 | + constructor(meta: CommonMetricData) { |
| 36 | + super("uuid", meta); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Sets to the specified value. |
| 41 | + * |
| 42 | + * @param value the value to set. |
| 43 | + * |
| 44 | + * @throws In case `value` is not a valid UUID. |
| 45 | + */ |
| 46 | + async set(value: string): Promise<void> { |
| 47 | + if (!this.shouldRecord()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (!value) { |
| 52 | + value = UUIDv4(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!isUUIDMetricPayload(value)) { |
| 56 | + // TODO: record error once Bug 1682574 is resolved. |
| 57 | + console.warn(`"${value}" is not a valid UUID. Ignoring`); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + await Glean.db.record(this, value); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Generates a new random uuid and sets the metric to it. |
| 66 | + * |
| 67 | + * @returns The generated value or `undefined` in case this metric shouldn't be recorded. |
| 68 | + */ |
| 69 | + async generateAndSet(): Promise<UUIDMetricPayload | undefined> { |
| 70 | + if (!this.shouldRecord()) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const value = UUIDv4(); |
| 75 | + await this.set(value); |
| 76 | + |
| 77 | + return value; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * **Test-only API** |
| 82 | + * |
| 83 | + * Gets the currently stored value as a string. |
| 84 | + * |
| 85 | + * This doesn't clear the stored value. |
| 86 | + * |
| 87 | + * TODO: Only allow this function to be called on test mode (depends on Bug 1682771). |
| 88 | + * |
| 89 | + * @param ping the ping from which we want to retrieve this metrics value from. |
| 90 | + * |
| 91 | + * @returns The value found in storage or `undefined` if nothing was found. |
| 92 | + */ |
| 93 | + async testGetValue(ping: string): Promise<UUIDMetricPayload | undefined> { |
| 94 | + return Glean.db.getMetric(ping, this); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export default UUIDMetric; |
0 commit comments