1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Analytics |
4
|
|
|
* |
5
|
|
|
* SPDX-FileCopyrightText: 2019-2022 Marcel Scherello |
6
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace OCA\Analytics\Db; |
10
|
|
|
|
11
|
|
|
use OCP\DB\Exception; |
|
|
|
|
12
|
|
|
use OCP\IDBConnection; |
|
|
|
|
13
|
|
|
use OCP\IL10N; |
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
15
|
|
|
|
16
|
|
|
class DatasetMapper { |
17
|
|
|
private $l10n; |
18
|
|
|
private $userId; |
19
|
|
|
private $db; |
20
|
|
|
private $logger; |
21
|
|
|
const TABLE_NAME = 'analytics_dataset'; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
$userId, |
25
|
|
|
IL10N $l10n, |
26
|
|
|
IDBConnection $db, |
27
|
|
|
LoggerInterface $logger |
28
|
|
|
) { |
29
|
|
|
$this->userId = $userId; |
30
|
|
|
$this->l10n = $l10n; |
31
|
|
|
$this->db = $db; |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* get datasets |
37
|
|
|
* @return array |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function index(): array { |
41
|
|
|
$sql = $this->db->getQueryBuilder(); |
42
|
|
|
$sql->from(self::TABLE_NAME)->select('id')->addSelect('name')->addSelect('dimension1')->addSelect('dimension2') |
43
|
|
|
->addSelect('value')->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId))) |
44
|
|
|
->andWhere($sql->expr()->eq('type', $sql->createNamedParameter('2')))->addOrderBy('name', 'ASC'); |
45
|
|
|
$statement = $sql->executeQuery(); |
46
|
|
|
$result = $statement->fetchAll(); |
47
|
|
|
$statement->closeCursor(); |
48
|
|
|
return $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* get datasets |
53
|
|
|
* @param $userId |
54
|
|
|
* @return array |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
public function indexByUser($userId): array { |
58
|
|
|
$sql = $this->db->getQueryBuilder(); |
59
|
|
|
$sql->from(self::TABLE_NAME)->select('id')->where($sql->expr() |
60
|
|
|
->eq('user_id', $sql->createNamedParameter($userId))); |
61
|
|
|
$statement = $sql->executeQuery(); |
62
|
|
|
$result = $statement->fetchAll(); |
63
|
|
|
$statement->closeCursor(); |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* create dataset |
69
|
|
|
* @param $name |
70
|
|
|
* @param $dimension1 |
71
|
|
|
* @param $dimension2 |
72
|
|
|
* @param $value |
73
|
|
|
* @return int |
74
|
|
|
* @throws Exception |
75
|
|
|
*/ |
76
|
|
|
public function create($name, $dimension1, $dimension2, $value): int { |
77
|
|
|
$sql = $this->db->getQueryBuilder(); |
78
|
|
|
$sql->insert(self::TABLE_NAME)->values([ |
79
|
|
|
'user_id' => $sql->createNamedParameter($this->userId), |
80
|
|
|
'name' => $sql->createNamedParameter($name), |
81
|
|
|
'dimension1' => $sql->createNamedParameter($dimension1), |
82
|
|
|
'dimension2' => $sql->createNamedParameter($dimension2), |
83
|
|
|
'value' => $sql->createNamedParameter($value), |
84
|
|
|
'type' => $sql->createNamedParameter('2'), |
85
|
|
|
'ai_index' => $sql->createNamedParameter('0'), |
86
|
|
|
]); |
87
|
|
|
$sql->executeStatement(); |
88
|
|
|
return $sql->getLastInsertId(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* get single dataset |
93
|
|
|
* @param int $id |
94
|
|
|
* @return array|bool |
95
|
|
|
* @throws Exception |
96
|
|
|
*/ |
97
|
|
|
public function readOwn(int $id) { |
98
|
|
|
$sql = $this->db->getQueryBuilder(); |
99
|
|
|
$sql->from(self::TABLE_NAME)->select('*')->where($sql->expr()->eq('id', $sql->createNamedParameter($id))) |
100
|
|
|
->andWhere($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))->orderBy('parent', 'ASC') |
101
|
|
|
->addOrderBy('name', 'ASC'); |
102
|
|
|
$statement = $sql->executeQuery(); |
103
|
|
|
$result = $statement->fetch(); |
104
|
|
|
$statement->closeCursor(); |
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* get single dataset |
110
|
|
|
* @param int $id |
111
|
|
|
* @return array|bool |
112
|
|
|
* @throws Exception |
113
|
|
|
*/ |
114
|
|
|
public function read(int $id) { |
115
|
|
|
$sql = $this->db->getQueryBuilder(); |
116
|
|
|
$sql->from(self::TABLE_NAME)->select('*')->where($sql->expr()->eq('id', $sql->createNamedParameter($id))) |
117
|
|
|
->orderBy('parent', 'ASC')->addOrderBy('name', 'ASC'); |
118
|
|
|
$statement = $sql->executeQuery(); |
119
|
|
|
$result = $statement->fetch(); |
120
|
|
|
$statement->closeCursor(); |
121
|
|
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* update dataset |
126
|
|
|
* @param $id |
127
|
|
|
* @param $namefl |
128
|
|
|
* @param $dimension1 |
129
|
|
|
* @param $dimension2 |
130
|
|
|
* @param $value |
131
|
|
|
* @param $subheader |
132
|
|
|
* @param $aiIndex |
133
|
|
|
* @return bool |
134
|
|
|
* @throws Exception |
135
|
|
|
*/ |
136
|
|
|
public function update($id, $name, $subheader, $dimension1, $dimension2, $value, $aiIndex): bool { |
137
|
|
|
$name = $this->truncate($name, 64); |
138
|
|
|
$sql = $this->db->getQueryBuilder(); |
139
|
|
|
$sql->update(self::TABLE_NAME)->set('name', $sql->createNamedParameter($name)) |
140
|
|
|
->set('subheader', $sql->createNamedParameter($subheader)) |
141
|
|
|
->set('dimension1', $sql->createNamedParameter($dimension1)) |
142
|
|
|
->set('dimension2', $sql->createNamedParameter($dimension2)) |
143
|
|
|
->set('value', $sql->createNamedParameter($value)) |
144
|
|
|
->set('ai_index', $sql->createNamedParameter($aiIndex)) |
145
|
|
|
->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))->andWhere($sql->expr() |
146
|
|
|
->eq('id', $sql->createNamedParameter($id))); |
147
|
|
|
$sql->executeStatement(); |
148
|
|
|
return true; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* delete dataset |
153
|
|
|
* @param $id |
154
|
|
|
* @return bool |
155
|
|
|
* @throws Exception |
156
|
|
|
*/ |
157
|
|
|
public function delete($id): bool { |
158
|
|
|
$sql = $this->db->getQueryBuilder(); |
159
|
|
|
$sql->delete(self::TABLE_NAME)->where($sql->expr()->eq('id', $sql->createNamedParameter($id))); |
160
|
|
|
$sql->executeStatement(); |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* get the newest timestamp of the data of a dataset |
166
|
|
|
* @param $datasetId |
167
|
|
|
* @return int |
168
|
|
|
* @throws Exception |
169
|
|
|
*/ |
170
|
|
|
public function getLastUpdate($datasetId): int { |
171
|
|
|
$sql = $this->db->getQueryBuilder(); |
172
|
|
|
$sql->from('analytics_facts')->select($sql->func()->max('timestamp'))->where($sql->expr() |
173
|
|
|
->eq('dataset', $sql->createNamedParameter($datasetId))); |
174
|
|
|
return (int)$sql->executeQuery()->fetchOne(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* get the report owner |
179
|
|
|
* @param $datasetId |
180
|
|
|
* @return string |
181
|
|
|
* @throws Exception |
182
|
|
|
*/ |
183
|
|
|
public function getOwner($datasetId): string { |
184
|
|
|
$sql = $this->db->getQueryBuilder(); |
185
|
|
|
$sql->from(self::TABLE_NAME)->select('user_id')->where($sql->expr() |
186
|
|
|
->eq('id', $sql->createNamedParameter($datasetId))); |
187
|
|
|
return (string)$sql->executeQuery()->fetchOne(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* truncates fields do DB-field size |
192
|
|
|
* |
193
|
|
|
* @param $string |
194
|
|
|
* @param int $length |
195
|
|
|
* @param string $dots |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
private function truncate($string, int $length, string $dots = "..."): string { |
199
|
|
|
return (strlen($string) > $length) ? mb_strcut($string, 0, $length - strlen($dots)) . $dots : $string; |
200
|
|
|
} |
201
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths