|
| 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 { isString, truncateStringAtBoundaryWithError } from "../../utils.js"; |
| 10 | +import type { JSONValue } from "../../utils.js"; |
| 11 | +import { ErrorType } from "../../error/error_type.js"; |
| 12 | + |
| 13 | +export const MAX_LIST_LENGTH = 20; |
| 14 | +export const MAX_STRING_LENGTH = 50; |
| 15 | + |
| 16 | +export class StringListMetric extends Metric<string[], string[]> { |
| 17 | + constructor(v: unknown) { |
| 18 | + super(v); |
| 19 | + } |
| 20 | + |
| 21 | + validate(v: unknown): v is string[] { |
| 22 | + if (!Array.isArray(v)) { |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + if (v.length > MAX_LIST_LENGTH) { |
| 27 | + return false; |
| 28 | + } |
| 29 | + |
| 30 | + for (const s of v) { |
| 31 | + if (!isString(s) || s.length > MAX_STRING_LENGTH) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + payload(): string[] { |
| 40 | + return this._inner; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * A string list metric. |
| 46 | + * |
| 47 | + * This allows appending a string value with arbitrary content to a list. |
| 48 | + * The list is length-limited to `MAX_LIST_LENGTH`. |
| 49 | + * Strings are length-limited to `MAX_STRING_LENGTH` characters. |
| 50 | + */ |
| 51 | +class StringListMetricType extends MetricType { |
| 52 | + constructor(meta: CommonMetricData) { |
| 53 | + super("string_list", meta); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets to the specified string list value. |
| 58 | + * |
| 59 | + * # Note |
| 60 | + * |
| 61 | + * Truncates the list if it is longer than `MAX_LIST_LENGTH` and records an error. |
| 62 | + * |
| 63 | + * Truncates the value if it is longer than `MAX_STRING_LENGTH` characters |
| 64 | + * and records an error. |
| 65 | + * |
| 66 | + * @param value The list of strings to set the metric to. |
| 67 | + */ |
| 68 | + set(value: string[]): void { |
| 69 | + Context.dispatcher.launch(async () => { |
| 70 | + if (!this.shouldRecord(Context.uploadEnabled)) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + const truncatedList: string[] = []; |
| 75 | + if (value.length > MAX_LIST_LENGTH) { |
| 76 | + await Context.errorManager.record( |
| 77 | + this, |
| 78 | + ErrorType.InvalidValue, |
| 79 | + `String list length of ${value.length} exceeds maximum of ${MAX_LIST_LENGTH}.` |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + for (let i = 0; i < Math.min(value.length, MAX_LIST_LENGTH); ++i) { |
| 84 | + const truncatedString = await truncateStringAtBoundaryWithError(this, value[i], MAX_STRING_LENGTH); |
| 85 | + truncatedList.push(truncatedString); |
| 86 | + } |
| 87 | + const metric = new StringListMetric(truncatedList); |
| 88 | + await Context.metricsDatabase.record(this, metric); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Adds a new string `value` to the list. |
| 94 | + * |
| 95 | + * # Note |
| 96 | + * |
| 97 | + * - If the list is already of length `MAX_LIST_LENGTH`, record an error. |
| 98 | + * - Truncates the value if it is longer than `MAX_STRING_LENGTH` characters |
| 99 | + * and records an error. |
| 100 | + * |
| 101 | + * @param value The string to add. |
| 102 | + */ |
| 103 | + add(value: string): void { |
| 104 | + Context.dispatcher.launch(async () => { |
| 105 | + if (!this.shouldRecord(Context.uploadEnabled)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const truncatedValue = await truncateStringAtBoundaryWithError(this, value, MAX_STRING_LENGTH); |
| 110 | + let currentLen = 0; |
| 111 | + |
| 112 | + const transformFn = ((value) => { |
| 113 | + return (v?: JSONValue): StringListMetric => { |
| 114 | + let metric: StringListMetric; |
| 115 | + let result: string[]; |
| 116 | + try { |
| 117 | + metric = new StringListMetric(v); |
| 118 | + result = metric.get(); |
| 119 | + currentLen = result.length; |
| 120 | + if (result.length < MAX_LIST_LENGTH) { |
| 121 | + result.push(value); |
| 122 | + } |
| 123 | + } catch { |
| 124 | + metric = new StringListMetric([value]); |
| 125 | + result = [value]; |
| 126 | + } |
| 127 | + metric.set(result); |
| 128 | + return metric; |
| 129 | + }; |
| 130 | + })(truncatedValue); |
| 131 | + |
| 132 | + await Context.metricsDatabase.transform(this, transformFn); |
| 133 | + |
| 134 | + if (currentLen >= MAX_LIST_LENGTH) { |
| 135 | + await Context.errorManager.record( |
| 136 | + this, |
| 137 | + ErrorType.InvalidValue, |
| 138 | + `String list length of ${currentLen+1} exceeds maximum of ${MAX_LIST_LENGTH}.` |
| 139 | + ); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Test-only API** |
| 146 | + * |
| 147 | + * Gets the currently stored value as a string array. |
| 148 | + * |
| 149 | + * This doesn't clear the stored value. |
| 150 | + * |
| 151 | + * TODO: Only allow this function to be called on test mode (depends on Bug 1682771). |
| 152 | + * |
| 153 | + * @param ping the ping from which we want to retrieve this metrics value from. |
| 154 | + * Defaults to the first value in `sendInPings`. |
| 155 | + * @returns The value found in storage or `undefined` if nothing was found. |
| 156 | + */ |
| 157 | + async testGetValue(ping: string = this.sendInPings[0]): Promise<string[] | undefined> { |
| 158 | + let metric: string[] | undefined; |
| 159 | + await Context.dispatcher.testLaunch(async () => { |
| 160 | + metric = await Context.metricsDatabase.getMetric<string[]>(ping, this); |
| 161 | + }); |
| 162 | + return metric; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +export default StringListMetricType; |
0 commit comments