1
|
|
|
<?php namespace Arcanedev\LaravelMetrics\Expressions\TrendDateFormat; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelMetrics\Exceptions\InvalidTrendUnitException; |
4
|
|
|
use Arcanedev\LaravelMetrics\Metrics\Trend; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class PostgresExpression |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelMetrics\Expressions\TrendDateFormat |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class PostgresExpression extends Expression |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Main Methods |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the value of the expression. |
21
|
|
|
* |
22
|
|
|
* @return mixed |
23
|
|
|
*/ |
24
|
44 |
|
public function getValue() |
25
|
|
|
{ |
26
|
44 |
|
$interval = $this->interval(); |
27
|
|
|
|
28
|
44 |
|
switch ($this->unit) { |
29
|
33 |
|
case Trend::BY_MONTHS: |
30
|
8 |
|
return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM')"; |
31
|
|
|
|
32
|
27 |
|
case Trend::BY_WEEKS: |
33
|
8 |
|
return "to_char({$this->wrap($this->value)}{$interval}, 'IYYY-IW')"; |
34
|
|
|
|
35
|
21 |
|
case Trend::BY_DAYS: |
36
|
8 |
|
return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD')"; |
37
|
|
|
|
38
|
15 |
|
case Trend::BY_HOURS: |
39
|
8 |
|
return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD HH24:00')"; |
40
|
|
|
|
41
|
9 |
|
case Trend::BY_MINUTES: |
42
|
8 |
|
return "to_char({$this->wrap($this->value)}{$interval}, 'YYYY-MM-DD HH24:mi:00')"; |
43
|
|
|
|
44
|
|
|
default: |
45
|
4 |
|
throw InvalidTrendUnitException::make($this->unit); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/* ----------------------------------------------------------------- |
50
|
|
|
| Other Methods |
51
|
|
|
| ----------------------------------------------------------------- |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the interval. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
44 |
|
private function interval(): string |
60
|
|
|
{ |
61
|
44 |
|
$offset = $this->offset(); |
62
|
|
|
|
63
|
44 |
|
if ($offset > 0) |
64
|
20 |
|
return ' + interval \''.$offset.' hour\''; |
65
|
44 |
|
elseif ($offset === 0) |
66
|
44 |
|
return ''; |
67
|
|
|
|
68
|
20 |
|
return ' - interval \''.($offset * -1).' HOUR\''; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|