mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-22 10:55:19 -04:00
Log app server analytics event to the new analytics server
I never managed to get the res.close event (responses from the server are 0-length) I feel like this is a bug with Node I should report.
This commit is contained in:
parent
d0ebd50339
commit
d0908f694c
1 changed files with 23 additions and 64 deletions
|
@ -2,6 +2,8 @@ log = require 'winston'
|
|||
mongoose = require 'mongoose'
|
||||
plugins = require '../plugins/plugins'
|
||||
utils = require '../lib/utils'
|
||||
http = require 'http'
|
||||
config = require '../../server_config'
|
||||
|
||||
AnalyticsLogEventSchema = new mongoose.Schema({
|
||||
u: mongoose.Schema.Types.ObjectId
|
||||
|
@ -17,74 +19,31 @@ AnalyticsLogEventSchema = new mongoose.Schema({
|
|||
AnalyticsLogEventSchema.index({event: 1, _id: 1})
|
||||
|
||||
AnalyticsLogEventSchema.statics.logEvent = (user, event, properties={}) ->
|
||||
# Replaces some keys and values with analytics string IDs to reduce the size of events
|
||||
unless user?
|
||||
log.warn 'No user given to analytics logEvent.'
|
||||
return
|
||||
|
||||
# TODO: Replace methods inefficient, watch logEvent server perf.
|
||||
doc = new AnalyticsLogEvent
|
||||
user: user
|
||||
event: event
|
||||
properties: properties
|
||||
if config.isProduction and not config.unittest
|
||||
docString = JSON.stringify doc
|
||||
headers =
|
||||
"Content-Type":'application/json'
|
||||
"Content-Length": docString.length
|
||||
|
||||
replaceKeys = (slimProperties, callback) ->
|
||||
# Replace all slimProperties key values with string IDs
|
||||
for key, value of slimProperties
|
||||
if isNaN(parseInt(key))
|
||||
utils.getAnalyticsStringID key, (stringID) ->
|
||||
if stringID > 0
|
||||
slimProperties[stringID] = value
|
||||
delete slimProperties[key]
|
||||
replaceKeys slimProperties, callback
|
||||
else
|
||||
callback()
|
||||
return
|
||||
callback()
|
||||
|
||||
replaceProperties = (slimProperties, callback) ->
|
||||
# Replace select slimProperties property values with string IDs
|
||||
for key, value of slimProperties
|
||||
if key in ['level', 'levelID', 'label', 'style'] and isNaN(parseInt(value))
|
||||
if key is 'levelID'
|
||||
key = 'level'
|
||||
slimProperties['level'] = _.cloneDeep slimProperties['levelID']
|
||||
delete slimProperties['levelID']
|
||||
utils.getAnalyticsStringID value, (stringID) ->
|
||||
if stringID > 0
|
||||
slimProperties[key] = stringID
|
||||
replaceProperties slimProperties, callback
|
||||
else
|
||||
callback()
|
||||
return
|
||||
callback()
|
||||
|
||||
saveDoc = (eventID, slimProperties) ->
|
||||
replaceProperties slimProperties, ->
|
||||
replaceKeys slimProperties, ->
|
||||
doc = new AnalyticsLogEvent
|
||||
u: user
|
||||
e: eventID
|
||||
p: slimProperties
|
||||
# TODO: Remove these legacy properties after we stop querying for them, sometime after ~3/10/15
|
||||
user: user
|
||||
event: event
|
||||
properties: properties
|
||||
doc.save()
|
||||
|
||||
utils.getAnalyticsStringID event, (eventID) ->
|
||||
if eventID > 0
|
||||
slimProperties = _.cloneDeep properties
|
||||
properties.ls = mongoose.Types.ObjectId properties.ls if properties.ls
|
||||
slimProperties.ls = mongoose.Types.ObjectId slimProperties.ls if slimProperties.ls
|
||||
|
||||
# Event-specific updates
|
||||
if event is 'Saw Victory'
|
||||
delete slimProperties.level
|
||||
if event is 'Heard Sprite' and slimProperties.message?
|
||||
utils.getAnalyticsStringID slimProperties.message, (stringID) ->
|
||||
slimProperties.message = stringID if stringID > 0
|
||||
saveDoc eventID, slimProperties
|
||||
return
|
||||
|
||||
saveDoc eventID, slimProperties
|
||||
else
|
||||
log.warn "Unable to get analytics string ID for " + event
|
||||
options =
|
||||
host: 'analytics.codecombat.com'
|
||||
port: 80
|
||||
path: '/analytics'
|
||||
method: 'POST'
|
||||
headers: headers
|
||||
req = http.request options, (res) ->
|
||||
req.on 'error', (e) -> log.warn e
|
||||
req.write(docString)
|
||||
req.end()
|
||||
else
|
||||
doc.save()
|
||||
|
||||
module.exports = AnalyticsLogEvent = mongoose.model('analytics.log.event', AnalyticsLogEventSchema)
|
||||
|
|
Loading…
Reference in a new issue