codecombat/server/models/AnalyticsLogEvent.coffee

37 lines
1.3 KiB
CoffeeScript
Raw Normal View History

log = require 'winston'
2014-12-15 14:45:12 -05:00
mongoose = require 'mongoose'
plugins = require '../plugins/plugins'
utils = require '../lib/utils'
http = require 'http'
config = require '../../server_config'
2014-12-15 14:45:12 -05:00
AnalyticsLogEventSchema = new mongoose.Schema({
user: String #Actually a `mongoose.Schema.Types.ObjectId` but ...
event: String
properties: mongoose.Schema.Types.Mixed
}, {strict: false, versionKey: false})
2015-05-01 12:37:43 -04:00
AnalyticsLogEventSchema.index({event: 1, _id: -1})
AnalyticsLogEventSchema.index({event: 1, 'properties.level': 1})
AnalyticsLogEventSchema.index({event: 1, 'properties.levelID': 1})
AnalyticsLogEventSchema.index({user: 1, event: 1})
2014-12-15 14:45:12 -05:00
AnalyticsLogEventSchema.statics.logEvent = (user, event, properties={}) ->
unless user?
log.warn 'No user given to analytics logEvent.'
return
doc = new AnalyticsLogEvent
user: user
event: event
properties: properties
doc.save()
unless config.proxy
analyticsMongoose = mongoose.createConnection()
analyticsMongoose.open "mongodb://#{config.mongo.analytics_host}:#{config.mongo.analytics_port}/#{config.mongo.analytics_db}", (error) ->
log.error "Couldnt connect to analytics", error if error
module.exports = AnalyticsLogEvent = analyticsMongoose.model('analytics.log.event', AnalyticsLogEventSchema, config.mongo.analytics_collection)