|
| 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 type { CommonMetricData } from "../index.js"; |
| 6 | +import { MetricType } from "../index.js"; |
| 7 | +import { Context } from "../../context.js"; |
| 8 | +import { Metric } from "../metric.js"; |
| 9 | +import { testOnly } from "../../utils.js"; |
| 10 | +import { isNumber, isObject } from "../../utils.js"; |
| 11 | +import type { JSONValue } from "../../utils.js"; |
| 12 | +import { ErrorType } from "../../error/error_type.js"; |
| 13 | + |
| 14 | +const LOG_TAG = "core.metrics.RateMetricType"; |
| 15 | + |
| 16 | +export type Rate = { |
| 17 | + numerator: number, |
| 18 | + denominator: number |
| 19 | +}; |
| 20 | + |
| 21 | +export class RateMetric extends Metric<Rate, Rate> { |
| 22 | + constructor(v: unknown) { |
| 23 | + super(v); |
| 24 | + } |
| 25 | + |
| 26 | + get numerator(): number { |
| 27 | + return this._inner.numerator; |
| 28 | + } |
| 29 | + |
| 30 | + get denominator(): number { |
| 31 | + return this._inner.denominator; |
| 32 | + } |
| 33 | + |
| 34 | + validate(v: unknown): v is Rate { |
| 35 | + if (!isObject(v) || Object.keys(v).length !== 2) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + const numeratorVerification = "numerator" in v && isNumber(v.numerator) && v.numerator >= 0; |
| 40 | + const denominatorVerification = "denominator" in v && isNumber(v.denominator) && v.denominator >= 0; |
| 41 | + return numeratorVerification && denominatorVerification; |
| 42 | + } |
| 43 | + |
| 44 | + payload(): Rate { |
| 45 | + return { |
| 46 | + numerator: this._inner.numerator, |
| 47 | + denominator: this._inner.denominator |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/* |
| 53 | + * A rate metric. |
| 54 | + * |
| 55 | + * Used to determine the proportion of things via two counts: |
| 56 | + * * A numerator defining the amount of times something happened, |
| 57 | + * * A denominator counting the amount of times someting could have happened. |
| 58 | + * |
| 59 | + * Both numerator and denominator can only be incremented, not decremented. |
| 60 | + */ |
| 61 | +class RateMetricType extends MetricType { |
| 62 | + constructor(meta: CommonMetricData) { |
| 63 | + super("rate", meta); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Increases the numerator by amount. |
| 68 | + * |
| 69 | + * # Note |
| 70 | + * |
| 71 | + * Records an `InvalidValue` error if the `amount` is negative. |
| 72 | + * |
| 73 | + * @param amount The amount to increase by. Should be non-negative. |
| 74 | + */ |
| 75 | + addToNumerator(amount: number): void { |
| 76 | + Context.dispatcher.launch(async () => { |
| 77 | + if (!this.shouldRecord(Context.uploadEnabled)) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (amount < 0) { |
| 82 | + await Context.errorManager.record( |
| 83 | + this, |
| 84 | + ErrorType.InvalidValue, |
| 85 | + `Added negative value ${amount} to numerator.` |
| 86 | + ); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const transformFn = ((amount) => { |
| 91 | + return (v?: JSONValue): RateMetric => { |
| 92 | + let metric: RateMetric; |
| 93 | + let result: number; |
| 94 | + try { |
| 95 | + metric = new RateMetric(v); |
| 96 | + result = metric.numerator + amount; |
| 97 | + } catch { |
| 98 | + metric = new RateMetric({ |
| 99 | + numerator: amount, |
| 100 | + denominator: 0 |
| 101 | + }); |
| 102 | + result = amount; |
| 103 | + } |
| 104 | + |
| 105 | + if (result > Number.MAX_SAFE_INTEGER) { |
| 106 | + result = Number.MAX_SAFE_INTEGER; |
| 107 | + } |
| 108 | + |
| 109 | + metric.set({ |
| 110 | + numerator: result, |
| 111 | + denominator: metric.denominator |
| 112 | + }); |
| 113 | + return metric; |
| 114 | + }; |
| 115 | + })(amount); |
| 116 | + |
| 117 | + await Context.metricsDatabase.transform(this, transformFn); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Increases the denominator by amount. |
| 123 | + * |
| 124 | + * # Note |
| 125 | + * |
| 126 | + * Records an `InvalidValue` error if the `amount` is negative. |
| 127 | + * |
| 128 | + * @param amount The amount to increase by. Should be non-negative. |
| 129 | + */ |
| 130 | + addToDenominator(amount: number): void { |
| 131 | + Context.dispatcher.launch(async () => { |
| 132 | + if (!this.shouldRecord(Context.uploadEnabled)) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + if (amount < 0) { |
| 137 | + await Context.errorManager.record( |
| 138 | + this, |
| 139 | + ErrorType.InvalidValue, |
| 140 | + `Added negative value ${amount} to denominator.` |
| 141 | + ); |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + const transformFn = ((amount) => { |
| 146 | + return (v?: JSONValue): RateMetric => { |
| 147 | + let metric: RateMetric; |
| 148 | + let result: number; |
| 149 | + try { |
| 150 | + metric = new RateMetric(v); |
| 151 | + result = metric.denominator + amount; |
| 152 | + } catch { |
| 153 | + metric = new RateMetric({ |
| 154 | + numerator: 0, |
| 155 | + denominator: amount |
| 156 | + }); |
| 157 | + result = amount; |
| 158 | + } |
| 159 | + |
| 160 | + if (result > Number.MAX_SAFE_INTEGER) { |
| 161 | + result = Number.MAX_SAFE_INTEGER; |
| 162 | + } |
| 163 | + |
| 164 | + metric.set({ |
| 165 | + numerator: metric.numerator, |
| 166 | + denominator: result |
| 167 | + }); |
| 168 | + return metric; |
| 169 | + }; |
| 170 | + })(amount); |
| 171 | + |
| 172 | + await Context.metricsDatabase.transform(this, transformFn); |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Test-only API.** |
| 178 | + * |
| 179 | + * Gets the currently stored value as an object. |
| 180 | + * |
| 181 | + * # Note |
| 182 | + * |
| 183 | + * This function will return the Rate for convenience. |
| 184 | + * |
| 185 | + * This doesn't clear the stored value. |
| 186 | + * |
| 187 | + * @param ping the ping from which we want to retrieve this metrics value from. |
| 188 | + * Defaults to the first value in `sendInPings`. |
| 189 | + * @returns The value found in storage or `undefined` if nothing was found. |
| 190 | + */ |
| 191 | + @testOnly(LOG_TAG) |
| 192 | + async testGetValue(ping: string = this.sendInPings[0]): Promise<Rate | undefined> { |
| 193 | + let metric: Rate | undefined; |
| 194 | + await Context.dispatcher.testLaunch(async () => { |
| 195 | + metric = await Context.metricsDatabase.getMetric<Rate>(ping, this); |
| 196 | + }); |
| 197 | + return metric; |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +export default RateMetricType; |
0 commit comments