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