2022-04-01 16:53:55 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-04-06 20:11:46 +03:00
|
|
|
import moment from 'moment';
|
2022-04-01 16:53:55 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-04-14 18:10:56 +03:00
|
|
|
import {getSymbol} from 'ghost-admin/utils/currency';
|
2022-04-01 16:53:55 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-04-14 18:10:56 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
2022-04-13 17:19:26 +03:00
|
|
|
const DATE_FORMAT = 'D MMM';
|
2022-04-06 20:11:46 +03:00
|
|
|
|
2022-04-12 18:03:52 +03:00
|
|
|
const DAYS_OPTIONS = [{
|
|
|
|
name: '7 Days',
|
|
|
|
value: 7
|
|
|
|
}, {
|
|
|
|
name: '30 Days',
|
|
|
|
value: 30
|
|
|
|
}, {
|
|
|
|
name: '90 Days',
|
|
|
|
value: 90
|
|
|
|
}, {
|
|
|
|
name: 'All Time',
|
|
|
|
value: 'all'
|
|
|
|
}];
|
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
const DISPLAY_OPTIONS = [{
|
2022-04-22 18:27:13 +03:00
|
|
|
name: 'Total members',
|
2022-04-20 15:43:11 +03:00
|
|
|
value: 'total'
|
2022-04-12 12:18:10 +03:00
|
|
|
}, {
|
2022-04-20 15:43:11 +03:00
|
|
|
name: 'Paid members',
|
|
|
|
value: 'paid'
|
|
|
|
}, {
|
|
|
|
name: 'Free members',
|
|
|
|
value: 'free'
|
2022-04-12 12:18:10 +03:00
|
|
|
}];
|
|
|
|
|
2022-04-12 15:09:00 +03:00
|
|
|
export default class Anchor extends Component {
|
2022-04-01 16:53:55 +03:00
|
|
|
@service dashboardStats;
|
2022-04-12 19:56:55 +03:00
|
|
|
@service feature;
|
2022-04-01 16:53:55 +03:00
|
|
|
@tracked chartDisplay = 'total';
|
2022-04-12 12:18:10 +03:00
|
|
|
|
2022-04-12 18:03:52 +03:00
|
|
|
daysOptions = DAYS_OPTIONS;
|
2022-04-20 15:43:11 +03:00
|
|
|
displayOptions = DISPLAY_OPTIONS;
|
2022-04-01 16:53:55 +03:00
|
|
|
|
2022-04-12 18:03:52 +03:00
|
|
|
get days() {
|
|
|
|
return this.dashboardStats.chartDays;
|
|
|
|
}
|
|
|
|
|
|
|
|
set days(days) {
|
|
|
|
this.dashboardStats.chartDays = days;
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
@action
|
|
|
|
loadCharts() {
|
|
|
|
this.dashboardStats.loadMemberCountStats();
|
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
if (this.hasPaidTiers) {
|
|
|
|
this.dashboardStats.loadMrrStats();
|
|
|
|
}
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:18:10 +03:00
|
|
|
@action
|
2022-04-20 15:43:11 +03:00
|
|
|
onDisplayChange(selected) {
|
|
|
|
this.chartDisplay = selected.value;
|
2022-04-12 12:18:10 +03:00
|
|
|
}
|
|
|
|
|
2022-04-12 18:03:52 +03:00
|
|
|
@action
|
|
|
|
onDaysChange(selected) {
|
|
|
|
this.days = selected.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedDaysOption() {
|
|
|
|
return this.daysOptions.find(d => d.value === this.days);
|
|
|
|
}
|
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
get selectedDisplayOption() {
|
|
|
|
return this.displayOptions.find(d => d.value === this.chartDisplay) ?? this.displayOptions[0];
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get loading() {
|
2022-04-20 15:43:11 +03:00
|
|
|
return this.dashboardStats.memberCountStats === null;
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get totalMembers() {
|
|
|
|
return this.dashboardStats.memberCounts?.total ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get paidMembers() {
|
|
|
|
return this.dashboardStats.memberCounts?.paid ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get freeMembers() {
|
|
|
|
return this.dashboardStats.memberCounts?.free ?? 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
get hasTrends() {
|
2022-04-13 15:52:12 +03:00
|
|
|
return this.dashboardStats.memberCounts !== null
|
2022-04-20 15:43:11 +03:00
|
|
|
&& this.dashboardStats.memberCountsTrend !== null;
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get totalMembersTrend() {
|
|
|
|
return this.calculatePercentage(this.dashboardStats.memberCountsTrend.total, this.dashboardStats.memberCounts.total);
|
|
|
|
}
|
|
|
|
|
|
|
|
get paidMembersTrend() {
|
|
|
|
return this.calculatePercentage(this.dashboardStats.memberCountsTrend.paid, this.dashboardStats.memberCounts.paid);
|
|
|
|
}
|
|
|
|
|
|
|
|
get freeMembersTrend() {
|
|
|
|
return this.calculatePercentage(this.dashboardStats.memberCountsTrend.free, this.dashboardStats.memberCounts.free);
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:02:30 +03:00
|
|
|
get hasPaidTiers() {
|
|
|
|
return this.dashboardStats.siteStatus?.hasPaidTiers;
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
get chartType() {
|
2022-04-11 19:17:31 +03:00
|
|
|
return 'line';
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:54:52 +03:00
|
|
|
get chartTitle() {
|
|
|
|
// paid
|
|
|
|
if (this.chartDisplay === 'paid') {
|
|
|
|
return 'Paid members';
|
|
|
|
// free
|
|
|
|
} else if (this.chartDisplay === 'free') {
|
|
|
|
return 'Free members';
|
|
|
|
}
|
|
|
|
// total
|
|
|
|
return 'Total members';
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
get chartData() {
|
2022-04-14 18:10:56 +03:00
|
|
|
let stats;
|
|
|
|
let labels;
|
|
|
|
let data;
|
2022-04-01 16:53:55 +03:00
|
|
|
|
2022-04-20 15:43:11 +03:00
|
|
|
if (this.chartDisplay === 'paid') {
|
|
|
|
// paid
|
2022-04-14 18:10:56 +03:00
|
|
|
stats = this.dashboardStats.filledMemberCountStats;
|
|
|
|
labels = stats.map(stat => stat.date);
|
2022-04-20 15:43:11 +03:00
|
|
|
data = stats.map(stat => stat.paid + stat.comped);
|
|
|
|
} else if (this.chartDisplay === 'free') {
|
|
|
|
// free
|
|
|
|
stats = this.dashboardStats.filledMemberCountStats;
|
2022-04-14 18:10:56 +03:00
|
|
|
labels = stats.map(stat => stat.date);
|
2022-04-20 15:43:11 +03:00
|
|
|
data = stats.map(stat => stat.free);
|
2022-04-14 18:10:56 +03:00
|
|
|
} else {
|
|
|
|
// total
|
|
|
|
stats = this.dashboardStats.filledMemberCountStats;
|
|
|
|
labels = stats.map(stat => stat.date);
|
|
|
|
data = stats.map(stat => stat.paid + stat.free + stat.comped);
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 16:52:46 +03:00
|
|
|
// gradient for line
|
|
|
|
const canvasLine = document.createElement('canvas');
|
|
|
|
const ctxLine = canvasLine.getContext('2d');
|
|
|
|
const gradientLine = ctxLine.createLinearGradient(0, 0, 1000, 0);
|
|
|
|
gradientLine.addColorStop(0, 'rgba(250, 45, 142, 1');
|
|
|
|
gradientLine.addColorStop(1, 'rgba(143, 66, 255, 1');
|
|
|
|
|
|
|
|
// gradient for fill
|
|
|
|
const canvasFill = document.createElement('canvas');
|
|
|
|
const ctxFill = canvasFill.getContext('2d');
|
|
|
|
const gradientFill = ctxFill.createLinearGradient(0, 0, 1000, 0);
|
|
|
|
gradientFill.addColorStop(0, 'rgba(250, 45, 142, 0.2');
|
|
|
|
gradientFill.addColorStop(1, 'rgba(143, 66, 255, 0.02');
|
|
|
|
|
2022-04-14 18:10:56 +03:00
|
|
|
return {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [{
|
|
|
|
data: data,
|
|
|
|
tension: 0,
|
|
|
|
cubicInterpolationMode: 'monotone',
|
|
|
|
fill: true,
|
2022-04-26 16:52:46 +03:00
|
|
|
fillColor: gradientFill,
|
|
|
|
backgroundColor: gradientFill,
|
2022-04-14 18:10:56 +03:00
|
|
|
pointRadius: 0,
|
|
|
|
pointHitRadius: 10,
|
2022-04-20 15:43:11 +03:00
|
|
|
pointBorderColor: '#8E42FF',
|
|
|
|
pointBackgroundColor: '#8E42FF',
|
|
|
|
pointHoverBackgroundColor: '#8E42FF',
|
|
|
|
pointHoverBorderColor: '#8E42FF',
|
2022-04-14 18:10:56 +03:00
|
|
|
pointHoverRadius: 0,
|
2022-04-26 16:52:46 +03:00
|
|
|
borderColor: gradientLine,
|
2022-04-14 18:10:56 +03:00
|
|
|
borderJoinStyle: 'miter'
|
|
|
|
}]
|
|
|
|
};
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
|
2022-04-14 18:05:01 +03:00
|
|
|
get mrrCurrencySymbol() {
|
|
|
|
if (this.dashboardStats.mrrStats === null) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const firstCurrency = this.dashboardStats.mrrStats[0] ? this.dashboardStats.mrrStats[0].currency : 'usd';
|
|
|
|
return getSymbol(firstCurrency);
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
get chartOptions() {
|
2022-04-14 17:28:45 +03:00
|
|
|
let barColor = this.feature.nightShift ? 'rgba(200, 204, 217, 0.25)' : 'rgba(200, 204, 217, 0.65)';
|
2022-04-26 16:52:46 +03:00
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
return {
|
|
|
|
maintainAspectRatio: false,
|
2022-04-27 16:40:38 +03:00
|
|
|
responsiveAnimationDuration: 1,
|
|
|
|
animation: false,
|
2022-04-01 16:53:55 +03:00
|
|
|
title: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
display: false
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
padding: {
|
2022-04-14 18:10:56 +03:00
|
|
|
top: 2,
|
2022-04-22 16:49:03 +03:00
|
|
|
bottom: 2,
|
2022-04-27 16:40:38 +03:00
|
|
|
left: 0,
|
|
|
|
right: 0
|
2022-04-06 20:11:46 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
hover: {
|
|
|
|
onHover: function (e) {
|
|
|
|
e.target.style.cursor = 'pointer';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tooltips: {
|
2022-04-26 20:54:52 +03:00
|
|
|
enabled: false,
|
2022-04-06 20:11:46 +03:00
|
|
|
intersect: false,
|
|
|
|
mode: 'index',
|
2022-04-26 20:54:52 +03:00
|
|
|
custom: function (tooltip) {
|
|
|
|
// get tooltip element
|
|
|
|
const tooltipEl = document.getElementById('gh-dashboard5-anchor-tooltip');
|
|
|
|
|
|
|
|
// only show tooltip when active
|
|
|
|
if (tooltip.opacity === 0) {
|
|
|
|
tooltipEl.style.display = 'none';
|
|
|
|
tooltipEl.style.opacity = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update tooltip styles
|
|
|
|
tooltipEl.style.display = 'block';
|
|
|
|
tooltipEl.style.opacity = 1;
|
|
|
|
tooltipEl.style.position = 'absolute';
|
|
|
|
tooltipEl.style.left = tooltip.x + 'px';
|
|
|
|
tooltipEl.style.top = tooltip.y + 'px';
|
|
|
|
},
|
2022-04-06 20:11:46 +03:00
|
|
|
callbacks: {
|
|
|
|
label: (tooltipItems, data) => {
|
2022-04-26 20:54:52 +03:00
|
|
|
const value = data.datasets[tooltipItems.datasetIndex].data[tooltipItems.index].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
document.querySelector('#gh-dashboard5-anchor-tooltip .gh-dashboard5-tooltip-value').innerHTML = value;
|
2022-04-06 20:11:46 +03:00
|
|
|
},
|
|
|
|
title: (tooltipItems) => {
|
2022-04-26 20:54:52 +03:00
|
|
|
const value = moment(tooltipItems[0].xLabel).format(DATE_FORMAT);
|
|
|
|
document.querySelector('#gh-dashboard5-anchor-tooltip .gh-dashboard5-tooltip-label').innerHTML = value;
|
2022-04-06 20:11:46 +03:00
|
|
|
}
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
scales: {
|
|
|
|
yAxes: [{
|
2022-04-13 17:19:26 +03:00
|
|
|
display: true,
|
2022-04-01 16:53:55 +03:00
|
|
|
gridLines: {
|
|
|
|
drawTicks: false,
|
2022-04-11 19:17:31 +03:00
|
|
|
display: true,
|
|
|
|
drawBorder: false,
|
|
|
|
color: 'transparent',
|
2022-04-14 17:28:45 +03:00
|
|
|
zeroLineColor: barColor,
|
2022-04-11 19:17:31 +03:00
|
|
|
zeroLineWidth: 1
|
2022-04-01 16:53:55 +03:00
|
|
|
},
|
|
|
|
ticks: {
|
2022-04-14 17:28:45 +03:00
|
|
|
display: false
|
2022-04-13 17:19:26 +03:00
|
|
|
}
|
2022-04-01 16:53:55 +03:00
|
|
|
}],
|
|
|
|
xAxes: [{
|
2022-04-13 17:19:26 +03:00
|
|
|
display: true,
|
|
|
|
scaleLabel: {
|
|
|
|
align: 'start'
|
|
|
|
},
|
2022-04-01 16:53:55 +03:00
|
|
|
gridLines: {
|
2022-04-14 17:28:45 +03:00
|
|
|
color: barColor,
|
2022-04-08 19:49:11 +03:00
|
|
|
borderDash: [4,4],
|
|
|
|
display: true,
|
|
|
|
drawBorder: true,
|
2022-04-01 16:53:55 +03:00
|
|
|
drawTicks: false,
|
2022-04-08 19:49:11 +03:00
|
|
|
zeroLineWidth: 1,
|
2022-04-14 17:28:45 +03:00
|
|
|
zeroLineColor: barColor,
|
2022-04-08 19:49:11 +03:00
|
|
|
zeroLineBorderDash: [4,4]
|
2022-04-01 16:53:55 +03:00
|
|
|
},
|
|
|
|
ticks: {
|
2022-04-13 17:19:26 +03:00
|
|
|
display: false,
|
2022-04-14 17:28:45 +03:00
|
|
|
beginAtZero: true,
|
2022-04-13 17:19:26 +03:00
|
|
|
callback: function (value, index, values) {
|
|
|
|
if (index === 0) {
|
|
|
|
document.getElementById('gh-dashboard5-anchor-date-start').innerHTML = moment(value).format(DATE_FORMAT);
|
|
|
|
}
|
|
|
|
if (index === values.length - 1) {
|
|
|
|
document.getElementById('gh-dashboard5-anchor-date-end').innerHTML = moment(value).format(DATE_FORMAT);
|
|
|
|
}
|
|
|
|
return value;
|
2022-04-01 16:53:55 +03:00
|
|
|
}
|
2022-04-13 17:19:26 +03:00
|
|
|
}
|
2022-04-01 16:53:55 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-06 20:11:46 +03:00
|
|
|
get chartHeight() {
|
2022-04-20 15:43:11 +03:00
|
|
|
return 200;
|
2022-04-12 18:03:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get chartHeightSmall() {
|
2022-04-26 20:54:52 +03:00
|
|
|
return 180;
|
2022-04-06 20:11:46 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:53:55 +03:00
|
|
|
calculatePercentage(from, to) {
|
|
|
|
if (from === 0) {
|
|
|
|
if (to > 0) {
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Math.round((to - from) / from * 100);
|
|
|
|
}
|
|
|
|
}
|