Ejemplo n.º 1
0
 /**
  * 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'));
     $points = $metric->points()->whereRaw('created_at > date("now", "-7 day")')->whereRaw('strftime("%Y%m%d", created_at) = "' . $dateTime->format('Ymd') . '"')->groupBy(DB::raw('strftime("%Y%m%d", created_at)'));
     if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
         $value = $points->sum('value');
     } elseif ($metric->calc_type == Metric::CALC_AVG) {
         $value = $points->avg('value');
     }
     if ($value === 0 && $metric->default_value != $value) {
         return $metric->default_value;
     }
     return round($value, $metric->places);
 }
Ejemplo n.º 2
0
 /**
  * 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'));
     $points = $metric->points()->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = ' . $dateTime->format('Ymd'))->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")'));
     if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
         $value = $points->sum('value');
     } elseif ($metric->calc_type == Metric::CALC_AVG) {
         $value = $points->avg('value');
     }
     if ($value === 0 && $metric->default_value != $value) {
         return $metric->default_value;
     }
     return round($value, $metric->places);
 }
Ejemplo n.º 3
0
 /**
  * Returns metrics for the week.
  *
  * @param \CachetHQ\Cachet\Models\Metric $metric
  *
  * @return int
  */
 public function getPointsForDayInWeek(Metric $metric, $day)
 {
     $dateTime = (new Date())->setTimezone($this->dateTimeZone);
     $dateTime->sub(new DateInterval('P' . $day . 'D'));
     $points = $metric->points()->whereRaw('created_at BETWEEN (created_at - interval \'1 week\') AND now()')->whereRaw('to_char(created_at, \'YYYYMMDD\') = \'' . $dateTime->format('Ymd') . '\'')->groupBy(DB::raw('to_char(created_at, \'YYYYMMDD\')'));
     if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
         $value = $points->sum('value');
     } elseif ($metric->calc_type == Metric::CALC_AVG) {
         $value = $points->avg('value');
     }
     if ($value === 0 && $metric->default_value != $value) {
         return $metric->default_value;
     }
     return round($value, $metric->places);
 }
Ejemplo n.º 4
0
 /**
  * Get all metric points.
  *
  * @param \CachetHQ\Cachet\Models\Metric $metric
  *
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public function getMetricPoints(Metric $metric)
 {
     $points = $metric->points()->paginate(Binput::get('per_page', 20));
     return $this->paginator($points, Request::instance());
 }
Ejemplo n.º 5
0
 /**
  * Returns metrics for the week.
  *
  * @param \CachetHQ\Cachet\Models\Metric $metric
  *
  * @return int
  */
 protected function getPointsForDayInWeek(Metric $metric, $day)
 {
     $dateTime = (new Date())->setTimezone($this->dateTimeZone);
     $dateTime->sub(new DateInterval('P' . $day . 'D'));
     if (Config::get('database.default') === 'mysql') {
         $points = $metric->points()->whereRaw('created_at BETWEEN DATE_SUB(created_at, INTERVAL 1 WEEK) AND NOW()')->whereRaw('DATE_FORMAT(created_at, "%Y%m%d") = ' . $dateTime->format('Ymd'))->groupBy(DB::raw('DATE_FORMAT(created_at, "%Y%m%d")'));
         if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
             $value = $points->sum('value');
         } elseif ($metric->calc_type == Metric::CALC_AVG) {
             $value = $points->avg('value');
         }
     } else {
         // Default metrics calculations.
         if (!isset($metric->calc_type) || $metric->calc_type == Metric::CALC_SUM) {
             $queryType = 'sum(metric_points.value)';
         } elseif ($metric->calc_type == Metric::CALC_AVG) {
             $queryType = 'avg(metric_points.value)';
         } else {
             $queryType = 'sum(metric_points.value)';
         }
         $query = DB::select("select {$queryType} as aggregate FROM metrics JOIN metric_points ON metric_points.metric_id = metrics.id WHERE metric_points.metric_id = {$metric->id} AND to_char(metric_points.created_at, 'YYYYMMDD') = :timestamp GROUP BY to_char(metric_points.created_at, 'YYYYMMDD')", ['timestamp' => $hourInterval]);
         if (isset($query[0])) {
             $value = $query[0]->aggregate;
         } else {
             $value = 0;
         }
     }
     if ($value === 0 && $metric->default_value != $value) {
         return $metric->default_value;
     }
     return round($value, $metric->places);
 }