Skip to content

Commit dcd9ef2

Browse files
committed
Split Metric and MetricType in different files
This makes sure Glean.ts isn't mistakenly imported, as a circular dependency, when it's not strictly needed.
1 parent be372a9 commit dcd9ef2

File tree

16 files changed

+286
-253
lines changed

16 files changed

+286
-253
lines changed

glean/src/core/glean.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { isUndefined, sanitizeApplicationId } from "./utils.js";
1111
import { CoreMetrics } from "./internal_metrics.js";
1212
import EventsDatabase from "./metrics/events_database.js";
1313
import UUIDMetricType from "./metrics/types/uuid.js";
14-
import DatetimeMetricType, { DatetimeMetric } from "./metrics/types/datetime.js";
14+
import DatetimeMetricType from "./metrics/types/datetime.js";
15+
import { DatetimeMetric } from "./metrics/types/datetime_metric.js";
1516
import Dispatcher from "./dispatcher.js";
1617
import CorePings from "./internal_pings.js";
1718
import { registerPluginToEvent, testResetEvents } from "./events/utils.js";

glean/src/core/metrics/types/boolean.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,8 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { MetricType, CommonMetricData } from "../index.js";
6-
import { Metric } from "../metric.js";
7-
import { isBoolean } from "../../utils.js";
86
import Glean from "../../glean.js";
9-
10-
export class BooleanMetric extends Metric<boolean, boolean> {
11-
constructor(v: unknown) {
12-
super(v);
13-
}
14-
15-
validate(v: unknown): v is boolean {
16-
return isBoolean(v);
17-
}
18-
payload(): boolean {
19-
return this._inner;
20-
}
21-
}
7+
import { BooleanMetric } from "./boolean_metric.js";
228

239
/**
2410
* A boolean metric.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 } from "../metric.js";
6+
import { isBoolean } from "../../utils.js";
7+
8+
export class BooleanMetric extends Metric<boolean, boolean> {
9+
constructor(v: unknown) {
10+
super(v);
11+
}
12+
13+
validate(v: unknown): v is boolean {
14+
return isBoolean(v);
15+
}
16+
payload(): boolean {
17+
return this._inner;
18+
}
19+
}

glean/src/core/metrics/types/counter.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,9 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { MetricType, CommonMetricData } from "../index.js";
6-
import { Metric } from "../metric.js";
7-
import { isNumber, isUndefined, JSONValue } from "../../utils.js";
6+
import { isUndefined, JSONValue } from "../../utils.js";
87
import Glean from "../../glean.js";
9-
10-
export class CounterMetric extends Metric<number, number> {
11-
constructor(v: unknown) {
12-
super(v);
13-
}
14-
15-
validate(v: unknown): v is number {
16-
if (!isNumber(v)) {
17-
return false;
18-
}
19-
20-
if (v <= 0) {
21-
return false;
22-
}
23-
24-
return true;
25-
}
26-
27-
payload(): number {
28-
return this._inner;
29-
}
30-
}
8+
import { CounterMetric } from "./counter_metric.js";
319

3210
/**
3311
* A counter metric.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 } from "../metric.js";
6+
import { isNumber } from "../../utils.js";
7+
8+
export class CounterMetric extends Metric<number, number> {
9+
constructor(v: unknown) {
10+
super(v);
11+
}
12+
13+
validate(v: unknown): v is number {
14+
if (!isNumber(v)) {
15+
return false;
16+
}
17+
18+
if (v <= 0) {
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
payload(): number {
26+
return this._inner;
27+
}
28+
}

glean/src/core/metrics/types/datetime.ts

Lines changed: 1 addition & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -3,158 +3,9 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
import { MetricType, CommonMetricData } from "../index.js";
6-
import { Metric } from "../metric.js";
76
import TimeUnit from "../../metrics/time_unit.js";
87
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";
1589

15910
/**
16011
* A datetime metric.

0 commit comments

Comments
 (0)