Statistic.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2009 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/database.php');
  16. require_once($install_path . '/data/Server.php');
  17. class Statistic {
  18. /*
  19. * returns an array counting appareances of a given field and his corresponding bargraph size
  20. * @param string $table table name to be queried
  21. * @param string $field field name to count
  22. * @param integer $limit limit of the query
  23. * @param string $constraint username or artistname depending on field
  24. * @param integer $maxwidth bargraph max width (to express visually the number of plays)
  25. * inaccurate @param integer $sizes quantity of possible sizes
  26. * inaccurate @param float $max_font_size maximum font size (px, em, %, etc)
  27. * @return array playstats
  28. */
  29. static function generatePlayStats($table, $field, $limit = 40, $constraint = null, $maxwidth = 100) {
  30. global $adodb;
  31. if (!is_string($field) || !is_string($table) || !is_integer($limit)) {
  32. return false;
  33. }
  34. $query = 'SELECT ' . $field . ', count(*) AS count FROM ' . $table;
  35. $query .= (!is_null($constraint)) ? ' WHERE ' : null;
  36. if ($field == 'track') {
  37. $query .= (!is_null($constraint)) ? ' artist = ' . $adodb->qstr($constraint) : null;
  38. } else {
  39. $query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
  40. }
  41. $query .= ' GROUP BY ' . $field . ' ORDER BY count DESC LIMIT ' . $limit;
  42. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  43. try {
  44. $res = $adodb->GetAll($query);
  45. } catch (Exception $e) {
  46. echo('ERROR' . $e->getMessage());
  47. }
  48. if (!$res) {
  49. return false;
  50. } else {
  51. $max = $res[0]['count'];
  52. foreach ($res as &$i){
  53. $i['pageurl'] = Server::getArtistURL($i['artist']);
  54. $i['size'] = round($i['count'] / $max * $maxwidth);
  55. }
  56. return $res;
  57. }
  58. }
  59. static function generatePlayByDays($table, $limit = 100, $constraint = null, $maxwidth = 100) {
  60. global $adodb;
  61. global $connect_string;
  62. if (!is_string($table) || !is_integer($limit)) {
  63. return false;
  64. }
  65. /*
  66. * todo: completly remove this dirty db type check.
  67. */
  68. $query = 'SELECT COUNT(*) as count, DATE(TO_TIMESTAMP(time)) as date FROM ' . $table;
  69. if (strpos($connect_string, 'mysql') === 0) {
  70. $query = 'SELECT COUNT(*) as count,DATE(FROM_UNIXTIME(time)) as date FROM ' . $table;
  71. }
  72. $query .= (!is_null($constraint)) ? ' WHERE ' : null;
  73. $query .= (!is_null($constraint)) ? ' userid = ' . ($constraint) : null;
  74. // Ignore timestamps older than $limit*3 days, this speeds up the query a lot
  75. // but will not return $limit data points if the user hasnt scrobbled at least once/day
  76. // in $limit of those $limit*3 days
  77. $query .= ' AND time > ' . (time() - (60 * 60 * 24 * ($limit * 3)));
  78. $query .= ' GROUP BY date ORDER BY date DESC LIMIT ' . $limit;
  79. $adodb->SetFetchMode(ADODB_FETCH_ASSOC);
  80. try {
  81. $res = $adodb->GetAll($query);
  82. } catch (Exception $e) {
  83. echo('ERROR' . $e->getMessage());
  84. }
  85. if (!$res) {
  86. return false;
  87. } else {
  88. $max = 0;
  89. foreach ($res as &$i){
  90. if ($i['count'] > $max) {
  91. $max = $i['count'];
  92. }
  93. }
  94. return $res;
  95. }
  96. }
  97. }