-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BACK-43] Dedup hash val #195
Changes from 24 commits
0b906be
da75a44
69c7645
f63537d
47b7e21
90c4a47
298d146
639286a
50759a8
b81bf9e
d8217eb
d2103b0
c000e04
f804770
e57f538
549aef6
8a8ff60
60e3601
32473a2
d58fa99
c11f9d2
6528cb4
80cc1b9
0d89f0b
572050b
24a6d7a
4d8b48a
986c7fa
21625df
a42583d
5da25ed
6ab36c1
618c957
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
var amoeba = require('amoeba'); | ||
var except = amoeba.except; | ||
var misc = require('../misc.js'); | ||
var schema = require('./schema.js'); | ||
|
||
var idHashFieldMap = {}; | ||
|
||
const getIdHashFields = function (type) { | ||
var retVal = idHashFieldMap[type]; | ||
if (retVal == null) { | ||
throw except.IAE('No known hashFields for type[%s]', type); | ||
} | ||
return retVal; | ||
}; | ||
|
||
exports.registerFieldsForDuplicator = function (type, idHashFields = []) { | ||
if (idHashFieldMap[type] == null) { | ||
idHashFieldMap[type] = [ | ||
'_userId', | ||
'deviceId', | ||
'time', | ||
'type', | ||
...idHashFields, | ||
]; | ||
} else { | ||
throw except.IAE( | ||
'Id hash fields for type[%s] already defined[%j], cannot set[%j]', | ||
type, | ||
idHashFieldMap[type], | ||
idHashFields | ||
); | ||
} | ||
}; | ||
|
||
exports.generateHash = function (datum) { | ||
if (typeof datum === 'string') { | ||
return datum; | ||
} | ||
var idHashFields = idHashFieldMap[datum.type]; | ||
if (!idHashFields) { | ||
return ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the caller check for empty string and not write the platform deduplicator hash? We don't want any with empty strings in the database I think. |
||
} | ||
var vals = new Array(idHashFields.length); | ||
for (var i = 0; i < idHashFields.length; ++i) { | ||
let val = datum[idHashFields[i]]; | ||
if (val == null) { | ||
throw except.IAE( | ||
"Can't generate hash, field[%s] didn't exist on datum of type[%s]", | ||
idHashFields[i], | ||
datum.type | ||
); | ||
} | ||
if (idHashFields[i] === 'time') { | ||
const dateTime = new Date(val); | ||
// NOTE: platform `time` is being returned minus millis so we need to do the same here | ||
val = dateTime.toISOString().split('.')[0] + 'Z'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Huh, I didn't know this. And it has the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to convert timezones here? |
||
} | ||
if (idHashFields[i] === 'value') { | ||
if ( | ||
datum.type === 'smbg' || | ||
datum.type === 'bloodKetone' || | ||
datum.type === 'cbg' | ||
) { | ||
// NOTE: platform `value` precision is being used so that the hash will be the same | ||
if (val.toString().length > 7) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we only want to do this for |
||
val = schema.convertMgToMmolPrecision(val); | ||
} | ||
} | ||
} | ||
vals[i] = String(val); | ||
} | ||
return misc.generateHash(vals); | ||
}; | ||
|
||
exports.getIdHashFields = getIdHashFields; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ var schema = require('./schema.js'); | |
|
||
var idFields = ['type', 'time', 'creatorId', 'text', 'deviceId']; | ||
schema.registerIdFields('note', idFields); | ||
schema.registerFieldsForDuplicator('note'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI - I don't think platform handles this data type. If Uploader sends this data type, we'll need to update platform. Please note this in the narrative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uploader does not use |
||
|
||
module.exports = function(streamDAO){ | ||
return schema.makeHandler('note', { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ var except = amoeba.except; | |
var util = require('util'); | ||
|
||
var misc = require('../misc.js'); | ||
var duplicate = require('./duplicate.js'); | ||
|
||
exports.validDeviceTime = function(val) { | ||
if (!/^(\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d)$/.test(val)) { | ||
|
@@ -252,6 +253,13 @@ exports.convertMgToMmol = function(mg) { | |
return mg / 18.01559; | ||
}; | ||
|
||
exports.convertMgToMmolPrecision = function(mgValue) { | ||
const mmolLToMgdLConversionFactor = 18.01559; | ||
const mmolLToMgdLPrecisionFactor = 100000.0; | ||
let mmolVal = parseInt(mgValue / mmolLToMgdLConversionFactor * mmolLToMgdLPrecisionFactor + 0.5); | ||
return mmolVal / mmolLToMgdLPrecisionFactor; | ||
}; | ||
|
||
exports.convertUnits = function(datum) { | ||
var fields = Array.prototype.slice.call(arguments, 1); | ||
var normalUnits = exports.normalizeUnitName(datum.units); | ||
|
@@ -513,3 +521,7 @@ exports.makeId = function(datum) { | |
|
||
return exports.generateId(datum, idFields); | ||
}; | ||
|
||
exports.generateHash = duplicate.generateHash; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps include "platform" name in exports so it is clear it is for the platform deduplication. |
||
exports.registerFieldsForDuplicator = duplicate.registerFieldsForDuplicator; | ||
exports.getIdHashFields = duplicate.getIdHashFields; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ var schema = require('./schema.js'); | |
|
||
var idFields = ['type', 'deviceId', 'time']; | ||
schema.registerIdFields('urineKetone', idFields); | ||
schema.registerFieldsForDuplicator('urineKetone'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI - I don't think platform handles this data type. If Uploader sends this data type, we'll need to update platform. Please note this in the narrative. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uploader does not use |
||
|
||
module.exports = schema.makeHandler('urineKetone', { | ||
schema: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Why is this file called
duplicate.js
? Perhaps,platform.js
or similar to indicate it is the platform deduplicator? That way when it has used it would beplatform.registerFieldsForDuplicator
which easily shows it is for platform.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, perhaps include "platform" in the exported functions so it is clear that it is for platform deduplication.