2022-03-23 13:53:32 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
|
|
|
import moment from 'moment';
|
2022-03-23 11:51:53 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @typedef MrrStat
|
|
|
|
* @type {Object}
|
|
|
|
* @property {string} date The date (YYYY-MM-DD) on which this MRR was recorded
|
|
|
|
* @property {number} mrr The MRR on this date
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef MemberCountStat
|
|
|
|
* @type {Object}
|
|
|
|
* @property {string} date The date (YYYY-MM-DD) on which these counts were recorded
|
|
|
|
* @property {number} paid Amount of paid members
|
|
|
|
* @property {number} free Amount of free members
|
|
|
|
* @property {number} comped Amount of comped members
|
|
|
|
* @property {number} newPaid Amount of new paid members
|
|
|
|
* @property {number} canceledPaid Amount of canceled paid members
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef MemberCounts
|
|
|
|
* @type {Object}
|
|
|
|
* @property {number} total Total amount of members
|
|
|
|
* @property {number} paid Amount of paid members
|
|
|
|
* @property {number} free Amount of free members
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo: THIS ONE IS TEMPORARY
|
|
|
|
* @typedef EmailOpenRateStat (Will be the same as post model probably)
|
|
|
|
* @type {Object}
|
|
|
|
* @property {string} id Post id
|
|
|
|
* @property {string} title Post title
|
|
|
|
* @property {?Object} Email model
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef PaidMembersByCadence
|
|
|
|
* @type {Object}
|
|
|
|
* @property {number} annual Paid memebrs on annual plan
|
|
|
|
* @property {number} monthly Paid memebrs on monthly plan
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef PaidMembersForTier
|
|
|
|
* @type {Object}
|
|
|
|
* @property {Object} tier Tier object
|
|
|
|
* @property {number} members Paid members on this tier
|
|
|
|
*/
|
|
|
|
|
2022-03-23 11:51:53 +03:00
|
|
|
export default class DashboardStatsService extends Service {
|
2022-03-23 13:53:32 +03:00
|
|
|
@service dashboardMocks;
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?MemberCounts} memberCounts
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
memberCounts = null;
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?MemberCountStat[]}
|
|
|
|
*/
|
|
|
|
@tracked
|
|
|
|
memberCountStats = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {?MrrStat[]}
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
2022-03-23 18:38:16 +03:00
|
|
|
mrrStats = null;
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {PaidMembersByCadence} Number of members for annual and monthly plans
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
2022-03-23 18:38:16 +03:00
|
|
|
paidMembersByCadence = null;
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {PaidMembersForTier[]} Number of members for each tier
|
|
|
|
*/
|
|
|
|
@tracked
|
|
|
|
paidMembersByTier = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {?number} Number of members last seen in last 30 days (could differ if filtered by member status)
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
membersLastSeen30d = null;
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?number} Number of members last seen in last 7 days (could differ if filtered by member status)
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
membersLastSeen7d = null;
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?MemberCounts} Number of members that are subscribed (grouped by status)
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
newsletterSubscribers = null;
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?number} Number of emails sent in last 30 days
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
emailsSent30d = null;
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* @type {?EmailOpenRateStat[]}
|
|
|
|
*/
|
2022-03-23 11:51:53 +03:00
|
|
|
@tracked
|
|
|
|
emailOpenRateStats = null;
|
|
|
|
|
|
|
|
loadMembersCounts() {
|
2022-03-23 13:53:32 +03:00
|
|
|
if (this.dashboardMocks.enabled) {
|
2022-03-24 11:01:30 +03:00
|
|
|
if (this.dashboardMocks.memberCounts === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-23 18:38:16 +03:00
|
|
|
this.memberCounts = {...this.dashboardMocks.memberCounts};
|
2022-03-23 11:51:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the members graphs
|
|
|
|
* - total paid
|
|
|
|
* - total members
|
|
|
|
* for each day in the last {{days}} days
|
|
|
|
* @param {number} days The number of days to fetch data for
|
|
|
|
*/
|
|
|
|
loadMemberCountStats(days) {
|
2022-03-23 13:53:32 +03:00
|
|
|
if (this.dashboardMocks.enabled) {
|
2022-03-23 18:38:16 +03:00
|
|
|
if (this.dashboardMocks.memberCountStats === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
this.memberCountStats = this.fillMissingDates(this.dashboardMocks.memberCountStats.slice(-days), {paid: 0, free: 0, comped: 0}, days);
|
2022-03-23 11:51:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the mrr graphs
|
|
|
|
* @param {number} days The number of days to fetch data for
|
|
|
|
*/
|
|
|
|
loadMrrStats(days) {
|
2022-03-23 13:53:32 +03:00
|
|
|
if (this.dashboardMocks.enabled) {
|
2022-03-23 18:38:16 +03:00
|
|
|
if (this.dashboardMocks.mrrStats === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-23 13:53:32 +03:00
|
|
|
this.mrrStats = this.fillMissingDates(this.dashboardMocks.mrrStats.slice(-days), {mrr: 0}, days);
|
2022-03-23 11:51:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
/**
|
|
|
|
* Loads the mrr graphs
|
|
|
|
* @param {'paid'|'free'|'total'} status filter by status
|
|
|
|
*/
|
|
|
|
loadLastSeen(status) {
|
2022-03-23 13:53:32 +03:00
|
|
|
if (this.dashboardMocks.enabled) {
|
2022-03-23 18:38:16 +03:00
|
|
|
if (status === 'paid') {
|
|
|
|
// @todo
|
|
|
|
}
|
2022-03-23 13:53:32 +03:00
|
|
|
this.membersLastSeen30d = this.dashboardMocks.membersLastSeen30d;
|
|
|
|
this.membersLastSeen7d = this.dashboardMocks.membersLastSeen7d;
|
2022-03-23 11:51:53 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:38:16 +03:00
|
|
|
loadPaidMembersByCadence() {
|
|
|
|
if (this.dashboardMocks.enabled) {
|
|
|
|
this.paidMembersByCadence = this.dashboardMocks.paidMembersByCadence;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
loadPaidMembersByTier() {
|
|
|
|
if (this.dashboardMocks.enabled) {
|
|
|
|
this.paidMembersByTier = this.dashboardMocks.paidMembersByTier;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
loadNewsletterSubscribers() {
|
|
|
|
if (this.dashboardMocks.enabled) {
|
|
|
|
this.newsletterSubscribers = this.dashboardMocks.newsletterSubscribers;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
loadEmailsSent() {
|
|
|
|
if (this.dashboardMocks.enabled) {
|
|
|
|
this.emailsSent30d = this.dashboardMocks.emailsSent30d;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
|
|
|
loadEmailOpenRateStats() {
|
|
|
|
if (this.dashboardMocks.enabled) {
|
|
|
|
this.emailOpenRateStats = this.dashboardMocks.emailOpenRateStats;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Normal implementation
|
|
|
|
// @todo
|
|
|
|
}
|
|
|
|
|
2022-03-23 13:53:32 +03:00
|
|
|
/**
|
|
|
|
* For now this is only used when reloading all the graphs after changing the mocked data
|
|
|
|
* @todo: reload only data that we loaded earlier
|
2022-03-23 18:38:16 +03:00
|
|
|
* @param {number} days Amount of days to load data for (used for member related graphs)
|
2022-03-23 13:53:32 +03:00
|
|
|
*/
|
|
|
|
reloadAll(days) {
|
|
|
|
this.loadMembersCounts();
|
|
|
|
this.loadMrrStats(days);
|
|
|
|
this.loadMemberCountStats(days);
|
2022-03-23 18:38:16 +03:00
|
|
|
this.loadLastSeen('paid');
|
|
|
|
this.loadPaidMembersByCadence();
|
|
|
|
this.loadPaidMembersByTier();
|
|
|
|
|
|
|
|
this.loadNewsletterSubscribers();
|
|
|
|
this.loadEmailsSent();
|
|
|
|
this.loadEmailOpenRateStats();
|
2022-03-23 13:53:32 +03:00
|
|
|
}
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 13:53:32 +03:00
|
|
|
/**
|
|
|
|
* Fill data to match a given amount of days
|
2022-03-23 18:38:16 +03:00
|
|
|
* @param {MemberCountStat[]|MrrStat[]} data
|
|
|
|
* @param {MemberCountStat|MrrStat} defaultData
|
|
|
|
* @param {number} days Amount of days to fill the graph with
|
2022-03-23 13:53:32 +03:00
|
|
|
*/
|
|
|
|
fillMissingDates(data, defaultData, days) {
|
|
|
|
let currentRangeDate = moment().subtract(days, 'days');
|
|
|
|
|
|
|
|
let endDate = moment().add(1, 'hour');
|
|
|
|
const output = [];
|
|
|
|
const firstDateInRangeIndex = data.findIndex((val) => {
|
|
|
|
return moment(val.date).isAfter(currentRangeDate);
|
|
|
|
});
|
|
|
|
let initialDateInRangeVal = firstDateInRangeIndex > 0 ? data[firstDateInRangeIndex - 1] : null;
|
|
|
|
if (firstDateInRangeIndex === 0 && !initialDateInRangeVal) {
|
|
|
|
initialDateInRangeVal = data[firstDateInRangeIndex];
|
|
|
|
}
|
|
|
|
if (data.length > 0 && !initialDateInRangeVal && firstDateInRangeIndex !== 0) {
|
|
|
|
initialDateInRangeVal = data[data.length - 1];
|
|
|
|
}
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 13:53:32 +03:00
|
|
|
let lastVal = initialDateInRangeVal ? initialDateInRangeVal : defaultData;
|
2022-03-23 11:51:53 +03:00
|
|
|
|
2022-03-23 13:53:32 +03:00
|
|
|
while (currentRangeDate.isBefore(endDate)) {
|
|
|
|
let dateStr = currentRangeDate.format('YYYY-MM-DD');
|
|
|
|
const dataOnDate = data.find(d => d.date === dateStr);
|
2022-03-24 11:46:16 +03:00
|
|
|
lastVal = dataOnDate ? dataOnDate : {...lastVal, date: dateStr};
|
2022-03-23 13:53:32 +03:00
|
|
|
output.push(lastVal);
|
|
|
|
currentRangeDate = currentRangeDate.add(1, 'day');
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2022-03-23 11:51:53 +03:00
|
|
|
}
|