2020-05-26 19:17:35 +03:00
|
|
|
import Service from '@ember/service';
|
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class MembersStatsService extends Service {
|
|
|
|
@service ajax;
|
|
|
|
@service ghostPaths;
|
|
|
|
|
|
|
|
@tracked stats = null;
|
|
|
|
|
|
|
|
fetch({days}) {
|
|
|
|
// return existing stats unless data is > 1 min old
|
|
|
|
let daysChanged = days === this._days;
|
2020-05-27 18:12:13 +03:00
|
|
|
let staleData = this._lastFetched && this._lastFetched - new Date() > 1 * 60 * 1000;
|
|
|
|
if (this.stats && !this._forceRefresh && !daysChanged && !staleData) {
|
2020-05-26 19:17:35 +03:00
|
|
|
return Promise.resolve(this.stats);
|
|
|
|
}
|
|
|
|
|
2020-05-27 18:12:13 +03:00
|
|
|
this._forceRefresh = false;
|
2020-05-26 19:17:35 +03:00
|
|
|
this._days = days;
|
|
|
|
this._lastFetched = new Date();
|
|
|
|
|
|
|
|
let statsUrl = this.ghostPaths.url.api('members/stats');
|
|
|
|
|
|
|
|
return this.ajax.request(statsUrl, {data: {days}}).then((stats) => {
|
|
|
|
this.stats = stats;
|
|
|
|
return stats;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidate() {
|
|
|
|
this._forceRefresh = true;
|
|
|
|
}
|
|
|
|
}
|