PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/models/msources.php

https://github.com/funkatron/twitter-stats-tracker
PHP | 109 lines | 56 code | 23 blank | 30 comment | 7 complexity | 076dcc6d11ae8025e0ff1061eb76956d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. error_reporting('on');
  3. class MSources extends Model {
  4. const CACHE_PATH = "/var/www/twittersource.info/ciapp_tss/cache/";
  5. const CACHE_DURATION = 3600;
  6. function MSources()
  7. {
  8. parent::Model();
  9. }
  10. /**
  11. * Retrieves source stats for a period of time
  12. *
  13. * @param string $date_start
  14. * @param int $num_days
  15. * @return array
  16. * @author Ed Finkler
  17. */
  18. public function getStatsForPeriod($date_start, $num_days, $nocache=null)
  19. {
  20. $date_start = strtotime($date_start);
  21. /*
  22. create start key
  23. */
  24. $Y = date('Y', $date_start);
  25. $m = date('n', $date_start)-1; // javascript stores months starting at 0
  26. $d = date('j', $date_start);
  27. $startkey = "[$Y,$m,$d]";
  28. $date_end = $date_start+($num_days*24*60*60);
  29. /*
  30. create end key
  31. */
  32. $Y = date('Y', $date_end);
  33. $m = date('n', $date_end)-1; // javascript stores months starting at 0
  34. $d = date('j', $date_end);
  35. $endkey = "[$Y,$m,$d]";
  36. $hashkey = $startkey.'_to_'.$endkey;
  37. /*
  38. Cache lookup
  39. */
  40. if (($nocache != 'nocache') && file_exists(self::CACHE_PATH."$hashkey")){
  41. $delta = strtotime('now') - filemtime(self::CACHE_PATH."$hashkey");
  42. if ($delta < self::CACHE_DURATION) {
  43. trigger_error('Loading from Cache '.self::CACHE_PATH."$hashkey", E_USER_NOTICE);
  44. $statObjs = json_decode(file_get_contents(self::CACHE_PATH."$hashkey"));
  45. if ($statObjs) {
  46. return $statObjs;
  47. }
  48. }
  49. }
  50. trigger_error('NOT Loading from Cache', E_USER_NOTICE);
  51. /*
  52. cache has expired or DNE, so fall through and query
  53. */
  54. $url = "http://127.0.0.1:5984/publictweets_sources/_view/sources/byday?group=true&startkey=$startkey&endkey=$endkey";
  55. // echo "<pre>"; echo print_r($url, true); echo "</pre>";
  56. $json_counts = file_get_contents($url);
  57. // echo "<pre>"; echo print_r($json_counts, true); echo "</pre>";
  58. $result = json_decode($json_counts);
  59. unset($json_counts);
  60. // echo "<pre>"; var_dump($result->rows); echo "</pre>";
  61. foreach ($result->rows as $row) {
  62. // echo "<pre>COUNT"; var_dump($row); echo "</pre>";
  63. if (isset($stats[$row->key[3]])) {
  64. $stats[$row->key[3]] += $row->value;
  65. } else {
  66. $stats[$row->key[3]] = $row->value;
  67. }
  68. }
  69. unset($result);
  70. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  71. foreach ($stats as $key => $value) {
  72. $obj->key = $key;
  73. $obj->value = $value;
  74. $statObjs[] = $obj;
  75. unset($obj);
  76. }
  77. unset($stats);
  78. /*
  79. Cache this
  80. */
  81. file_put_contents(self::CACHE_PATH."$hashkey", json_encode($statObjs));
  82. // echo "<pre>"; var_dump($statObjs); echo "</pre>";
  83. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  84. return $statObjs;
  85. }
  86. }