@@ -42,23 +42,46 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
4242 const thisResultRef = admin . database ( ) . ref ( '/v2/results/' + context . params . projectId + '/' + context . params . groupId + '/' + context . params . userId ) ;
4343 const userGroupsRef = admin . database ( ) . ref ( '/v2/userGroups/' ) ;
4444
45+ let appVersionString : string | undefined | null = undefined ;
46+
47+ type Args = Record < string , string | number | null | undefined >
48+ // eslint-disable-next-line require-jsdoc
49+ function logger ( message : string , extraArgs : Args = { } , logFunction : ( typeof console . log ) = console . log ) {
50+ const ctx : Args = {
51+ message : message ,
52+ ...extraArgs ,
53+ project : context . params . projectId ,
54+ user : context . params . userId ,
55+ group : context . params . groupId ,
56+ version : appVersionString ,
57+ } ;
58+ const items = Object . keys ( ctx ) . reduce < string [ ] > (
59+ ( acc , key ) => {
60+ const value = ctx [ key ] ;
61+ if ( value === undefined || value === null || value === '' ) {
62+ return acc ;
63+ }
64+ const item = `${ key } [${ value } ]` ;
65+ return [ ...acc , item ] ;
66+ } ,
67+ [ ]
68+ ) ;
69+ logFunction ( items . join ( ' ' ) ) ;
70+ }
4571
4672 // Check for specific user ids which have been identified as problematic.
4773 // These users have repeatedly uploaded harmful results.
4874 // Add new user ids to this list if needed.
4975 const userIds : string [ ] = [ ] ;
50- if ( userIds . includes ( context . params . userId ) ) {
51- console . log ( 'suspicious user: ' + context . params . userId ) ;
52- console . log ( 'will remove this result and not update counters' ) ;
76+ if ( userIds . includes ( context . params . userId ) ) {
77+ console . log ( 'Result removed because of suspicious user activity' ) ;
5378 return thisResultRef . remove ( ) ;
5479 }
5580
5681 const result = snapshot . val ( ) ;
57-
58-
5982 // New versions of app will have the appVersion defined (> 2.2.5)
6083 // appVersion: 2.2.5 (14)-dev
61- const appVersionString = result . appVersion as string | undefined | null ;
84+ appVersionString = result . appVersion ;
6285
6386 // Check if the app is of older version
6487 // (no need to check for specific version since old app won't sent the version info)
@@ -68,11 +91,11 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
6891
6992 if ( dataSnapshot . exists ( ) ) {
7093 const project = dataSnapshot . val ( ) ;
71- // Check if project type is validate and also has
94+ // Check if project type is ' validate' and also has
7295 // custom options (i.e. these are new type of projects)
7396 if ( project . projectType === 2 && project . customOptions ) {
7497 // We remove the results submitted from older version of app (< v2.2.6)
75- console . info ( ` Result submitted for ${ context . params . projectId } was discarded: submitted from older version of app` ) ;
98+ logger ( ' Result removed because it was submitted from an older version' , undefined , console . error ) ;
7699 return thisResultRef . remove ( ) ;
77100 }
78101 }
@@ -81,16 +104,13 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
81104 // if result ref does not contain all required attributes we don't updated counters
82105 // e.g. due to some error when uploading from client
83106 if ( ! Object . prototype . hasOwnProperty . call ( result , 'results' ) ) {
84- console . log ( 'no results attribute for ' + snapshot . ref ) ;
85- console . log ( 'will not update counters' ) ;
107+ logger ( 'Not updating counters because results attribute was not found.' , { result : String ( snapshot . ref ) } , console . error ) ;
86108 return null ;
87109 } else if ( ! Object . prototype . hasOwnProperty . call ( result , 'endTime' ) ) {
88- console . log ( 'no endTime attribute for ' + snapshot . ref ) ;
89- console . log ( 'will not update counters' ) ;
110+ logger ( 'Not updating counters because endTime attribute was not found.' , { result : String ( snapshot . ref ) } , console . error ) ;
90111 return null ;
91112 } else if ( ! Object . prototype . hasOwnProperty . call ( result , 'startTime' ) ) {
92- console . log ( 'no startTime attribute for ' + snapshot . ref ) ;
93- console . log ( 'will not update counters' ) ;
113+ logger ( 'Not updating counters because startTime attribute was not found.' , { result : String ( snapshot . ref ) } , console . error ) ;
94114 return null ;
95115 }
96116
@@ -103,8 +123,7 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
103123 const mappingSpeed = ( endTime - startTime ) / numberOfTasks ;
104124 if ( mappingSpeed < 0.125 ) {
105125 // this about 8-times faster than the average time needed per task
106- console . log ( 'unlikely high mapping speed: ' + mappingSpeed ) ;
107- console . log ( 'will remove this result and not update counters' ) ;
126+ logger ( 'Result removed because of unlikely high mapping speed' , { mappingSpeed : mappingSpeed } , console . warn ) ;
108127 return thisResultRef . remove ( ) ;
109128 }
110129
@@ -117,10 +136,12 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
117136 */
118137 const dataSnapshot = await groupUsersRef . child ( context . params . userId ) . once ( 'value' ) ;
119138 if ( dataSnapshot . exists ( ) ) {
120- console . log ( 'group contribution exists already. user: ' + context . params . userId + ' project: ' + context . params . projectId + ' group: ' + context . params . groupId ) ;
139+ logger ( 'Group contribution already exists.' ) ;
121140 return null ;
122141 }
123142
143+ // Update contributions
144+
124145 const latestNumberOfTasks = Object . keys ( result [ 'results' ] ) . length ;
125146 await Promise . all ( [
126147 userContributionRef . child ( context . params . groupId ) . set ( true ) ,
@@ -136,8 +157,8 @@ exports.groupUsersCounter = functions.database.ref('/v2/results/{projectId}/{gro
136157 } ) ,
137158 ] ) ;
138159
139-
140160 // Tag userGroups of the user in the result
161+
141162 const userGroupsOfTheUserSnapshot = await userRef . child ( 'userGroups' ) . once ( 'value' ) ;
142163 if ( ! userGroupsOfTheUserSnapshot . exists ( ) ) {
143164 return null ;
0 commit comments