Integrate mixpanel

This commit is contained in:
Petros Angelatos 2014-06-18 17:54:36 +01:00 committed by Pablo Carranza Vélez
parent 0772638d92
commit 1501cf3122
7 changed files with 44 additions and 10 deletions

View File

@ -7,19 +7,20 @@
"start": "./entry.sh" "start": "./entry.sh"
}, },
"dependencies": { "dependencies": {
"bluebird": "~1.1.1",
"coffee-script": "~1.7.1", "coffee-script": "~1.7.1",
"request": "~2.22.0",
"express": "~3.2.6",
"lodash": "~2.4.1",
"csr-gen": "~0.2.1", "csr-gen": "~0.2.1",
"dockerode": "~2.0.0", "dockerode": "~2.0.0",
"knex": "~0.5.1",
"bluebird": "~1.1.1",
"JSONStream": "~0.7.1",
"event-stream": "~3.0.20", "event-stream": "~3.0.20",
"sqlite3": "~2.1.19", "express": "~3.2.6",
"JSONStream": "~0.7.1",
"knex": "~0.5.1",
"lodash": "~2.4.1",
"mixpanel": "0.0.20",
"pubnub": "~3.6.4",
"request": "~2.22.0",
"resin-platform-api": "git+ssh://git@bitbucket.org:rulemotion/resin-platform-api.git#v0.2.3", "resin-platform-api": "git+ssh://git@bitbucket.org:rulemotion/resin-platform-api.git#v0.2.3",
"pubnub": "~3.6.4" "sqlite3": "~2.1.19"
}, },
"engines": { "engines": {
"node": "0.10.22" "node": "0.10.22"

View File

@ -15,6 +15,7 @@ blink = (ms = 200) ->
.then -> fs.writeFileAsync(LED_FILE, 0) .then -> fs.writeFileAsync(LED_FILE, 0)
api.post '/v1/blink', (req, res) -> api.post '/v1/blink', (req, res) ->
utils.mixpanelTrack('Device blink')
interval = setInterval(blink, 400) interval = setInterval(blink, 400)
setTimeout(-> setTimeout(->
clearInterval(interval) clearInterval(interval)
@ -22,6 +23,7 @@ api.post '/v1/blink', (req, res) ->
res.send(200) res.send(200)
api.post '/v1/update', (req, res) -> api.post '/v1/update', (req, res) ->
utils.mixpanelTrack('Update notification')
console.log("Got application update") console.log("Got application update")
application.update() application.update()
res.send(204) res.send(204)

View File

@ -7,12 +7,19 @@ utils = require './utils'
bootstrap = require './bootstrap' bootstrap = require './bootstrap'
console.log('Supervisor started..') console.log('Supervisor started..')
utils.mixpanelTrack('Supervisor start')
knex('config').select('value').where(key: 'uuid').then ([uuid]) -> knex('config').select('value').where(key: 'uuid').then ([uuid]) ->
if not uuid?.value if not uuid?.value
console.log('New device detected. Bootstrapping..') console.log('New device detected. Bootstrapping..')
utils.mixpanelTrack('Device bootstrap')
bootstrap() bootstrap()
.then -> else
uuid.value
.then (uuid) ->
# Persist the uuid in subsequent metrics
utils.mixpanelProperties.uuid = uuid
api = require './api' api = require './api'
application = require './application' application = require './application'
supervisor = require './supervisor-update' supervisor = require './supervisor-update'

View File

@ -51,6 +51,8 @@ exports.kill = kill = (app) ->
.then -> .then ->
container.removeAsync() container.removeAsync()
) )
.tap ->
utils.mixpanelTrack('Application stop', app.imageId)
exports.start = start = (app) -> exports.start = start = (app) ->
docker.getImage(app.imageId).inspectAsync() docker.getImage(app.imageId).inspectAsync()
@ -110,6 +112,7 @@ exports.start = start = (app) ->
es.mapSync(publish) es.mapSync(publish)
) )
.tap -> .tap ->
utils.mixpanelTrack('Application start', app.imageId)
console.log('Started container:', app.imageId) console.log('Started container:', app.imageId)
exports.restart = restart = (app) -> exports.restart = restart = (app) ->
@ -186,19 +189,23 @@ exports.update = ->
# Delete all the ones to remove in one go # Delete all the ones to remove in one go
Promise.map toBeRemoved, (imageId) -> Promise.map toBeRemoved, (imageId) ->
kill(apps[imageId]) app = apps[imageId]
utils.mixpanelTrack('Application remove', app)
kill(app)
.then -> .then ->
knex('app').where('imageId', imageId).delete() knex('app').where('imageId', imageId).delete()
.then -> .then ->
# Then install the apps and add each to the db as they succeed # Then install the apps and add each to the db as they succeed
installingPromises = toBeInstalled.map (imageId) -> installingPromises = toBeInstalled.map (imageId) ->
app = remoteApps[imageId] app = remoteApps[imageId]
utils.mixpanelTrack('Application install', app)
start(app) start(app)
.then -> .then ->
knex('app').insert(app) knex('app').insert(app)
# And restart updated apps and update db as they succeed # And restart updated apps and update db as they succeed
updatingPromises = toBeUpdated.map (imageId) -> updatingPromises = toBeUpdated.map (imageId) ->
app = remoteApps[imageId] app = remoteApps[imageId]
utils.mixpanelTrack('Application update', app)
restart(app) restart(app)
.then -> .then ->
knex('app').update(app).where(imageId: app.imageId) knex('app').update(app).where(imageId: app.imageId)

View File

@ -85,3 +85,4 @@ module.exports = ->
]) ])
knex('app').truncate() knex('app').truncate()
]) ])
.return(uuid)

View File

@ -4,10 +4,12 @@ module.exports = config =
pubnub: pubnub:
subscribe_key: process.env.PUBNUB_SUBSCRIBE_KEY subscribe_key: process.env.PUBNUB_SUBSCRIBE_KEY
publish_key: process.env.PUBNUB_PUBLISH_KEY publish_key: process.env.PUBNUB_PUBLISH_KEY
mixpanelToken: process.env.MIXPANEL_TOKEN
dockerSocket: '/run/docker.sock' dockerSocket: '/run/docker.sock'
expectedEnvVars: [ expectedEnvVars: [
'API_ENDPOINT' 'API_ENDPOINT'
'REGISTRY_ENDPOINT' 'REGISTRY_ENDPOINT'
'MIXPANEL_TOKEN'
'PUBNUB_SUBSCRIBE_KEY' 'PUBNUB_SUBSCRIBE_KEY'
'PUBNUB_PUBLISH_KEY' 'PUBNUB_PUBLISH_KEY'
] ]

View File

@ -1,5 +1,8 @@
Promise = require 'bluebird' Promise = require 'bluebird'
_ = require 'lodash'
fs = Promise.promisifyAll require 'fs' fs = Promise.promisifyAll require 'fs'
config = require './config'
mixpanel = require 'mixpanel'
# Parses package.json and returns resin-supervisor's version # Parses package.json and returns resin-supervisor's version
exports.getSupervisorVersion = -> exports.getSupervisorVersion = ->
@ -7,3 +10,14 @@ exports.getSupervisorVersion = ->
.then (data) -> .then (data) ->
obj = JSON.parse data obj = JSON.parse data
return obj.version return obj.version
mixpanel.init(config.mixpanelToken)
exports.mixpanelProperties = mixpanelProperties =
username: require('/boot/config.json').username
exports.mixpanelTrack = (event, properties={}) ->
# Mutation is bad, and it should feel bad
properties = _.assign(_.cloneDeep(properties), mixpanelProperties)
mixpanel.track(event, properties)