issue #2211 optimize date_posted filter values generation

This commit is contained in:
plegall
2024-09-04 21:57:38 +02:00
parent 38951c9876
commit 960e8479cb
+54 -59
View File
@@ -289,7 +289,11 @@ SELECT
if (isset($my_search['fields']['date_posted']))
{
$filter_clause = get_clause_for_filter('date_posted');
$cache_key = $persistent_cache->make_key('filter_date_posted'.$user['id'].$user['cache_update_time']);
$set_persistent_cache = !preg_match('/^image_id IN/', $filter_clause) and !$persistent_cache->get($cache_key, $date_posted);
if (!isset($date_posted))
{
$query = '
SELECT
SUBDATE(NOW(), INTERVAL 24 HOUR) AS 24h,
@@ -302,25 +306,47 @@ SELECT
$query = '
SELECT
image_id,
date_available
DISTINCT id,
date_available as date
FROM '.IMAGES_TABLE.' AS i
JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id
WHERE '.$filter_clause.'
;';
$dates = query2array($query);
$pre_counters = array_fill_keys(array_keys($thresholds), array());
foreach ($dates as $date_row)
$list_of_dates = array();
$pre_counters = array();
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
$year = date('Y', strtotime($date_row['date_available']));
@$pre_counters['y'.$year][ $date_row['image_id'] ] = 1;
foreach ($thresholds as $threshold => $date_limit)
{
if ($date_row['date_available'] > $date_limit)
if ($row['date'] > $date_limit)
{
@$pre_counters[$threshold][ $date_row['image_id'] ] = 1;
@$pre_counters[$threshold]++;
}
}
list($date_without_time) = explode(' ', $row['date']);
list($y, $m) = explode('-', $date_without_time);
@$list_of_dates[$y]['months'][$y.'-'.$m]['days'][$date_without_time]['count']++;
@$list_of_dates[$y]['months'][$y.'-'.$m]['count']++;
@$list_of_dates[$y]['count']++;
}
$date_posted = array(
'pre_counters' => $pre_counters,
'list_of_dates' => $list_of_dates,
);
if ($set_persistent_cache)
{
// for this filter, we do not store in cache the $filter_rows : for a big gallery it may
// take more than 10MB. It is smarter to store in cache the result of the computation,
// which is just around 100 bytes.
$persistent_cache->set($cache_key, $date_posted);
}
}
$label_for_threshold = array(
@@ -331,68 +357,37 @@ SELECT
'6m' => l10n('last 6 months'),
);
// pre_counters need to be deduplicated because a photo can be in several albums
$counters = array_fill_keys(array_keys($thresholds), array('label'=>'default label', 'counter'=>0));
foreach (array_keys($thresholds) as $threshold)
$counters = array();
foreach (array_keys($label_for_threshold) as $threshold)
{
if (isset($date_posted['pre_counters'][$threshold]))
{
$counters[$threshold] = array(
'label' => $label_for_threshold[$threshold],
'counter' => count(array_keys($pre_counters[$threshold]))
'counter' => $date_posted['pre_counters'][$threshold],
);
}
}
foreach ($counters as $key => $counter)
foreach (array_keys($date_posted['list_of_dates']) as $y)
{
if (0 == $counter['counter'])
$date_posted['list_of_dates'][$y]['label'] = l10n('year %d', $y);
foreach (array_keys($date_posted['list_of_dates'][$y]['months']) as $ym)
{
unset($counters[$key]);
list(,$m) = explode('-', $ym);
$date_posted['list_of_dates'][$y]['months'][$ym]['label'] = $lang['month'][(int)$m].' '.$y;
foreach (array_keys($date_posted['list_of_dates'][$y]['months'][$ym]['days']) as $ymd)
{
list(,,$d) = explode('-', $ymd);
$date_posted['list_of_dates'][$y]['months'][$ym]['days'][$ymd]['label'] = format_date($ymd);
}
}
}
$template->assign('LIST_DATE_POSTED', $date_posted['list_of_dates']);
$template->assign('DATE_POSTED', $counters);
// Custom date
$query = '
SELECT
DISTINCT id,
date_available as date
FROM '.IMAGES_TABLE.' AS i
JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.image_id = i.id
WHERE '.$filter_clause.'
;';
$list_of_dates = array();
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
list($date_without_time) = explode(' ', $row['date']);
list($y, $m) = explode('-', $date_without_time);
@$list_of_dates[$y]['months'][$y.'-'.$m]['days'][$date_without_time]['count']++;
@$list_of_dates[$y]['months'][$y.'-'.$m]['count']++;
@$list_of_dates[$y]['count']++;
// Benchmark if better to put the label in another array rather than keep in this array
if (!isset($list_of_dates[$y]['months'][$y.'-'.$m]['days'][$date_without_time]['label']))
{
$list_of_dates[$y]['months'][$y.'-'.$m]['days'][$date_without_time]['label'] = format_date($row['date']);
}
if (!isset($list_of_dates[$y]['months'][$m]['label']))
{
$list_of_dates[$y]['months'][$y.'-'.$m]['label'] = $lang['month'][(int)$m].' '.$y;
}
if (!isset($list_of_dates[$y]['label']))
{
$list_of_dates[$y]['label'] = l10n('year %d', $y);
}
}
$template->assign('LIST_DATE_POSTED', $list_of_dates);
}
if (isset($my_search['fields']['added_by']))