Skip to content

Commit 35dcc8c

Browse files
committed
Use the table prefix on metric_points
In the '.env' file, a 'DB_PREFIX' sets the prefix that should be used on every table name. When writing an SQL query the 'DB_PREFIX' value has to be prefixed to the table name. The repository PgSqlRepository, MySqlRepository and SqliteRepository, located in 'app/Repositories/Metric/' did not apply this prefix on the 'metric_points' table. The problem occured only when we set a 'DB_PREFIX' not null, the rest of the application worked correctly but the part about 'metric_points' couldn't work, saying the table was inexistant. A method was added in the repository AbstractMetricRepository to get the 'metric_points' table name with the prefix if there is one. This method is used in the three repositories to get the right table name. Note: This problem was present in 2.3, but was already fixed in 2.4 so there is no need to apply this commit on the 2.4 branch. See: CachetHQ/Cachet/cachethq#2955
1 parent 93d1f87 commit 35dcc8c

File tree

4 files changed

+58
-27
lines changed

4 files changed

+58
-27
lines changed

app/Repositories/Metric/AbstractMetricRepository.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,37 @@ public function __construct(Repository $config)
4848
}
4949

5050
/**
51-
* Get the metrics table name.
51+
* Get the table names prefix.
5252
*
53-
* @return string
53+
* @return string
5454
*/
55-
protected function getTableName()
55+
protected function getPrefix()
5656
{
5757
$driver = $this->config->get('database.default');
5858
$connection = $this->config->get('database.connections.'.$driver);
5959
$prefix = $connection['prefix'];
60+
return $prefix;
61+
}
6062

63+
/**
64+
* Get the metrics table name.
65+
*
66+
* @return string
67+
*/
68+
protected function getTableName()
69+
{
70+
$prefix = $this->getPrefix();
6171
return $prefix.'metrics';
6272
}
73+
74+
/**
75+
* Get the metric points table name.
76+
*
77+
* @return string
78+
*/
79+
protected function getMetricPointsTableName()
80+
{
81+
$prefix = $this->getPrefix();
82+
return $prefix.'metric_points';
83+
}
6384
}

app/Repositories/Metric/MySqlRepository.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function getPointsLastHour(Metric $metric, $hour, $minute)
3636
{
3737
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
3838
$timeInterval = $dateTime->format('YmdHi');
39+
$metricPointsTableName = $this->getMetricPointsTableName();
40+
3941

4042
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
4143
$queryType = 'SUM(mp.`value` * mp.`counter`) AS `value`';
@@ -45,7 +47,7 @@ public function getPointsLastHour(Metric $metric, $hour, $minute)
4547

4648
$value = 0;
4749

48-
$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 DATE_FORMAT(mp.`created_at`, '%Y%m%d%H%i') = :timeInterval GROUP BY HOUR(mp.`created_at`), MINUTE(mp.`created_at`)", [
50+
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN $metricPointsTableName mp ON m.id = mp.metric_id WHERE m.id = :metricId AND DATE_FORMAT(mp.`created_at`, '%Y%m%d%H%i') = :timeInterval GROUP BY HOUR(mp.`created_at`), MINUTE(mp.`created_at`)", [
4951
'metricId' => $metric->id,
5052
'timeInterval' => $timeInterval,
5153
]);
@@ -73,6 +75,7 @@ public function getPointsByHour(Metric $metric, $hour)
7375
{
7476
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
7577
$hourInterval = $dateTime->format('YmdH');
78+
$metricPointsTableName = $this->getMetricPointsTableName();
7679

7780
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
7881
$queryType = 'SUM(mp.`value` * mp.`counter`) AS `value`';
@@ -82,7 +85,7 @@ public function getPointsByHour(Metric $metric, $hour)
8285

8386
$value = 0;
8487

85-
$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 DATE_FORMAT(mp.`created_at`, '%Y%m%d%H') = :hourInterval GROUP BY HOUR(mp.`created_at`)", [
88+
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN $metricPointsTableName mp ON m.id = mp.metric_id WHERE m.id = :metricId AND DATE_FORMAT(mp.`created_at`, '%Y%m%d%H') = :hourInterval GROUP BY HOUR(mp.`created_at`)", [
8689
'metricId' => $metric->id,
8790
'hourInterval' => $hourInterval,
8891
]);
@@ -108,6 +111,7 @@ public function getPointsByHour(Metric $metric, $hour)
108111
public function getPointsForDayInWeek(Metric $metric, $day)
109112
{
110113
$dateTime = (new Date())->sub(new DateInterval('P'.$day.'D'));
114+
$metricPointsTableName = $this->getMetricPointsTableName();
111115

112116
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
113117
$queryType = 'SUM(mp.`value` * mp.`counter`) AS `value`';
@@ -117,7 +121,7 @@ public function getPointsForDayInWeek(Metric $metric, $day)
117121

118122
$value = 0;
119123

120-
$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 DATE_SUB(mp.`created_at`, INTERVAL 1 WEEK) AND DATE_ADD(NOW(), INTERVAL 1 DAY) AND DATE_FORMAT(mp.`created_at`, '%Y%m%d') = :timeInterval GROUP BY DATE_FORMAT(mp.`created_at`, '%Y%m%d')", [
124+
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN $metricPointsTableName mp ON m.id = mp.metric_id WHERE m.id = :metricId AND mp.`created_at` BETWEEN DATE_SUB(mp.`created_at`, INTERVAL 1 WEEK) AND DATE_ADD(NOW(), INTERVAL 1 DAY) AND DATE_FORMAT(mp.`created_at`, '%Y%m%d') = :timeInterval GROUP BY DATE_FORMAT(mp.`created_at`, '%Y%m%d')", [
121125
'metricId' => $metric->id,
122126
'timeInterval' => $dateTime->format('Ymd'),
123127
]);

app/Repositories/Metric/PgSqlRepository.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,20 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac
3434
*/
3535
public function getPointsLastHour(Metric $metric, $hour, $minute)
3636
{
37+
$metricPointsTableName = $this->getMetricPointsTableName();
3738
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
3839

3940
// Default metrics calculations.
4041
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
41-
$queryType = 'sum(metric_points.value * metric_points.counter)';
42+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
4243
} elseif ($metric->calc_type == Metric::CALC_AVG) {
43-
$queryType = 'avg(metric_points.value * metric_points.counter)';
44+
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
4445
} else {
45-
$queryType = 'sum(metric_points.value * metric_points.counter)';
46+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
4647
}
4748

4849
$value = 0;
49-
$query = DB::select("select {$queryType} as value 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')", [
50+
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN $metricPointsTableName ON $metricPointsTableName.metric_id = m.id WHERE m.id = :metricId AND to_char($metricPointsTableName.created_at, 'YYYYMMDDHH24MI') = :timeInterval GROUP BY to_char($metricPointsTableName.created_at, 'HHMI')", [
5051
'metricId' => $metric->id,
5152
'timeInterval' => $dateTime->format('YmdHi'),
5253
]);
@@ -73,18 +74,19 @@ public function getPointsLastHour(Metric $metric, $hour, $minute)
7374
public function getPointsByHour(Metric $metric, $hour)
7475
{
7576
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
77+
$metricPointsTableName = $this->getMetricPointsTableName();
7678

7779
// Default metrics calculations.
7880
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
79-
$queryType = 'sum(metric_points.value * metric_points.counter)';
81+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
8082
} elseif ($metric->calc_type == Metric::CALC_AVG) {
81-
$queryType = 'avg(metric_points.value * metric_points.counter)';
83+
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
8284
} else {
83-
$queryType = 'sum(metric_points.value * metric_points.counter)';
85+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
8486
}
8587

8688
$value = 0;
87-
$query = DB::select("select {$queryType} as value 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')", [
89+
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN $metricPointsTableName ON $metricPointsTableName.metric_id = m.id WHERE $metricPointsTableName.metric_id = :metricId AND to_char($metricPointsTableName.created_at, 'YYYYMMDDHH24') = :timeInterval GROUP BY to_char($metricPointsTableName.created_at, 'H')", [
8890
'metricId' => $metric->id,
8991
'timeInterval' => $dateTime->format('YmdH'),
9092
]);
@@ -110,6 +112,7 @@ public function getPointsByHour(Metric $metric, $hour)
110112
public function getPointsForDayInWeek(Metric $metric, $day)
111113
{
112114
$dateTime = (new Date())->sub(new DateInterval('P'.$day.'D'));
115+
$metricPointsTableName = $this->getMetricPointsTableName();
113116

114117
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
115118
$queryType = 'sum(mp.value * mp.counter) AS value';
@@ -118,7 +121,7 @@ public function getPointsForDayInWeek(Metric $metric, $day)
118121
}
119122

120123
$value = 0;
121-
$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')", [
124+
$points = DB::select("SELECT {$queryType} FROM {$this->getTableName()} m INNER JOIN $metricPointsTableName 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')", [
122125
'metricId' => $metric->id,
123126
'timeInterval' => $dateTime->format('Ymd'),
124127
]);

app/Repositories/Metric/SqliteRepository.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa
3030
public function getPointsLastHour(Metric $metric, $hour, $minute)
3131
{
3232
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
33+
$metricPointsTableName = $this->getMetricPointsTableName();
3334

3435
// Default metrics calculations.
3536
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
36-
$queryType = 'sum(metric_points.value * metric_points.counter)';
37+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
3738
} elseif ($metric->calc_type == Metric::CALC_AVG) {
38-
$queryType = 'avg(metric_points.value * metric_points.counter)';
39+
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
3940
} else {
40-
$queryType = 'sum(metric_points.value * metric_points.counter)';
41+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
4142
}
4243

4344
$value = 0;
44-
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE m.id = :metricId AND strftime('%Y%m%d%H%M', metric_points.created_at) = :timeInterval GROUP BY strftime('%H%M', metric_points.created_at)", [
45+
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN $metricPointsTableName ON $metricPointsTableName.metric_id = m.id WHERE m.id = :metricId AND strftime('%Y%m%d%H%M', $metricPointsTableName.created_at) = :timeInterval GROUP BY strftime('%H%M', $metricPointsTableName.created_at)", [
4546
'metricId' => $metric->id,
4647
'timeInterval' => $dateTime->format('YmdHi'),
4748
]);
@@ -68,18 +69,19 @@ public function getPointsLastHour(Metric $metric, $hour, $minute)
6869
public function getPointsByHour(Metric $metric, $hour)
6970
{
7071
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
72+
$metricPointsTableName = $this->getMetricPointsTableName();
7173

7274
// Default metrics calculations.
7375
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
74-
$queryType = 'sum(metric_points.value * metric_points.counter)';
76+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
7577
} elseif ($metric->calc_type == Metric::CALC_AVG) {
76-
$queryType = 'avg(metric_points.value * metric_points.counter)';
78+
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
7779
} else {
78-
$queryType = 'sum(metric_points.value * metric_points.counter)';
80+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
7981
}
8082

8183
$value = 0;
82-
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE m.id = :metricId AND strftime('%Y%m%d%H', metric_points.created_at) = :timeInterval GROUP BY strftime('%H', metric_points.created_at)", [
84+
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN $metricPointsTableName ON $metricPointsTableName.metric_id = m.id WHERE m.id = :metricId AND strftime('%Y%m%d%H', $metricPointsTableName.created_at) = :timeInterval GROUP BY strftime('%H', $metricPointsTableName.created_at)", [
8385
'metricId' => $metric->id,
8486
'timeInterval' => $dateTime->format('YmdH'),
8587
]);
@@ -105,18 +107,19 @@ public function getPointsByHour(Metric $metric, $hour)
105107
public function getPointsForDayInWeek(Metric $metric, $day)
106108
{
107109
$dateTime = (new Date())->sub(new DateInterval('P'.$day.'D'));
110+
$metricPointsTableName = $this->getMetricPointsTableName();
108111

109112
// Default metrics calculations.
110113
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
111-
$queryType = 'sum(metric_points.value * metric_points.counter)';
114+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
112115
} elseif ($metric->calc_type == Metric::CALC_AVG) {
113-
$queryType = 'avg(metric_points.value * metric_points.counter)';
116+
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
114117
} else {
115-
$queryType = 'sum(metric_points.value * metric_points.counter)';
118+
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
116119
}
117120

118121
$value = 0;
119-
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN metric_points ON metric_points.metric_id = m.id WHERE m.id = :metricId AND metric_points.created_at > date('now', '-7 day') AND strftime('%Y%m%d', metric_points.created_at) = :timeInterval GROUP BY strftime('%Y%m%d', metric_points.created_at)", [
122+
$query = DB::select("select {$queryType} as value FROM {$this->getTableName()} m JOIN $metricPointsTableName ON $metricPointsTableName.metric_id = m.id WHERE m.id = :metricId AND $metricPointsTableName.created_at > date('now', '-7 day') AND strftime('%Y%m%d', $metricPointsTableName.created_at) = :timeInterval GROUP BY strftime('%Y%m%d', $metricPointsTableName.created_at)", [
120123
'metricId' => $metric->id,
121124
'timeInterval' => $dateTime->format('Ymd'),
122125
]);

0 commit comments

Comments
 (0)