PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/db.class.php

https://github.com/bisontim/oc-apps_storage-charts
PHP | 290 lines | 202 code | 25 blank | 63 comment | 26 complexity | c3a7a2aa9b9920f6415794fbc928de5c MD5 | raw file
  1. <?php
  2. /**
  3. * ownCloud - DjazzLab Storage Charts plugin
  4. *
  5. * @author Xavier Beurois
  6. * @copyright 2012 Xavier Beurois www.djazz-lab.net
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This class manages storage_charts.
  24. */
  25. class OC_DLStCharts {
  26. /**
  27. * UPDATE day use for a user
  28. * @param $used user used space
  29. * @param $total total users used space
  30. */
  31. public static function update($used, $total){
  32. $query = OCP\DB::prepare("SELECT stc_id FROM *PREFIX*dlstcharts WHERE oc_uid = ? AND stc_dayts = ?");
  33. $result = $query->execute(Array(OCP\User::getUser(), mktime(0,0,0)))->fetchRow();
  34. if($result){
  35. $query = OCP\DB::prepare("UPDATE *PREFIX*dlstcharts SET stc_used = ?, stc_total = ? WHERE stc_id = ?");
  36. $query->execute(Array($used, $total, $result['stc_id']));
  37. }else{
  38. $query = OCP\DB::prepare("INSERT INTO *PREFIX*dlstcharts (oc_uid,stc_month,stc_dayts,stc_used,stc_total) VALUES (?,?,?,?,?)");
  39. $query->execute(Array(OCP\User::getUser(), date('Ym'), mktime(0,0,0), $used, $total));
  40. }
  41. }
  42. /**
  43. * Get the size of the data folder
  44. * @param $path path to the folder you want to calculate the total size
  45. */
  46. public static function getTotalDataSize($path){
  47. if(is_file($path)){
  48. $path = dirname($path);
  49. }
  50. $path = str_replace('//', '/', $path);
  51. if(is_dir($path) and strcmp(substr($path, -1), '/') != 0){
  52. $path .= '/';
  53. }
  54. $size = 0;
  55. if($dh = opendir($path)){
  56. while(($filename = readdir($dh)) !== false) {
  57. if(strcmp($filename, '.') != 0 and strcmp($filename, '..') != 0){
  58. $subFile = $path . '/' . $filename;
  59. if(is_file($subFile)){
  60. $size += filesize($subFile);
  61. }else{
  62. $size += self::getTotalDataSize($subFile);
  63. }
  64. }
  65. }
  66. }
  67. return $size;
  68. }
  69. /**
  70. * Get data to build the pie about the Free-Used space ratio
  71. */
  72. public static function getPieFreeUsedSpaceRatio(){
  73. if(OC_Group::inGroup(OCP\User::getUser(), 'admin')){
  74. $query = OCP\DB::prepare("SELECT stc_id, stc_dayts, oc_uid FROM (SELECT * FROM *PREFIX*dlstcharts ORDER BY stc_dayts DESC) last GROUP BY oc_uid");
  75. $results = $query->execute()->fetchAll();
  76. }else{
  77. $query = OCP\DB::prepare("SELECT stc_id, MAX(stc_dayts) as stc_dayts FROM *PREFIX*dlstcharts WHERE oc_uid = ?");
  78. $results = $query->execute(Array(OCP\User::getUser()))->fetchAll();
  79. }
  80. $return = Array();
  81. foreach($results as $result){
  82. $query = OCP\DB::prepare("SELECT oc_uid, stc_used, stc_total FROM *PREFIX*dlstcharts WHERE stc_id = ?");
  83. $return[] = $query->execute(Array($result['stc_id']))->fetchAll();
  84. }
  85. return $return;
  86. }
  87. /**
  88. * Get data to build the line chart about last 7 days used space evolution
  89. */
  90. public static function getUsedSpaceOverTime($time){
  91. $return = Array();
  92. if(OC_Group::inGroup(OCP\User::getUser(), 'admin')){
  93. foreach(OCP\User::getUsers() as $user){
  94. if(strcmp($time, 'daily') == 0){
  95. $return[$user] = self::getDataByUserToLineChart($user);
  96. }else{
  97. $return[$user] = self::getDataByUserToHistoChart($user);
  98. }
  99. }
  100. }else{
  101. if(strcmp($time, 'daily') == 0){
  102. $return[OCP\User::getUser()] = self::getDataByUserToLineChart(OCP\User::getUser());
  103. }else{
  104. $return[OCP\User::getUser()] = self::getDataByUserToHistoChart(OCP\User::getUser());
  105. }
  106. }
  107. return $return;
  108. }
  109. /**
  110. * Get configuration values stored in the database
  111. * @param $key The conf key
  112. * @return Array The conf value
  113. */
  114. public static function getUConfValue($key, $default = NULL){
  115. $query = OCP\DB::prepare("SELECT uc_id,uc_val FROM *PREFIX*dlstcharts_uconf WHERE oc_uid = ? AND uc_key = ?");
  116. $result = $query->execute(Array(OCP\User::getUser(), $key))->fetchRow();
  117. if($result){
  118. return $result;
  119. }
  120. return $default;
  121. }
  122. /**
  123. * Set configuration values stored in the database
  124. * @param $key The conf key
  125. * @param $val The conf value
  126. */
  127. public static function setUConfValue($key,$val){
  128. $conf = self::getUConfValue($key);
  129. if(!is_null($conf)){
  130. $query = OCP\DB::prepare("UPDATE *PREFIX*dlstcharts_uconf SET uc_val = ? WHERE uc_id = ?");
  131. $query->execute(Array($val, $conf['uc_id']));
  132. }else{
  133. $query = OCP\DB::prepare("INSERT INTO *PREFIX*dlstcharts_uconf (oc_uid,uc_key,uc_val) VALUES (?,?,?)");
  134. $query->execute(Array(OCP\User::getUser(), $key, $val));
  135. }
  136. }
  137. /**
  138. * Parse an array and return data in the highCharts format
  139. * @param $operation operation to do
  140. * @param $elements elements to parse
  141. */
  142. public static function arrayParser($operation, $elements, $l, $data_sep = ',', $ck = 'hu_size'){
  143. $return = "";
  144. switch($operation){
  145. case 'pie':
  146. $free = $total = 0;
  147. foreach($elements as $element){
  148. $element = $element[0];
  149. $total = $element['stc_total'];
  150. $free += $element['stc_used'];
  151. $return .= "['" . $element['oc_uid'] . "', " . $element['stc_used'] . "],";
  152. }
  153. $return .= "['" . $l->t('Free space') . "', " . ($total - $free) . "]";
  154. break;
  155. case 'histo':
  156. case 'line':
  157. $conf = self::getUConfValue($ck, Array('uc_val' => 3));
  158. $div = 1;
  159. switch($conf['uc_val']){
  160. case 4:
  161. $div = 1024;
  162. case 3:
  163. $div *= 1024;
  164. case 2:
  165. $div *= 1024;
  166. case 1:
  167. $div *= 1024;
  168. }
  169. foreach($elements as $user => $data){
  170. $return_tmp = '{"name":"' . $user . '","data":[';
  171. foreach($data as $number){
  172. $return_tmp .= round($number/$div, 2) . ",";
  173. }
  174. $return_tmp = substr($return_tmp, 0, -1) . "]}";
  175. $return .= $return_tmp . $data_sep;
  176. }
  177. $return = substr($return, 0, -(strlen($data_sep)));
  178. break;
  179. }
  180. return $return;
  181. }
  182. /**
  183. * Get data by user for Seven Days Line Chart
  184. * @param $user the user
  185. * @return Array
  186. */
  187. private static function getDataByUserToLineChart($user){
  188. $dates = Array(
  189. mktime(0,0,0,date('m'),date('d')-6),
  190. mktime(0,0,0,date('m'),date('d')-5),
  191. mktime(0,0,0,date('m'),date('d')-4),
  192. mktime(0,0,0,date('m'),date('d')-3),
  193. mktime(0,0,0,date('m'),date('d')-2),
  194. mktime(0,0,0,date('m'),date('d')-1),
  195. mktime(0,0,0,date('m'),date('d'))
  196. );
  197. $return = Array();
  198. foreach($dates as $kd => $date){
  199. $query = OCP\DB::prepare("SELECT stc_used FROM *PREFIX*dlstcharts WHERE oc_uid = ? AND stc_dayts = ?");
  200. $result = $query->execute(Array($user, $date))->fetchAll();
  201. if(count($result) > 0){
  202. $return[] = $result[0]['stc_used'];
  203. }else{
  204. if($kd == 0){
  205. $query = OCP\DB::prepare("SELECT stc_used FROM *PREFIX*dlstcharts WHERE oc_uid = ? AND stc_dayts < ? ORDER BY stc_dayts DESC");
  206. $result = $query->execute(Array($user, $date))->fetchAll();
  207. if(count($result) > 0){
  208. $return[] = $result[0]['stc_used'];
  209. }else{
  210. $return[] = 0;
  211. }
  212. }else{
  213. $return[] = 0;
  214. }
  215. }
  216. }
  217. $last = 0;
  218. foreach ($return as $key => $value) {
  219. if($value == 0){
  220. $return[$key] = $last;
  221. }
  222. $last = $return[$key];
  223. }
  224. return $return;
  225. }
  226. /**
  227. * Get data by users for monthly evolution
  228. * @param $user The user
  229. * @return Array
  230. */
  231. private static function getDataByUserToHistoChart($user){
  232. $months = Array(
  233. date('Ym',mktime(0,0,0,date('m')-11)),
  234. date('Ym',mktime(0,0,0,date('m')-10)),
  235. date('Ym',mktime(0,0,0,date('m')-9)),
  236. date('Ym',mktime(0,0,0,date('m')-8)),
  237. date('Ym',mktime(0,0,0,date('m')-7)),
  238. date('Ym',mktime(0,0,0,date('m')-6)),
  239. date('Ym',mktime(0,0,0,date('m')-5)),
  240. date('Ym',mktime(0,0,0,date('m')-4)),
  241. date('Ym',mktime(0,0,0,date('m')-3)),
  242. date('Ym',mktime(0,0,0,date('m')-2)),
  243. date('Ym',mktime(0,0,0,date('m')-1)),
  244. date('Ym',mktime(0,0,0,date('m')))
  245. );
  246. $return = Array();
  247. foreach($months as $km => $month){
  248. $query = OCP\DB::prepare("SELECT AVG(stc_used) as stc_used FROM *PREFIX*dlstcharts WHERE oc_uid = ? AND stc_month = ?");
  249. $result = $query->execute(Array($user, $month))->fetchAll();
  250. if(count($result) > 0){
  251. $return[] = $result[0]['stc_used'];
  252. }else{
  253. $return[] = 0;
  254. }
  255. }
  256. $last = 0;
  257. foreach ($return as $key => $value) {
  258. if($value == 0){
  259. $return[$key] = $last;
  260. }
  261. $last = $return[$key];
  262. }
  263. return $return;
  264. }
  265. }