Fix metic point calculations.

This commit is contained in:
James Brooks
2015-04-18 14:06:14 +01:00
parent 35dc0f6f77
commit 9c221b7b2a

View File

@@ -81,23 +81,23 @@ class Metric extends Model
$dateTime->sub(new DateInterval('PT'.$hour.'H'));
if (Config::get('database.default') === 'mysql') {
if (! isset($this->calc_type) || $this->calc_type === self::CALC_SUM) {
$value = (int) $this->points()->whereRaw('DATE_FORMAT(created_at, "%Y%m%e%H") = '.$dateTime->format('YmdH'))->whereRaw('HOUR(created_at) = HOUR(DATE_SUB(NOW(), INTERVAL '.$hour.' HOUR))')->groupBy(DB::raw('HOUR(created_at)'))->sum('value');
} elseif ($this->calc_type === self::CALC_AVG) {
$value = (int) $this->points()->whereRaw('DATE_FORMAT(created_at, "%Y%m%e%H") = '.$dateTime->format('YmdH'))->whereRaw('HOUR(created_at) = HOUR(DATE_SUB(NOW(), INTERVAL '.$hour.' HOUR))')->groupBy(DB::raw('HOUR(created_at)'))->avg('value');
if (! isset($this->calc_type) || $this->calc_type == self::CALC_SUM) {
$value = (int) $this->points()->whereRaw('DATE_FORMAT(created_at, "%Y%m%e%H") = '.$dateTime->sub(new DateInterval('PT'.$hour.'H'))->format('YmdH'))->groupBy(DB::raw('HOUR(created_at)'))->sum('value');
} elseif ($this->calc_type == self::CALC_AVG) {
$value = (int) $this->points()->whereRaw('DATE_FORMAT(created_at, "%Y%m%e%H") = '.$dateTime->sub(new DateInterval('PT'.$hour.'H'))->format('YmdH'))->groupBy(DB::raw('HOUR(created_at)'))->avg('value');
}
} else {
// Default metrics calculations.
if (! isset($this->calc_type) || $this->calc_type === self::CALC_SUM) {
$queryType = "sum(metric_points.value)";
} elseif ($this->calc_type === self::CALC_AVG) {
$queryType = "avg(metric_points.value)";
if (! isset($this->calc_type) || $this->calc_type == self::CALC_SUM) {
$queryType = 'sum(metric_points.value)';
} elseif ($this->calc_type == self::CALC_AVG) {
$queryType = 'avg(metric_points.value)';
} else {
$queryType = "sum(metric_points.value)";
$queryType = 'sum(metric_points.value)';
}
$query = DB::select("select {$queryType} as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = {$this->id} AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timestamp AND to_char(metric_points.created_at, 'H') = to_char(now() - interval '{$hour} hour', 'H') GROUP BY to_char(metric_points.created_at, 'H')", [
'timestamp' => $dateTime->format('YmdH'),
$query = DB::select("select {$queryType} as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = {$this->id} AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timestamp GROUP BY to_char(metric_points.created_at, 'H')", [
'timestamp' => $dateTime->sub(new DateInterval('PT'.$hour.'H'))->format('YmdH'),
]);
if (isset($query[0])) {