/**
 * Hook into the task scheduler. This uses the queries defined in the summary_builder.php
 * file to create and populate cache tables. The tables are not always up to date as they
 * are only updated when the scheduler runs, but they have the advantage of simplifying
 * the data model for reporting as well as reducing the need to join in queries, therefore
 * significantly improving report performance.
 * @param string $last_run_date Date last run, or null if never run
 * @param object $db Database object.
 */
function summary_builder_scheduled_task($last_run_date, $db)
{
    if ($last_run_date === null) {
        // first run, so get all records changed in last day. Query will automatically gradually pick up the rest.
        $last_run_date = date('Y-m-d', time() - 60 * 60 * 24);
    }
    try {
        // unlike cache builder, summary has a single table.
        summary_builder::populate_summary_table($db, $last_run_date, isset($_GET['force_summary_rebuild']) ? $_GET['force_summary_rebuild'] != '' ? $_GET['force_summary_rebuild'] : true : false, isset($_GET['force_summary_missing_check']) ? $_GET['force_summary_missing_check'] != '' ? $_GET['force_summary_missing_check'] : true : false);
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}
 private static function apply_estimates($db, $definition, &$data)
 {
     $firstAnchor = $definition['anchors'][0];
     $lastAnchor = $definition['anchors'][1];
     $thisLocation = false;
     $lastDataPeriod = false;
     foreach ($data as $period => $detail) {
         if ($detail['hasData']) {
             $data[$period]['estimate'] = $detail['summary'];
             $data[$period]['hasEstimate'] = true;
             if ($lastDataPeriod === false && ($firstAnchor === false || $period - 1 > $firstAnchor) && ($lastAnchor === false || $period - 1 < $lastAnchor) && $definition['first_value'] == 'H') {
                 $lastDataPeriod = $period - 2;
                 $lastDataPeriodValue = 0;
             }
             if ($lastDataPeriod !== false && $period - $lastDataPeriod > 1) {
                 for ($j = 1; $j < $period - $lastDataPeriod; $j++) {
                     // fill in periods between data points
                     $estimate = $data[$lastDataPeriod]['summary'] + ($j . ".0") * ($data[$period]['summary'] - $lastDataPeriodValue) / ($period - $lastDataPeriod);
                     $data[$lastDataPeriod + $j]['estimate'] = summary_builder::apply_data_rounding($definition, $estimate);
                     $data[$lastDataPeriod + $j]['hasEstimate'] = true;
                 }
             }
             $lastDataPeriod = $period;
             $lastDataPeriodValue = $data[$lastDataPeriod]['summary'];
         }
     }
     if ($lastDataPeriod && ($firstAnchor === false || $lastDataPeriod >= $firstAnchor) && ($lastAnchor === false || $lastDataPeriod - 1 < $lastAnchor) && $lastDataPeriod < count($data) && $definition['last_value'] == 'H') {
         $data[$lastDataPeriod + 1]['estimate'] = summary_builder::apply_data_rounding($definition, $data[$lastDataPeriod]['summary'] / 2.0);
         $data[$lastDataPeriod + 1]['hasEstimate'] = true;
     }
 }