require_once "overall.php"; $params = $_POST + $_GET; try { $apikey = @$params['apikey']; $allowedKeys = preg_split('/\\s+/s', trim(getSetting("apikeys", ""))); if (!$apikey || !in_array($apikey, $allowedKeys)) { throw new Exception('Invalid "apikey" parameter'); } $itemIds = trim(@$params['item_ids']); if (!$itemIds) { throw new Exception('Parameter "item_ids" must be specified and not empty'); } $dataNames = trim(@$params['data_names']); $dataNames = $dataNames ? explode(TAGS_SEP, $dataNames) : array(); $onlyPeriods = trim(@$params['periods']); $onlyPeriods = $onlyPeriods ? explode(TAGS_SEP, $onlyPeriods) : array(); list($to, $back) = parseToBackPeriod($params); $result = array(); foreach (getPeriods() as $period => $periodName) { if ($onlyPeriods && !in_array($period, $onlyPeriods)) { continue; } $result[$period] = generateTableData($to, $back, $period, $itemIds, $dataNames); } // print_r($result); header("Content-Type: application/json"); echo json_encode($result); } catch (Exception $e) { header("Content-Type: text/plain"); echo $e->getMessage(); }
<?php // // Command-line usage: // php recalc.php {<only_period>|all} {<back>} // // No HTTP GZIP must be here! // define("NO_AUTH", 1); require_once "overall.php"; // By default recalc only 1 period back. if (!isset($_GET['back'])) { $_GET['back'] = 1; } list($to, $back, $period) = parseToBackPeriod($_GET, true); if (is_numeric(@$_SERVER['argv'][2])) { $back = $_SERVER['argv'][2]; } $fromId = intval(@$_GET['fromid']); if (!$fromId) { $fromId = 0; } $periods = getPeriods(); $onlyPeriod = @$_SERVER['argv'][1]; if (!isset($periods[$onlyPeriod])) { $onlyPeriod = null; } if (isCgi()) { echo "<body>"; } writeLogLine(($fromId ? "Continuing" : "Starting") . " recalculation.\n");
} else { if (!empty($_POST['doSave']) || !empty($_POST['doTest']) || !empty($_POST['doRecalc'])) { try { $DB->beginTransaction(); $item = validateItem($_POST['item']); if (!$id) { $DB->update('INSERT INTO item(id, name, "sql", dsn_id, recalculatable, archived, dim, tags, created, modified, relative_to) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', $id = $DB->getSeq(), $item['name'], $item['sql'], $item['dsn_id'], $item['recalculatable'], $item['archived'], $item['dim'], $item['tags'], time(), time(), $item['relative_to']); } else { $DB->update('UPDATE item SET name=?, "sql"=?, dsn_id=?, recalculatable=?, archived=?, dim=?, tags=?, modified=?, relative_to=? WHERE id=?', $item['name'], $item['sql'], $item['dsn_id'], $item['recalculatable'], $item['archived'], $item['dim'], $item['tags'], time(), $item['relative_to'], $id); } if (!empty($_POST['doSave'])) { $DB->commit(); redirect("{$retpath}#{$id}", "Data is saved."); } else { if (!empty($_POST['doTest']) || !empty($_POST['doRecalc'])) { list($to, $back, $period) = parseToBackPeriod($_POST); $periods = $period ? array($period) : array_keys(getPeriods()); $tables = array(); $hideLogJs = '</div><script type="text/javascript">document.getElementById("log").style.display="none"</script>'; try { echo '<div id="log">'; foreach ($periods as $period) { recalcItemRow($id, $to, $back, $period); $data = generateTableData($to + 1, $back, $period, $id); $periods = getPeriods(); $tables[$periods[$period]] = generateHtmlTableFromData($data, true); } echo $hideLogJs; } catch (Exception $e) { echo $hideLogJs; throw $e;