2015-03-27 14:22:21 -04:00
|
|
|
RootView = require 'views/core/RootView'
|
|
|
|
template = require 'templates/admin/analytics-subscriptions'
|
2015-04-07 18:00:50 -04:00
|
|
|
ThangType = require 'models/ThangType'
|
|
|
|
User = require 'models/User'
|
2015-03-27 14:22:21 -04:00
|
|
|
|
2015-03-27 18:27:22 -04:00
|
|
|
# TODO: Graphing code copied/mangled from campaign editor level view. OMG, DRY.
|
|
|
|
|
2015-03-27 14:22:21 -04:00
|
|
|
require 'vendor/d3'
|
|
|
|
|
|
|
|
module.exports = class AnalyticsSubscriptionsView extends RootView
|
|
|
|
id: 'admin-analytics-subscriptions-view'
|
|
|
|
template: template
|
|
|
|
|
2015-08-23 18:32:44 -04:00
|
|
|
events:
|
|
|
|
'click .btn-show-more-cancellations': 'onClickShowMoreCancellations'
|
|
|
|
|
2015-03-27 14:22:21 -04:00
|
|
|
constructor: (options) ->
|
|
|
|
super options
|
2015-08-23 18:32:44 -04:00
|
|
|
@showMoreCancellations = false
|
2015-04-08 14:53:22 -04:00
|
|
|
@resetSubscriptionsData()
|
2016-02-26 13:36:04 -05:00
|
|
|
@refreshData() if me.isAdmin()
|
2015-03-27 14:22:21 -04:00
|
|
|
|
|
|
|
getRenderData: ->
|
|
|
|
context = super()
|
2015-03-30 14:59:14 -04:00
|
|
|
context.analytics = @analytics ? graphs: []
|
2015-08-23 18:32:44 -04:00
|
|
|
context.cancellations = if @showMoreCancellations then @cancellations else (@cancellations ? []).slice(0, 40)
|
|
|
|
context.showMoreCancellations = @showMoreCancellations
|
2015-04-01 23:23:05 -04:00
|
|
|
context.subs = _.cloneDeep(@subs ? []).reverse()
|
2015-04-07 18:00:50 -04:00
|
|
|
context.subscribers = @subscribers ? []
|
2015-04-08 14:53:22 -04:00
|
|
|
context.subscriberCancelled = _.find context.subscribers, (subscriber) -> subscriber.cancel
|
2015-04-23 16:34:41 -04:00
|
|
|
context.subscriberSponsored = _.find context.subscribers, (subscriber) -> subscriber.user?.stripe?.sponsorID
|
2015-03-27 14:22:21 -04:00
|
|
|
context.total = @total ? 0
|
2015-03-27 18:27:22 -04:00
|
|
|
context.monthlyChurn = @monthlyChurn ? 0.0
|
2015-04-06 19:58:36 -04:00
|
|
|
context.monthlyGrowth = @monthlyGrowth ? 0.0
|
2015-07-08 20:34:31 -04:00
|
|
|
context.outstandingCancels = @outstandingCancels ? []
|
|
|
|
context.refreshDataState = @refreshDataState
|
2015-03-27 14:22:21 -04:00
|
|
|
context
|
|
|
|
|
2015-03-27 18:27:22 -04:00
|
|
|
afterRender: ->
|
|
|
|
super()
|
|
|
|
@updateAnalyticsGraphs()
|
|
|
|
|
2015-08-23 18:32:44 -04:00
|
|
|
onClickShowMoreCancellations: (e) ->
|
|
|
|
@showMoreCancellations = true
|
|
|
|
@render?()
|
|
|
|
|
2015-04-08 14:53:22 -04:00
|
|
|
resetSubscriptionsData: ->
|
2015-03-27 18:27:22 -04:00
|
|
|
@analytics = graphs: []
|
2015-03-27 14:22:21 -04:00
|
|
|
@subs = []
|
|
|
|
@total = 0
|
2015-03-27 18:27:22 -04:00
|
|
|
@monthlyChurn = 0.0
|
2015-04-08 14:53:22 -04:00
|
|
|
@monthlyGrowth = 0.0
|
2015-07-08 20:34:31 -04:00
|
|
|
@refreshDataState = 'Fetching dashboard data...'
|
2015-03-30 14:59:14 -04:00
|
|
|
|
|
|
|
refreshData: ->
|
|
|
|
return unless me.isAdmin()
|
2015-04-08 14:53:22 -04:00
|
|
|
@resetSubscriptionsData()
|
2015-04-11 13:36:00 -04:00
|
|
|
@getCancellations (cancellations) =>
|
2015-07-08 20:34:31 -04:00
|
|
|
@cancellations = cancellations
|
|
|
|
@render?()
|
|
|
|
@getOutstandingCancelledSubscriptions cancellations, (outstandingCancels) =>
|
|
|
|
@outstandingCancels = outstandingCancels
|
|
|
|
@getSubscriptions cancellations, (subscriptions) =>
|
|
|
|
@updateAnalyticsGraphData()
|
|
|
|
@render?()
|
|
|
|
@getSubscribers subscriptions, =>
|
|
|
|
@render?()
|
|
|
|
|
|
|
|
updateFetchDataState: (msg) ->
|
|
|
|
@refreshDataState = msg
|
|
|
|
@render?()
|
2015-04-11 13:36:00 -04:00
|
|
|
|
|
|
|
getCancellations: (done) ->
|
2015-07-08 20:34:31 -04:00
|
|
|
cancellations = []
|
|
|
|
@getCancellationEvents (cancelledSubscriptions) =>
|
|
|
|
# Get user objects for cancelled subscriptions
|
|
|
|
userIDs = _.map cancelledSubscriptions, (a) -> a.userID
|
|
|
|
options =
|
|
|
|
url: '/db/user/-/users'
|
|
|
|
method: 'POST'
|
|
|
|
data: {ids: userIDs}
|
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
console.error 'Failed to get cancelled users', response
|
|
|
|
options.success = (cancelledUsers, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
userMap = {}
|
|
|
|
userMap[user._id] = user for user in cancelledUsers
|
|
|
|
for cancellation in cancelledSubscriptions when cancellation.userID of userMap
|
|
|
|
cancellation.user = userMap[cancellation.userID]
|
|
|
|
cancellation.level = User.levelFromExp(cancellation.user.points)
|
|
|
|
cancelledSubscriptions.sort (a, b) -> if a.cancel > b.cancel then -1 else 1
|
|
|
|
done(cancelledSubscriptions)
|
|
|
|
@updateFetchDataState 'Fetching cancellations...'
|
|
|
|
@supermodel.addRequestResource('get_cancelled_users', options, 0).load()
|
|
|
|
|
|
|
|
getCancellationEvents: (done) ->
|
|
|
|
cancellationEvents = []
|
|
|
|
earliestEventDate = new Date()
|
2015-08-23 18:32:44 -04:00
|
|
|
earliestEventDate.setUTCMonth(earliestEventDate.getUTCMonth() - 2)
|
2015-07-08 20:34:31 -04:00
|
|
|
earliestEventDate.setUTCDate(earliestEventDate.getUTCDate() - 8)
|
|
|
|
nextBatch = (starting_after, done) =>
|
|
|
|
@updateFetchDataState "Fetching cancellations #{cancellationEvents.length}..."
|
|
|
|
options =
|
|
|
|
url: '/db/subscription/-/stripe_events'
|
|
|
|
method: 'POST'
|
|
|
|
data: {options: {limit: 100}}
|
|
|
|
options.data.options.starting_after = starting_after if starting_after
|
|
|
|
options.data.options.type = 'customer.subscription.updated'
|
|
|
|
options.data.options.created = gte: Math.floor(earliestEventDate.getTime() / 1000)
|
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
console.error 'Failed to get cancelled events', response
|
|
|
|
options.success = (events, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
for event in events.data
|
|
|
|
continue unless event.data?.object?.cancel_at_period_end is true and event.data?.previous_attributes.cancel_at_period_end is false
|
|
|
|
continue unless event.data?.object?.plan?.id is 'basic'
|
|
|
|
continue unless event.data?.object?.id?
|
|
|
|
cancellationEvents.push
|
|
|
|
cancel: new Date(event.created * 1000)
|
|
|
|
customerID: event.data.object.customer
|
|
|
|
start: new Date(event.data.object.start * 1000)
|
|
|
|
subscriptionID: event.data.object.id
|
|
|
|
userID: event.data.object.metadata?.id
|
|
|
|
|
|
|
|
if events.has_more
|
|
|
|
return nextBatch(events.data[events.data.length - 1].id, done)
|
|
|
|
done(cancellationEvents)
|
|
|
|
@supermodel.addRequestResource('get_cancellation_events', options, 0).load()
|
|
|
|
nextBatch null, done
|
|
|
|
|
|
|
|
getOutstandingCancelledSubscriptions: (cancellations, done) ->
|
|
|
|
@updateFetchDataState "Fetching oustanding cancellations..."
|
2015-04-11 13:36:00 -04:00
|
|
|
options =
|
2015-07-08 20:34:31 -04:00
|
|
|
url: '/db/subscription/-/stripe_subscriptions'
|
|
|
|
method: 'POST'
|
|
|
|
data: {subscriptions: cancellations}
|
2015-04-11 13:36:00 -04:00
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
2015-07-08 20:34:31 -04:00
|
|
|
console.error 'Failed to get outstanding cancellations', response
|
|
|
|
options.success = (subscriptions, response, options) =>
|
2015-04-11 13:36:00 -04:00
|
|
|
return if @destroyed
|
2015-07-08 20:34:31 -04:00
|
|
|
outstandingCancelledSubscriptions = []
|
|
|
|
for subscription in subscriptions
|
|
|
|
continue unless subscription?.cancel_at_period_end
|
|
|
|
outstandingCancelledSubscriptions.push
|
|
|
|
cancel: new Date(subscription.canceled_at * 1000)
|
|
|
|
customerID: subscription.customerID
|
|
|
|
start: new Date(subscription.start * 1000)
|
|
|
|
subscriptionID: subscription.id
|
|
|
|
userID: subscription.metadata?.id
|
|
|
|
done(outstandingCancelledSubscriptions)
|
|
|
|
@supermodel.addRequestResource('get_outstanding_cancelled_subscriptions', options, 0).load()
|
2015-03-30 14:59:14 -04:00
|
|
|
|
2015-07-08 20:34:31 -04:00
|
|
|
getSubscribers: (subscriptions, done) ->
|
|
|
|
# console.log 'getSubscribers', subscriptions.length
|
2015-07-28 18:51:09 -04:00
|
|
|
@updateFetchDataState "Fetching recent subscribers..."
|
2015-07-08 20:34:31 -04:00
|
|
|
@render?()
|
2015-04-23 16:34:41 -04:00
|
|
|
maxSubscribers = 40
|
|
|
|
|
|
|
|
subscribers = _.filter subscriptions, (a) -> a.userID?
|
2015-07-08 20:34:31 -04:00
|
|
|
subscribers.sort (a, b) -> if a.start > b.start then -1 else 1
|
2015-04-23 16:34:41 -04:00
|
|
|
subscribers = subscribers.slice(0, maxSubscribers)
|
|
|
|
subscriberUserIDs = _.map subscribers, (a) -> a.userID
|
|
|
|
|
2015-04-07 18:00:50 -04:00
|
|
|
options =
|
|
|
|
url: '/db/subscription/-/subscribers'
|
|
|
|
method: 'POST'
|
2015-04-23 16:34:41 -04:00
|
|
|
data: {ids: subscriberUserIDs}
|
2015-04-07 18:00:50 -04:00
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
console.error 'Failed to get subscribers', response
|
2015-04-23 16:34:41 -04:00
|
|
|
options.success = (userMap, response, options) =>
|
2015-04-07 18:00:50 -04:00
|
|
|
return if @destroyed
|
2015-04-23 16:34:41 -04:00
|
|
|
for subscriber in subscribers
|
|
|
|
continue unless subscriber.userID of userMap
|
|
|
|
subscriber.user = userMap[subscriber.userID]
|
2015-04-07 18:00:50 -04:00
|
|
|
subscriber.level = User.levelFromExp subscriber.user.points
|
|
|
|
if hero = subscriber.user.heroConfig?.thangType
|
2015-04-07 22:03:35 -04:00
|
|
|
subscriber.hero = _.invert(ThangType.heroes)[hero]
|
2015-04-23 16:34:41 -04:00
|
|
|
@subscribers = subscribers
|
2015-07-08 20:34:31 -04:00
|
|
|
done()
|
2015-04-07 18:00:50 -04:00
|
|
|
@supermodel.addRequestResource('get_subscribers', options, 0).load()
|
|
|
|
|
2015-04-23 16:34:41 -04:00
|
|
|
getSubscriptions: (cancellations=[], done) ->
|
2015-07-08 20:34:31 -04:00
|
|
|
@getInvoices (invoices) =>
|
|
|
|
subMap = {}
|
|
|
|
for invoice in invoices
|
|
|
|
subID = invoice.subscriptionID
|
|
|
|
if subID of subMap
|
|
|
|
subMap[subID].first = new Date(invoice.date)
|
|
|
|
else
|
|
|
|
subMap[subID] =
|
|
|
|
first: new Date(invoice.date)
|
|
|
|
last: new Date(invoice.date)
|
|
|
|
customerID: invoice.customerID
|
|
|
|
subMap[subID].userID = invoice.userID if invoice.userID
|
|
|
|
|
|
|
|
@getSponsors (sponsors) =>
|
|
|
|
@getRecipientSubscriptions sponsors, (recipientSubscriptions) =>
|
|
|
|
for subscription in recipientSubscriptions
|
|
|
|
subMap[subscription.id] =
|
|
|
|
first: new Date(subscription.start * 1000)
|
|
|
|
subMap[subscription.id].userID = subscription.metadata.id if subscription.metadata?.id?
|
|
|
|
if subscription.cancel_at_period_end
|
|
|
|
subMap[subscription.id].cancel = new Date(subscription.canceled_at * 1000)
|
|
|
|
subMap[subscription.id].end = new Date(subscription.current_period_end * 1000)
|
|
|
|
|
|
|
|
subs = []
|
|
|
|
for subID of subMap
|
|
|
|
sub =
|
|
|
|
customerID: subMap[subID].customerID
|
|
|
|
start: subMap[subID].first
|
|
|
|
subscriptionID: subID
|
|
|
|
sub.cancel = subMap[subID].cancel if subMap[subID].cancel
|
|
|
|
oneMonthAgo = new Date()
|
|
|
|
oneMonthAgo.setUTCMonth(oneMonthAgo.getUTCMonth() - 1)
|
|
|
|
if subMap[subID].end?
|
|
|
|
sub.end = subMap[subID].end
|
|
|
|
else if subMap[subID].last < oneMonthAgo
|
|
|
|
sub.end = subMap[subID].last
|
|
|
|
sub.end.setUTCMonth(sub.end.getUTCMonth() + 1)
|
|
|
|
sub.userID = subMap[subID].userID if subMap[subID].userID
|
|
|
|
subs.push sub
|
|
|
|
|
|
|
|
subDayMap = {}
|
|
|
|
for sub in subs
|
|
|
|
startDay = sub.start.toISOString().substring(0, 10)
|
|
|
|
subDayMap[startDay] ?= {}
|
|
|
|
subDayMap[startDay]['start'] ?= 0
|
|
|
|
subDayMap[startDay]['start']++
|
|
|
|
if endDay = sub?.end?.toISOString().substring(0, 10)
|
|
|
|
subDayMap[endDay] ?= {}
|
|
|
|
subDayMap[endDay]['end'] ?= 0
|
|
|
|
subDayMap[endDay]['end']++
|
|
|
|
for cancellation in cancellations
|
|
|
|
if cancellation.subscriptionID is sub.subscriptionID
|
|
|
|
sub.cancel = cancellation.cancel
|
|
|
|
cancelDay = cancellation.cancel.toISOString().substring(0, 10)
|
|
|
|
subDayMap[cancelDay] ?= {}
|
|
|
|
subDayMap[cancelDay]['cancel'] ?= 0
|
|
|
|
subDayMap[cancelDay]['cancel']++
|
|
|
|
break
|
|
|
|
|
|
|
|
today = new Date().toISOString().substring(0, 10)
|
|
|
|
for day of subDayMap
|
|
|
|
continue if day > today
|
|
|
|
@subs.push
|
|
|
|
day: day
|
|
|
|
started: subDayMap[day]['start'] or 0
|
|
|
|
cancelled: subDayMap[day]['cancel'] or 0
|
|
|
|
ended: subDayMap[day]['end'] or 0
|
|
|
|
|
|
|
|
@subs.sort (a, b) -> a.day.localeCompare(b.day)
|
2015-08-23 23:54:39 -04:00
|
|
|
cancelledThisMonth = 0
|
2015-07-08 20:34:31 -04:00
|
|
|
totalLastMonth = 0
|
|
|
|
for sub, i in @subs
|
|
|
|
@total += sub.started
|
|
|
|
@total -= sub.ended
|
|
|
|
sub.total = @total
|
2015-08-23 23:54:39 -04:00
|
|
|
cancelledThisMonth += sub.cancelled if @subs.length - i < 31
|
2015-07-08 20:34:31 -04:00
|
|
|
totalLastMonth = @total if @subs.length - i is 31
|
2015-08-23 23:54:39 -04:00
|
|
|
@monthlyChurn = cancelledThisMonth / totalLastMonth * 100.0 if totalLastMonth > 0
|
2015-07-08 20:34:31 -04:00
|
|
|
if @subs.length > 30 and @subs[@subs.length - 31].total > 0
|
|
|
|
startMonthTotal = @subs[@subs.length - 31].total
|
|
|
|
endMonthTotal = @subs[@subs.length - 1].total
|
|
|
|
@monthlyGrowth = (endMonthTotal / startMonthTotal - 1) * 100
|
|
|
|
done(subs)
|
|
|
|
|
|
|
|
getInvoices: (done) ->
|
|
|
|
invoices = {}
|
2015-07-31 19:19:36 -04:00
|
|
|
|
|
|
|
addInvoice = (invoice) =>
|
|
|
|
return unless invoice.paid
|
|
|
|
return unless invoice.subscription
|
|
|
|
return unless invoice.total > 0
|
|
|
|
return unless invoice.lines?.data?[0]?.plan?.id is 'basic'
|
|
|
|
invoices[invoice.id] =
|
|
|
|
customerID: invoice.customer
|
|
|
|
subscriptionID: invoice.subscription
|
|
|
|
date: new Date(invoice.date * 1000)
|
|
|
|
invoices[invoice.id].userID = invoice.lines.data[0].metadata.id if invoice.lines?.data?[0]?.metadata?.id
|
|
|
|
|
|
|
|
getLiveInvoices = (ending_before, done) =>
|
|
|
|
|
|
|
|
nextBatch = (ending_before, done) =>
|
|
|
|
@updateFetchDataState "Fetching invoices #{Object.keys(invoices).length}..."
|
|
|
|
options =
|
|
|
|
url: '/db/subscription/-/stripe_invoices'
|
|
|
|
method: 'POST'
|
|
|
|
data: {options: {ending_before: ending_before, limit: 100}}
|
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
console.error 'Failed to get live invoices', response
|
|
|
|
options.success = (invoiceData, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
addInvoice(invoice) for invoice in invoiceData.data
|
|
|
|
if invoiceData.has_more
|
|
|
|
return nextBatch(invoiceData.data[0].id, done)
|
|
|
|
else
|
|
|
|
invoices = (invoice for invoiceID, invoice of invoices)
|
|
|
|
invoices.sort (a, b) -> if a.date > b.date then -1 else 1
|
|
|
|
return done(invoices)
|
|
|
|
@supermodel.addRequestResource('get_live_invoices', options, 0).load()
|
|
|
|
|
|
|
|
nextBatch ending_before, done
|
|
|
|
|
|
|
|
getAnalyticsInvoices = (done) =>
|
2015-07-08 20:34:31 -04:00
|
|
|
@updateFetchDataState "Fetching invoices #{Object.keys(invoices).length}..."
|
|
|
|
options =
|
2015-11-22 12:26:32 -05:00
|
|
|
url: '/db/analytics.stripe.invoice/-/all'
|
2015-07-31 19:19:36 -04:00
|
|
|
method: 'GET'
|
2015-07-08 20:34:31 -04:00
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
2015-07-31 19:19:36 -04:00
|
|
|
console.error 'Failed to get analytics stripe invoices', response
|
|
|
|
options.success = (docs, response, options) =>
|
2015-07-08 20:34:31 -04:00
|
|
|
return if @destroyed
|
2015-07-31 19:19:36 -04:00
|
|
|
docs.sort (a, b) -> b.date - a.date
|
|
|
|
addInvoice(doc.properties) for doc in docs
|
|
|
|
getLiveInvoices(docs[0]._id, done)
|
|
|
|
@supermodel.addRequestResource('get_analytics_invoices', options, 0).load()
|
|
|
|
|
|
|
|
getAnalyticsInvoices(done)
|
2015-07-08 20:34:31 -04:00
|
|
|
|
|
|
|
getRecipientSubscriptions: (sponsors, done) ->
|
|
|
|
@updateFetchDataState "Fetching recipient subscriptions..."
|
|
|
|
subscriptionsToFetch = []
|
|
|
|
for user in sponsors
|
|
|
|
for recipient in user.stripe?.recipients
|
|
|
|
subscriptionsToFetch.push
|
|
|
|
customerID: user.stripe.customerID
|
|
|
|
subscriptionID: recipient.subscriptionID
|
2015-07-31 19:19:36 -04:00
|
|
|
return done([]) if _.isEmpty subscriptionsToFetch
|
2015-03-30 14:59:14 -04:00
|
|
|
options =
|
2015-07-08 20:34:31 -04:00
|
|
|
url: '/db/subscription/-/stripe_subscriptions'
|
|
|
|
method: 'POST'
|
|
|
|
data: {subscriptions: subscriptionsToFetch}
|
2015-03-30 14:59:14 -04:00
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
2015-07-08 20:34:31 -04:00
|
|
|
console.error 'Failed to get recipient subscriptions', response
|
|
|
|
options.success = (subscriptions, response, options) =>
|
2015-03-30 14:59:14 -04:00
|
|
|
return if @destroyed
|
2015-07-08 20:34:31 -04:00
|
|
|
done(subscriptions)
|
|
|
|
@supermodel.addRequestResource('get_recipient_subscriptions', options, 0).load()
|
|
|
|
|
|
|
|
getSponsors: (done) ->
|
|
|
|
@updateFetchDataState "Fetching sponsors..."
|
|
|
|
options =
|
|
|
|
url: '/db/user/-/sub_sponsors'
|
|
|
|
method: 'POST'
|
|
|
|
options.error = (model, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
console.error 'Failed to get sponsors', response
|
|
|
|
options.success = (sponsors, response, options) =>
|
|
|
|
return if @destroyed
|
|
|
|
done(sponsors)
|
|
|
|
@supermodel.addRequestResource('get_sponsors', options, 0).load()
|
2015-03-27 18:27:22 -04:00
|
|
|
|
|
|
|
updateAnalyticsGraphData: ->
|
|
|
|
# console.log 'updateAnalyticsGraphData'
|
|
|
|
# Build graphs based on available @analytics data
|
|
|
|
# Currently only one graph
|
2015-06-19 16:47:06 -04:00
|
|
|
@analytics.graphs = []
|
2015-04-01 23:23:05 -04:00
|
|
|
|
2015-03-27 18:27:22 -04:00
|
|
|
return unless @subs?.length > 0
|
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
@addGraphData(60)
|
|
|
|
@addGraphData(180, true)
|
|
|
|
|
|
|
|
addGraphData: (timeframeDays, skipCancelled=false) ->
|
|
|
|
graph = {graphID: 'total-subs', lines: []}
|
|
|
|
|
2015-03-27 18:27:22 -04:00
|
|
|
# TODO: Where should this metadata live?
|
|
|
|
# TODO: lineIDs assumed to be unique across graphs
|
|
|
|
totalSubsID = 'total-subs'
|
|
|
|
startedSubsID = 'started-subs'
|
|
|
|
cancelledSubsID = 'cancelled-subs'
|
2015-04-01 23:23:05 -04:00
|
|
|
netSubsID = 'net-subs'
|
2015-06-19 16:47:06 -04:00
|
|
|
averageNewID = 'average-new'
|
2015-03-27 18:27:22 -04:00
|
|
|
lineMetadata = {}
|
|
|
|
lineMetadata[totalSubsID] =
|
|
|
|
description: 'Total Active Subscriptions'
|
|
|
|
color: 'green'
|
2015-04-01 23:23:05 -04:00
|
|
|
strokeWidth: 1
|
2015-03-27 18:27:22 -04:00
|
|
|
lineMetadata[startedSubsID] =
|
|
|
|
description: 'New Subscriptions'
|
|
|
|
color: 'blue'
|
2015-04-01 23:23:05 -04:00
|
|
|
strokeWidth: 1
|
2015-03-27 18:27:22 -04:00
|
|
|
lineMetadata[cancelledSubsID] =
|
|
|
|
description: 'Cancelled Subscriptions'
|
|
|
|
color: 'red'
|
2015-04-01 23:23:05 -04:00
|
|
|
strokeWidth: 1
|
|
|
|
lineMetadata[netSubsID] =
|
2015-05-14 19:08:29 -04:00
|
|
|
description: '7-day Average Net Subscriptions (started - cancelled)'
|
2015-04-01 23:23:05 -04:00
|
|
|
color: 'black'
|
|
|
|
strokeWidth: 4
|
2015-06-19 16:47:06 -04:00
|
|
|
lineMetadata[averageNewID] =
|
|
|
|
description: '7-day Average New Subscriptions'
|
|
|
|
color: 'black'
|
|
|
|
strokeWidth: 4
|
2015-03-27 18:27:22 -04:00
|
|
|
|
|
|
|
days = (sub.day for sub in @subs)
|
|
|
|
if days.length > 0
|
|
|
|
currentIndex = 0
|
|
|
|
currentDay = days[currentIndex]
|
|
|
|
currentDate = new Date(currentDay + "T00:00:00.000Z")
|
|
|
|
lastDay = days[days.length - 1]
|
|
|
|
while currentDay isnt lastDay
|
|
|
|
days.splice currentIndex, 0, currentDay if days[currentIndex] isnt currentDay
|
|
|
|
currentIndex++
|
|
|
|
currentDate.setUTCDate(currentDate.getUTCDate() + 1)
|
|
|
|
currentDay = currentDate.toISOString().substr(0, 10)
|
|
|
|
|
|
|
|
## Totals
|
|
|
|
|
|
|
|
# Build line data
|
|
|
|
levelPoints = []
|
|
|
|
for sub, i in @subs
|
|
|
|
levelPoints.push
|
|
|
|
x: i
|
|
|
|
y: sub.total
|
|
|
|
day: sub.day
|
|
|
|
pointID: "#{totalSubsID}#{i}"
|
|
|
|
values: []
|
|
|
|
|
|
|
|
# Ensure points for each day
|
|
|
|
for day, i in days
|
|
|
|
if levelPoints.length <= i or levelPoints[i].day isnt day
|
|
|
|
prevY = if i > 0 then levelPoints[i - 1].y else 0.0
|
|
|
|
levelPoints.splice i, 0,
|
|
|
|
y: prevY
|
|
|
|
day: day
|
|
|
|
values: []
|
|
|
|
levelPoints[i].x = i
|
|
|
|
levelPoints[i].pointID = "#{totalSubsID}#{i}"
|
|
|
|
|
2015-04-01 23:23:05 -04:00
|
|
|
levelPoints.splice(0, levelPoints.length - timeframeDays) if levelPoints.length > timeframeDays
|
2015-03-27 18:27:22 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
graph.lines.push
|
2015-03-27 18:27:22 -04:00
|
|
|
lineID: totalSubsID
|
|
|
|
enabled: true
|
|
|
|
points: levelPoints
|
|
|
|
description: lineMetadata[totalSubsID].description
|
|
|
|
lineColor: lineMetadata[totalSubsID].color
|
2015-04-08 14:53:22 -04:00
|
|
|
strokeWidth: lineMetadata[totalSubsID].strokeWidth
|
2015-03-27 18:27:22 -04:00
|
|
|
min: 0
|
2015-08-31 08:46:13 -04:00
|
|
|
max: d3.max(@subs, (d) -> d.total)
|
2015-03-27 18:27:22 -04:00
|
|
|
|
|
|
|
## Started
|
|
|
|
|
|
|
|
# Build line data
|
|
|
|
levelPoints = []
|
|
|
|
for sub, i in @subs
|
|
|
|
levelPoints.push
|
|
|
|
x: i
|
|
|
|
y: sub.started
|
|
|
|
day: sub.day
|
|
|
|
pointID: "#{startedSubsID}#{i}"
|
|
|
|
values: []
|
|
|
|
|
|
|
|
# Ensure points for each day
|
|
|
|
for day, i in days
|
|
|
|
if levelPoints.length <= i or levelPoints[i].day isnt day
|
|
|
|
prevY = if i > 0 then levelPoints[i - 1].y else 0.0
|
|
|
|
levelPoints.splice i, 0,
|
|
|
|
y: prevY
|
|
|
|
day: day
|
|
|
|
values: []
|
|
|
|
levelPoints[i].x = i
|
|
|
|
levelPoints[i].pointID = "#{startedSubsID}#{i}"
|
|
|
|
|
2015-04-01 23:23:05 -04:00
|
|
|
levelPoints.splice(0, levelPoints.length - timeframeDays) if levelPoints.length > timeframeDays
|
2015-03-27 18:27:22 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
graph.lines.push
|
2015-03-27 18:27:22 -04:00
|
|
|
lineID: startedSubsID
|
|
|
|
enabled: true
|
|
|
|
points: levelPoints
|
|
|
|
description: lineMetadata[startedSubsID].description
|
|
|
|
lineColor: lineMetadata[startedSubsID].color
|
2015-04-08 14:53:22 -04:00
|
|
|
strokeWidth: lineMetadata[startedSubsID].strokeWidth
|
2015-03-27 18:27:22 -04:00
|
|
|
min: 0
|
2015-04-10 14:27:55 -04:00
|
|
|
max: d3.max(@subs[-timeframeDays..], (d) -> d.started + 2)
|
2015-03-27 18:27:22 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
if skipCancelled
|
2015-04-10 14:27:55 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
## 7-Day average started
|
2015-03-27 18:27:22 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
# Build line data
|
|
|
|
levelPoints = []
|
|
|
|
sevenStarts = []
|
|
|
|
for sub, i in @subs
|
|
|
|
average = 0
|
|
|
|
sevenStarts.push sub.started
|
|
|
|
if sevenStarts.length > 7
|
|
|
|
sevenStarts.shift()
|
|
|
|
if sevenStarts.length is 7
|
|
|
|
average = sevenStarts.reduce((a, b) -> a + b) / sevenStarts.length
|
|
|
|
levelPoints.push
|
|
|
|
x: i
|
|
|
|
y: average
|
|
|
|
day: sub.day
|
|
|
|
pointID: "#{averageNewID}#{i}"
|
2015-03-27 18:27:22 -04:00
|
|
|
values: []
|
2015-04-01 23:23:05 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
# Ensure points for each day
|
|
|
|
for day, i in days
|
|
|
|
if levelPoints.length <= i or levelPoints[i].day isnt day
|
|
|
|
prevY = if i > 0 then levelPoints[i - 1].y else 0.0
|
|
|
|
levelPoints.splice i, 0,
|
|
|
|
y: prevY
|
|
|
|
day: day
|
|
|
|
values: []
|
|
|
|
levelPoints[i].x = i
|
|
|
|
levelPoints[i].pointID = "#{averageNewID}#{i}"
|
|
|
|
|
|
|
|
levelPoints.splice(0, levelPoints.length - timeframeDays) if levelPoints.length > timeframeDays
|
|
|
|
|
|
|
|
graph.lines.push
|
|
|
|
lineID: averageNewID
|
|
|
|
enabled: true
|
|
|
|
points: levelPoints
|
|
|
|
description: lineMetadata[averageNewID].description
|
|
|
|
lineColor: lineMetadata[averageNewID].color
|
|
|
|
strokeWidth: lineMetadata[averageNewID].strokeWidth
|
|
|
|
min: 0
|
|
|
|
max: d3.max(@subs[-timeframeDays..], (d) -> d.started + 2)
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
## Cancelled
|
|
|
|
|
|
|
|
# Build line data
|
|
|
|
levelPoints = []
|
2015-08-23 18:32:44 -04:00
|
|
|
for sub, i in @subs
|
2015-06-19 16:47:06 -04:00
|
|
|
levelPoints.push
|
|
|
|
x: @subs.length - 30 + i
|
|
|
|
y: sub.cancelled
|
|
|
|
day: sub.day
|
|
|
|
pointID: "#{cancelledSubsID}#{@subs.length - 30 + i}"
|
|
|
|
values: []
|
2015-04-01 23:23:05 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
# Ensure points for each day
|
|
|
|
for day, i in days
|
|
|
|
if levelPoints.length <= i or levelPoints[i].day isnt day
|
|
|
|
prevY = if i > 0 then levelPoints[i - 1].y else 0.0
|
|
|
|
levelPoints.splice i, 0,
|
|
|
|
y: prevY
|
|
|
|
day: day
|
|
|
|
values: []
|
|
|
|
levelPoints[i].x = i
|
|
|
|
levelPoints[i].pointID = "#{cancelledSubsID}#{i}"
|
|
|
|
|
|
|
|
levelPoints.splice(0, levelPoints.length - timeframeDays) if levelPoints.length > timeframeDays
|
|
|
|
|
|
|
|
graph.lines.push
|
|
|
|
lineID: cancelledSubsID
|
|
|
|
enabled: true
|
|
|
|
points: levelPoints
|
|
|
|
description: lineMetadata[cancelledSubsID].description
|
|
|
|
lineColor: lineMetadata[cancelledSubsID].color
|
|
|
|
strokeWidth: lineMetadata[cancelledSubsID].strokeWidth
|
|
|
|
min: 0
|
|
|
|
max: d3.max(@subs[-timeframeDays..], (d) -> d.started + 2)
|
|
|
|
|
|
|
|
## 7-Day Net Subs
|
|
|
|
|
|
|
|
# Build line data
|
|
|
|
levelPoints = []
|
|
|
|
sevenNets = []
|
|
|
|
for sub, i in @subs
|
|
|
|
net = 0
|
2015-08-23 18:32:44 -04:00
|
|
|
sevenNets.push sub.started - sub.cancelled
|
2015-06-19 16:47:06 -04:00
|
|
|
if sevenNets.length > 7
|
|
|
|
sevenNets.shift()
|
|
|
|
if sevenNets.length is 7
|
|
|
|
net = sevenNets.reduce((a, b) -> a + b) / 7
|
|
|
|
levelPoints.push
|
|
|
|
x: i
|
|
|
|
y: net
|
|
|
|
day: sub.day
|
|
|
|
pointID: "#{netSubsID}#{i}"
|
|
|
|
values: []
|
2015-04-01 23:23:05 -04:00
|
|
|
|
2015-06-19 16:47:06 -04:00
|
|
|
# Ensure points for each day
|
|
|
|
for day, i in days
|
|
|
|
if levelPoints.length <= i or levelPoints[i].day isnt day
|
|
|
|
prevY = if i > 0 then levelPoints[i - 1].y else 0.0
|
|
|
|
levelPoints.splice i, 0,
|
|
|
|
y: prevY
|
|
|
|
day: day
|
|
|
|
values: []
|
|
|
|
levelPoints[i].x = i
|
|
|
|
levelPoints[i].pointID = "#{netSubsID}#{i}"
|
|
|
|
|
|
|
|
levelPoints.splice(0, levelPoints.length - timeframeDays) if levelPoints.length > timeframeDays
|
|
|
|
|
|
|
|
graph.lines.push
|
|
|
|
lineID: netSubsID
|
|
|
|
enabled: true
|
|
|
|
points: levelPoints
|
|
|
|
description: lineMetadata[netSubsID].description
|
|
|
|
lineColor: lineMetadata[netSubsID].color
|
|
|
|
strokeWidth: lineMetadata[netSubsID].strokeWidth
|
|
|
|
min: 0
|
|
|
|
max: d3.max(@subs[-timeframeDays..], (d) -> d.started + 2)
|
|
|
|
|
|
|
|
@analytics.graphs.push(graph)
|
2015-04-01 23:23:05 -04:00
|
|
|
|
2015-03-27 18:27:22 -04:00
|
|
|
updateAnalyticsGraphs: ->
|
|
|
|
# Build d3 graphs
|
|
|
|
return unless @analytics?.graphs?.length > 0
|
|
|
|
containerSelector = '.line-graph-container'
|
|
|
|
# console.log 'updateAnalyticsGraphs', containerSelector, @analytics.graphs
|
|
|
|
|
|
|
|
margin = 20
|
|
|
|
keyHeight = 20
|
|
|
|
xAxisHeight = 20
|
|
|
|
yAxisWidth = 40
|
|
|
|
containerWidth = $(containerSelector).width()
|
|
|
|
containerHeight = $(containerSelector).height()
|
|
|
|
|
|
|
|
for graph in @analytics.graphs
|
|
|
|
graphLineCount = _.reduce graph.lines, ((sum, item) -> if item.enabled then sum + 1 else sum), 0
|
|
|
|
svg = d3.select(containerSelector).append("svg")
|
|
|
|
.attr("width", containerWidth)
|
|
|
|
.attr("height", containerHeight)
|
2015-04-01 23:23:05 -04:00
|
|
|
width = containerWidth - margin * 2 - yAxisWidth * 2
|
2015-03-27 18:27:22 -04:00
|
|
|
height = containerHeight - margin * 2 - xAxisHeight - keyHeight * graphLineCount
|
|
|
|
currentLine = 0
|
|
|
|
for line in graph.lines
|
|
|
|
continue unless line.enabled
|
|
|
|
xRange = d3.scale.linear().range([0, width]).domain([d3.min(line.points, (d) -> d.x), d3.max(line.points, (d) -> d.x)])
|
|
|
|
yRange = d3.scale.linear().range([height, 0]).domain([line.min, line.max])
|
|
|
|
|
2015-04-08 14:53:22 -04:00
|
|
|
# x-Axis
|
2015-03-27 18:27:22 -04:00
|
|
|
if currentLine is 0
|
|
|
|
startDay = new Date(line.points[0].day)
|
|
|
|
endDay = new Date(line.points[line.points.length - 1].day)
|
|
|
|
xAxisRange = d3.time.scale()
|
|
|
|
.domain([startDay, endDay])
|
|
|
|
.range([0, width])
|
|
|
|
xAxis = d3.svg.axis()
|
|
|
|
.scale(xAxisRange)
|
|
|
|
svg.append("g")
|
|
|
|
.attr("class", "x axis")
|
|
|
|
.call(xAxis)
|
|
|
|
.selectAll("text")
|
|
|
|
.attr("dy", ".35em")
|
2015-04-01 23:23:05 -04:00
|
|
|
.attr("transform", "translate(" + (margin + yAxisWidth) + "," + (height + margin) + ")")
|
2015-03-27 18:27:22 -04:00
|
|
|
.style("text-anchor", "start")
|
|
|
|
|
2015-04-01 23:23:05 -04:00
|
|
|
if line.lineID is 'started-subs'
|
2015-03-27 18:27:22 -04:00
|
|
|
# Horizontal guidelines
|
2015-04-01 23:23:05 -04:00
|
|
|
marks = (Math.round(i * line.max / 5) for i in [1...5])
|
|
|
|
svg.selectAll(".line")
|
|
|
|
.data(marks)
|
|
|
|
.enter()
|
|
|
|
.append("line")
|
|
|
|
.attr("x1", margin + yAxisWidth * 2)
|
|
|
|
.attr("y1", (d) -> margin + yRange(d))
|
|
|
|
.attr("x2", margin + yAxisWidth * 2 + width)
|
|
|
|
.attr("y2", (d) -> margin + yRange(d))
|
|
|
|
.attr("stroke", line.lineColor)
|
|
|
|
.style("opacity", "0.5")
|
|
|
|
|
|
|
|
if currentLine < 2
|
|
|
|
# y-Axis
|
|
|
|
yAxisRange = d3.scale.linear().range([height, 0]).domain([line.min, line.max])
|
|
|
|
yAxis = d3.svg.axis()
|
|
|
|
.scale(yRange)
|
|
|
|
.orient("left")
|
|
|
|
svg.append("g")
|
|
|
|
.attr("class", "y axis")
|
|
|
|
.attr("transform", "translate(" + (margin + yAxisWidth * currentLine) + "," + margin + ")")
|
|
|
|
.style("color", line.lineColor)
|
|
|
|
.call(yAxis)
|
|
|
|
.selectAll("text")
|
|
|
|
.attr("y", 0)
|
|
|
|
.attr("x", 0)
|
|
|
|
.attr("fill", line.lineColor)
|
|
|
|
.style("text-anchor", "start")
|
2015-03-27 18:27:22 -04:00
|
|
|
|
|
|
|
# Key
|
|
|
|
svg.append("line")
|
|
|
|
.attr("x1", margin)
|
|
|
|
.attr("y1", margin + height + xAxisHeight + keyHeight * currentLine + keyHeight / 2)
|
|
|
|
.attr("x2", margin + 40)
|
|
|
|
.attr("y2", margin + height + xAxisHeight + keyHeight * currentLine + keyHeight / 2)
|
|
|
|
.attr("stroke", line.lineColor)
|
|
|
|
.attr("class", "key-line")
|
|
|
|
svg.append("text")
|
|
|
|
.attr("x", margin + 40 + 10)
|
|
|
|
.attr("y", margin + height + xAxisHeight + keyHeight * currentLine + (keyHeight + 10) / 2)
|
2015-04-08 14:53:22 -04:00
|
|
|
.attr("fill", if line.lineColor is 'gold' then 'orange' else line.lineColor)
|
2015-03-27 18:27:22 -04:00
|
|
|
.attr("class", "key-text")
|
|
|
|
.text(line.description)
|
|
|
|
|
|
|
|
# Path and points
|
|
|
|
svg.selectAll(".circle")
|
|
|
|
.data(line.points)
|
|
|
|
.enter()
|
|
|
|
.append("circle")
|
2015-04-01 23:23:05 -04:00
|
|
|
.attr("transform", "translate(" + (margin + yAxisWidth * 2) + "," + margin + ")")
|
2015-03-27 18:27:22 -04:00
|
|
|
.attr("cx", (d) -> xRange(d.x))
|
|
|
|
.attr("cy", (d) -> yRange(d.y))
|
|
|
|
.attr("r", 2)
|
|
|
|
.attr("fill", line.lineColor)
|
|
|
|
.attr("stroke-width", 1)
|
|
|
|
.attr("class", "graph-point")
|
|
|
|
.attr("data-pointid", (d) -> "#{line.lineID}#{d.x}")
|
|
|
|
d3line = d3.svg.line()
|
|
|
|
.x((d) -> xRange(d.x))
|
|
|
|
.y((d) -> yRange(d.y))
|
|
|
|
.interpolate("linear")
|
|
|
|
svg.append("path")
|
|
|
|
.attr("d", d3line(line.points))
|
2015-04-01 23:23:05 -04:00
|
|
|
.attr("transform", "translate(" + (margin + yAxisWidth * 2) + "," + margin + ")")
|
|
|
|
.style("stroke-width", line.strokeWidth)
|
2015-03-27 18:27:22 -04:00
|
|
|
.style("stroke", line.lineColor)
|
|
|
|
.style("fill", "none")
|
|
|
|
currentLine++
|