Skip to content

Commit

Permalink
Finish MySQL changes, start on Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk committed Aug 8, 2016
1 parent e248753 commit 2f02441
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
6 changes: 3 additions & 3 deletions app/Repositories/Metric/AbstractMetricRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ protected function getTableName()
protected function getQueryType(Metric $metric)
{
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
return 'sum(mp.`value` * mp.`counter`) AS `value`';
return 'sum(metric_points.`value` * metric_points.`counter`) AS `value`';
} elseif ($metric->calc_type == Metric::CALC_AVG) {
return 'avg(mp.`value` * mp.`counter`) AS `value`';
return 'avg(metric_points.`value` * metric_points.`counter`) AS `value`';
} else {
return 'sum(mp.`value` * mp.`counter`) AS `value`';
return 'sum(metric_points.`value` * metric_points.`counter`) AS `value`';
}
}

Expand Down
35 changes: 20 additions & 15 deletions app/Repositories/Metric/MetricRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,20 @@ public function listPointsToday(Metric $metric, $hours = 12)
public function listPointsForWeek(Metric $metric)
{
$dateTime = $this->dates->make();

$points = [];

$pointKey = $dateTime->format('D jS M');
$pointKey = $dateTime->format('Y-m-d');
$points = $this->repository->getPointsSinceDay($metric, 7)->pluck('value', 'key');

for ($i = 0; $i <= 7; $i++) {
$points[$pointKey] = $this->repository->getPointsSinceDay($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}

$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
}

return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}

/**
Expand All @@ -136,18 +139,20 @@ public function listPointsForWeek(Metric $metric)
public function listPointsForMonth(Metric $metric)
{
$dateTime = $this->dates->make();

$pointKey = $dateTime->format('Y-m-d');
$daysInMonth = $dateTime->format('t');

$points = [];

$pointKey = $dateTime->format('jS M');
$points = $this->repository->getPointsSinceDay($metric, $daysInMonth)->pluck('value', 'key');

for ($i = 0; $i <= $daysInMonth; $i++) {
$points[$pointKey] = $this->repository->getPointsSinceDay($metric, $i);
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
if (!$points->has($pointKey)) {
$points->put($pointKey, $metric->default_value);
}

$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
}

return array_reverse($points);
return $points->sortBy(function ($point, $key) use ($points) {
return $key;
});
}
}
6 changes: 3 additions & 3 deletions app/Repositories/Metric/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MySql extends AbstractMetricRepository implements MetricInterface
public function getPointsSinceMinutes(Metric $metric, $minutes)
{
$queryType = $this->getQueryType($metric);
$points = Collection::make(DB::select("SELECT DATE_FORMAT(mp.`created_at`, '%H:%i') AS `key`, {$queryType} FROM {$this->getTableName()} m INNER JOIN metric_points mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.`created_at` >= DATE_SUB(NOW(), INTERVAL :minutes MINUTE) GROUP BY HOUR(mp.`created_at`), MINUTE(mp.`created_at`) ORDER BY mp.`created_at`", [
$points = Collection::make(DB::select("SELECT DATE_FORMAT(metric_points.`created_at`, '%H:%i') AS `key`, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.`created_at` >= DATE_SUB(NOW(), INTERVAL :minutes MINUTE) GROUP BY HOUR(metric_points.`created_at`), MINUTE(metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
'metricId' => $metric->id,
'minutes' => $minutes,
]));
Expand All @@ -52,7 +52,7 @@ public function getPointsSinceMinutes(Metric $metric, $minutes)
public function getPointsSinceHour(Metric $metric, $hour)
{
$queryType = $this->getQueryType($metric);
$points = Collection::make(DB::select("SELECT DATE_FORMAT(mp.`created_at`, '%H:00') AS `key`, {$queryType} FROM {$this->getTableName()} m INNER JOIN metric_points mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.`created_at` >= DATE_SUB(NOW(), INTERVAL :hour HOUR) GROUP BY HOUR(mp.`created_at`) ORDER BY mp.`created_at`", [
$points = Collection::make(DB::select("SELECT DATE_FORMAT(metric_points.`created_at`, '%H:00') AS `key`, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.`created_at` >= DATE_SUB(NOW(), INTERVAL :hour HOUR) GROUP BY HOUR(metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
'metricId' => $metric->id,
'hour' => $hour,
]));
Expand All @@ -70,7 +70,7 @@ public function getPointsSinceHour(Metric $metric, $hour)
public function getPointsSinceDay(Metric $metric, $day)
{
$queryType = $this->getQueryType($metric);
$points = Collection::make(DB::select("SELECT DATE_FORMAT(mp.`created_at`, '%Y-%m-%d') AS `key`, {$queryType} FROM {$this->getTableName()} m INNER JOIN metric_points mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.`created_at` >= DATE_SUB(NOW(), INTERVAL :day DAY) GROUP BY DATE_FORMAT(mp.`created_at`) ORDER BY mp.`created_at`", [
$points = Collection::make(DB::select("SELECT DATE_FORMAT(metric_points.`created_at`, '%Y-%m-%d') AS `key`, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.`created_at` >= DATE_SUB(NOW(), INTERVAL :day DAY) GROUP BY DATE(metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
'metricId' => $metric->id,
'day' => $day,
]));
Expand Down
46 changes: 13 additions & 33 deletions app/Repositories/Metric/PgSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,13 @@ class PgSql extends AbstractMetricRepository implements MetricInterface
*/
public function getPointsSinceMinutes(Metric $metric, $minutes)
{
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));

$queryType = $this->getQueryType($metric);
$points = Collection::make(DB::select("SELECT to_char(metric_points.`created_at`, 'HHMI') AS `key`, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.`created_at` >= NOW() -, INTERVAL ':minutes' MINUTE) GROUP BY to_char(metric_points.`created_at`, 'HHMI') ORDER BY metric_points.`created_at`", [
'metricId' => $metric->id,
'minutes' => $minutes,
]));

$value = 0;
$query = DB::select("select {$queryType} FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE m.id = :metricId AND to_char(metric_points.created_at, 'YYYYMMDDHH24MI') = :timeInterval GROUP BY to_char(metric_points.created_at, 'HHMI')", [
'metricId' => $metric->id,
'timeInterval' => $dateTime->format('YmdHi'),
]);

if (isset($query[0])) {
$value = $query[0]->value;
}

if ($value === 0 && $metric->default_value != $value) {
return $metric->default_value;
}

return round($value, $metric->places);
return $this->mapResults($metric, $points);
}

/**
Expand All @@ -64,26 +52,18 @@ public function getPointsSinceMinutes(Metric $metric, $minutes)
*/
public function getPointsByHour(Metric $metric, $hour)
{
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));

// Default metrics calculations.
$queryType = $this->getQueryType($metric);

$value = 0;
$query = DB::select("select {$queryType} FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE metric_points.metric_id = :metricId AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timeInterval GROUP BY to_char(metric_points.created_at, 'H')", [
$query = DB::select("select {$queryType} FROM {$this->getTableName()} JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = :metricId AND to_char(metric_points.created_at, 'YYYYMMDDHH24') = :timeInterval GROUP BY to_char(metric_points.created_at, 'H')", [
'metricId' => $metric->id,
'timeInterval' => $dateTime->format('YmdH'),
]);

if (isset($query[0])) {
$value = $query[0]->value;
}

if ($value === 0 && $metric->default_value != $value) {
return $metric->default_value;
}
$queryType = $this->getQueryType($metric);
$points = Collection::make(DB::select("SELECT to_char(metric_points.`created_at`, 'H') AS `key`, {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.`created_at` >= NOW() -, INTERVAL ':minutes' MINUTE) GROUP BY to_char(metric_points.`created_at`, 'H') ORDER BY metric_points.`created_at`", [
'metricId' => $metric->id,
'minutes' => $minutes,
]));

return round($value, $metric->places);
return $this->mapResults($metric, $points);
}

/**
Expand All @@ -100,7 +80,7 @@ public function getPointsForDayInWeek(Metric $metric, $day)
$queryType = $this->getQueryType($metric);

$value = 0;
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN metric_points mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.created_at BETWEEN (mp.created_at - interval '1 week') AND (now() + interval '1 day') AND to_char(mp.created_at, 'YYYYMMDD') = :timeInterval GROUP BY to_char(mp.created_at, 'YYYYMMDD')", [
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} INNER JOIN metric_points ON metrics.id = metric_points.metric_id WHERE metrics.id = :metricId AND metric_points.created_at BETWEEN (metric_points.created_at - interval '1 week') AND (now() + interval '1 day') AND to_char(metric_points.created_at, 'YYYYMMDD') = :timeInterval GROUP BY to_char(metric_points.created_at, 'YYYYMMDD')", [
'metricId' => $metric->id,
'timeInterval' => $dateTime->format('Ymd'),
]);
Expand Down

0 comments on commit 2f02441

Please sign in to comment.