44
55import type Store from "../../storage/index.js" ;
66import type { JSONArray , JSONObject , JSONValue } from "../../utils.js" ;
7+ import { isString } from "../../utils.js" ;
78import type { StorageBuilder } from "../../../platform/index.js" ;
89import { isUndefined } from "../../utils.js" ;
910import EventMetricType from "../types/event.js" ;
@@ -12,6 +13,7 @@ import CounterMetricType from "../types/counter.js";
1213import { Lifetime } from "../lifetime.js" ;
1314import { Context } from "../../context.js" ;
1415import { generateReservedMetricIdentifiers } from "../database.js" ;
16+ import type { ExtraValues } from "./recorded_event.js" ;
1517import { RecordedEvent } from "./recorded_event.js" ;
1618import {
1719 GLEAN_EXECUTION_COUNTER_EXTRA_KEY ,
@@ -29,7 +31,11 @@ const LOG_TAG = "core.Metric.EventsDatabase";
2931 * @param str The string to generate a date from.
3032 * @returns The Date object created.
3133 */
32- function createDateObject ( str = "" ) : Date {
34+ function createDateObject ( str ?: ExtraValues ) : Date {
35+ if ( ! isString ( str ) ) {
36+ str = "" ;
37+ }
38+
3339 const date = new Date ( str ) ;
3440 // Date object will not throw errors when we attempt to create an invalid date.
3541 if ( isNaN ( date . getTime ( ) ) ) {
@@ -158,7 +164,7 @@ class EventsDatabase {
158164 for ( const ping of metric . sendInPings ) {
159165 const executionCounter = getExecutionCounterMetric ( [ ping ] ) ;
160166
161- let currentExecutionCount = await Context . metricsDatabase . getMetric ( ping , executionCounter ) ;
167+ let currentExecutionCount = await Context . metricsDatabase . getMetric < number > ( ping , executionCounter ) ;
162168 // There might not be an execution counter stored in case:
163169 //
164170 // 1. The ping was already sent during this session and the events storage was cleared;
@@ -171,8 +177,7 @@ class EventsDatabase {
171177 // this must **always** be the first event of any events list.
172178 await recordGleanRestartedEvent ( [ ping ] , new Date ( ) ) ;
173179 }
174- // TODO (bug 1693487): Remove the stringification once the new events API is implemented.
175- value . addExtra ( GLEAN_EXECUTION_COUNTER_EXTRA_KEY , currentExecutionCount . toString ( ) ) ;
180+ value . addExtra ( GLEAN_EXECUTION_COUNTER_EXTRA_KEY , currentExecutionCount ) ;
176181
177182 const transformFn = ( v ?: JSONValue ) : JSONArray => {
178183 const existing : JSONArray = ( v as JSONArray ) ?? [ ] ;
@@ -273,7 +278,7 @@ class EventsDatabase {
273278 * 1. Sorts event by execution counter and timestamp;
274279 * 2. Applies offset to events timestamps;
275280 * 3. Removes the first event if it is a `glean.restarted` event;
276- * 4. Removes reserved extra keys.
281+ * 4. Removes reserved extra keys and stringifies all extras values .
277282 *
278283 * @param pingName The name of the ping for which the payload is being prepared.
279284 * @param pingData An unsorted list of events.
@@ -285,7 +290,6 @@ class EventsDatabase {
285290 ) : Promise < JSONArray > {
286291 // Sort events by execution counter and by timestamp.
287292 const sortedEvents = pingData . sort ( ( a , b ) => {
288- // TODO (bug 1693487): Remove the number casting once the new events API is implemented.
289293 const executionCounterA = Number ( a . extra ?. [ GLEAN_EXECUTION_COUNTER_EXTRA_KEY ] ) ;
290294 const executionCounterB = Number ( b . extra ?. [ GLEAN_EXECUTION_COUNTER_EXTRA_KEY ] ) ;
291295 // Sort by execution counter, in case they are different.
@@ -364,7 +368,7 @@ class EventsDatabase {
364368 ) ;
365369 }
366370
367- return sortedEvents . map ( ( e ) => RecordedEvent . toJSONObject ( e . withoutReservedExtras ( ) ) ) ;
371+ return sortedEvents . map ( ( e ) => RecordedEvent . toJSONObject ( e . payload ( ) ) ) ;
368372 }
369373
370374 /**
0 commit comments