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

/models/mtrends.php

https://github.com/funkatron/twitter-stats-tracker
PHP | 389 lines | 168 code | 103 blank | 118 comment | 31 complexity | 9d28513cccfab6b36fe997b58a70336b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. error_reporting('on');
  3. class MTrends extends Model {
  4. const CACHE_PATH = "/var/www/twittersource.info/ciapp_tss/cache/";
  5. const CACHE_DURATION = 3600;
  6. function MTrends()
  7. {
  8. parent::Model();
  9. }
  10. /**
  11. * Retrieves source stats for a period of time segmented by days
  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. $offset_secs = (int)date('Z');
  21. // if ($term) { $nocache='nocache'; }
  22. // echo "<pre>"; echo print_r($term, true); echo "</pre>";
  23. $date_start = strtotime($date_start);
  24. // echo "<pre>"; echo print_r($date_start, true); echo "</pre>";
  25. // $date_start = $date_start + $offset_secs; // trends data is stored in GMT, so adjust for offset
  26. // echo "<pre>"; echo print_r($date_start, true); echo "</pre>";
  27. /*
  28. create start key
  29. */
  30. $Y = date('Y', $date_start);
  31. $m = date('n', $date_start)-1; // javascript stores months starting at 0
  32. $d = date('j', $date_start);
  33. $startkey = "[$Y,$m,$d]";
  34. // echo "<pre>"; echo print_r(date("Y-m-d H:i:s", $date_start), true); echo "</pre>";
  35. $date_end = $date_start+($num_days*24*60*60);
  36. // echo "<pre>"; echo print_r(date("Y-m-d H:i:s", $date_end), true); echo "</pre>";
  37. /*
  38. create end key
  39. */
  40. $Y = date('Y', $date_end);
  41. $m = date('n', $date_end)-1; // javascript stores months starting at 0
  42. $d = date('j', $date_end);
  43. $endkey = "[$Y,$m,$d]";
  44. $hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey;
  45. // echo "<pre>"; echo print_r($hashkey, true); echo "</pre>";
  46. /*
  47. Cache lookup
  48. */
  49. if (($nocache != 'nocache') && file_exists(self::CACHE_PATH."$hashkey")){
  50. $delta = strtotime('now') - filemtime(self::CACHE_PATH."$hashkey");
  51. if ($delta < self::CACHE_DURATION) {
  52. // echo "cached";
  53. $statObjs = json_decode(file_get_contents(self::CACHE_PATH."$hashkey"));
  54. // echo "<pre>"; echo print_r($statObjs, true); echo "</pre>";
  55. return $statObjs;
  56. }
  57. }
  58. // echo "nocached";
  59. /*
  60. cache has expired or DNE, so fall through and query
  61. */
  62. $url = "http://127.0.0.1:5984/twitter_trends/_view/rank/byday?group=true&startkey=$startkey&endkey=$endkey";
  63. // echo "<pre>"; echo print_r($url, true); echo "</pre>";
  64. $json_counts = file_get_contents($url);
  65. // echo "<pre>"; echo print_r($json_counts, true); echo "</pre>";
  66. $result = json_decode($json_counts);
  67. // echo "<pre>"; var_dump($result->rows); echo "</pre>";
  68. foreach ($result->rows as $row) {
  69. // echo "<pre>COUNT"; var_dump($row); echo "</pre>";
  70. if (isset($stats[$row->key[3]])) {
  71. $stats[$row->key[3]] += $row->value;
  72. } else {
  73. $stats[$row->key[3]] = $row->value;
  74. }
  75. }
  76. unset($result);
  77. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  78. foreach ($stats as $key => $value) {
  79. $obj->key = $key;
  80. $obj->value = $value;
  81. $statObjs[] = $obj;
  82. unset($obj);
  83. }
  84. unset($stats);
  85. /*
  86. Cache this
  87. */
  88. file_put_contents(self::CACHE_PATH."$hashkey", json_encode($statObjs));
  89. // echo "<pre>"; var_dump($statObjs); echo "</pre>";
  90. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  91. return $statObjs;
  92. }
  93. /**
  94. * Retrieves source stats for a period of time segmented by hours
  95. *
  96. * @param string $date_start
  97. * @param int $num_days
  98. * @return array
  99. * @author Ed Finkler
  100. */
  101. public function getStatsForHourPeriod($datetime_start, $num_hours, $term=null, $nocache=null)
  102. {
  103. $offset_secs = (int)date('Z');
  104. if ($term) { $nocache='nocache'; }
  105. // echo "<pre>"; echo print_r($term, true); echo "</pre>";
  106. $date_start = strtotime($datetime_start);
  107. // echo "<pre>"; echo print_r($date_start, true); echo "</pre>";
  108. // $date_start = $date_start + $offset_secs; // trends data is stored in GMT, so adjust for offset
  109. // echo "<pre>"; echo print_r($date_start, true); echo "</pre>";
  110. /*
  111. create start key
  112. */
  113. $Y = date('Y', $date_start);
  114. $m = date('n', $date_start)-1; // javascript stores months starting at 0
  115. $d = date('j', $date_start);
  116. $h = date('G', $date_start);
  117. if ($term) {
  118. $startkey = "[$Y,$m,$d,$h,\"$term\"]";
  119. } else {
  120. $startkey = "[$Y,$m,$d,$h]";
  121. }
  122. $date_end = $date_start+($num_hours*60*60);
  123. /*
  124. create end key
  125. */
  126. $Y = date('Y', $date_end);
  127. $m = date('n', $date_end)-1; // javascript stores months starting at 0
  128. $d = date('j', $date_end);
  129. $h = date('G', $date_end);
  130. if ($term) {
  131. $endkey = "[$Y,$m,$d,$h,\"$term\"]";
  132. } else {
  133. $endkey = "[$Y,$m,$d,$h]";
  134. }
  135. if ($term){
  136. $hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey.'-'.$term;
  137. } else {
  138. $hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey;
  139. }
  140. /*
  141. Cache lookup
  142. */
  143. if (($nocache != 'nocache') && file_exists(self::CACHE_PATH."$hashkey")){
  144. $delta = strtotime('now') - filemtime(self::CACHE_PATH."$hashkey");
  145. if ($delta < self::CACHE_DURATION) {
  146. // echo "cached";
  147. $statObjs = json_decode(file_get_contents(self::CACHE_PATH."$hashkey"));
  148. // echo "<pre>"; echo print_r($statObjs, true); echo "</pre>";
  149. return $statObjs;
  150. }
  151. }
  152. // echo "nocached";
  153. /*
  154. cache has expired or DNE, so fall through and query
  155. */
  156. $url = "http://127.0.0.1:5984/twitter_trends/_view/rank/byhour?group=true&startkey=$startkey&endkey=$endkey";
  157. // echo "<pre>"; echo print_r($url, true); echo "</pre>";
  158. $json_counts = file_get_contents($url);
  159. // echo "<pre>"; echo print_r($json_counts, true); echo "</pre>";
  160. $result = json_decode($json_counts);
  161. // echo "<pre>"; var_dump($result->rows); echo "</pre>";
  162. foreach ($result->rows as $row) {
  163. // echo "<pre>COUNT"; var_dump($row); echo "</pre>";
  164. if (isset($stats[$row->key[4]])) {
  165. $stats[$row->key[4]] += $row->value;
  166. } else {
  167. $stats[$row->key[4]] = $row->value;
  168. }
  169. }
  170. unset($result);
  171. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  172. foreach ($stats as $key => $value) {
  173. $obj->key = $key;
  174. $obj->value = $value;
  175. $statObjs[] = $obj;
  176. unset($obj);
  177. }
  178. unset($stats);
  179. /*
  180. Cache this
  181. */
  182. if (!$nocache) {
  183. file_put_contents(self::CACHE_PATH."$hashkey", json_encode($statObjs));
  184. }
  185. // echo "<pre>"; var_dump($statObjs); echo "</pre>";
  186. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  187. return $statObjs;
  188. }
  189. /**
  190. * Retrieves source stats for a period of time segmented by hours
  191. *
  192. * @param string $date_start
  193. * @param int $num_days
  194. * @return array
  195. * @author Ed Finkler
  196. */
  197. public function getTermStatsForHourPeriod($datetime_start, $num_hours, $term=null, $nocache=null)
  198. {
  199. if ($term) { $nocache='nocache'; }
  200. $terms = explode(',',$term);
  201. $offset_secs = (int)date('Z');
  202. $date_start = strtotime($datetime_start);
  203. $date_start = $date_start - $offset_secs; // trends data is stored in GMT, so adjust for offset
  204. /*
  205. create start key
  206. */
  207. $Y = date('Y', $date_start);
  208. $m = date('n', $date_start)-1; // javascript stores months starting at 0
  209. $d = date('j', $date_start);
  210. $h = date('G', $date_start);
  211. $startkey = "[$Y,$m,$d,$h]";
  212. $unixstart= mktime($h,0,0,$m+1,$d,$Y);
  213. $date_end = $date_start+($num_hours*60*60);
  214. /*
  215. create end key
  216. */
  217. $Y = date('Y', $date_end);
  218. $m = date('n', $date_end)-1; // javascript stores months starting at 0
  219. $d = date('j', $date_end);
  220. $h = date('G', $date_end);
  221. $endkey = "[$Y,$m,$d,$h]";
  222. $unixend= mktime($h,0,0,$m+1,$d,$Y);
  223. /*
  224. make array of unixtimes for sample points
  225. */
  226. $samplepoints = array();
  227. for ($x=$unixstart; $x<=$unixend; $x += (60*60)) {
  228. $samplepoints[$x] = 0;
  229. }
  230. // echo "<pre>"; echo print_r($samplepoints, true); echo "</pre>";
  231. if ($term){
  232. $hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey.'-'.$term;
  233. } else {
  234. $hashkey = 'TRENDS-'.$startkey.'_to_'.$endkey;
  235. }
  236. /*
  237. Cache lookup
  238. */
  239. if (($nocache != 'nocache') && file_exists(self::CACHE_PATH."$hashkey")){
  240. $delta = strtotime('now') - filemtime(self::CACHE_PATH."$hashkey");
  241. if ($delta < self::CACHE_DURATION) {
  242. // echo "cached";
  243. $statObjs = json_decode(file_get_contents(self::CACHE_PATH."$hashkey"));
  244. // echo "<pre>"; echo print_r($statObjs, true); echo "</pre>";
  245. return $statObjs;
  246. }
  247. }
  248. // echo "nocached";
  249. /*
  250. cache has expired or DNE, so fall through and query
  251. */
  252. $url = "http://127.0.0.1:5984/twitter_trends/_view/rank/byhour?group=true&startkey=$startkey&endkey=$endkey";
  253. // echo "<pre>"; echo print_r($url, true); echo "</pre>";
  254. $json_counts = file_get_contents($url);
  255. $result = json_decode($json_counts);
  256. // echo "<pre>"; var_dump($result->rows); echo "</pre>";
  257. foreach($terms as $term) {
  258. $stats->term = $term;
  259. $stats->points = $samplepoints;
  260. foreach ($result->rows as $row) {
  261. // echo "<pre>"; var_dump($row); echo "</pre>";
  262. if (strtolower($row->key[4]) == strtolower($term)) {
  263. // $strtime = $row->key[0]."-".($row->key[1]+1)."-".$row->key[2].' '.($row->key[3]+1).":00:00";
  264. // echo "<pre>"; echo print_r($strtime, true); echo "</pre>";
  265. // $unixhour = strtotime($strtime);
  266. $unixhour = mktime($row->key[3]+1, 0, 0, $row->key[1]+1, $row->key[2], $row->key[0]);
  267. // echo date("Y-m-d H:i:s", $unixhour)."<br>";
  268. $stats->points[$unixhour] = $row->value;
  269. }
  270. }
  271. $termstats[] = $stats;
  272. unset($stats);
  273. }
  274. // echo "<pre>"; var_dump($result); echo "</pre>";
  275. unset($result);
  276. /*
  277. Cache this
  278. */
  279. if (!$nocache) {
  280. file_put_contents(self::CACHE_PATH."$hashkey", json_encode($statObjs));
  281. }
  282. // echo "<pre>"; var_dump($termstats); echo "</pre>";
  283. // echo "<pre>"; echo print_r($stats, true); echo "</pre>";
  284. return $termstats;
  285. }
  286. }