Skip to content

Commit

Permalink
add rollups and break out demos
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew J Cronk committed Feb 8, 2012
1 parent b2eb186 commit 348a2f0
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 87 deletions.
53 changes: 0 additions & 53 deletions tempodb-demo.php

This file was deleted.

28 changes: 28 additions & 0 deletions tempodb-read-demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
require('./tempodb.php');
date_default_timezone_set("America/Chicago");

$tdb = new TempoDB("your-api-key", "your-api-secret");

$series_key = "custom-series-key";
$start = new DateTime("2011-01-03");
$end = new DateTime("2011-01-06");

/* http://tempo-db.com/api/read-series/#read-series-by-key */


/* read query with no rollup interval or function specified */
/* rollup interval will be auto-calculated based on start-end date read */
/* rollup function defaults to avg */
$result = $tdb->read_key($series_key, $start, $end);

/* read query with no rollup interval specified */
/* rollup function defaults to avg */
//$result = $tdb->read_key($series_key, $start, $end, $interval="1month");

/* read query with a rollup interval specified */
//$result = $tdb->read_key($series_key, $start, $end, $interval="1month", $function="min");

var_dump($result);

?>
33 changes: 33 additions & 0 deletions tempodb-write-demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
require('./tempodb.php');
date_default_timezone_set("America/Chicago");

$tdb = new TempoDB("your-api-key", "your-api-secret");

$series_key = "custom-series-key";

/* http://tempo-db.com/api/write-series/#write-series-by-key */


$date = new DateTime("2012-01-01");

// insert random data for testing
// write in ten days worth of data, starting on midnight of $date
for ($day = 0; $day < 10; $day++)
{
$data = array();

// build up array of timestamp/value pairs for one day
for ($min=0; $min < 1440; $min++)
{
$data[] = array('t' => $date->format("c"), 'v' => rand()/17);
// increment by 1 min
$date->modify("+1 minute");
}

// send the days worth of data
$result = $tdb->write_key($series_key, $data);
var_dump($result);
}

?>
74 changes: 40 additions & 34 deletions tempodb.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,59 @@ function getAPIServer()
return "https://".$this->api_server."/".$this->api_version;
}

function range($start, $end, $series_id=NULL, $series_key=NULL)
{
// must provide either $series_id or $series_key
$series_type = NULL;
$series_val = NULL;

if ($series_id) {
$series_type = "id";
$series_val = $series_id;
}
elseif ($series_key) {
$series_type = "key";
$series_val = $series_key;
}
else {
// TODO: throw error
}
function read_id($series_id, $start, $end, $interval=NULL, $function=NULL){
$series_type = "id";
$series_val = $series_id;
return $this->read($series_type, $series_val, $start, $end, $interval, $function);
}

function read_key($series_key, $start, $end, $interval=NULL, $function=NULL){
$series_type = "key";
$series_val = $series_key;
return $this->read($series_type, $series_val, $start, $end, $interval, $function);
}

function read($series_type, $series_val, $start, $end, $interval=NULL, $function=NULL)
{
// send GET request, formatting dates in ISO 8601
$params = array("start"=>$start->format("c"), "end"=>$end->format("c"));
$params = array(
"start"=>$start->format("c"),
"end"=>$end->format("c"),
"interval"=>$interval,
"function"=>$function);

$querystring = http_build_query($params, null, '&');

return $this->http_req->jsonGetReq($this->getAPIServer()."/series/".$series_type."/".$series_val."/data/?".$querystring);
}

function add($data, $series_id=NULL, $series_key=NULL)

function write_id($series_id, $data)
{
$series_type = "id";
$series_val = $series_id;
return $this->write($series_type, $series_val, $data);
}

function write_key($series_key, $data)
{
// must provide either $series_id or $series_key
$series_type = NULL;
$series_val = NULL;

if ($series_id) {
$series_type = "id";
$series_val = $series_id;
}
elseif ($series_key) {
$series_type = "key";
$series_val = $series_key;
}
else {
// TODO: throw error
}
$series_type = "key";
$series_val = $series_key;
return $this->write($series_type, $series_val, $data);
}

function write($series_type, $series_val, $data)
{
// send POST request, formatting dates in ISO 8601
return $this->http_req->jsonPostReq($this->getAPIServer()."/series/".$series_type."/".$series_val."/data/", $data);
}

function write_bulk($data)
{
// send POST request, formatting dates in ISO 8601
return $this->http_req->jsonPostReq($this->getAPIServer()."/data/", $data);
}

}

class HTTPReq
Expand Down

0 comments on commit 348a2f0

Please sign in to comment.