From 6772da4ab2c9f2c01937103c1d7d5be56fb5ecd7 Mon Sep 17 00:00:00 2001 From: plegall Date: Fri, 22 Jan 2021 11:54:36 +0100 Subject: [PATCH] fixes #1293 dashboard activity chart, activity chart optimized * make the biggest part of aggregations in the database directly * use a 5 minutes cache in user session This cache is not optimal. No reason to make it specific to the user session, no reason to recalculate previous days each time. To be improved. It was urgent to find a fix for Piwigo 11.1.0. --- admin/intro.php | 92 +++++++++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 38 deletions(-) diff --git a/admin/intro.php b/admin/intro.php index a3bb94f44..1e587d651 100644 --- a/admin/intro.php +++ b/admin/intro.php @@ -192,9 +192,7 @@ trigger_notify('loc_end_intro'); // +-----------------------------------------------------------------------+ $nb_weeks = $conf['dashboard_activity_nb_weeks']; -$date = new DateTime(); -//Array for the JS tooltip -$activity_last_weeks = array(); + //Count mondays $mondays = 0; //Get mondays number for the chart legend @@ -202,48 +200,66 @@ $week_number = array(); //Array for sorting days in circle size $temp_data = array(); -//Get data from $nb_weeks last weeks -while ($mondays < $nb_weeks) +if (!isset($_SESSION['cache_activity_last_weeks']) or $_SESSION['cache_activity_last_weeks']['calculated_on'] < strtotime('5 minutes ago')) { - if ($date->format('D') == 'Mon') + $activity_last_weeks = array(); + $date = new DateTime(); + + //Get data from $nb_weeks last weeks + while ($mondays < $nb_weeks) { - $week_number[] = $date->format('W'); - $mondays += 1; - } - - $date->sub(new DateInterval('P1D')); -} - -$week_number = array_reverse($week_number); - -$date_string = $date->format('Y-m-d'); -$query = ' -SELECT * - FROM `'.ACTIVITY_TABLE.'` - WHERE occured_on >= "'.$date_string.'%" -;'; - -$result = query2array($query, null); - -foreach ($result as $row) -{ - $day_date = new DateTime($row['occured_on']); - - $week = 0; - for ($i=0; $i < $nb_weeks; $i++) - { - if ($week_number[$i] == $day_date->format('W')) + if ($date->format('D') == 'Mon') { - $week = $i; + $week_number[] = $date->format('W'); + $mondays += 1; } - } - $day_nb = $day_date->format('N'); - @$activity_last_weeks[$week][$day_nb]['details'][ucfirst($row['object'])][ucfirst($row['action'])] += 1; - @$activity_last_weeks[$week][$day_nb]['number'] += 1; - @$activity_last_weeks[$week][$day_nb]['date'] = format_date($day_date->getTimestamp()); + $date->sub(new DateInterval('P1D')); + } + + $week_number = array_reverse($week_number); + + $date_string = $date->format('Y-m-d'); + $query = ' + SELECT + DATE_FORMAT(occured_on , \'%Y-%m-%d\') AS activity_day, + object, + action, + COUNT(*) AS activity_counter + FROM `'.ACTIVITY_TABLE.'` + WHERE occured_on >= \''.$date_string.'\' + GROUP BY activity_day, object, action + ;'; + $activity_actions = query2array($query); + + foreach ($activity_actions as $action) + { + $day_date = new DateTime($action['activity_day']); + + $week = 0; + for ($i=0; $i < $nb_weeks; $i++) + { + if ($week_number[$i] == $day_date->format('W')) + { + $week = $i; + } + } + $day_nb = $day_date->format('N'); + + @$activity_last_weeks[$week][$day_nb]['details'][ucfirst($action['object'])][ucfirst($action['action'])] = $action['activity_counter']; + @$activity_last_weeks[$week][$day_nb]['number'] += $action['activity_counter']; + @$activity_last_weeks[$week][$day_nb]['date'] = format_date($day_date->getTimestamp()); + } + + $_SESSION['cache_activity_last_weeks'] = array( + 'calculated_on' => time(), + 'data' => $activity_last_weeks, + ); } +$activity_last_weeks = $_SESSION['cache_activity_last_weeks']['data']; + + foreach($activity_last_weeks as $week => $i) { foreach($i as $day => $j)