From 3a9f0adba8a04ec1a49c733b21d8fa0772e29e19 Mon Sep 17 00:00:00 2001 From: Rob Date: Thu, 9 Jun 2016 15:02:01 -0700 Subject: [PATCH] Switch to using actual node server to receive log events --- app/core/Tracker.coffee | 23 ++++++------------- server/models/AnalyticsLogEvent.coffee | 31 +++++--------------------- server_config.coffee | 3 +++ 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/app/core/Tracker.coffee b/app/core/Tracker.coffee index 2f2aab758..8c0f2eefc 100644 --- a/app/core/Tracker.coffee +++ b/app/core/Tracker.coffee @@ -165,22 +165,13 @@ module.exports = class Tracker extends CocoClass properties[key] = value for key, value of @explicitTraits if @explicitTraits? console.log 'Tracking internal analytics event:', event, properties if debugAnalytics - if @isProduction - eventObject = {} - eventObject["event"] = event - eventObject["properties"] = properties unless _.isEmpty properties - eventObject["user"] = me.id - dataToSend = JSON.stringify eventObject - # console.log dataToSend if debugAnalytics - $.post("#{window.location.protocol or 'http:'}//analytics-cf.codecombat.com/analytics", dataToSend).fail -> - console.error "Analytics post failed!" - else - request = @supermodel.addRequestResource { - url: '/db/analytics.log.event/-/log_event' - data: {event: event, properties: properties} - method: 'POST' - }, 0 - request.load() + + request = @supermodel.addRequestResource { + url: '/db/analytics.log.event/-/log_event' + data: {event: event, properties: properties} + method: 'POST' + }, 0 + request.load() trackTiming: (duration, category, variable, label) -> # https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings diff --git a/server/models/AnalyticsLogEvent.coffee b/server/models/AnalyticsLogEvent.coffee index 076f98be3..b1e998702 100644 --- a/server/models/AnalyticsLogEvent.coffee +++ b/server/models/AnalyticsLogEvent.coffee @@ -6,15 +6,10 @@ http = require 'http' config = require '../../server_config' AnalyticsLogEventSchema = new mongoose.Schema({ - u: mongoose.Schema.Types.ObjectId - e: Number # event analytics.string ID - p: mongoose.Schema.Types.Mixed - - # TODO: Remove these legacy properties after we stop querying for them (probably 30 days, ~2/16/15) - user: mongoose.Schema.Types.ObjectId + user: String #Actually a `mongoose.Schema.Types.ObjectId` but ... event: String properties: mongoose.Schema.Types.Mixed -}, {strict: false}) +}, {strict: false, versionKey: false}) AnalyticsLogEventSchema.index({event: 1, _id: -1}) AnalyticsLogEventSchema.index({event: 1, 'properties.level': 1}) @@ -30,23 +25,9 @@ AnalyticsLogEventSchema.statics.logEvent = (user, event, properties={}) -> 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 - 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() + doc.save() -module.exports = AnalyticsLogEvent = mongoose.model('analytics.log.event', AnalyticsLogEventSchema) +analyticsMongoose = mongoose.createConnection "mongodb://#{config.mongo.analytics_host}:#{config.mongo.analytics_port}/#{config.mongo.analytics_db}" + +module.exports = AnalyticsLogEvent = analyticsMongoose.model(config.mongo.analytics_collection, AnalyticsLogEventSchema) diff --git a/server_config.coffee b/server_config.coffee index 6f6eaf7ee..d76451789 100644 --- a/server_config.coffee +++ b/server_config.coffee @@ -21,10 +21,13 @@ config.mongo = analytics_port: process.env.COCO_MONGO_ANALYTICS_PORT or 27017 analytics_host: process.env.COCO_MONGO_ANALYTICS_HOST or 'localhost' analytics_db: process.env.COCO_MONGO_ANALYTICS_DATABASE_NAME or 'analytics' + analytics_collection: process.env.COCO_MONGO_ANALYTICS_COLLECTION or 'analytics.log.event' mongoose_replica_string: process.env.COCO_MONGO_MONGOOSE_REPLICA_STRING or '' mongoose_tokyo_replica_string: process.env.COCO_MONGO_MONGOOSE_TOKYO_REPLICA_STRING or '' mongoose_saoPaulo_replica_string : process.env.COCO_MONGO_MONGOOSE_SAOPAULO_REPLICA_STRING or '' + + if config.tokyo or config.saoPaulo config.mongo.readpref = 'nearest' else