2022-01-21 22:25:47 +03:00
|
|
|
import Service, {inject as service} from '@ember/service';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2020-05-26 19:17:35 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2021-03-10 13:06:06 +03:00
|
|
|
const ONE_MINUTE = 1 * 60 * 1000;
|
|
|
|
|
2020-05-26 19:17:35 +03:00
|
|
|
export default class MembersStatsService extends Service {
|
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
2021-02-22 11:29:48 +03:00
|
|
|
@service store;
|
2020-05-26 19:17:35 +03:00
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
@tracked days = '30';
|
2020-05-26 19:17:35 +03:00
|
|
|
@tracked stats = null;
|
2021-02-18 17:17:10 +03:00
|
|
|
@tracked events = null;
|
2021-02-17 16:08:08 +03:00
|
|
|
@tracked countStats = null;
|
|
|
|
@tracked mrrStats = null;
|
2021-02-22 11:29:48 +03:00
|
|
|
@tracked newsletterStats = null;
|
2022-09-06 13:08:16 +03:00
|
|
|
@tracked totalMemberCount = null;
|
2020-05-26 19:17:35 +03:00
|
|
|
|
2022-09-09 09:36:14 +03:00
|
|
|
get memberCount() {
|
|
|
|
let stats = this.totalMemberCount;
|
|
|
|
if (!stats) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const {free, paid, comped} = stats.meta.totals;
|
|
|
|
const total = free + paid + comped || 0;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
fetch() {
|
|
|
|
let daysChanged = this._lastFetchedDays !== this.days;
|
2021-03-10 13:06:06 +03:00
|
|
|
let staleData = this._lastFetched && (new Date() - this._lastFetched) > ONE_MINUTE;
|
2020-05-28 13:03:33 +03:00
|
|
|
|
2022-09-26 19:37:35 +03:00
|
|
|
// return existing stats unless data is > 1 min old or days param has changed
|
|
|
|
if (this.stats && !this._forceRefresh && !daysChanged && !staleData && this._fetchTask.last) {
|
2020-05-28 13:03:33 +03:00
|
|
|
return this._fetchTask.last;
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
return this._fetchTask.perform();
|
2020-05-26 19:17:35 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 16:08:08 +03:00
|
|
|
fetchCounts() {
|
2021-03-10 13:06:06 +03:00
|
|
|
let staleData = this._lastFetchedCounts && (new Date() - this._lastFetchedCounts) > ONE_MINUTE;
|
2021-02-17 16:08:08 +03:00
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
2022-09-26 19:37:35 +03:00
|
|
|
if (this.countStats && !this._forceRefresh && !staleData && this._fetchCountsTask.last) {
|
|
|
|
return this._fetchCountsTask.last;
|
2021-02-17 16:08:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchCountsTask.perform();
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:08:16 +03:00
|
|
|
fetchMemberCount() {
|
2024-01-13 20:40:28 +03:00
|
|
|
// if already running, return existing promise
|
|
|
|
if (this._fetchMemberCountsTask.isRunning) {
|
|
|
|
return this._fetchMemberCountsTask.last;
|
|
|
|
}
|
2022-09-06 13:08:16 +03:00
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
2024-01-13 20:40:28 +03:00
|
|
|
let staleData = this._lastFetchedMemberCounts && (new Date() - this._lastFetchedMemberCounts) > ONE_MINUTE;
|
2022-09-26 19:37:35 +03:00
|
|
|
if (this.totalMemberCount && !this._forceRefresh && !staleData && this._fetchMemberCountsTask.last) {
|
|
|
|
return this._fetchMemberCountsTask.last;
|
2022-09-06 13:08:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchMemberCountsTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-02-22 11:29:48 +03:00
|
|
|
fetchNewsletterStats() {
|
2021-03-10 13:06:06 +03:00
|
|
|
let staleData = this._lastFetchedNewsletterStats && (new Date() - this._lastFetchedNewsletterStats) > ONE_MINUTE;
|
2021-02-22 11:29:48 +03:00
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
2022-09-26 19:37:35 +03:00
|
|
|
if (this.newsletterStats && !this._forceRefresh && !staleData && this._fetchNewsletterStatsTask.last) {
|
|
|
|
return this._fetchNewsletterStatsTask.last;
|
2021-02-22 11:29:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchNewsletterStatsTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-02-25 18:32:50 +03:00
|
|
|
fillDates(data = []) {
|
2021-02-19 14:04:15 +03:00
|
|
|
let currentRangeDate = moment().subtract(30, 'days');
|
2021-02-17 16:08:08 +03:00
|
|
|
|
|
|
|
let endDate = moment().add(1, 'hour');
|
|
|
|
const output = {};
|
2021-03-15 12:53:55 +03:00
|
|
|
|
|
|
|
const firstDateInRangeIndex = data.findIndex((val) => {
|
|
|
|
return moment(val.date).isAfter(currentRangeDate);
|
|
|
|
});
|
2021-08-09 15:17:09 +03:00
|
|
|
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];
|
|
|
|
}
|
2021-03-15 12:53:55 +03:00
|
|
|
let lastVal = initialDateInRangeVal ? initialDateInRangeVal.value : 0;
|
|
|
|
|
2021-02-17 16:08:08 +03:00
|
|
|
while (currentRangeDate.isBefore(endDate)) {
|
|
|
|
let dateStr = currentRangeDate.format('YYYY-MM-DD');
|
|
|
|
const dataOnDate = data.find(d => d.date === dateStr);
|
2021-02-19 08:48:01 +03:00
|
|
|
output[dateStr] = dataOnDate ? dataOnDate.value : lastVal;
|
|
|
|
lastVal = output[dateStr];
|
|
|
|
currentRangeDate = currentRangeDate.add(1, 'day');
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-02-25 18:32:50 +03:00
|
|
|
fillCountDates(data = {}) {
|
2021-02-19 14:04:15 +03:00
|
|
|
let currentRangeDate = moment().subtract(30, 'days');
|
2021-02-17 16:08:08 +03:00
|
|
|
|
2021-02-19 08:48:01 +03:00
|
|
|
let endDate = moment().add(1, 'hour');
|
|
|
|
const output = {};
|
2021-02-25 18:32:50 +03:00
|
|
|
const firstDateInRangeIndex = data.findIndex((val) => {
|
|
|
|
return moment(val.date).isAfter(currentRangeDate);
|
|
|
|
});
|
2021-03-24 19:11:35 +03:00
|
|
|
let initialDateInRangeVal = firstDateInRangeIndex > 0 ? data[firstDateInRangeIndex - 1] : null;
|
2021-08-09 15:17:09 +03:00
|
|
|
if (firstDateInRangeIndex === 0 && !initialDateInRangeVal) {
|
|
|
|
initialDateInRangeVal = data[firstDateInRangeIndex];
|
|
|
|
}
|
2021-03-24 19:11:35 +03:00
|
|
|
if (data.length > 0 && !initialDateInRangeVal && firstDateInRangeIndex !== 0) {
|
|
|
|
initialDateInRangeVal = data[data.length - 1];
|
|
|
|
}
|
2021-02-19 08:48:01 +03:00
|
|
|
let lastVal = {
|
2021-02-25 18:32:50 +03:00
|
|
|
paid: initialDateInRangeVal ? initialDateInRangeVal.paid : 0,
|
|
|
|
free: initialDateInRangeVal ? initialDateInRangeVal.free : 0,
|
|
|
|
comped: initialDateInRangeVal ? initialDateInRangeVal.comped : 0,
|
|
|
|
total: initialDateInRangeVal ? (initialDateInRangeVal.paid + initialDateInRangeVal.free + initialDateInRangeVal.comped) : 0
|
2021-02-19 08:48:01 +03:00
|
|
|
};
|
|
|
|
while (currentRangeDate.isBefore(endDate)) {
|
|
|
|
let dateStr = currentRangeDate.format('YYYY-MM-DD');
|
|
|
|
const dataOnDate = data.find(d => d.date === dateStr);
|
|
|
|
output[dateStr] = dataOnDate ? {
|
|
|
|
paid: dataOnDate.paid,
|
|
|
|
free: dataOnDate.free,
|
|
|
|
comped: dataOnDate.comped,
|
|
|
|
total: dataOnDate.paid + dataOnDate.free + dataOnDate.comped
|
|
|
|
} : lastVal;
|
|
|
|
lastVal = output[dateStr];
|
2021-02-17 16:08:08 +03:00
|
|
|
currentRangeDate = currentRangeDate.add(1, 'day');
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchMRR() {
|
2021-03-10 13:06:06 +03:00
|
|
|
let staleData = this._lastFetchedMRR && (new Date() - this._lastFetchedMRR) > ONE_MINUTE;
|
2021-02-17 16:08:08 +03:00
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
2022-09-26 19:37:35 +03:00
|
|
|
if (this.mrrStats && !this._forceRefresh && !staleData && this._fetchMRRTask) {
|
|
|
|
return this._fetchMRRTask.last;
|
2021-02-17 16:08:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchMRRTask.perform();
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:17:35 +03:00
|
|
|
invalidate() {
|
|
|
|
this._forceRefresh = true;
|
|
|
|
}
|
2020-05-28 13:03:33 +03:00
|
|
|
|
2021-02-22 11:29:48 +03:00
|
|
|
@task
|
|
|
|
*_fetchNewsletterStatsTask() {
|
2021-02-23 14:50:54 +03:00
|
|
|
const limit = 5;
|
2021-02-22 11:29:48 +03:00
|
|
|
let query = {
|
|
|
|
filter: 'email_count:-0',
|
|
|
|
order: 'submitted_at desc',
|
2021-02-23 14:50:54 +03:00
|
|
|
limit: limit
|
2021-02-22 11:29:48 +03:00
|
|
|
};
|
|
|
|
const results = yield this.store.query('email', query);
|
2021-02-23 00:45:59 +03:00
|
|
|
const data = results.toArray();
|
|
|
|
let stats = data.map((d) => {
|
2021-02-22 11:29:48 +03:00
|
|
|
return {
|
2021-02-23 00:45:59 +03:00
|
|
|
subject: d.subject,
|
|
|
|
submittedAt: moment(d.submittedAtUTC).format('YYYY-MM-DD'),
|
|
|
|
openRate: d.openRate
|
2021-02-22 11:29:48 +03:00
|
|
|
};
|
|
|
|
});
|
2021-02-23 00:18:36 +03:00
|
|
|
|
|
|
|
const paddedResults = [];
|
2021-02-23 14:50:54 +03:00
|
|
|
if (data.length < limit) {
|
|
|
|
const pad = limit - data.length;
|
2021-02-23 00:45:59 +03:00
|
|
|
const lastSubmittedAt = data.length > 0 ? data[results.length - 1].submittedAtUTC : moment();
|
2021-02-23 00:18:36 +03:00
|
|
|
for (let i = 0; i < pad; i++) {
|
|
|
|
paddedResults.push({
|
|
|
|
subject: '',
|
|
|
|
submittedAt: moment(lastSubmittedAt).subtract(i + 1, 'days').format('YYYY-MM-DD'),
|
|
|
|
openRate: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 14:50:54 +03:00
|
|
|
stats = stats .concat(paddedResults);
|
2021-02-23 00:18:36 +03:00
|
|
|
stats.reverse();
|
2021-02-22 11:29:48 +03:00
|
|
|
this.newsletterStats = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:08:08 +03:00
|
|
|
@task
|
|
|
|
*_fetchCountsTask() {
|
2021-02-18 20:13:51 +03:00
|
|
|
this._lastFetchedCounts = new Date();
|
2021-02-17 16:08:08 +03:00
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('members/stats/count');
|
|
|
|
let stats = yield this.ajax.request(statsUrl);
|
|
|
|
this.countStats = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2022-09-06 13:08:16 +03:00
|
|
|
@task
|
|
|
|
*_fetchMemberCountsTask() {
|
|
|
|
this._lastFetchedMemberCounts = new Date();
|
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('stats/member_count/');
|
|
|
|
let stats = yield this.ajax.request(statsUrl);
|
|
|
|
this.totalMemberCount = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:08:08 +03:00
|
|
|
@task
|
|
|
|
*_fetchMRRTask() {
|
2021-02-18 20:13:51 +03:00
|
|
|
this._lastFetchedMRR = new Date();
|
2021-02-17 16:08:08 +03:00
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('members/stats/mrr');
|
|
|
|
let stats = yield this.ajax.request(statsUrl);
|
|
|
|
this.mrrStats = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
2020-05-28 13:03:33 +03:00
|
|
|
@task
|
2020-06-01 17:48:46 +03:00
|
|
|
*_fetchTask() {
|
|
|
|
let {days} = this;
|
|
|
|
|
|
|
|
this._lastFetchedDays = days;
|
2020-05-28 13:03:33 +03:00
|
|
|
this._lastFetched = new Date();
|
|
|
|
this._forceRefresh = false;
|
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('members/stats');
|
|
|
|
let stats = yield this.ajax.request(statsUrl, {data: {days}});
|
|
|
|
this.stats = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
2020-05-26 19:17:35 +03:00
|
|
|
}
|