|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | 5 | import { MetricType, CommonMetricData } from "../index.js"; |
6 | | -import { Metric } from "../metric.js"; |
7 | 6 | import TimeUnit from "../../metrics/time_unit.js"; |
8 | 7 | import Glean from "../../glean.js"; |
9 | | -import { isNumber, isObject, isString } from "../../utils.js"; |
10 | | - |
11 | | -/** |
12 | | - * Builds the formatted timezone offset string frim a given timezone. |
13 | | - * |
14 | | - * The format of the resulting string is `+02:00`. |
15 | | - * |
16 | | - * @param timezone A number representing the timezone offset to format, |
17 | | - * this is expected to be in minutes. |
18 | | - * |
19 | | - * @returns The formatted timezone offset string. |
20 | | - */ |
21 | | -function formatTimezoneOffset(timezone: number): string { |
22 | | - const offset = (timezone / 60) * -1; |
23 | | - const sign = offset > 0 ? "+" : "-"; |
24 | | - const hours = Math.abs(offset).toString().padStart(2, "0"); |
25 | | - return `${sign}${hours}:00`; |
26 | | -} |
27 | | - |
28 | | -type DatetimeInternalRepresentation = { |
29 | | - // The time unit of the metric type at the time of recording. |
30 | | - timeUnit: TimeUnit, |
31 | | - // This timezone should be the exact output of `Date.getTimezoneOffset` |
32 | | - // and as such it should alwaye be in minutes. |
33 | | - timezone: number, |
34 | | - // This date string should be the exact output of `Date.toISOString` |
35 | | - // and as such it is always in UTC. |
36 | | - date: string, |
37 | | -}; |
38 | | - |
39 | | -export class DatetimeMetric extends Metric<DatetimeInternalRepresentation, string> { |
40 | | - constructor(v: unknown) { |
41 | | - super(v); |
42 | | - } |
43 | | - |
44 | | - static fromDate(v: Date, timeUnit: TimeUnit): DatetimeMetric { |
45 | | - return new DatetimeMetric({ |
46 | | - timeUnit, |
47 | | - timezone: v.getTimezoneOffset(), |
48 | | - date: v.toISOString() |
49 | | - }); |
50 | | - } |
51 | | - |
52 | | - /** |
53 | | - * Gets the datetime data as a Date object. |
54 | | - * |
55 | | - * # Note |
56 | | - * |
57 | | - * The object created here will be relative to local time. |
58 | | - * If the timezone at the time of recording is different, |
59 | | - * the timezone offset will be applied before transforming to an object. |
60 | | - * |
61 | | - * @returns A date object. |
62 | | - */ |
63 | | - get date(): Date { |
64 | | - return new Date(this._inner.date); |
65 | | - } |
66 | | - |
67 | | - private get timezone(): number { |
68 | | - return this._inner.timezone; |
69 | | - } |
70 | | - |
71 | | - private get timeUnit(): TimeUnit { |
72 | | - return this._inner.timeUnit; |
73 | | - } |
74 | | - |
75 | | - private get dateISOString(): string { |
76 | | - return this._inner.date; |
77 | | - } |
78 | | - |
79 | | - validate(v: unknown): v is DatetimeInternalRepresentation { |
80 | | - if (!isObject(v) || Object.keys(v).length !== 3) { |
81 | | - return false; |
82 | | - } |
83 | | - |
84 | | - const timeUnitVerification = "timeUnit" in v && isString(v.timeUnit) && Object.values(TimeUnit).includes(v.timeUnit as TimeUnit); |
85 | | - const timezoneVerification = "timezone" in v && isNumber(v.timezone); |
86 | | - const dateVerification = "date" in v && isString(v.date) && v.date.length === 24 && !isNaN(Date.parse(v.date)); |
87 | | - if (!timeUnitVerification || !timezoneVerification || !dateVerification) { |
88 | | - return false; |
89 | | - } |
90 | | - |
91 | | - return true; |
92 | | - } |
93 | | - |
94 | | - /** |
95 | | - * Gets this metrics value in its payload representation. |
96 | | - * |
97 | | - * For this metric, the payload is the timezone aware ISO date string truncated to the time unit |
98 | | - * given at the time of recording. |
99 | | - * |
100 | | - * # Note |
101 | | - * |
102 | | - * The timezone of the final string is the timezone at the time of recording. |
103 | | - * |
104 | | - * @returns The metric value. |
105 | | - */ |
106 | | - payload(): string { |
107 | | - const extractedDateInfo = this.dateISOString.match(/\d+/g); |
108 | | - if (!extractedDateInfo || extractedDateInfo.length < 0) { |
109 | | - // This is impossible because the date is always validated before setting |
110 | | - throw new Error("IMPOSSIBLE: Unable to extract date information from DatetimeMetric."); |
111 | | - } |
112 | | - const correctedDate = new Date( |
113 | | - /* year */ parseInt(extractedDateInfo[0]), |
114 | | - /* month */ parseInt(extractedDateInfo[1]) - 1, |
115 | | - /* day */ parseInt(extractedDateInfo[2]), |
116 | | - /* hour */ parseInt(extractedDateInfo[3]) - (this.timezone / 60), |
117 | | - /* minute */ parseInt(extractedDateInfo[4]), |
118 | | - /* second */ parseInt(extractedDateInfo[5]), |
119 | | - /* millisecond */ parseInt(extractedDateInfo[6]) |
120 | | - ); |
121 | | - |
122 | | - const timezone = formatTimezoneOffset(this.timezone); |
123 | | - const year = correctedDate.getFullYear().toString().padStart(2, "0"); |
124 | | - // `Date.prototype.getMonth` returns the month starting at 0. |
125 | | - const month = (correctedDate.getMonth() + 1).toString().padStart(2, "0"); |
126 | | - const day = correctedDate.getDate().toString().padStart(2, "0"); |
127 | | - if (this.timeUnit === TimeUnit.Day) { |
128 | | - return `${year}-${month}-${day}${timezone}`; |
129 | | - } |
130 | | - |
131 | | - const hours = correctedDate.getHours().toString().padStart(2, "0"); |
132 | | - if (this.timeUnit === TimeUnit.Hour) { |
133 | | - return `${year}-${month}-${day}T${hours}${timezone}`; |
134 | | - } |
135 | | - |
136 | | - const minutes = correctedDate.getMinutes().toString().padStart(2, "0"); |
137 | | - if (this.timeUnit === TimeUnit.Minute) { |
138 | | - return `${year}-${month}-${day}T${hours}:${minutes}${timezone}`; |
139 | | - } |
140 | | - |
141 | | - const seconds = correctedDate.getSeconds().toString().padStart(2, "0"); |
142 | | - if (this.timeUnit === TimeUnit.Second) { |
143 | | - return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${timezone}`; |
144 | | - } |
145 | | - |
146 | | - const milliseconds = correctedDate.getMilliseconds().toString().padStart(3, "0"); |
147 | | - if (this.timeUnit === TimeUnit.Millisecond) { |
148 | | - return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}${timezone}`; |
149 | | - } |
150 | | - |
151 | | - if (this.timeUnit === TimeUnit.Microsecond) { |
152 | | - return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}000${timezone}`; |
153 | | - } |
154 | | - |
155 | | - return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}.${milliseconds}000000${timezone}`; |
156 | | - } |
157 | | -} |
| 8 | +import { DatetimeInternalRepresentation, DatetimeMetric } from "./datetime_metric.js"; |
158 | 9 |
|
159 | 10 | /** |
160 | 11 | * A datetime metric. |
|
0 commit comments