2015-01-14 20:51:04 -05:00
|
|
|
AnalyticsString = require '../analytics/AnalyticsString'
|
2015-02-09 18:02:45 -05:00
|
|
|
log = require 'winston'
|
2015-01-14 14:09:01 -05:00
|
|
|
mongoose = require 'mongoose'
|
2014-08-14 10:38:50 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
isID: (id) -> _.isString(id) and id.length is 24 and id.match(/[a-f0-9]/gi)?.length is 24
|
2015-01-14 14:09:01 -05:00
|
|
|
objectIdFromTimestamp: (timestamp) ->
|
|
|
|
# mongoDB ObjectId contains creation date in first 4 bytes
|
|
|
|
# So, it can be used instead of a redundant created field
|
|
|
|
# http://docs.mongodb.org/manual/reference/object-id/
|
|
|
|
# http://stackoverflow.com/questions/8749971/can-i-query-mongodb-objectid-by-date
|
|
|
|
# Convert string date to Date object (otherwise assume timestamp is a date)
|
|
|
|
timestamp = new Date(timestamp) if typeof(timestamp) == 'string'
|
|
|
|
# Convert date object to hex seconds since Unix epoch
|
|
|
|
hexSeconds = Math.floor(timestamp/1000).toString(16)
|
|
|
|
# Create an ObjectId with that hex timestamp
|
|
|
|
mongoose.Types.ObjectId(hexSeconds + "0000000000000000")
|
2015-01-14 20:51:04 -05:00
|
|
|
getAnalyticsStringID: (str, callback) ->
|
2015-02-09 18:02:45 -05:00
|
|
|
unless str?
|
|
|
|
log.error "getAnalyticsStringID given invalid str param"
|
|
|
|
return callback -1
|
2015-01-14 20:51:04 -05:00
|
|
|
@analyticsStringCache ?= {}
|
|
|
|
return callback @analyticsStringCache[str] if @analyticsStringCache[str]
|
|
|
|
|
|
|
|
insertString = =>
|
|
|
|
# http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/#auto-increment-optimistic-loop
|
|
|
|
AnalyticsString.find({}, {_id: 1}).sort({_id: -1}).limit(1).exec (err, documents) =>
|
2015-02-09 18:02:45 -05:00
|
|
|
if err?
|
|
|
|
log.error "Failed to find next analytics string _id for #{str}"
|
|
|
|
return callback -1
|
2015-01-14 20:51:04 -05:00
|
|
|
seq = if documents.length > 0 then documents[0]._id + 1 else 1
|
|
|
|
doc = new AnalyticsString _id: seq, v: str
|
2015-02-09 18:02:45 -05:00
|
|
|
doc.save (err) =>
|
|
|
|
if err?
|
|
|
|
log.error "Failed to save analytics string ID for #{str}"
|
|
|
|
return callback -1
|
2015-01-14 20:51:04 -05:00
|
|
|
@analyticsStringCache[str] = seq
|
|
|
|
callback seq
|
|
|
|
|
|
|
|
# Find existing string
|
|
|
|
AnalyticsString.findOne(v: str).exec (err, document) =>
|
2015-02-09 18:02:45 -05:00
|
|
|
if err?
|
|
|
|
log.error "Failed to lookup analytics string #{str}"
|
|
|
|
return callback -1
|
2015-01-14 20:51:04 -05:00
|
|
|
if document
|
|
|
|
@analyticsStringCache[str] = document._id
|
|
|
|
return callback @analyticsStringCache[str]
|
|
|
|
insertString()
|