mirror of
https://github.com/flarum/core.git
synced 2025-07-30 21:20:24 +02:00
Add "today" period with hourly breakdown, and fix timezone issues
This fix ensures that before aggregating daily/hourly statistics, dates are converted into the local timezone ("flarum-statistics.timezone" in the settings table).
This commit is contained in:
61
extensions/statistics/js/admin/dist/extension.js
vendored
61
extensions/statistics/js/admin/dist/extension.js
vendored
@@ -36,14 +36,14 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon
|
||||
value: function init() {
|
||||
babelHelpers.get(StatisticsWidget.prototype.__proto__ || Object.getPrototypeOf(StatisticsWidget.prototype), 'init', this).call(this);
|
||||
|
||||
var today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
var today = new Date().setHours(0, 0, 0, 0) / 1000;
|
||||
|
||||
this.entities = ['users', 'discussions', 'posts'];
|
||||
this.periods = {
|
||||
last_7_days: { start: today - 86400000 * 6, end: today, step: 86400000 },
|
||||
last_28_days: { start: today - 86400000 * 27, end: today, step: 86400000 },
|
||||
last_12_months: { start: today - 86400000 * 364, end: today, step: 86400000 * 7 }
|
||||
today: { start: today, end: today + 86400, step: 3600 },
|
||||
last_7_days: { start: today - 86400 * 7, end: today, step: 86400 },
|
||||
last_28_days: { start: today - 86400 * 28, end: today, step: 86400 },
|
||||
last_12_months: { start: today - 86400 * 364, end: today, step: 86400 * 7 }
|
||||
};
|
||||
|
||||
this.selectedEntity = 'users';
|
||||
@@ -81,7 +81,10 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon
|
||||
Object.keys(this.periods).map(function (period) {
|
||||
return m(
|
||||
Button,
|
||||
{ active: period === _this2.selectedPeriod, onclick: _this2.changePeriod.bind(_this2, period) },
|
||||
{
|
||||
active: period === _this2.selectedPeriod,
|
||||
onclick: _this2.changePeriod.bind(_this2, period),
|
||||
icon: period === _this2.selectedPeriod ? 'check' : true },
|
||||
app.translator.trans('flarum-statistics.admin.statistics.' + period + '_label')
|
||||
);
|
||||
})
|
||||
@@ -128,22 +131,37 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon
|
||||
}, {
|
||||
key: 'drawChart',
|
||||
value: function drawChart(elm, isInitialized, context) {
|
||||
var entity = this.selectedEntity;
|
||||
if (context.chart && context.entity === this.selectedEntity && context.period === this.selectedPeriod) {
|
||||
return;
|
||||
}
|
||||
|
||||
var period = this.periods[this.selectedPeriod];
|
||||
var periodLength = period.end - period.start;
|
||||
var labels = [];
|
||||
var thisPeriod = [];
|
||||
var lastPeriod = [];
|
||||
|
||||
for (var i = period.start; i <= period.end; i += period.step) {
|
||||
labels.push(moment(i).format('D MMM'));
|
||||
for (var i = period.start; i < period.end; i += period.step) {
|
||||
var label = void 0;
|
||||
|
||||
thisPeriod.push(this.getPeriodCount(entity, { start: i, end: i + period.step }));
|
||||
if (period.step < 86400) {
|
||||
label = moment.unix(i).format('h A');
|
||||
} else {
|
||||
label = moment.unix(i).format('D MMM');
|
||||
|
||||
var periodLength = period.end - period.start;
|
||||
lastPeriod.push(this.getPeriodCount(entity, { start: i - periodLength, end: i - periodLength + period.step }));
|
||||
if (period.step > 86400) {
|
||||
label += ' - ' + moment.unix(i + period.step - 1).format('D MMM');
|
||||
}
|
||||
}
|
||||
|
||||
labels.push(label);
|
||||
|
||||
thisPeriod.push(this.getPeriodCount(this.selectedEntity, { start: i, end: i + period.step }));
|
||||
|
||||
lastPeriod.push(this.getPeriodCount(this.selectedEntity, { start: i - periodLength, end: i - periodLength + period.step }));
|
||||
}
|
||||
|
||||
var datasets = [{ values: lastPeriod, title: 'Last period ➡' }, { values: thisPeriod, title: 'This period' }];
|
||||
var datasets = [{ values: lastPeriod }, { values: thisPeriod }];
|
||||
|
||||
if (!context.chart) {
|
||||
context.chart = new Chart({
|
||||
@@ -155,17 +173,14 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon
|
||||
y_axis_mode: 'span',
|
||||
is_series: 1,
|
||||
show_dots: 0,
|
||||
colors: ['rgba(0,0,0,0.1)', app.forum.attribute('themePrimaryColor')],
|
||||
format_tooltip_x: function format_tooltip_x(d) {
|
||||
return d;
|
||||
},
|
||||
format_tooltip_y: function format_tooltip_y(d) {
|
||||
return d;
|
||||
}
|
||||
colors: ['rgba(0, 0, 0, 0.1)', app.forum.attribute('themePrimaryColor')]
|
||||
});
|
||||
} else {
|
||||
context.chart.update_values(datasets, labels);
|
||||
}
|
||||
|
||||
context.chart.update_values(datasets, labels);
|
||||
context.entity = this.selectedEntity;
|
||||
context.period = this.selectedPeriod;
|
||||
}
|
||||
}, {
|
||||
key: 'changeEntity',
|
||||
@@ -189,9 +204,7 @@ System.register('flarum/statistics/components/StatisticsWidget', ['flarum/compon
|
||||
var count = 0;
|
||||
|
||||
for (var day in daily) {
|
||||
var date = new Date(day);
|
||||
|
||||
if (date > period.start && date < period.end) {
|
||||
if (day >= period.start && day < period.end) {
|
||||
count += daily[day];
|
||||
}
|
||||
}
|
||||
|
@@ -19,14 +19,14 @@ export default class StatisticsWidget extends DashboardWidget {
|
||||
init() {
|
||||
super.init();
|
||||
|
||||
const today = new Date();
|
||||
today.setHours(0, 0, 0, 0);
|
||||
const today = new Date().setHours(0, 0, 0, 0) / 1000;
|
||||
|
||||
this.entities = ['users', 'discussions', 'posts'];
|
||||
this.periods = {
|
||||
last_7_days: {start: today - 86400000 * 6, end: today, step: 86400000},
|
||||
last_28_days: {start: today - 86400000 * 27, end: today, step: 86400000},
|
||||
last_12_months: {start: today - 86400000 * 364, end: today, step: 86400000 * 7}
|
||||
today: {start: today, end: today + 86400, step: 3600},
|
||||
last_7_days: {start: today - 86400 * 7, end: today, step: 86400},
|
||||
last_28_days: {start: today - 86400 * 28, end: today, step: 86400},
|
||||
last_12_months: {start: today - 86400 * 364, end: today, step: 86400 * 7}
|
||||
};
|
||||
|
||||
this.selectedEntity = 'users';
|
||||
@@ -91,19 +91,30 @@ export default class StatisticsWidget extends DashboardWidget {
|
||||
return;
|
||||
}
|
||||
|
||||
const entity = this.selectedEntity;
|
||||
const period = this.periods[this.selectedPeriod];
|
||||
const periodLength = period.end - period.start;
|
||||
const labels = [];
|
||||
const thisPeriod = [];
|
||||
const lastPeriod = [];
|
||||
|
||||
for (let i = period.start; i <= period.end; i += period.step) {
|
||||
labels.push(moment(i).format('D MMM'));
|
||||
for (let i = period.start; i < period.end; i += period.step) {
|
||||
let label;
|
||||
|
||||
thisPeriod.push(this.getPeriodCount(entity, {start: i, end: i + period.step}));
|
||||
if (period.step < 86400) {
|
||||
label = moment.unix(i).format('h A');
|
||||
} else {
|
||||
label = moment.unix(i).format('D MMM');
|
||||
|
||||
const periodLength = period.end - period.start;
|
||||
lastPeriod.push(this.getPeriodCount(entity, {start: i - periodLength, end: i - periodLength + period.step}));
|
||||
if (period.step > 86400) {
|
||||
label += ' - ' + moment.unix(i + period.step - 1).format('D MMM');
|
||||
}
|
||||
}
|
||||
|
||||
labels.push(label);
|
||||
|
||||
thisPeriod.push(this.getPeriodCount(this.selectedEntity, {start: i, end: i + period.step}));
|
||||
|
||||
lastPeriod.push(this.getPeriodCount(this.selectedEntity, {start: i - periodLength, end: i - periodLength + period.step}));
|
||||
}
|
||||
|
||||
const datasets = [
|
||||
@@ -148,9 +159,7 @@ export default class StatisticsWidget extends DashboardWidget {
|
||||
let count = 0;
|
||||
|
||||
for (const day in daily) {
|
||||
const date = new Date(day);
|
||||
|
||||
if (date > period.start && date < period.end) {
|
||||
if (day >= period.start && day < period.end) {
|
||||
count += daily[day];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user