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"
},
"dependencies": {
"bluebird": "~1.1.1",
"coffee-script": "~1.7.1",
"request": "~2.22.0",
"express": "~3.2.6",
"lodash": "~2.4.1",
"csr-gen": "~0.2.1",
"dockerode": "~2.0.0",
"knex": "~0.5.1",
"bluebird": "~1.1.1",
"JSONStream": "~0.7.1",
"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",
"pubnub": "~3.6.4"
"sqlite3": "~2.1.19"
},
"engines": {
"node": "0.10.22"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,8 @@
Promise = require 'bluebird'
_ = require 'lodash'
fs = Promise.promisifyAll require 'fs'
config = require './config'
mixpanel = require 'mixpanel'
# Parses package.json and returns resin-supervisor's version
exports.getSupervisorVersion = ->
@ -7,3 +10,14 @@ exports.getSupervisorVersion = ->
.then (data) ->
obj = JSON.parse data
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)