Skip to content

Commit ac9635e

Browse files
committed
Bug 1695884 - Put PingType in a standalone file (#183)
Add entry to CHANGELOG.md
1 parent aed7911 commit ac9635e

File tree

12 files changed

+82
-75
lines changed

12 files changed

+82
-75
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* [#173](https://github.com/mozilla/glean.js/pull/173): Drop Node.js support from webext entry points
66
* [#155](https://github.com/mozilla/glean.js/pull/155): Allow to define custom uploaders in the configuration.
7+
* [#183](https://github.com/mozilla/glean.js/pull/183): Put PingType in a standalone file
78

89
# v0.7.0 (2021-03-26)
910

glean/src/core/internal_pings.ts

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

55
import { DELETION_REQUEST_PING_NAME } from "./constants.js";
6-
import PingType from "./pings/index.js";
6+
import PingType from "./pings/pingtype.js";
77

88
/**
99
* Glean-provided pings, all enabled by default.

glean/src/core/pings/index.ts

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
import { DELETION_REQUEST_PING_NAME } from "../constants.js";
6-
import { generateUUIDv4 } from "../utils.js";
7-
import collectAndStorePing from "../pings/maker.js";
8-
import Glean from "../glean.js";
9-
105
/**
116
* The common set of data for creating a new ping.
127
*/
@@ -21,64 +16,4 @@ interface CommonPingData {
2116
readonly reasonCodes?: string[]
2217
}
2318

24-
/**
25-
* Stores information about a ping.
26-
*
27-
* This is required so that given metric data queued on disk we can send
28-
* pings with the correct settings, e.g. whether it has a client_id.
29-
*/
30-
class PingType implements CommonPingData {
31-
readonly name: string;
32-
readonly includeClientId: boolean;
33-
readonly sendIfEmpty: boolean;
34-
readonly reasonCodes: string[];
35-
36-
constructor (meta: CommonPingData) {
37-
this.name = meta.name;
38-
this.includeClientId = meta.includeClientId;
39-
this.sendIfEmpty = meta.sendIfEmpty;
40-
this.reasonCodes = meta.reasonCodes ?? [];
41-
}
42-
43-
private isDeletionRequest(): boolean {
44-
return this.name === DELETION_REQUEST_PING_NAME;
45-
}
46-
47-
/**
48-
* Collects and submits a ping for eventual uploading.
49-
*
50-
* The ping content is assembled as soon as possible, but upload is not
51-
* guaranteed to happen immediately, as that depends on the upload policies.
52-
*
53-
* If the ping currently contains no content, it will not be sent,
54-
* unless it is configured to be sent if empty.
55-
*
56-
* @param reason The reason the ping was triggered. Included in the
57-
* `ping_info.reason` part of the payload.
58-
*/
59-
submit(reason?: string): void {
60-
Glean.dispatcher.launch(async () => {
61-
if (!Glean.initialized) {
62-
console.info("Glean must be initialized before submitting pings.");
63-
return;
64-
}
65-
66-
if (!Glean.isUploadEnabled() && !this.isDeletionRequest()) {
67-
console.info("Glean disabled: not submitting pings. Glean may still submit the deletion-request ping.");
68-
return;
69-
}
70-
71-
let correctedReason = reason;
72-
if (reason && !this.reasonCodes.includes(reason)) {
73-
console.error(`Invalid reason code ${reason} from ${this.name}. Ignoring.`);
74-
correctedReason = undefined;
75-
}
76-
77-
const identifier = generateUUIDv4();
78-
await collectAndStorePing(identifier, this, correctedReason);
79-
return;
80-
});
81-
}
82-
}
83-
84-
export default PingType;
19+
export default CommonPingData;

glean/src/core/pings/maker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import DatetimeMetricType, { DatetimeMetric } from "../metrics/types/datetime.js
88
import { Lifetime } from "../metrics/index.js";
99
import TimeUnit from "../metrics/time_unit.js";
1010
import { ClientInfo, PingInfo, PingPayload } from "../pings/ping_payload.js";
11-
import PingType from "../pings/index.js";
11+
import PingType from "../pings/pingtype.js";
1212
import Glean from "../glean.js";
1313
import CoreEvents from "../events/index.js";
1414

glean/src/core/pings/pingtype.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 {DELETION_REQUEST_PING_NAME} from "../constants";
6+
import Glean from "../glean";
7+
import {generateUUIDv4} from "../utils";
8+
import collectAndStorePing from "./maker";
9+
import CommonPingData from "./index";
10+
11+
/**
12+
* Stores information about a ping.
13+
*
14+
* This is required so that given metric data queued on disk we can send
15+
* pings with the correct settings, e.g. whether it has a client_id.
16+
*/
17+
class PingType implements CommonPingData {
18+
readonly name: string;
19+
readonly includeClientId: boolean;
20+
readonly sendIfEmpty: boolean;
21+
readonly reasonCodes: string[];
22+
23+
constructor (meta: CommonPingData) {
24+
this.name = meta.name;
25+
this.includeClientId = meta.includeClientId;
26+
this.sendIfEmpty = meta.sendIfEmpty;
27+
this.reasonCodes = meta.reasonCodes ?? [];
28+
}
29+
30+
private isDeletionRequest(): boolean {
31+
return this.name === DELETION_REQUEST_PING_NAME;
32+
}
33+
34+
/**
35+
* Collects and submits a ping for eventual uploading.
36+
*
37+
* The ping content is assembled as soon as possible, but upload is not
38+
* guaranteed to happen immediately, as that depends on the upload policies.
39+
*
40+
* If the ping currently contains no content, it will not be sent,
41+
* unless it is configured to be sent if empty.
42+
*
43+
* @param reason The reason the ping was triggered. Included in the
44+
* `ping_info.reason` part of the payload.
45+
*/
46+
submit(reason?: string): void {
47+
Glean.dispatcher.launch(async () => {
48+
if (!Glean.initialized) {
49+
console.info("Glean must be initialized before submitting pings.");
50+
return;
51+
}
52+
53+
if (!Glean.isUploadEnabled() && !this.isDeletionRequest()) {
54+
console.info("Glean disabled: not submitting pings. Glean may still submit the deletion-request ping.");
55+
return;
56+
}
57+
58+
let correctedReason = reason;
59+
if (reason && !this.reasonCodes.includes(reason)) {
60+
console.error(`Invalid reason code ${reason} from ${this.name}. Ignoring.`);
61+
correctedReason = undefined;
62+
}
63+
64+
const identifier = generateUUIDv4();
65+
await collectAndStorePing(identifier, this, correctedReason);
66+
return;
67+
});
68+
}
69+
}
70+
71+
export default PingType;

glean/src/index/qt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ConfigurationInterface } from "../core/config.js";
88
import platform from "../platform/qt/index.js";
99

1010
// Private Glean types to export.
11-
import PingType from "../core/pings/index.js";
11+
import PingType from "../core/pings/pingtype.js";
1212
import BooleanMetricType from "../core/metrics/types/boolean.js";
1313
import CounterMetricType from "../core/metrics/types/counter.js";
1414
import DatetimeMetricType from "../core/metrics/types/datetime.js";

glean/tests/core/glean.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Glean from "../../src/core/glean";
1111
import { Lifetime } from "../../src/core/metrics";
1212
import StringMetricType from "../../src/core/metrics/types/string";
1313
import CounterMetricType from "../../src/core/metrics/types/counter";
14-
import PingType from "../../src/core/pings";
14+
import PingType from "../../src/core/pings/pingtype";
1515
import { isObject, JSONObject } from "../../src/core/utils";
1616
import TestPlatform from "../../src/platform/qt";
1717
import Plugin from "../../src/plugins";

glean/tests/core/metrics/labeled.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import BooleanMetricType from "../../../src/core/metrics/types/boolean";
1111
import CounterMetricType from "../../../src/core/metrics/types/counter";
1212
import LabeledMetricType from "../../../src/core/metrics/types/labeled";
1313
import StringMetricType from "../../../src/core/metrics/types/string";
14-
import PingType from "../../../src/core/pings";
14+
import PingType from "../../../src/core/pings/pingtype";
1515
import { JSONObject } from "../../../src/core/utils";
1616

1717
const sandbox = sinon.createSandbox();

glean/tests/core/pings/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import assert from "assert";
66
import sinon from "sinon";
77

8-
import PingType from "../../../src/core/pings";
8+
import PingType from "../../../src/core/pings/pingtype";
99
import CounterMetricType from "../../../src/core/metrics/types/counter";
1010
import { Lifetime } from "../../../src/core/metrics";
1111
import Glean from "../../../src/core/glean";

glean/tests/core/pings/maker.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import assert from "assert";
66
import sinon from "sinon";
77

8-
import PingType from "../../../src/core/pings";
8+
import PingType from "../../../src/core/pings/pingtype";
99
import * as PingMaker from "../../../src/core/pings/maker";
1010
import Glean from "../../../src/core/glean";
1111
import CoreEvents from "../../../src/core/events";

0 commit comments

Comments
 (0)