Пример #1
0
 /**
  * Add single or multiple tuples
  *
  * @todo deduplicate Model\Channel code
  * @param string|array uuid
  */
 public function add($uuid)
 {
     $channel = $this->ec->getSingleEntity($uuid, true);
     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, function ($carry, $tuple) {
             return array_merge($carry, $tuple);
         }, array());
     } catch (\RuntimeException $e) {
         /* fallback to old method */
         $timestamp = $this->request->query->get('ts');
         $value = $this->request->query->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);
 }
Пример #2
0
 /**
  * Render response and send it to the client
  */
 public function send()
 {
     if ($this->response instanceof StreamedResponse) {
         $this->response->setCallback(function () {
             $this->renderDeferred();
             flush();
         });
     } else {
         ob_start();
         $this->renderDeferred();
         $json = ob_get_contents();
         ob_end_clean();
         if (Util\Debug::isActivated()) {
             $json = Util\JSON::format($json);
         }
         $this->response->setContent($json);
     }
     return $this->response;
 }
Пример #3
0
 /**
  * Sporadic test/demo implemenation
  *
  * @todo replace by pluggable api parser
  */
 public function add(Model\Channel $channel)
 {
     try {
         /* to parse new submission protocol */
         $rawPost = file_get_contents('php://input');
         $json = Util\JSON::decode($rawPost);
         foreach ($json as $tuple) {
             $channel->addData(new Model\Data($channel, (double) round($tuple[0]), $tuple[1]));
         }
     } catch (Util\JSONException $e) {
         /* fallback to old method */
         $timestamp = $this->view->request->getParameter('ts');
         $value = $this->view->request->getParameter('value');
         if (is_null($timestamp)) {
             $timestamp = (double) round(microtime(TRUE) * 1000);
         }
         if (is_null($value)) {
             $value = 1;
         }
         $channel->addData(new Model\Data($channel, $timestamp, $value));
     }
     $this->em->flush();
 }
Пример #4
0
 /**
  * Add single or multiple tuples
  *
  * @todo deduplicate Model\Channel code
  * @param string|array uuid
  */
 public function add($uuid)
 {
     $channel = EntityController::factory($this->em, $uuid, true);
     if (!$channel instanceof Model\Channel) {
         throw new \Exception('Adding data is only supported for channels');
     }
     try {
         /* to parse new submission protocol */
         $rawPost = $this->request->getContent();
         // file_get_contents('php://input')
         // check maximum size allowed
         if ($maxSize = Util\Configuration::read('security.maxbodysize')) {
             if (strlen($rawPost) > $maxSize) {
                 throw new \Exception('Maximum message size exceeded');
             }
         }
         $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, function ($carry, $tuple) {
             return array_merge($carry, $tuple);
         }, array());
     } catch (\RuntimeException $e) {
         /* fallback to old method */
         $timestamp = $this->getParameters()->get('ts');
         $value = $this->getParameters()->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);
 }
Пример #5
0
 /**
  * Render response and send it to the client
  */
 public function send()
 {
     if ($this->response instanceof StreamedResponse) {
         $this->response->setCallback(function () {
             // callback happens outside Router->handle() and requires explicit exception handling
             try {
                 $this->renderDeferred();
             } catch (\Exception $e) {
                 $this->renderStreamedException($e);
             }
             flush();
         });
     } else {
         ob_start();
         $this->renderDeferred();
         $json = ob_get_contents();
         ob_end_clean();
         if (Util\Debug::isActivated()) {
             $json = Util\JSON::format($json);
         }
         $this->response->setContent($json);
     }
     return $this->response;
 }
Пример #6
0
 /**
  * Load JSON definitions from file (via lazy loading from get())
  *
  * @todo add caching
  */
 protected static function load()
 {
     static::$definitions = array();
     $json = Util\JSON::decode(file_get_contents(VZ_DIR . static::FILE));
     foreach ($json as $property) {
         static::$definitions[$property->name] = new static($property);
     }
 }
Пример #7
0
 /**
  * Load JSON definitions from file (via lazy loading from get())
  *
  * @todo add caching
  */
 protected static function load()
 {
     static::$definitions = array();
     $cache_id = static::CACHE_KEY . static::FILE;
     if (Util\Configuration::read('devmode') == FALSE && extension_loaded('apc') && apc_exists($cache_id) && time() - filemtime(__DIR__ . '/' . static::FILE) > Util\Configuration::read('cache.ttl')) {
         static::$definitions = apc_fetch($cache_id);
     } else {
         // expensive - cache results
         $json = Util\JSON::decode(file_get_contents(__DIR__ . '/' . static::FILE));
         foreach ($json as $property) {
             static::$definitions[$property->name] = new static($property);
         }
         if (extension_loaded('apc')) {
             apc_store($cache_id, static::$definitions, Util\Configuration::read('cache.ttl'));
         }
     }
 }
Пример #8
0
 * along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
 */
use Volkszaehler\Util;
include '../lib/Util/JSON.php';
echo '<pre>';
$data = '{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}';
$json = Util\JSON::decode($data);
$json['test'] = 2;
echo $json->encode(JSON_PRETTY);
echo '</pre>';
Пример #9
0
 /**
  * Render response and send it to the client
  */
 public function send()
 {
     // use StreamedResponse unless pretty-printing is required
     if (Util\Debug::isActivated()) {
         $this->add(Util\Debug::getInstance());
     } else {
         $this->response = new StreamedResponse();
     }
     // JSONP
     if ($this->padding) {
         $this->response->headers->set('Content-Type', 'application/javascript');
     } else {
         $this->response->headers->set('Content-Type', 'application/json');
         // enable CORS if not JSONP
         $this->response->headers->set('Access-Control-Allow-Origin', '*');
     }
     if ($this->response instanceof StreamedResponse) {
         $this->response->setCallback(function () {
             $this->renderDeferred();
             flush();
         });
     } else {
         ob_start();
         $this->renderDeferred();
         $json = ob_get_contents();
         ob_end_clean();
         // padded response is js, not json
         if (Util\Debug::isActivated() && !$this->padding) {
             $json = Util\JSON::format($json);
         }
         $this->response->setContent($json);
     }
     return $this->response;
 }