Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Fusselwurm/float-point-status
Float point status
- Loading branch information
Showing
6 changed files
with
133 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,97 @@ | ||
var db; | ||
|
||
var async = require('async'); | ||
var _ = require('underscore'); | ||
var noop = function () {}; | ||
|
||
function getRedisZsetProbabiltiesKey(tid) { | ||
return 'tid:%d:attendance:probabilities'.replace('%d', tid); | ||
function getRedisZsetKeyAttendanceInfo(tid, infoName) { | ||
return 'tid:%d:attendance:%s'.replace('%d', tid).replace('%s', infoName); | ||
} | ||
function getRedisZsetLastUpdateKey(tid) { | ||
return 'tid:%d:attendance:last_updated_at'.replace('%d', tid); | ||
|
||
function getRedisListKeyAttendanceHistory(tid, uid) { | ||
return 'tid:%d:attendance:user:%d:history'.replace('%d', tid).replace('%d', uid); | ||
} | ||
|
||
function setAsyncParallelAttendanceInfo(tid, infoName, infoNameRedis, userAttendanceInfo, next) { | ||
db.sortedSetAdd( | ||
getRedisZsetKeyAttendanceInfo(tid, infoNameRedis), | ||
userAttendanceInfo[infoName], | ||
userAttendanceInfo.uid, | ||
next | ||
); | ||
} | ||
|
||
function setAsyncParallelAttendanceHistory(tid, userAttendanceInfo, next) { | ||
db.listPrepend( | ||
getRedisListKeyAttendanceHistory(tid, userAttendanceInfo.uid), | ||
[userAttendanceInfo.timestamp, userAttendanceInfo.probability].join(':'), | ||
next | ||
); | ||
db.listTrim(getRedisListKeyAttendanceHistory(tid, userAttendanceInfo.uid), 0, 100, noop); | ||
} | ||
|
||
function getAsyncParallelAttendanceHistory(tid, uid, next) { | ||
db.getListRange(getRedisListKeyAttendanceHistory(tid, uid), 0, -1, function (err, results) { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
return next(null, results.map(function (item) { | ||
var bits = item.split(':'); | ||
return { | ||
timestamp: parseInt(bits[0], 10), | ||
probability: parseFloat(bits[1] || -1) | ||
}; | ||
})); | ||
}); | ||
} | ||
|
||
function getAsyncParallelAttendanceInfo(tid, infoNameRedis, next) { | ||
db.getSortedSetRangeWithScores(getRedisZsetKeyAttendanceInfo(tid, infoNameRedis), 0, -1, next); | ||
} | ||
|
||
function mergeAttendanceInfos(propertyNames, callback, err, results /*[probabilityInfos, lastUpdatedAtInfos]*/) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var userAttendanceInfos = {}; | ||
|
||
function setSomeValue(someValueName, info) { | ||
var userValue = userAttendanceInfos[info.value] = (userAttendanceInfos[info.value] || { uid: info.value }); | ||
userValue[someValueName] = info.score; | ||
} | ||
|
||
propertyNames.forEach(function (propertyName, idx) { | ||
results[idx].forEach(function (result) { | ||
setSomeValue(propertyName, result); | ||
}); | ||
}); | ||
|
||
callback(null, _.values(userAttendanceInfos)); | ||
} | ||
|
||
module.exports.setDatabase = function (databaseModule) { | ||
db = databaseModule; | ||
}; | ||
|
||
module.exports.set = function (tid, userAttendanceInfo /* {uid, probability, lastUpdatedAt} */, callback) { | ||
module.exports.set = function (tid, userAttendanceInfo /* {uid, probability, timestamp} */, callback) { | ||
async.parallel([ | ||
function (next) { | ||
db.sortedSetAdd( | ||
getRedisZsetLastUpdateKey(tid), | ||
userAttendanceInfo.lastUpdatedAt, | ||
userAttendanceInfo.uid, | ||
next | ||
); | ||
}, | ||
function (next) { | ||
db.sortedSetAdd( | ||
getRedisZsetProbabiltiesKey(tid), | ||
userAttendanceInfo.probability, | ||
userAttendanceInfo.uid, | ||
next | ||
); | ||
} | ||
_.partial(setAsyncParallelAttendanceInfo, tid, 'timestamp', 'last_updated_at', userAttendanceInfo), | ||
_.partial(setAsyncParallelAttendanceInfo, tid, 'probability', 'probability', userAttendanceInfo), | ||
_.partial(setAsyncParallelAttendanceHistory, tid, userAttendanceInfo) | ||
], callback); | ||
}; | ||
|
||
module.exports.get = function (tid, callback) { | ||
async.parallel([ | ||
function () { | ||
db.getSortedSetRangeWithScores(getRedisZsetProbabiltiesKey(tid), 0, -1, next); | ||
}, | ||
function () { | ||
db.getSortedSetRangeWithScores(getRedisZsetLastUpdateKey(tid), 0, -1, next); | ||
} | ||
], function (err, results) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var probabilityInfos /*Array<{score, value}>*/ = results[0]; | ||
var lastUpdatedAtInfos /*Array<{score, value}>*/ = results[1]; | ||
|
||
var userAttendanceInfos = {}; | ||
|
||
probabilityInfos.forEach(function (probabilityInfo) { | ||
var userValue = userAttendanceInfos[probabilityInfo.value] || { | ||
uid: probabilityInfo.value | ||
}; | ||
userValue.probability = probabilityInfo.score; | ||
userAttendanceInfos[userValue.uid] = userValue; | ||
}); | ||
|
||
lastUpdatedAtInfos.forEach(function (lastUpdateInfo) { | ||
var userValue = userAttendanceInfos[lastUpdateInfo.value] || { | ||
uid: lastUpdateInfo.value | ||
}; | ||
userValue.lastUpdatedAt = lastUpdateInfo.score; | ||
userAttendanceInfos[userValue.uid] = userValue; | ||
}); | ||
async.parallel( | ||
[ | ||
_.partial(getAsyncParallelAttendanceInfo, tid, 'probability'), | ||
_.partial(getAsyncParallelAttendanceInfo, tid, 'last_updated_at') | ||
], | ||
_.partial(mergeAttendanceInfos, ['probability', 'timestamp'], callback) | ||
); | ||
}; | ||
|
||
callback(null, userAttendanceInfos); | ||
}); | ||
module.exports.getUserHistory = function (tid, uid, callback) { | ||
getAsyncParallelAttendanceHistory(tid, uid, callback); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters