if ($onlyPeriod !== null && $period != $onlyPeriod) { continue; } try { recalcItemRow($item['id'], $to, $back, $period); } catch (Exception $e) { // nothing; error is already displayed above $hasError = true; } } writeLogLine("\n"); if (isCgi() && microtime(true) - $t0 > MAX_RECALC_CGI_TIME) { writeLogLine("Continuing recalculation in a second...\n"); $url = preg_replace('/fromid=[^&]*/s', $fid = 'fromid=' . $item['id'], $_SERVER['REQUEST_URI']); if ($url === $_SERVER['REQUEST_URI']) { $url .= (false === strpos($url, '?') ? '?' : '&') . $fid; } echo '<meta http-equiv="Refresh" content="1; URL=' . htmlspecialchars($url) . '"/>'; exit; } } $t1 = microtime(true); writeLogLine(sprintf("Finished. Took %.2f s\n", $t1 - $t0)); if (isCgi()) { $url = 'index.php?to=' . urlencode(@$_GET['to'] ? $_GET['to'] : "") . '&back=' . urlencode($back); if ($hasError) { writeLogLine('<a href="' . $url . '">Back to statistics</a>', true); } else { writeLogLine('<meta http-equiv="refresh" content="1; url=' . $url . '"/>', true); } }
function recalcItemCell($item, $interval) { global $DB; try { $t0 = microtime(true); // for catch {} block writeLogLine("[" . preg_replace('/\\s+/s', ' ', $interval['caption']) . "] \"{$item['name']}\" " . sprintf("%-13s", strtolower($interval['periodCaption']) . "...")); // Test if we could calculate this item. if (!$item['recalculatable']) { if (trunkTime(time()) != trunkTime($interval['to']) && trunkTime(trunkTime(time()) - 1) != trunkTime($interval['to'])) { writeLogLine("skipped (cannot be recalculated to the past)\n"); return; } } // Connect to the database with connection pooling. $dsn = $DB->selectCell("SELECT value FROM dsn WHERE id=?", $item['dsn_id']); static $dbs = array(); if (!isset($dbs[$dsn])) { $dbs[$dsn] = new PDO_Simple($dsn); } $db = $dbs[$dsn]; // Run the calculation. $t0 = microtime(true); // refresh $t0 excluding connect time $sql = replaceMacrosInSql($item['sql'], $interval); $rowset = $db->select($sql); // Parse single or column-returning result. $values = array(); if ($item['dim'] == 1) { $values = array("" => @current(current($rowset))); } else { foreach ($rowset as $i => $row) { reset($row); if (count($row) > 1) { $key = current($row); next($row); $value = current($row); } else { $key = $i; $value = current($row); } if (!strlen($key)) { $key = '<empty>'; } $values[$key] = $value; } } // Insert the data. $DB->update('DELETE FROM data WHERE item_id=? AND period=? AND (created BETWEEN ? AND ?)', $item['id'], $interval['period'], $interval['from'] + 1, $interval['to']); foreach ($values as $key => $value) { $DB->update('INSERT INTO data(id, item_id, period, created, value, name) VALUES(?, ?, ?, ?, ?, ?)', $DB->getSeq(), $item['id'], $interval['period'], $interval['to'], $value, $key); } $t1 = microtime(true); writeLogLine("OK (" . join(", ", $values) . "); took " . sprintf("%d ms", ($t1 - $t0) * 1000) . "\n"); } catch (Exception $e) { $t1 = microtime(true); writeLogLine(htmlspecialchars("ERROR! " . preg_replace('/[\\r\\n]+/', ' ', $e->getMessage()) . "; took " . sprintf("%d ms", ($t1 - $t0) * 1000) . "\n"), true); throw $e; } }