예제 #1
0
 /**
  * Delete tuples from single or multiple channels
  *
  * @todo deduplicate Model\Channel code
  * @param string|array uuid
  */
 public function delete($uuids)
 {
     $from = null;
     $to = null;
     // parse interval
     if (null !== ($from = $this->getParameters()->get('from'))) {
         $from = Interpreter::parseDateTimeString($from);
         if (null !== ($to = $this->getParameters()->get('to'))) {
             $to = Interpreter::parseDateTimeString($to);
             if ($from > $to) {
                 throw new \Exception('From is larger than to');
             }
         }
     } elseif ($from = $this->getParameters()->get('ts')) {
         $to = $from;
     } else {
         throw new \Exception('Missing timestamp (ts, from, to)');
     }
     $rows = 0;
     foreach (self::makeArray($uuids) as $uuid) {
         $channel = EntityController::factory($this->em, $uuid, true);
         $rows += $channel->clearData($this->em->getConnection(), $from, $to);
     }
     return array('rows' => $rows);
 }
 /**
  * Add single or multiple tuples
  *
  * @todo replace by pluggable api parser
  * @param Model\Channel $channel
  */
 public function add($channel)
 {
     try {
         /* to parse new submission protocol */
         $rawPost = $this->request->getContent();
         // file_get_contents('php://input')
         $json = Util\JSON::decode($rawPost);
         if (isset($json['data'])) {
             throw new \Exception('Can only add data for a single channel at a time');
             /* backed out b111cfa2 */
         }
         // convert nested ArrayObject to plain array with flattened tuples
         $data = array_reduce($json->getArrayCopy(), function ($carry, $tuple) {
             return array_merge($carry, $tuple);
         }, array());
     } catch (Util\JSONException $e) {
         /* fallback to old method */
         $timestamp = $this->request->parameters->get('ts');
         $value = $this->request->parameters->get('value');
         if (is_null($timestamp)) {
             $timestamp = (double) round(microtime(TRUE) * 1000);
         } else {
             $timestamp = Interpreter::parseDateTimeString($timestamp);
         }
         if (is_null($value)) {
             $value = 1;
         }
         // same structure as JSON request result
         $data = array($timestamp, $value);
     }
     $sql = 'INSERT ' . (in_array(self::OPT_SKIP_DUPLICATES, $this->options) ? 'IGNORE ' : '') . 'INTO data (channel_id, timestamp, value) ' . 'VALUES ' . implode(', ', array_fill(0, count($data) >> 1, '(' . $channel->getId() . ',?,?)'));
     $rows = $this->em->getConnection()->executeUpdate($sql, $data);
     return array('rows' => $rows);
 }