1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data Analytics |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the LICENSE.md file. |
7
|
|
|
* |
8
|
|
|
* @author Marcel Scherello <[email protected]> |
9
|
|
|
* @copyright 2019 Marcel Scherello |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Analytics\Db; |
13
|
|
|
|
14
|
|
|
use OCP\IDbConnection; |
15
|
|
|
use OCP\IL10N; |
16
|
|
|
use OCP\ILogger; |
17
|
|
|
|
18
|
|
|
class DataMapper |
19
|
|
|
{ |
20
|
|
|
private $userId; |
21
|
|
|
private $l10n; |
22
|
|
|
private $db; |
23
|
|
|
private $logger; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
$userId, |
27
|
|
|
IL10N $l10n, |
28
|
|
|
IDbConnection $db, |
29
|
|
|
ILogger $logger |
30
|
|
|
) |
31
|
|
|
{ |
32
|
|
|
$this->userId = $userId; |
33
|
|
|
$this->l10n = $l10n; |
34
|
|
|
$this->db = $db; |
35
|
|
|
$this->logger = $logger; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get file id for single track |
40
|
|
|
* @param int $dataset |
41
|
|
|
* @param $objectDrilldown |
42
|
|
|
* @param $dateDrilldown |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getData(int $dataset, $objectDrilldown = null, $dateDrilldown = null) |
46
|
|
|
{ |
47
|
|
|
$SQL = 'SELECT'; |
48
|
|
|
if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,'; |
49
|
|
|
if ($dateDrilldown === 'true') $SQL .= ' `dimension2`,'; |
50
|
|
|
$SQL .= ' SUM(`dimension3`) AS `dimension3`'; |
51
|
|
|
$SQL .= ' FROM `*PREFIX*analytics_facts` |
52
|
|
|
WHERE `dataset` = ? |
53
|
|
|
GROUP BY `dataset`'; |
54
|
|
|
if ($objectDrilldown === 'true') $SQL .= ', `dimension1`'; |
55
|
|
|
if ($dateDrilldown === 'true') $SQL .= ', `dimension2`'; |
56
|
|
|
$SQL .= ' ORDER BY'; |
57
|
|
|
if ($objectDrilldown === 'true') $SQL .= ' `dimension1`,'; |
58
|
|
|
$SQL .= ' `dimension2` ASC'; |
59
|
|
|
|
60
|
|
|
//$this->logger->error($SQL); |
61
|
|
|
|
62
|
|
|
$stmt = $this->db->prepare($SQL); |
63
|
|
|
$stmt->execute(array($dataset)); |
64
|
|
|
return $stmt->fetchAll(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* delete data |
69
|
|
|
*/ |
70
|
|
|
public function deleteData(int $datasetId, $dimension1, $dimension2) |
71
|
|
|
{ |
72
|
|
|
$SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?'; |
73
|
|
|
$stmt = $this->db->prepare($SQL); |
74
|
|
|
$stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2)); |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* delete all data of a dataset |
80
|
|
|
*/ |
81
|
|
|
public function deleteDataByDataset(int $datasetId) |
82
|
|
|
{ |
83
|
|
|
$SQL = 'DELETE FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ?'; |
84
|
|
|
$stmt = $this->db->prepare($SQL); |
85
|
|
|
$stmt->execute(array($this->userId, $datasetId)); |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* create data |
91
|
|
|
* @param int $datasetId |
92
|
|
|
* @param $dimension1 |
93
|
|
|
* @param $dimension2 |
94
|
|
|
* @param $dimension3 |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function createData(int $datasetId, $dimension1, $dimension2, $dimension3) |
98
|
|
|
{ |
99
|
|
|
$SQL = 'SELECT `id` FROM `*PREFIX*analytics_facts` WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?'; |
100
|
|
|
$stmt = $this->db->prepare($SQL); |
101
|
|
|
$stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2)); |
102
|
|
|
$row = $stmt->fetch(); |
103
|
|
|
if ($row) { |
104
|
|
|
$SQL = 'UPDATE `*PREFIX*analytics_facts` SET `dimension3` = ? WHERE `user_id` = ? AND `dataset` = ? AND `dimension1` = ? AND `dimension2` = ?'; |
105
|
|
|
$stmt = $this->db->prepare($SQL); |
106
|
|
|
$stmt->execute(array($dimension3, $this->userId, $datasetId, $dimension1, $dimension2)); |
107
|
|
|
//$stmt->fetch(); |
108
|
|
|
return 'update'; |
109
|
|
|
} else { |
110
|
|
|
$SQL = 'INSERT INTO `*PREFIX*analytics_facts` (`user_id`,`dataset`,`dimension1`,`dimension2`,`dimension3`) VALUES(?,?,?,?,?)'; |
111
|
|
|
$stmt = $this->db->prepare($SQL); |
112
|
|
|
$stmt->execute(array($this->userId, $datasetId, $dimension1, $dimension2, $dimension3)); |
113
|
|
|
return 'insert'; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |