mirror of
https://github.com/cachethq/cachet.git
synced 2026-07-01 08:15:55 +00:00
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/#2955
138 lines
5.3 KiB
PHP
138 lines
5.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of Cachet.
|
|
*
|
|
* (c) Alt Three Services Limited
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace CachetHQ\Cachet\Repositories\Metric;
|
|
|
|
use CachetHQ\Cachet\Models\Metric;
|
|
use DateInterval;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Jenssegers\Date\Date;
|
|
|
|
class SqliteRepository extends AbstractMetricRepository implements MetricInterface
|
|
{
|
|
/**
|
|
* Returns metrics for the last hour.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
* @param int $hour
|
|
* @param int $minute
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getPointsLastHour(Metric $metric, $hour, $minute)
|
|
{
|
|
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'))->sub(new DateInterval('PT'.$minute.'M'));
|
|
$metricPointsTableName = $this->getMetricPointsTableName();
|
|
|
|
// Default metrics calculations.
|
|
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
|
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} else {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
}
|
|
|
|
$value = 0;
|
|
$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)", [
|
|
'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);
|
|
}
|
|
|
|
/**
|
|
* Returns metrics for a given hour.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
* @param int $hour
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getPointsByHour(Metric $metric, $hour)
|
|
{
|
|
$dateTime = (new Date())->sub(new DateInterval('PT'.$hour.'H'));
|
|
$metricPointsTableName = $this->getMetricPointsTableName();
|
|
|
|
// Default metrics calculations.
|
|
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
|
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} else {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
}
|
|
|
|
$value = 0;
|
|
$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)", [
|
|
'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;
|
|
}
|
|
|
|
return round($value, $metric->places);
|
|
}
|
|
|
|
/**
|
|
* Returns metrics for the week.
|
|
*
|
|
* @param \CachetHQ\Cachet\Models\Metric $metric
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getPointsForDayInWeek(Metric $metric, $day)
|
|
{
|
|
$dateTime = (new Date())->sub(new DateInterval('P'.$day.'D'));
|
|
$metricPointsTableName = $this->getMetricPointsTableName();
|
|
|
|
// Default metrics calculations.
|
|
if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} elseif ($metric->calc_type == Metric::CALC_AVG) {
|
|
$queryType = "avg($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
} else {
|
|
$queryType = "sum($metricPointsTableName.value * $metricPointsTableName.counter)";
|
|
}
|
|
|
|
$value = 0;
|
|
$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)", [
|
|
'metricId' => $metric->id,
|
|
'timeInterval' => $dateTime->format('Ymd'),
|
|
]);
|
|
|
|
if (isset($query[0])) {
|
|
$value = $query[0]->value;
|
|
}
|
|
|
|
if ($value === 0 && $metric->default_value != $value) {
|
|
return $metric->default_value;
|
|
}
|
|
|
|
return round($value, $metric->places);
|
|
}
|
|
}
|