33// * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55import { Store } from "storage" ;
6- import WeakStore from "storage/weak" ;
7- import { MetricType } from "metrics" ;
6+ import PersistentStore from "storage/persistent" ;
87import { isUndefined , JSONArray , JSONObject , JSONValue } from "utils" ;
8+ import EventMetricType from "./types/event" ;
99
1010export interface Metrics {
1111 [ aMetricType : string ] : {
@@ -14,21 +14,28 @@ export interface Metrics {
1414}
1515
1616// An helper type for the 'extra' map.
17- export type ExtraMap = { [ name : string ] : string } ;
17+ export type ExtraMap = Record < string , string > ;
1818
1919// Represents the recorded data for a single event.
2020export class RecordedEvent {
2121 constructor (
22- category : string ,
23- name : string ,
24- timestamp : number ,
25- extra ?: ExtraMap ,
26- ) {
27- this . category = category ;
28- this . name = name ;
29- this . timestamp = timestamp ;
30- this . extra = extra ;
31- }
22+ // The event's category.
23+ //
24+ // This is defined by users in the metrics file.
25+ readonly category : string ,
26+ // The event's name.
27+ //
28+ // This is defined by users in the metrics file.
29+ readonly name : string ,
30+ // The timestamp of when the event was recorded.
31+ //
32+ // This allows to order events.
33+ readonly timestamp : number ,
34+ // A map of all extra data values.
35+ //
36+ // The set of allowed extra keys is defined by users in the metrics file.
37+ readonly extra ?: ExtraMap ,
38+ ) { }
3239
3340 static toJSONObject ( e : RecordedEvent ) : JSONObject {
3441 return {
@@ -47,23 +54,6 @@ export class RecordedEvent {
4754 e [ "extra" ] as ExtraMap | undefined
4855 ) ;
4956 }
50-
51- // The event's category.
52- //
53- // This is defined by users in the metrics file.
54- readonly category : string ;
55- // The event's name.
56- //
57- // This is defined by users in the metrics file.
58- readonly name : string ;
59- // The timestamp of when the event was recorded.
60- //
61- // This allows to order events.
62- readonly timestamp : number ;
63- // A map of all extra data values.
64- //
65- // The set of allowed extra keys is defined by users in the metrics file.
66- readonly extra ?: ExtraMap ;
6757}
6858
6959/**
@@ -91,7 +81,7 @@ class EventsDatabase {
9181 private eventsStore : Store ;
9282
9383 constructor ( ) {
94- this . eventsStore = new WeakStore ( "unused" ) ;
84+ this . eventsStore = new PersistentStore ( "unused" ) ;
9585 }
9686
9787 /**
@@ -100,7 +90,7 @@ class EventsDatabase {
10090 * @param metric The metric to record to.
10191 * @param value The value we want to record to the given metric.
10292 */
103- async record ( metric : MetricType , value : RecordedEvent ) : Promise < void > {
93+ async record ( metric : EventMetricType , value : RecordedEvent ) : Promise < void > {
10494 if ( metric . disabled ) {
10595 return ;
10696 }
@@ -127,26 +117,19 @@ class EventsDatabase {
127117 * @returns an array of `RecordedEvent` containing the found events or `undefined`
128118 * if no recorded event was found.
129119 */
130- async testGetValue (
120+ async getEvents (
131121 ping : string ,
132- metric : MetricType
122+ metric : EventMetricType
133123 ) : Promise < RecordedEvent [ ] | undefined > {
134- const value = await this . eventsStore . get ( [ ping ] ) ;
135- if ( ! value ) {
124+ const events = await this . getAndValidatePingData ( ping ) ;
125+ if ( events . length === 0 ) {
136126 return undefined ;
137127 }
138128
139- const rawEvents = value as JSONArray ;
140- return rawEvents
129+ return events
141130 // Only report events for the requested metric.
142131 . filter ( ( e ) => {
143- const rawEventObj = e as JSONObject ;
144- return ( rawEventObj [ "category" ] === metric . category )
145- && ( rawEventObj [ "name" ] === metric . name ) ;
146- } )
147- // Convert them to `RecordedEvent`s.
148- . map ( ( e ) => {
149- return RecordedEvent . fromJSONObject ( e as JSONObject ) ;
132+ return ( e . category === metric . category ) && ( e . name === metric . name ) ;
150133 } ) ;
151134 }
152135
@@ -164,7 +147,7 @@ class EventsDatabase {
164147 * @returns The ping payload found for the given parameters or an empty object
165148 * in case no data was found or the data that was found, was invalid.
166149 */
167- private async getAndValidatePingData ( ping : string ) : Promise < JSONArray > {
150+ private async getAndValidatePingData ( ping : string ) : Promise < RecordedEvent [ ] > {
168151 const data = await this . eventsStore . get ( [ ping ] ) ;
169152 if ( isUndefined ( data ) ) {
170153 return [ ] ;
@@ -177,7 +160,7 @@ class EventsDatabase {
177160 return [ ] ;
178161 }
179162
180- return data ;
163+ return data . map ( ( e ) => RecordedEvent . fromJSONObject ( e as JSONObject ) ) ;
181164 }
182165
183166 /**
@@ -189,7 +172,7 @@ class EventsDatabase {
189172 * @returns An object containing all the metrics recorded to the given ping,
190173 * `undefined` in case the ping doesn't contain any recorded metrics.
191174 */
192- async getPingMetrics ( ping : string , clearPingLifetimeData : boolean ) : Promise < JSONArray | undefined > {
175+ async getPingEvents ( ping : string , clearPingLifetimeData : boolean ) : Promise < JSONArray | undefined > {
193176 const pingData = await this . getAndValidatePingData ( ping ) ;
194177
195178 if ( clearPingLifetimeData ) {
@@ -202,20 +185,16 @@ class EventsDatabase {
202185
203186 // Sort the events by their timestamp.
204187 const sortedData = pingData . sort ( ( a , b ) => {
205- const objA = a as unknown as RecordedEvent ;
206- const objB = b as unknown as RecordedEvent ;
207- return objA [ "timestamp" ] - objB [ "timestamp" ] ;
188+ return a . timestamp - b . timestamp ;
208189 } ) ;
209190
210191 // Make all the events relative to the first one.
211- const firstTimestamp =
212- ( sortedData [ 0 ] as unknown as RecordedEvent ) [ "timestamp" ] ;
192+ const firstTimestamp = sortedData [ 0 ] . timestamp ;
213193
214194 return sortedData . map ( ( e ) => {
215- const objE = e as JSONObject ;
216- const timestamp = ( objE [ "timestamp" ] as number ) ?? 0 ;
217- objE [ "timestamp" ] = timestamp - firstTimestamp ;
218- return objE ;
195+ const adjusted = RecordedEvent . toJSONObject ( e ) ;
196+ adjusted [ "timestamp" ] = e . timestamp - firstTimestamp ;
197+ return adjusted ;
219198 } ) ;
220199 }
221200
0 commit comments