- users active is recorded when users logges in - add active broker and active customer multi axis line chart - add filter option of 30 days and 7 days
151 lines
5.6 KiB
PHP
151 lines
5.6 KiB
PHP
<div id="active-users-chart-parent" class="col-span-2">
|
|
<x-dashboard.card data-is-loading="false" class="h-75">
|
|
<x-ui.toggle-button-group class="mb-4">
|
|
<x-ui.toggle-button :active="request('sortBy') == null">
|
|
<button onclick="switchGraph(this, 30)" class="graphBtn flex items-center px-2 space-x-2">
|
|
<p class="font-bold text-xs sm:text-sm md:text-md">30 Days</p>
|
|
</button>
|
|
</x-ui.toggle-button>
|
|
|
|
<x-ui.toggle-button>
|
|
<button onclick="switchGraph(this, 7)" class="graphBtn flex items-center px-2 space-x-2">
|
|
<p class="font-bold text-xs sm:text-sm md:text-md">7 Days</p>
|
|
</button>
|
|
</x-ui.toggle-button>
|
|
|
|
<x-ui.toggle-button>
|
|
<button class="flex items-center pt-0.5 px-4 space-x-2">
|
|
<x-heroicon-o-calendar-date-range class="w-4"/>
|
|
</button>
|
|
</x-ui.toggle-button>
|
|
</x-ui.toggle-button-group>
|
|
|
|
<div class="h-50">
|
|
<canvas id="active-users-chart"></canvas>
|
|
</div>
|
|
</x-dashboard.card>
|
|
</div>
|
|
@push('scripts')
|
|
<script>
|
|
|
|
window.addEventListener('DOMContentLoaded', () => {
|
|
// BY default load 30 days data
|
|
const end = new Date();
|
|
const start = new Date();
|
|
start.setDate(end.getDate() - 30);
|
|
const startDate = start.toISOString().split('T')[0];
|
|
const endDate = end.toISOString().split('T')[0];
|
|
|
|
generateActiveUsersChart(startDate, endDate);
|
|
})
|
|
|
|
const generateActiveUsersChart = async (startDay, endDay) => {
|
|
const activeChartParent = document.getElementById('active-users-chart-parent');
|
|
const activeUsersChart = document.getElementById('active-users-chart')
|
|
|
|
if (!activeUsersChart || !activeChartParent) {
|
|
console.error('canvas not defined');
|
|
return;
|
|
}
|
|
|
|
toggleShimmer(false, activeChartParent);
|
|
|
|
// Generate last 30-day labels from today
|
|
let labels = getDaysArray(startDay, endDay);
|
|
|
|
try {
|
|
const {data: apiData} = await axios.get('/api/stats/active-users/', {
|
|
params: {from: startDay, to: endDay}
|
|
});
|
|
|
|
const customerDataFromAPI = apiData?.activeCustomers?.data || [];
|
|
const brokerDataFromAPI = apiData?.activeBrokers?.data || [];
|
|
|
|
// Fill the data from api response
|
|
const customerData = labels.map((date) => {
|
|
let found = customerDataFromAPI.find(item => item.date === date);
|
|
return found ? found.userCount : 0;
|
|
})
|
|
|
|
const brokerData = labels.map((date) => {
|
|
let found = brokerDataFromAPI.find(item => item.date === date);
|
|
return found ? found.userCount : 0;
|
|
})
|
|
|
|
const data = {
|
|
labels: labels,
|
|
datasets: [
|
|
{
|
|
label: `Active Customers`,
|
|
backgroundColor: 'rgba(255, 99, 132, 0.3)',
|
|
borderColor: 'rgb(255, 99, 132)',
|
|
borderRadius: 4,
|
|
borderWidth: 1,
|
|
data: customerData,
|
|
},
|
|
{
|
|
label: `Active Brokers`,
|
|
backgroundColor: 'rgba(91,162,238,0.3)',
|
|
borderColor: 'rgb(99,102,255)',
|
|
borderRadius: 4,
|
|
borderWidth: 1,
|
|
data: brokerData,
|
|
}
|
|
]
|
|
}
|
|
const config = {
|
|
type: 'line',
|
|
data: data,
|
|
options: {
|
|
responsive: true,
|
|
maintainAspectRatio: false,
|
|
scales: {
|
|
y: {beginAtZero: true, ticks: {precision: 0}}
|
|
},
|
|
interaction: {intersect: false, mode: 'index'},
|
|
}
|
|
};
|
|
|
|
const existingChart = Chart.getChart(activeUsersChart);
|
|
if (existingChart) {
|
|
existingChart.destroy()
|
|
}
|
|
|
|
const chart = new Chart(activeUsersChart, config);
|
|
|
|
toggleShimmer(true, activeChartParent);
|
|
} catch (e) {
|
|
console.log(e)
|
|
}
|
|
};
|
|
|
|
|
|
const switchGraph = (el, daysCount) => {
|
|
activeBtn(el);
|
|
const end = new Date();
|
|
const start = new Date();
|
|
start.setDate(end.getDate() - daysCount);
|
|
|
|
generateActiveUsersChart(
|
|
start.toISOString().split('T')[0],
|
|
end.toISOString().split('T')[0]
|
|
);
|
|
}
|
|
|
|
const activeBtn = (el) => {
|
|
const graphBtns = document.getElementById('active-users-chart-parent').querySelectorAll('.toggleBtn');
|
|
graphBtns.forEach(btn => btn.classList.remove('bg-white'));
|
|
el.closest('.toggleBtn').classList.add('bg-white');
|
|
}
|
|
|
|
const getDaysArray = (start, end) => {
|
|
let arr = [];
|
|
for (let dt = new Date(start); dt <= new Date(end); dt.setDate(dt.getDate() + 1)) {
|
|
arr.push(new Date(dt).toISOString().split('T')[0]);
|
|
}
|
|
return arr;
|
|
};
|
|
|
|
</script>
|
|
@endpush
|