fixed sql group by statement
added some exception throwing in invalid arguments
This commit is contained in:
parent
d20ba8b589
commit
cc7d33eed6
5 changed files with 37 additions and 14 deletions
|
@ -61,7 +61,7 @@ abstract class Channel extends DatabaseObject implements ChannelInterface {
|
|||
* @param $groupBy determines how readings are grouped. Possible values are: year, month, day, hour, minute or an integer for the desired size of the returned array
|
||||
*/
|
||||
public function getData($from = NULL, $to = NULL, $groupBy = NULL) {
|
||||
$ts = 'FROM_UNIXTIME(timestamp)'; // just for saving space
|
||||
$ts = 'FROM_UNIXTIME(timestamp/1000)'; // just for saving space
|
||||
switch ($groupBy) {
|
||||
case 'year':
|
||||
$sqlGroupBy = 'YEAR(' . $ts . ')';
|
||||
|
@ -100,7 +100,6 @@ abstract class Channel extends DatabaseObject implements ChannelInterface {
|
|||
}
|
||||
|
||||
$sql .= ' ORDER BY timestamp DESC';
|
||||
|
||||
$result = $this->dbh->query($sql);
|
||||
$totalCount = $result->count();
|
||||
|
||||
|
@ -138,14 +137,20 @@ abstract class Channel extends DatabaseObject implements ChannelInterface {
|
|||
* simple self::getByFilter() wrapper
|
||||
*/
|
||||
static public function getByUcid($ucid) {
|
||||
return current(self::getByFilter(array('ucid' => $ucid)));
|
||||
$channel = self::getByFilter(array('ucid' => $ucid));
|
||||
|
||||
if (current($channel) === false) {
|
||||
throw new InvalidArgumentException('No such channel!');
|
||||
}
|
||||
|
||||
return $channel;
|
||||
}
|
||||
|
||||
/*
|
||||
* simple self::getByFilter() wrapper
|
||||
*/
|
||||
static public function getByType($type) {
|
||||
return current(self::getByFilter(array('type' => $type)));
|
||||
return self::getByFilter(array('type' => $type));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -30,7 +30,7 @@ abstract class Meter extends Channel {
|
|||
|
||||
$result = $this->dbh->query($sql)->rewind();
|
||||
|
||||
return $result['count'] / $this->resolution;
|
||||
return $result['count'] / $this->resolution / 1000; // returns Wh
|
||||
}
|
||||
|
||||
public function getMin($from = NULL, $to = NULL) {
|
||||
|
@ -57,8 +57,8 @@ abstract class Meter extends Channel {
|
|||
return $min;
|
||||
}
|
||||
|
||||
public function getAverage($from, $to) {
|
||||
return $this->getConsumption($from, $to) / ($to - $from) * 1000;
|
||||
public function getAverage($from = NULL, $to = NULL) { // TODO calculate timeinterval if no params were given
|
||||
return $this->getConsumption($from, $to) / ($to - $from) / 1000; // return W
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -79,9 +79,9 @@ abstract class Meter extends Channel {
|
|||
$delta = $pulses[$i]['timestamp'] - $pulses[$i-1]['timestamp'];
|
||||
|
||||
$pulses[$i]['timestamp'] -= $delta/2;
|
||||
$pulses[$i]['value'] *= 3600000/(($this->resolution / 1000) * $delta);
|
||||
$pulses[$i]['value'] *= 3600000/(($this->resolution / 1000) * $delta); // TODO untested
|
||||
}
|
||||
|
||||
return $pulses;
|
||||
return $pulses; // returns W
|
||||
}
|
||||
}
|
|
@ -145,7 +145,7 @@ class PgSql extends Database {
|
|||
}
|
||||
|
||||
public function getLastInsertId() {
|
||||
throw new Exception('PgSql::getLastInsertId() hasnt implemented yet!'); // TODO find solution
|
||||
throw new Exception('PgSql::getLastInsertId() hasnt implemented yet!'); // TODO find solution, use PDO?
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -37,8 +37,14 @@ class Group extends DatabaseObject {
|
|||
return $groups;
|
||||
}
|
||||
|
||||
public static function getByUgid($uuid) {
|
||||
return current(self::getByFilter(array('ugid' => $ugid)));
|
||||
public static function getByUgid($ugid) {
|
||||
$group = self::getByFilter(array('ugid' => $ugid));
|
||||
|
||||
if (current($group) === false) {
|
||||
throw new InvalidArgumentException('No such group!');
|
||||
}
|
||||
|
||||
return $group;
|
||||
}
|
||||
|
||||
public function getUsers($recursive = false) {
|
||||
|
|
|
@ -29,11 +29,23 @@ class User extends DatabaseObject {
|
|||
* simple self::getByFilter() wrapper
|
||||
*/
|
||||
public static function getByUuid($uuid) {
|
||||
return current(self::getByFilter(array('uuid' => $uuid)));
|
||||
$user = self::getByFilter(array('uuid' => $uuid));
|
||||
|
||||
if (current($user) === false) {
|
||||
throw new InvalidArgumentException('No such user!');
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public static function getByEMail($email) {
|
||||
return current(self::getByFilter(array('email' => $email)));
|
||||
$user = self::getByFilter(array('email' => $email));
|
||||
|
||||
if (current($user) === false) {
|
||||
throw new InvalidArgumentException('No such user!');
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function getChannels($recursive = false) {
|
||||
|
|
Loading…
Add table
Reference in a new issue