Always use dynamic table names
This commit is contained in:
@@ -53,7 +53,7 @@ abstract class AbstractMetricRepository
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getTableName()
|
protected function getMetricsTable()
|
||||||
{
|
{
|
||||||
$driver = $this->config->get('database.default');
|
$driver = $this->config->get('database.default');
|
||||||
$connection = $this->config->get('database.connections.'.$driver);
|
$connection = $this->config->get('database.connections.'.$driver);
|
||||||
@@ -62,6 +62,20 @@ abstract class AbstractMetricRepository
|
|||||||
return $prefix.'metrics';
|
return $prefix.'metrics';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the metric points table name.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function getMetricPointsTable()
|
||||||
|
{
|
||||||
|
$driver = $this->config->get('database.default');
|
||||||
|
$connection = $this->config->get('database.connections.'.$driver);
|
||||||
|
$prefix = $connection['prefix'];
|
||||||
|
|
||||||
|
return $prefix.'metric_points';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the query type.
|
* Return the query type.
|
||||||
*
|
*
|
||||||
@@ -72,11 +86,11 @@ abstract class AbstractMetricRepository
|
|||||||
protected function getQueryType(Metric $metric)
|
protected function getQueryType(Metric $metric)
|
||||||
{
|
{
|
||||||
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
||||||
return 'sum(metric_points.value * metric_points.counter) AS value';
|
return "sum({$this->getMetricPointsTable()}.value * {$this->getMetricPointsTable()}.counter) AS value";
|
||||||
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
||||||
return 'avg(metric_points.value * metric_points.counter) AS value';
|
return "avg({$this->getMetricPointsTable()}.value * {$this->getMetricPointsTable()}.counter) AS value";
|
||||||
} else {
|
} else {
|
||||||
return 'sum(metric_points.value * metric_points.counter) AS value';
|
return "sum({$this->getMetricPointsTable()}.value * {$this->getMetricPointsTable()}.counter) AS value";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = 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`", [
|
$points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:%i') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :minutes MINUTE) GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`), MINUTE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
'minutes' => $minutes,
|
'minutes' => $minutes,
|
||||||
]);
|
]);
|
||||||
@@ -51,7 +51,7 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceHour(Metric $metric, $hour)
|
public function getPointsSinceHour(Metric $metric, $hour)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = 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`", [
|
$points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%H:00') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :hour HOUR) GROUP BY HOUR({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
'hour' => $hour,
|
'hour' => $hour,
|
||||||
]);
|
]);
|
||||||
@@ -70,7 +70,7 @@ class MySqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceDay(Metric $metric, $day)
|
public function getPointsSinceDay(Metric $metric, $day)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = 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`", [
|
$points = DB::select("SELECT DATE_FORMAT({$this->getMetricPointsTable()}.`created_at`, '%Y-%m-%d') AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= DATE_SUB(NOW(), INTERVAL :day DAY) GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
'day' => $day,
|
'day' => $day,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT to_char(metric_points.created_at, 'HH24:MI') 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, 'HH24:MI') ORDER BY to_char(metric_points.created_at, 'HH24:MI')", [
|
$points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$minutes}' MINUTE) GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI') ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:MI')", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceHour(Metric $metric, $hour)
|
public function getPointsSinceHour(Metric $metric, $hour)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT to_char(metric_points.created_at, 'HH24: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 >= (NOW() - INTERVAL '{$hour}' HOUR) GROUP BY to_char(metric_points.created_at, 'HH24:00') ORDER BY to_char(metric_points.created_at, 'HH24:00')", [
|
$points = DB::select("SELECT to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (NOW() - INTERVAL '{$hour}' HOUR) GROUP BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00') ORDER BY to_char({$this->getMetricPointsTable()}.created_at, 'HH24:00')", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ class PgSqlRepository extends AbstractMetricRepository implements MetricInterfac
|
|||||||
public function getPointsSinceDay(Metric $metric, $day)
|
public function getPointsSinceDay(Metric $metric, $day)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT DATE(metric_points.created_at) 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(NOW()) - INTERVAL '{$day}' DAY) GROUP BY DATE(metric_points.created_at) ORDER BY DATE(metric_points.created_at)", [
|
$points = DB::select("SELECT DATE({$this->getMetricPointsTable()}.created_at) AS key, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.created_at >= (DATE(NOW()) - INTERVAL '{$day}' DAY) GROUP BY DATE({$this->getMetricPointsTable()}.created_at) ORDER BY DATE({$this->getMetricPointsTable()}.created_at)", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa
|
|||||||
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
public function getPointsSinceMinutes(Metric $metric, $minutes)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT strftime('%H:%M', metric_points.`created_at`) 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` >= datetime('now', '-{$minutes} minutes') GROUP BY strftime('%H', metric_points.`created_at`), strftime('%M', metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
|
$points = DB::select("SELECT strftime('%H:%M', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$minutes} minutes') GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`), strftime('%M', {$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa
|
|||||||
public function getPointsSinceHour(Metric $metric, $hour)
|
public function getPointsSinceHour(Metric $metric, $hour)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT strftime('%H:00', metric_points.`created_at`) 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` >= datetime('now', '-{$hour} hours') GROUP BY strftime('%H', metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
|
$points = DB::select("SELECT strftime('%H:00', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$hour} hours') GROUP BY strftime('%H', {$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ class SqliteRepository extends AbstractMetricRepository implements MetricInterfa
|
|||||||
public function getPointsSinceDay(Metric $metric, $day)
|
public function getPointsSinceDay(Metric $metric, $day)
|
||||||
{
|
{
|
||||||
$queryType = $this->getQueryType($metric);
|
$queryType = $this->getQueryType($metric);
|
||||||
$points = DB::select("SELECT strftime('%Y-%m-%d', metric_points.`created_at`) 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` >= datetime('now', '-{$day} days') GROUP BY DATE(metric_points.`created_at`) ORDER BY metric_points.`created_at`", [
|
$points = DB::select("SELECT strftime('%Y-%m-%d', {$this->getMetricPointsTable()}.`created_at`) AS `key`, {$queryType} FROM {$this->getMetricsTable()} INNER JOIN {$this->getMetricPointsTable()} ON metrics.id = {$this->getMetricPointsTable()}.metric_id WHERE metrics.id = :metricId AND {$this->getMetricPointsTable()}.`created_at` >= datetime('now', '-{$day} days') GROUP BY DATE({$this->getMetricPointsTable()}.`created_at`) ORDER BY {$this->getMetricPointsTable()}.`created_at`", [
|
||||||
'metricId' => $metric->id,
|
'metricId' => $metric->id,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user