/** * Update a milestone's fields. * * @param int $id * @param string $name * @param mixed $when UNIX timestamp or DATETIME string. * @return boolean True on success. * @throws Exception On query error. */ public function updateMilestone($id, $name, $when) { $when = Hashmark_Util::toDatetime($when); $sql = "UPDATE {$this->_dbName}`milestones` " . 'SET `name` = ?, ' . '`when` = ? ' . 'WHERE `id` = ?'; $stmt = $this->_db->query($sql, array($name, $when, $id)); return 1 == $stmt->rowCount(); }
$scalarFields = array('type' => HASHMARK_DUMP_RANDOMSAMPLES_TYPE, 'name' => Hashmark_Util::randomSha1()); $sql = 'INSERT IGNORE INTO `scalars` ' . '(`id`, `type`) ' . 'VALUES (' . $scalarId++ . ', \'' . HASHMARK_DUMP_RANDOMSAMPLES_TYPE . "');\n"; file_put_contents(HASHMARK_DUMP_RANDOMSAMPLES_FILE, $sql); $scalarSampleCnt = array(); $createdTables = array(); // Chunk random samples into sets to avoid memory limit. $scalarSampleCnt = 0; while ($scalarSampleCnt < HASHMARK_DUMP_RANDOMSAMPLES_COUNT) { // Last parameter will sort $samples by date ascending. $samples = hashmark_random_samples(HASHMARK_DUMP_RANDOMSAMPLES_TYPE, HASHMARK_DUMP_RANDOMSAMPLES_STARTDATE, HASHMARK_DUMP_RANDOMSAMPLES_ENDDATE, min(HASHMARK_DUMP_RANDOMSAMPLES_RANDOM_SET_MAX, HASHMARK_DUMP_RANDOMSAMPLES_COUNT - $scalarSampleCnt), false, false, null, null, true); $scalarSampleCnt += count($samples); $buffer = ''; $bufferSize = 0; foreach ($samples as $timeData => $value) { list($time) = explode('=', $timeData); $sampleDate = Hashmark_Util::toDatetime($time); if ('string' == HASHMARK_DUMP_RANDOMSAMPLES_TYPE) { $value = $partition->escape($value); } // Create partitions as needed based on sample date. $tableName = $partition->getIntervalTableName($scalarId, $sampleDate); if (!isset($createdTables[$tableName])) { $createdTables[$tableName] = 1; $buffer .= "CREATE TABLE IF NOT EXISTS `{$tableName}` {$tableDef} AUTO_INCREMENT=1;\n"; $bufferSize++; } $buffer .= "INSERT INTO `{$tableName}` " . '(`value`, `end`) ' . "VALUES ('{$value}', '{$sampleDate}');\n"; $bufferSize++; if ($bufferSize > HASHMARK_DUMP_RANDOMSAMPLES_SQL_BUFFER_MAX) { file_put_contents(HASHMARK_DUMP_RANDOMSAMPLES_FILE, $buffer, FILE_APPEND); $buffer = '';
/** * Add new row to `samples` and update `scalars` statistics. * * @param int $scalarId * @param string $value * @param mixed $end UNIX timestamp or DATETIME string. * @return boolean True on success. * @throws Exception On query error. */ public function createSample($scalarId, $value, $end) { $end = Hashmark_Util::toDatetime($end); // `sample_count` seeds AUTO_INCREMENT `id` values in sample partitions $sql = "UPDATE {$this->_dbName}`scalars` " . 'SET `value` = ?, ' . '`last_agent_change` = ?, ' . '`sample_count` = `sample_count` + 1 ' . 'WHERE `id` = ?'; $this->_db->query($sql, array($value, $end, $scalarId)); $sql = 'INSERT INTO ~samples ' . '(`value`, `end`) ' . 'VALUES (?, ?)'; // queryAtDate() instead of queryCurrent() so unit tests can // create backdated samples. $bind = array($value, $end); $stmt = $this->queryAtDate($scalarId, $sql, $end, $bind); return 1 == $stmt->rowCount(); }