|
| 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 Metric, { CommonMetricData } from "metrics"; |
| 6 | +import Glean from "glean"; |
| 7 | +import { isNumber, isUndefined } from "utils"; |
| 8 | + |
| 9 | +export const MAX_LENGTH_VALUE = 100; |
| 10 | + |
| 11 | +export type CounterMetricPayload = number; |
| 12 | + |
| 13 | +/** |
| 14 | + * Checks whether or not `v` is a valid counter metric payload. |
| 15 | + * |
| 16 | + * # Note |
| 17 | + * |
| 18 | + * Not only will this verify if `v` is a number, |
| 19 | + * it will also check if it is greater than 0. |
| 20 | + * |
| 21 | + * @param v The value to verify. |
| 22 | + * |
| 23 | + * @returns A special Typescript value (which compiles down to a boolean) |
| 24 | + * stating wether `v` is a valid string metric payload. |
| 25 | + */ |
| 26 | +export function isCounterMetricPayload(v: unknown): v is CounterMetricPayload { |
| 27 | + if (!isNumber(v)) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + if (v <= 0) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + return true; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * A counter metric. |
| 40 | + * |
| 41 | + * Used to count things. |
| 42 | + * The value can only be incremented, not decremented. |
| 43 | + */ |
| 44 | +class CounterMetric extends Metric { |
| 45 | + constructor(meta: CommonMetricData) { |
| 46 | + super("counter", meta); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Increases the counter by `amount`. |
| 51 | + * |
| 52 | + * # Note |
| 53 | + * |
| 54 | + * - Logs an error if the `amount` is 0 or negative. |
| 55 | + * - If the addition yields a number larger than Number.MAX_SAFE_INTEGER, |
| 56 | + * Number.MAX_SAFE_INTEGER will be recorded. |
| 57 | + * |
| 58 | + * @param amount The amount to increase by. Should be positive. |
| 59 | + * If not provided will default to `1`. |
| 60 | + */ |
| 61 | + async add(amount?: number): Promise<void> { |
| 62 | + if (!this.shouldRecord()) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (isUndefined(amount)) { |
| 67 | + amount = 1; |
| 68 | + } |
| 69 | + |
| 70 | + if (amount <= 0) { |
| 71 | + // TODO: record error once Bug 1682574 is resolved. |
| 72 | + console.warn(`Attempted to add an invalid amount ${amount}. `); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const transformFn = ((amount) => { |
| 77 | + return (v?: CounterMetricPayload): CounterMetricPayload => { |
| 78 | + let result = v ? v + amount : amount; |
| 79 | + if (result > Number.MAX_SAFE_INTEGER) { |
| 80 | + result = Number.MAX_SAFE_INTEGER; |
| 81 | + } |
| 82 | + return result; |
| 83 | + }; |
| 84 | + })(amount); |
| 85 | + |
| 86 | + await Glean.db.transform(this, transformFn); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * **Test-only API.** |
| 91 | + * |
| 92 | + * Gets the currently stored value as a number. |
| 93 | + * |
| 94 | + * This doesn't clear the stored value. |
| 95 | + * |
| 96 | + * TODO: Only allow this function to be called on test mode (depends on Bug 1682771). |
| 97 | + * |
| 98 | + * @param ping the ping from which we want to retrieve this metrics value from. |
| 99 | + * |
| 100 | + * @returns The value found in storage or `undefined` if nothing was found. |
| 101 | + */ |
| 102 | + async testGetValue(ping: string): Promise<CounterMetricPayload | undefined> { |
| 103 | + return Glean.db.getMetric(ping, this); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +export default CounterMetric; |
0 commit comments