cache dashboard graph

This commit is contained in:
Ibnu Maksum 2024-02-06 16:41:26 +07:00
parent 80cecabfb0
commit f3d7687cdb
No known key found for this signature in database
GPG Key ID: 7FC82848810579E5
2 changed files with 58 additions and 47 deletions

View File

@ -100,38 +100,47 @@ foreach ($tmp as $plan) {
} }
} }
//Monthly Registered Customers $cacheMRfile = File::pathFixer('system/cache/monthlyRegistered.json');
$result = ORM::for_table('tbl_customers') //Cache for 1 hour
if(file_exists($cacheMRfile) && time()- filemtime($cacheMRfile) < 3600){
$monthlyRegistered = json_decode(file_get_contents($cacheMRfile), true);
}else{
//Monthly Registered Customers
$result = ORM::for_table('tbl_customers')
->select_expr('MONTH(created_at)', 'month') ->select_expr('MONTH(created_at)', 'month')
->select_expr('COUNT(*)', 'count') ->select_expr('COUNT(*)', 'count')
->where_raw('YEAR(created_at) = YEAR(NOW())') ->where_raw('YEAR(created_at) = YEAR(NOW())')
->group_by_expr('MONTH(created_at)') ->group_by_expr('MONTH(created_at)')
->find_many(); ->find_many();
$counts = []; $monthlyRegistered = [];
foreach ($result as $row) { foreach ($result as $row) {
$counts[] = [ $monthlyRegistered[] = [
'date' => $row->month, 'date' => $row->month,
'count' => $row->count 'count' => $row->count
]; ];
}
file_put_contents($cacheMRfile, json_encode($monthlyRegistered));
} }
// Query to retrieve monthly data $cacheMSfile = File::pathFixer('system/cache/monthlySales.json');
$query = ORM::for_table('tbl_transactions') //Cache for 24 hours
if(file_exists($cacheMSfile) && time()- filemtime($cacheMSfile) < 86400){
$monthlySales = json_decode(file_get_contents($cacheMSfile), true);
}else{
// Query to retrieve monthly data
$results = ORM::for_table('tbl_transactions')
->select_expr('MONTH(recharged_on)', 'month') ->select_expr('MONTH(recharged_on)', 'month')
->select_expr('SUM(price)', 'total') ->select_expr('SUM(price)', 'total')
->where_raw("YEAR(recharged_on) = YEAR(CURRENT_DATE())") // Filter by the current year ->where_raw("YEAR(recharged_on) = YEAR(CURRENT_DATE())") // Filter by the current year
->group_by_expr('MONTH(recharged_on)') ->group_by_expr('MONTH(recharged_on)')
->find_many(); ->find_many();
// Execute the query and retrieve the monthly sales data // Create an array to hold the monthly sales data
$results = $query->find_many(); $monthlySales = array();
// Create an array to hold the monthly sales data // Iterate over the results and populate the array
$monthlySales = array(); foreach ($results as $result) {
// Iterate over the results and populate the array
foreach ($results as $result) {
$month = $result->month; $month = $result->month;
$totalSales = $result->total; $totalSales = $result->total;
@ -139,29 +148,31 @@ foreach ($results as $result) {
'month' => $month, 'month' => $month,
'totalSales' => $totalSales 'totalSales' => $totalSales
); );
} }
// Fill in missing months with zero sales // Fill in missing months with zero sales
for ($month = 1; $month <= 12; $month++) { for ($month = 1; $month <= 12; $month++) {
if (!isset($monthlySales[$month])) { if (!isset($monthlySales[$month])) {
$monthlySales[$month] = array( $monthlySales[$month] = array(
'month' => $month, 'month' => $month,
'totalSales' => 0 'totalSales' => 0
); );
} }
}
// Sort the array by month
ksort($monthlySales);
// Reindex the array
$monthlySales = array_values($monthlySales);
file_put_contents($cacheMSfile, json_encode($monthlySales));
} }
// Sort the array by month
ksort($monthlySales);
// Reindex the array
$monthlySales = array_values($monthlySales);
// Assign the monthly sales data to Smarty // Assign the monthly sales data to Smarty
$ui->assign('monthlySales', $monthlySales); $ui->assign('monthlySales', $monthlySales);
$ui->assign('xheader', '<script src="https://cdn.jsdelivr.net/npm/apexcharts@3.28.0/dist/apexcharts.min.js"></script>'); $ui->assign('xheader', '<script src="https://cdn.jsdelivr.net/npm/apexcharts@3.28.0/dist/apexcharts.min.js"></script>');
$ui->assign('xfooter', ''); $ui->assign('xfooter', '');
$ui->assign('counts', $counts); $ui->assign('monthlyRegistered', $monthlyRegistered);
$ui->assign('stocks', $stocks); $ui->assign('stocks', $stocks);
$ui->assign('plans', $plans); $ui->assign('plans', $plans);

View File

@ -194,7 +194,7 @@
{literal} {literal}
<script type="text/javascript"> <script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function() {
var counts = JSON.parse('{/literal}{$counts|json_encode}{literal}'); var counts = JSON.parse('{/literal}{$monthlyRegistered|json_encode}{literal}');
var monthNames = [ var monthNames = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',