@@ -19,20 +19,23 @@ export class NetworkCallController {
19
19
private countryCollection : CollectionType
20
20
private usageCollection : CollectionType
21
21
private hostToCountryCollection : CollectionType
22
+ private totalUsageCollection : CollectionType
22
23
private firestore : Firestore
23
24
24
25
constructor (
25
26
firestore : Firestore ,
26
27
hostCollection : CollectionType ,
27
28
countryCollection : CollectionType ,
28
29
usageCollection : CollectionType ,
29
- hostToCountryCollection : CollectionType
30
+ hostToCountryCollection : CollectionType ,
31
+ totalUsageCollection : CollectionType
30
32
) {
31
33
this . firestore = firestore
32
34
this . hostCollection = hostCollection
33
35
this . countryCollection = countryCollection
34
36
this . usageCollection = usageCollection
35
37
this . hostToCountryCollection = hostToCountryCollection
38
+ this . totalUsageCollection = usageCollection
36
39
}
37
40
38
41
getAllNetworkCalls = async ( ) => {
@@ -87,14 +90,16 @@ export class NetworkCallController {
87
90
date
88
91
} : {
89
92
identifier ?: string
90
- userId : string
93
+ userId ? : string
91
94
date ?: number
92
95
} ) => {
93
- return identifier ?. length && date
96
+ return identifier ?. length && date && userId
94
97
? `${ userId } -${ identifier } -${ date } `
95
- : identifier
98
+ : identifier && userId
96
99
? `${ userId } -${ identifier } `
97
- : `${ userId } -${ date } `
100
+ : userId
101
+ ? `${ userId } -${ date } `
102
+ : String ( date )
98
103
}
99
104
100
105
private getFieldValue = ( numberOfIncrements : number ) => {
@@ -187,6 +192,13 @@ export class NetworkCallController {
187
192
. set ( hostToCountryDoc , { merge : true } )
188
193
}
189
194
195
+ updateTotalUsage = ( networkCall : BaseUsageDoc ) => {
196
+ const uid = this . getDocId ( { date : networkCall . date } )
197
+ // @ts -ignore
198
+ delete networkCall . userId
199
+ return this . totalUsageCollection . doc ( uid ) . set ( networkCall , { merge : true } )
200
+ }
201
+
190
202
private getHostName = ( url : string ) => {
191
203
if ( ! url ) return ''
192
204
return url . replace ( / ^ (?: h t t p s ? : \/ \/ ) ? (?: w w w \. ) ? / i, '' ) . split ( '/' ) [ 0 ]
@@ -200,7 +212,7 @@ export class NetworkCallController {
200
212
const { CO2 , KWH } = Country . calculateEmission ( { size, countryCode } )
201
213
const usageId = this . getDocId ( { userId, date } )
202
214
const strippedHostOrigin = this . getHostName ( hostOrigin )
203
- console . log ( strippedHostOrigin )
215
+
204
216
const baseUsageDoc : BaseUsageDoc = {
205
217
uid : usageId ,
206
218
CO2 : this . getFieldValue ( CO2 ) ,
@@ -217,8 +229,10 @@ export class NetworkCallController {
217
229
this . updateUserStats ( baseUsageDoc ) ,
218
230
this . setHostDoc ( baseUsageDoc , strippedHostOrigin , usageId ) ,
219
231
this . setCountryDoc ( baseUsageDoc , countryCode , countryName , usageId ) ,
220
- this . setUsageDoc ( baseUsageDoc )
232
+ this . setUsageDoc ( baseUsageDoc ) ,
233
+ this . updateTotalUsage ( baseUsageDoc )
221
234
]
235
+
222
236
if ( strippedHostOrigin . length && countryCode )
223
237
promises . push (
224
238
this . setHostToCountryDoc (
@@ -230,10 +244,9 @@ export class NetworkCallController {
230
244
)
231
245
232
246
try {
233
- const [ host , network , country , usage , hostToCountry ] = await Promise . all (
234
- promises
235
- )
236
- return { host, network, country, usage, hostToCountry }
247
+ const [ host , network , country , usage , totalUsage , hostToCountry ] =
248
+ await Promise . all ( promises )
249
+ return { host, network, country, usage, totalUsage, hostToCountry }
237
250
} catch ( e ) {
238
251
console . log ( e )
239
252
}
0 commit comments