1 | <?php |
---|
2 | class DB{ |
---|
3 | |
---|
4 | private $dbhost; |
---|
5 | private $dbname; |
---|
6 | private $dbuser; |
---|
7 | private $dbpass; |
---|
8 | private $alias; |
---|
9 | public $dbconn; |
---|
10 | |
---|
11 | function DB(){ |
---|
12 | require_once('config.php'); |
---|
13 | global $dbhost,$dbname,$dbpass,$dbuser; |
---|
14 | |
---|
15 | $this->dbhost=$dbhost; |
---|
16 | $this->dbname=$dbname; |
---|
17 | $this->dbpass=$dbpass; |
---|
18 | $this->dbuser=$dbuser; |
---|
19 | $this->alias=array(); |
---|
20 | } |
---|
21 | function connect(){ |
---|
22 | $this->dbconn=new mysqli($this->dbhost, $this->dbuser , $this->dbpass, $this->dbname); |
---|
23 | if ($this->dbconn->connect_error) { |
---|
24 | die('Connect Error:'. $this->dbconn->connect_error); |
---|
25 | } |
---|
26 | } |
---|
27 | function disconnect(){ |
---|
28 | $this->dbconn->close(); |
---|
29 | } |
---|
30 | function init_trans(){ |
---|
31 | $this->dbconn->autocommit(FALSE); |
---|
32 | $this->dbconn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); |
---|
33 | } |
---|
34 | function insert_data($user,$date){ |
---|
35 | $sql = "INSERT INTO clients (user,lastlogin) VALUES (?,?) ON DUPLICATE KEY UPDATE lastlogin = VALUES(lastlogin)"; |
---|
36 | $query=$this->dbconn->prepare($sql); |
---|
37 | if (! $query) |
---|
38 | throw new Exception($this->dbconn->error); |
---|
39 | $query->bind_param("ss",$user,$date); |
---|
40 | $query->execute(); |
---|
41 | if ( $query->affected_rows < 0 ) |
---|
42 | throw new Exception($query->mysqli_error()); |
---|
43 | $query->close(); |
---|
44 | } |
---|
45 | function update_data($data=''){ |
---|
46 | $sql="INSERT INTO packages (app,count) VALUES (?,?) ON DUPLICATE KEY UPDATE count = count + ?"; |
---|
47 | $query=$this->dbconn->prepare($sql); |
---|
48 | $query->bind_param("sss",$k,$v,$v); |
---|
49 | if (! $query) |
---|
50 | throw new Exception($this->dbconn->error); |
---|
51 | foreach ($data as $k => $v){ |
---|
52 | $noerr=$query->execute(); |
---|
53 | if (! $noerr){ |
---|
54 | throw new Exception($query->mysqli_error()); |
---|
55 | } |
---|
56 | } |
---|
57 | $query->close(); |
---|
58 | } |
---|
59 | private function load_alias(){ |
---|
60 | $sql="SELECT name,alias from alias"; |
---|
61 | $result=$this->dbconn->query($sql); |
---|
62 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
63 | $this->alias[$row['name']]=$row['alias']; |
---|
64 | } |
---|
65 | } |
---|
66 | function rotate(){ |
---|
67 | $sql="select max(fecha) from historico;"; |
---|
68 | $thismonth=intval(date("Ym")); |
---|
69 | $lastmonth=$thismonth; |
---|
70 | if ($result=$this->dbconn->query($sql)){ |
---|
71 | while($row=$result->fetch_row()){ |
---|
72 | if(empty($row[0])){ |
---|
73 | $sql="insert into historico(app,count,fecha) values (?,?,?)"; |
---|
74 | $query=$this->dbconn->prepare($sql); |
---|
75 | $name='dummy'; $value=0; $today=date("Y-m-d"); |
---|
76 | $query->bind_param('sds',$name,$value,$today); |
---|
77 | $query->execute(); |
---|
78 | $query->close(); |
---|
79 | }else{ |
---|
80 | $lastmonth=intval(explode('-',$row[0])[0].explode('-',$row[0])[1]); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | if ($thismonth > $lastmonth){ |
---|
85 | $sql="insert into historico(app,count,fecha) (select app,count,DATE(NOW()) as fecha from packages);"; |
---|
86 | $this->dbconn->query($sql); |
---|
87 | $sql="truncate packages;"; |
---|
88 | $this->dbconn->query($sql); |
---|
89 | } |
---|
90 | } |
---|
91 | function get_historic_data(){ |
---|
92 | $this->load_alias(); |
---|
93 | $today=date("Y-m-d"); |
---|
94 | $first_this_month=date("Y-m-").'01'; |
---|
95 | $last_one_month_ago=date("Y-m-d",strtotime("$first_this_month -1 days")); |
---|
96 | $first_one_month_ago=date("Y-m-d",strtotime("$first_this_month -1 months")); |
---|
97 | $last_two_month_ago=date("Y-m-d",strtotime("$first_one_month_ago -1 days")); |
---|
98 | $first_two_month_ago=date("Y-m-d",strtotime("$first_one_month_ago -1 months")); |
---|
99 | //echo $today.' '.$first_this_month.' '.$first_one_month_ago.' '.$last_one_month_ago.' '.$first_two_month_ago.' '.$last_two_month_ago.' EOL'; |
---|
100 | $obj=[]; |
---|
101 | $sql="SELECT app,count from packages UNION SELECT app,count from historico where fecha BETWEEN '$first_this_month' and '$today' order by count DESC LIMIT 30"; |
---|
102 | if ($result=$this->dbconn->query($sql)){ |
---|
103 | $obj2=[]; |
---|
104 | $nobj=0; |
---|
105 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
106 | if (array_key_exists($row['app'],$this->alias)){ |
---|
107 | if (! empty($this->alias[$row['app']])){ |
---|
108 | if ($nobj < 10) |
---|
109 | $obj2[$this->alias[$row['app']]]=$row['count']; |
---|
110 | $nobj++; |
---|
111 | } |
---|
112 | }else{ |
---|
113 | if ($nobj < 10) |
---|
114 | $obj2[$row['app']]=$row['count']; |
---|
115 | $nobj++; |
---|
116 | } |
---|
117 | } |
---|
118 | $obj[]=$obj2; |
---|
119 | } |
---|
120 | $sql="SELECT app,count,fecha from historico where fecha BETWEEN '$first_one_month_ago' and '$last_one_month_ago' order by count DESC LIMIT 30"; |
---|
121 | if ($result=$this->dbconn->query($sql)){ |
---|
122 | $obj2=[]; |
---|
123 | $nobj=0; |
---|
124 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
125 | if (array_key_exists($row['app'],$this->alias)){ |
---|
126 | if (! empty($this->alias[$row['app']])){ |
---|
127 | if ($nobj < 10) |
---|
128 | $obj2[$this->alias[$row['app']]]=$row['count']; |
---|
129 | $nobj++; |
---|
130 | } |
---|
131 | }else{ |
---|
132 | if ($nobj < 10) |
---|
133 | $obj2[$row['app']]=$row['count']; |
---|
134 | $nobj++; |
---|
135 | } |
---|
136 | } |
---|
137 | $obj[]=$obj2; |
---|
138 | } |
---|
139 | $sql="SELECT app,count,fecha from historico where fecha BETWEEN '$first_two_month_ago' and '$last_two_month_ago' order by count DESC LIMIT 30"; |
---|
140 | if ($result=$this->dbconn->query($sql)){ |
---|
141 | $obj2=[]; |
---|
142 | $nobj=0; |
---|
143 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
144 | if (array_key_exists($row['app'],$this->alias)){ |
---|
145 | if (! empty($this->alias[$row['app']])){ |
---|
146 | if ($nobj < 10) |
---|
147 | $obj2[$this->alias[$row['app']]]=$row['count']; |
---|
148 | $nobj++; |
---|
149 | } |
---|
150 | }else{ |
---|
151 | if ($nobj < 10) |
---|
152 | $obj2[$row['app']]=$row['count']; |
---|
153 | $nobj++; |
---|
154 | } |
---|
155 | } |
---|
156 | $obj[]=$obj2; |
---|
157 | } |
---|
158 | |
---|
159 | return json_encode($obj); |
---|
160 | |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | ?> |
---|