Skip to content

Commit a791656

Browse files
committed
Finish MySQL changes, start on Postgres
1 parent 02f0336 commit a791656

File tree

4 files changed

+39
-54
lines changed

4 files changed

+39
-54
lines changed

app/Repositories/Metric/AbstractMetricRepository.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ protected function getTableName()
7272
protected function getQueryType(Metric $metric)
7373
{
7474
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
75-
return 'sum(mp.`value` * mp.`counter`) AS `value`';
75+
return 'sum(metric_points.`value` * metric_points.`counter`) AS `value`';
7676
} elseif ($metric->calc_type == Metric::CALC_AVG) {
77-
return 'avg(mp.`value` * mp.`counter`) AS `value`';
77+
return 'avg(metric_points.`value` * metric_points.`counter`) AS `value`';
7878
} else {
79-
return 'sum(mp.`value` * mp.`counter`) AS `value`';
79+
return 'sum(metric_points.`value` * metric_points.`counter`) AS `value`';
8080
}
8181
}
8282

app/Repositories/Metric/MetricRepository.php

+20-15
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,20 @@ public function listPointsToday(Metric $metric, $hours = 12)
113113
public function listPointsForWeek(Metric $metric)
114114
{
115115
$dateTime = $this->dates->make();
116-
117-
$points = [];
118-
119-
$pointKey = $dateTime->format('D jS M');
116+
$pointKey = $dateTime->format('Y-m-d');
117+
$points = $this->repository->getPointsSinceDay($metric, 7)->pluck('value', 'key');
120118

121119
for ($i = 0; $i <= 7; $i++) {
122-
$points[$pointKey] = $this->repository->getPointsSinceDay($metric, $i);
123-
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('D jS M');
120+
if (!$points->has($pointKey)) {
121+
$points->put($pointKey, $metric->default_value);
122+
}
123+
124+
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
124125
}
125126

126-
return array_reverse($points);
127+
return $points->sortBy(function ($point, $key) use ($points) {
128+
return $key;
129+
});
127130
}
128131

129132
/**
@@ -136,18 +139,20 @@ public function listPointsForWeek(Metric $metric)
136139
public function listPointsForMonth(Metric $metric)
137140
{
138141
$dateTime = $this->dates->make();
139-
142+
$pointKey = $dateTime->format('Y-m-d');
140143
$daysInMonth = $dateTime->format('t');
141-
142-
$points = [];
143-
144-
$pointKey = $dateTime->format('jS M');
144+
$points = $this->repository->getPointsSinceDay($metric, $daysInMonth)->pluck('value', 'key');
145145

146146
for ($i = 0; $i <= $daysInMonth; $i++) {
147-
$points[$pointKey] = $this->repository->getPointsSinceDay($metric, $i);
148-
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('jS M');
147+
if (!$points->has($pointKey)) {
148+
$points->put($pointKey, $metric->default_value);
149+
}
150+
151+
$pointKey = $dateTime->sub(new DateInterval('P1D'))->format('Y-m-d');
149152
}
150153

151-
return array_reverse($points);
154+
return $points->sortBy(function ($point, $key) use ($points) {
155+
return $key;
156+
});
152157
}
153158
}

app/Repositories/Metric/MySql.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class MySql extends AbstractMetricRepository implements MetricInterface
3333
public function getPointsSinceMinutes(Metric $metric, $minutes)
3434
{
3535
$queryType = $this->getQueryType($metric);
36-
$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`", [
36+
$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`", [
3737
'metricId' => $metric->id,
3838
'minutes' => $minutes,
3939
]));
@@ -52,7 +52,7 @@ public function getPointsSinceMinutes(Metric $metric, $minutes)
5252
public function getPointsSinceHour(Metric $metric, $hour)
5353
{
5454
$queryType = $this->getQueryType($metric);
55-
$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`", [
55+
$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`", [
5656
'metricId' => $metric->id,
5757
'hour' => $hour,
5858
]));
@@ -70,7 +70,7 @@ public function getPointsSinceHour(Metric $metric, $hour)
7070
public function getPointsSinceDay(Metric $metric, $day)
7171
{
7272
$queryType = $this->getQueryType($metric);
73-
$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`", [
73+
$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`", [
7474
'metricId' => $metric->id,
7575
'day' => $day,
7676
]));

app/Repositories/Metric/PgSql.php

+13-33
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,13 @@ class PgSql extends AbstractMetricRepository implements MetricInterface
3333
*/
3434
public function getPointsSinceMinutes(Metric $metric, $minutes)
3535
{
36-
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
37-
3836
$queryType = $this->getQueryType($metric);
37+
$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`", [
38+
'metricId' => $metric->id,
39+
'minutes' => $minutes,
40+
]));
3941

40-
$value = 0;
41-
$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')", [
42-
'metricId' => $metric->id,
43-
'timeInterval' => $dateTime->format('YmdHi'),
44-
]);
45-
46-
if (isset($query[0])) {
47-
$value = $query[0]->value;
48-
}
49-
50-
if ($value === 0 && $metric->default_value != $value) {
51-
return $metric->default_value;
52-
}
53-
54-
return round($value, $metric->places);
42+
return $this->mapResults($metric, $points);
5543
}
5644

5745
/**
@@ -64,26 +52,18 @@ public function getPointsSinceMinutes(Metric $metric, $minutes)
6452
*/
6553
public function getPointsByHour(Metric $metric, $hour)
6654
{
67-
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
68-
69-
// Default metrics calculations.
70-
$queryType = $this->getQueryType($metric);
71-
72-
$value = 0;
73-
$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')", [
55+
$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')", [
7456
'metricId' => $metric->id,
7557
'timeInterval' => $dateTime->format('YmdH'),
7658
]);
7759

78-
if (isset($query[0])) {
79-
$value = $query[0]->value;
80-
}
81-
82-
if ($value === 0 && $metric->default_value != $value) {
83-
return $metric->default_value;
84-
}
60+
$queryType = $this->getQueryType($metric);
61+
$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`", [
62+
'metricId' => $metric->id,
63+
'minutes' => $minutes,
64+
]));
8565

86-
return round($value, $metric->places);
66+
return $this->mapResults($metric, $points);
8767
}
8868

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

10282
$value = 0;
103-
$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')", [
83+
$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')", [
10484
'metricId' => $metric->id,
10585
'timeInterval' => $dateTime->format('Ymd'),
10686
]);

0 commit comments

Comments
 (0)