-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
200 lines (182 loc) · 6.15 KB
/
database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Created by PhpStorm.
* User: matti
* Date: 2018-03-01
* Time: 14:24
*/
error_reporting(-1);
require 'interfaces/DataStorage.php';
require_once 'Entities/Coin.php';
require_once 'Entities/Holding.php';
require_once 'dbconfig.php';
class database implements \interfaces\DataStorage
{
private $dblink;
public function __construct($dbconfig)
{
$this->dblink = new mysqli($dbconfig["hostname"],$dbconfig["user"],$dbconfig["pass"],$dbconfig["dbname"]);
}
/***
* @return array Array of holdings
*/
public function GetHoldings()
{
$query ="SELECT * FROM `holding` ";
$res = $this->dblink->query($query);
$result = array();
while($row = mysqli_fetch_assoc($res))
{
$result[] = new \Entities\Holding($this->GetCoin($row["coinid"]),$row["dateAdded"],$row["amount"]);
}
return $result;
}
/***
* @param Entities\Coin $coin The coin that with the new value
* @param $usdvalue The new usd value
*/
public function AddHourPriceData($coin, $usdvalue)
{
$coinid = $coin->GetId();
$query = "INSERT INTO `hourly` (`coinid`,`usdvalue`) VALUES ($coinid,$usdvalue)";
$this->dblink->query($query);
}
/***
* @param $coin
* @param $usdvalue
* @param $day
*/
public function AddDailyPriceData( $coin, $usdvalue,$day)
{
$coinid = $coin->GetId();
$query = "INSERT INTO `daily` (`coinid`,`usdvalue`,`dateAdded`) VALUES ($coinid,$usdvalue,'$day')";
$this->dblink->query($query);
}
/***
* @param $startTimestamp
* @param $endTimestamp
* @param $holdings
* @param $interval
* @return array
*/
private function GetPriceData($startTimestamp, $endTimestamp, $interval)
{
$query = "SELECT SUM(`usdvalue`),`dateAdded` FROM `" . $interval . "` WHERE `dateAdded` > FROM_UNIXTIME($startTimestamp) AND `dateAdded` < FROM_UNIXTIME($endTimestamp) GROUP BY `dateAdded` ORDER BY `dateAdded`";
$res = $this->dblink->query($query);
while($row = mysqli_fetch_array($res))
{
$holdingDataForChart[] = array(
"date" => $row[1],
"value" => intval($row[0]));
}
echo mysqli_error($this->dblink);
return $holdingDataForChart;
}
/***
* @param $startTimeStamp
* @param $endTimestamp
* @param $holdings
* @return array
*/
public function GetDailyPriceData( $startTimeStamp, $endTimestamp)
{
return $this->GetPriceData($startTimeStamp,$endTimestamp,"daily");
}
public function GetDailyPriceDataByCoin($starttime,$endtime)
{
$coins = $this->GetCoins();
$completeArray = array();
foreach($coins as $coin)
{
$coinid = $coin->GetId();
$query = "SELECT * FROM `daily` WHERE `dateAdded` > FROM_UNIXTIME($starttime) AND `dateAdded` < FROM_UNIXTIME($endtime) AND `coinid` = $coinid ORDER BY `dateAdded`"; // TODO: Run that query for all coins in holdings
$res = $this->dblink->query($query);
$coinarray = array();
while($row = mysqli_fetch_assoc($res))
{
$coinarray[] = intval($row["usdvalue"]);
}
$dataarray = array(
"name" => $coin->GetName(),
"data" => $coinarray
);
$completeArray[] = $dataarray;
}
return $completeArray;
}
public function GetDropLabels($start,$end)
{
$dailydata = $this->GetDailyPriceData($start,$end);
$index = 0;
$labels =array();
foreach($dailydata as $day)
{
// var_dump($day);
$currentdate = $day["date"];
$query = "SELECT description from holding WHERE date(dateAdded) = DATE('$currentdate')";
$res = $this->dblink->query($query);
while($row = mysqli_fetch_assoc($res))
{
$labels[] = array("point"=> array(
"x" => $index, "y" => $day["value"],"xAxis" =>0, "yAxis" => 0)
, "text" =>$row["description"]);
}
$index = $index +1;
}
return $labels;
}
/***
* @return array Return all coins in the DB
*/
public function GetCoins()
{
$res = $this->dblink->query("SELECT * FROM coin");
$coinarray = array();
while($row = mysqli_fetch_assoc($res))
{
$coinarray[] = new Entities\Coin($row["id"],$row["name"],$row["coinmarketticker"],$row["image"]);
}
return $coinarray;
}
/**
* @param $id Id of the coin
* @return \Entities\Coin the coin requested
*/
public function GetCoin($id)
{
$res = $this->dblink->query("SELECT * FROM coin WHERE id = $id");
$coinarray = array();
$row = mysqli_fetch_assoc($res);
return new Entities\Coin($row["id"],$row["name"],$row["coinmarketticker"],$row["image"]);
}
/***
* @param $coinid
* @return int Amount held of certain coin
*/
public function GetAmountsOfCertainCoin($coinid) : int
{
$res = $this->dblink->query("SELECT SUM(`amount`) FROM holding WHERE coinid = $coinid");
$amount = mysqli_fetch_array($res)[0];
return intval($amount);
}
/***
* @param $coins An array of Coins that should be updated.
*/
public function UpdateDaily($coins)
{
foreach($coins as $coin)
{
$query = "SELECT AVG(`usdvalue`) FROM `hourly` WHERE DATE(`dateAdded`) = SUBDATE(CURDATE(),1) AND `coinid` = ".$coin->GetId();
$res = $this->dblink->query($query);
$value = mysqli_fetch_array($res)[0];
$yesterday = date('Y-m-d',strtotime("-1 days"));
$this->AddDailyPriceData($coin,$value,$yesterday);
}
}
public function GetLatestValueOfCoin($coinid)
{
$res = $this->dblink->query("SELECT `usdvalue` FROM `hourly` WHERE `coinid` = $coinid ORDER BY `dateAdded` DESC LIMIT 1");
$row = mysqli_fetch_assoc($res);
return $row["usdvalue"];
}
}