2020-05-26 19:17:35 +03:00
|
|
|
import Service from '@ember/service';
|
2021-02-17 16:08:08 +03:00
|
|
|
import moment from 'moment';
|
2020-05-26 19:17:35 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-28 13:03:33 +03:00
|
|
|
import {task} from 'ember-concurrency-decorators';
|
2020-05-26 19:17:35 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class MembersStatsService extends Service {
|
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
|
|
|
|
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;
|
2020-05-26 19:17:35 +03:00
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
fetch() {
|
|
|
|
let daysChanged = this._lastFetchedDays !== this.days;
|
2020-05-27 18:12:13 +03:00
|
|
|
let staleData = this._lastFetched && this._lastFetched - new Date() > 1 * 60 * 1000;
|
2020-05-28 13:03:33 +03:00
|
|
|
|
|
|
|
// return an already in-progress promise unless params have changed
|
|
|
|
if (this._fetchTask.isRunning && !this._forceRefresh && !daysChanged) {
|
|
|
|
return this._fetchTask.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
2020-05-27 18:12:13 +03:00
|
|
|
if (this.stats && !this._forceRefresh && !daysChanged && !staleData) {
|
2020-05-26 19:17:35 +03:00
|
|
|
return Promise.resolve(this.stats);
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:48:46 +03:00
|
|
|
return this._fetchTask.perform();
|
2020-05-26 19:17:35 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 17:17:10 +03:00
|
|
|
fetchTimeline() {
|
|
|
|
let staleData = this._lastFetchedTimeline && this._lastFetchedTimeline - new Date() > 1 * 60 * 1000;
|
|
|
|
|
|
|
|
if (this._fetchTimelineTask.isRunning) {
|
|
|
|
return this._fetchTask.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.events && !this._forceRefresh && !staleData) {
|
|
|
|
return Promise.resolve(this.events);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchTimelineTask.perform();
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:08:08 +03:00
|
|
|
fetchCounts() {
|
|
|
|
let daysChanged = this._lastFetchedDays !== this.days;
|
|
|
|
let staleData = this._lastFetched && this._lastFetched - new Date() > 1 * 60 * 1000;
|
|
|
|
|
|
|
|
// return an already in-progress promise unless params have changed
|
|
|
|
if (this._fetchCountsTask.isRunning && !this._forceRefresh && !daysChanged) {
|
|
|
|
return this._fetchCountsTask.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
|
|
|
if (this.countStats && !this._forceRefresh && !daysChanged && !staleData) {
|
|
|
|
return Promise.resolve(this.stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._fetchCountsTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
fillDates(data) {
|
|
|
|
let currentRangeDate = moment().subtract(29, 'days');
|
|
|
|
|
|
|
|
let endDate = moment().add(1, 'hour');
|
|
|
|
const output = {};
|
|
|
|
|
|
|
|
while (currentRangeDate.isBefore(endDate)) {
|
|
|
|
let dateStr = currentRangeDate.format('YYYY-MM-DD');
|
|
|
|
const dataOnDate = data.find(d => d.date === dateStr);
|
|
|
|
output[dateStr] = dataOnDate ? dataOnDate.value : 0;
|
|
|
|
|
|
|
|
currentRangeDate = currentRangeDate.add(1, 'day');
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchMRR() {
|
|
|
|
let daysChanged = this._lastFetchedDays !== this.days;
|
|
|
|
let staleData = this._lastFetched && this._lastFetched - new Date() > 1 * 60 * 1000;
|
|
|
|
|
|
|
|
// return an already in-progress promise unless params have changed
|
|
|
|
if (this._fetchMRRTask.isRunning && !this._forceRefresh && !daysChanged) {
|
|
|
|
return this._fetchMRRTask.last;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return existing stats unless data is > 1 min old
|
|
|
|
if (this.mrrStats && !this._forceRefresh && !daysChanged && !staleData) {
|
|
|
|
return Promise.resolve(this.stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-17 16:08:08 +03:00
|
|
|
@task
|
|
|
|
*_fetchCountsTask() {
|
|
|
|
this._lastFetched = new Date();
|
|
|
|
this._forceRefresh = false;
|
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('members/stats/count');
|
|
|
|
let stats = yield this.ajax.request(statsUrl);
|
|
|
|
this.countStats = stats;
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*_fetchMRRTask() {
|
|
|
|
this._lastFetched = new Date();
|
|
|
|
this._forceRefresh = false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2021-02-18 17:17:10 +03:00
|
|
|
|
|
|
|
@task
|
|
|
|
*_fetchTimelineTask() {
|
|
|
|
this._lastFetchedTimeline = new Date();
|
|
|
|
let eventsUrl = this.ghostPaths.url.api('members/events');
|
|
|
|
let events = yield this.ajax.request(eventsUrl);
|
|
|
|
this.events = events;
|
|
|
|
return events;
|
|
|
|
}
|
2020-05-26 19:17:35 +03:00
|
|
|
}
|