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,6 +100,11 @@ foreach ($tmp as $plan) {
} }
} }
$cacheMRfile = File::pathFixer('system/cache/monthlyRegistered.json');
//Cache for 1 hour
if(file_exists($cacheMRfile) && time()- filemtime($cacheMRfile) < 3600){
$monthlyRegistered = json_decode(file_get_contents($cacheMRfile), true);
}else{
//Monthly Registered Customers //Monthly Registered Customers
$result = ORM::for_table('tbl_customers') $result = ORM::for_table('tbl_customers')
->select_expr('MONTH(created_at)', 'month') ->select_expr('MONTH(created_at)', 'month')
@ -108,25 +113,29 @@ $result = ORM::for_table('tbl_customers')
->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));
}
$cacheMSfile = File::pathFixer('system/cache/monthlySales.json');
//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 // Query to retrieve monthly data
$query = ORM::for_table('tbl_transactions') $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
$results = $query->find_many();
// Create an array to hold the monthly sales data // Create an array to hold the monthly sales data
$monthlySales = array(); $monthlySales = array();
@ -156,12 +165,14 @@ ksort($monthlySales);
// Reindex the array // Reindex the array
$monthlySales = array_values($monthlySales); $monthlySales = array_values($monthlySales);
file_put_contents($cacheMSfile, json_encode($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',