PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/trends.php

https://github.com/funkatron/twitter-stats-tracker
PHP | 541 lines | 309 code | 164 blank | 68 comment | 19 complexity | dd72093c475bc71d838f801b06a7d4da MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. ini_set('display_errors','on');
  3. require_once('/var/www/shared/gChart2.php');
  4. class Trends extends Controller {
  5. const JSON_FILE_PATH = '/var/www/twittersource.info/htdocs/twitter-source-stats.json';
  6. const PAGE_TITLE = 'Twitter Trends';
  7. public function Trends() {
  8. parent::Controller();
  9. }
  10. public function index() {
  11. $this->lasthour();
  12. }
  13. public function lasthour()
  14. {
  15. $this->load->model('mtrends');
  16. $rows = $this->mtrends->getStatsForHourPeriod('-1 hour',1);
  17. // echo "<pre>"; echo print_r($rows, true); echo "</pre>";
  18. $view_data = $this->_prepDataForView($rows);
  19. // $view_data['last_updated'] = filemtime(self::JSON_FILE_PATH);
  20. $view_data['period_interval'] = 'hour';
  21. $view_data['period_start'] = '-1 hour';
  22. $view_data['period_duration'] = 1;
  23. $view_data['page_title'] = self::PAGE_TITLE . ": Last Hour";
  24. $this->load->view('trends', $view_data);
  25. }
  26. public function last12hours($term=null)
  27. {
  28. if ($term) {
  29. $this->termslast12hours($term);
  30. return;
  31. }
  32. $this->load->model('mtrends');
  33. $rows = $this->mtrends->getStatsForHourPeriod('-12 hours',12,$term);
  34. // echo "<pre>"; echo print_r($rows, true); echo "</pre>";
  35. $view_data = $this->_prepDataForView($rows);
  36. // $view_data['last_updated'] = filemtime(self::JSON_FILE_PATH);
  37. $view_data['period_interval'] = 'hour';
  38. $view_data['period_start'] = '-12 hours';
  39. $view_data['period_duration'] = 12;
  40. $view_data['page_title'] = self::PAGE_TITLE . ": Last 12 Hours";
  41. $this->load->view('trends', $view_data);
  42. }
  43. public function last24hours($term=null)
  44. {
  45. if ($term) {
  46. $this->termslast24hours($term);
  47. return;
  48. }
  49. $duration=24;
  50. $this->load->model('mtrends');
  51. $rows = $this->mtrends->getStatsForHourPeriod('-12 hours',12,$term);
  52. // echo "<pre>"; echo print_r($rows, true); echo "</pre>";
  53. $view_data = $this->_prepDataForView($rows);
  54. // $view_data['last_updated'] = filemtime(self::JSON_FILE_PATH);
  55. $view_data['period_interval'] = 'hour';
  56. $view_data['period_start'] = '-'.$duration.' hours';
  57. $view_data['period_duration'] = $duration;
  58. $view_data['page_title'] = self::PAGE_TITLE . ": Last 24 Hours";
  59. $this->load->view('trends', $view_data);
  60. }
  61. public function termslast12hours($term=null)
  62. {
  63. $duration = 12;
  64. $this->load->model('mtrends');
  65. $rows = $this->mtrends->getTermStatsForHourPeriod('-'.$duration.' hours', $duration, $term);
  66. $this->_showTermStats($rows, '-'.$duration.' hours', $duration);
  67. }
  68. public function termslast24hours($term=null)
  69. {
  70. $duration = 24;
  71. $this->load->model('mtrends');
  72. $rows = $this->mtrends->getTermStatsForHourPeriod('-'.$duration.' hours', $duration, $term);
  73. $this->_showTermStats($rows, '-'.$duration.' hours', $duration);
  74. }
  75. public function termslast48hours($term=null)
  76. {
  77. $duration = 48;
  78. $this->load->model('mtrends');
  79. $rows = $this->mtrends->getTermStatsForHourPeriod('-'.$duration.' hours', $duration, $term);
  80. $this->_showTermStats($rows, '-'.$duration.' hours', $duration);
  81. }
  82. public function today()
  83. {
  84. $this->load->model('mtrends');
  85. $rows = $this->mtrends->getStatsForPeriod('today',1);
  86. // echo "<pre>"; echo print_r($rows, true); echo "</pre>";
  87. $view_data = $this->_prepDataForView($rows);
  88. // $view_data['last_updated'] = filemtime(self::JSON_FILE_PATH);
  89. $view_data['period_start'] = 'today';
  90. $view_data['period_duration'] = 1;
  91. $view_data['page_title'] = self::PAGE_TITLE . ": Today";
  92. $this->load->view('trends', $view_data);
  93. }
  94. public function yesterday()
  95. {
  96. $this->load->model('mtrends');
  97. $rows = $this->mtrends->getStatsForPeriod('yesterday',1);
  98. $view_data = $this->_prepDataForView($rows);
  99. $view_data['period_start'] = 'yesterday';
  100. $view_data['period_duration'] = 1;
  101. $view_data['page_title'] = self::PAGE_TITLE . ": Yesterday";
  102. $this->load->view('trends', $view_data);
  103. }
  104. public function lastsevendays() {
  105. $this->load->model('mtrends');
  106. $rows = $this->mtrends->getStatsForPeriod('-7 days',7);
  107. $view_data = $this->_prepDataForView($rows);
  108. $view_data['period_start'] = '-7 days';
  109. $view_data['period_duration'] = 7;
  110. $view_data['page_title'] = self::PAGE_TITLE . ": Last Seven Days";
  111. $this->load->view('trends', $view_data);
  112. }
  113. public function lastmonth() {
  114. $this->load->model('mtrends');
  115. $rows = $this->mtrends->getStatsForPeriod('-1 month',30);
  116. $view_data = $this->_prepDataForView($rows);
  117. $view_data['period_start'] = '-1 month';
  118. $view_data['period_duration'] = 30;
  119. $view_data['page_title'] = self::PAGE_TITLE . ": Last Month";
  120. $this->load->view('trends', $view_data);
  121. }
  122. private function _showTermStats($rows, $period_start, $num_hours)
  123. {
  124. $offset_secs = (int)date('Z');
  125. // build arrays
  126. $xlabels = array_keys($rows[0]->points);
  127. // echo "<pre>"; echo print_r($xlabels, true); echo "</pre>";
  128. array_shift($xlabels);
  129. /*
  130. Adjust for GMT so labels show local server time
  131. */
  132. foreach($xlabels as &$val) {
  133. $val = $val+$offset_secs;
  134. }
  135. $maxval = 0;
  136. foreach($rows as $row) {
  137. if ($maxval < max($row->points)) {
  138. $maxval = max($row->points);
  139. }
  140. // echo "<pre>"; echo print_r($maxval, true); echo "</pre>";
  141. foreach($row->points as &$val) {
  142. $val = round(($val/$maxval)*100,0);
  143. }
  144. ksort($row->points);
  145. array_shift($row->points);
  146. // echo "<pre>"; var_dump($row); echo "</pre>";
  147. $yvals[] = implode(",", $row->points);
  148. $terms[] = urlencode($row->term);
  149. $colors[] = $this->_random_color('nowhite');
  150. }
  151. $url = $this->_makeGchartLineUrl($xlabels, $yvals, $colors, $terms);
  152. /*
  153. make view_data array
  154. */
  155. // $view_data['source_counts'] = $counts;
  156. // $view_data['sources_total'] = $total;
  157. $view_data['chart_url'] = $url;
  158. // $view_data['last_updated'] = filemtime(self::JSON_FILE_PATH);
  159. $view_data['period_interval'] = 'hour';
  160. $view_data['period_start'] = $period_start;
  161. $view_data['period_duration'] = $num_hours;
  162. $view_data['terms'] = $terms;
  163. $terms_str = implode(', ', $terms);
  164. $view_data['page_title'] = self::PAGE_TITLE . " for " . urldecode($terms_str);
  165. $this->load->view('trends', $view_data);
  166. }
  167. private function _prepDataForView(array $rows)
  168. {
  169. $total = 0;
  170. $subtotal = 0;
  171. $values = Array();
  172. $pers = Array();
  173. $labels = Array();
  174. // calc total
  175. foreach($rows as $row) {
  176. $total += $row->value;
  177. }
  178. // normalize names (URLs can change, names should not)
  179. foreach ($rows as $row) {
  180. $normkey = strtolower($row->key);
  181. if (!isset($counts[$normkey])) {
  182. $counts[$normkey] = 0;
  183. }
  184. $counts[$normkey] += $row->value;
  185. }
  186. // build data
  187. foreach($counts as $key=>$val) {
  188. $per = round(($val/$total)*100);
  189. if ($per >= 1) {
  190. $values[] = $val;
  191. $pers[] = $per;
  192. $labels[] = urlencode($key);
  193. $colors[] = $this->_random_color();
  194. $subtotal += $val;
  195. }
  196. }
  197. // calculate size of "others"
  198. $others_val = $total - $subtotal;
  199. if ($others_val > 0) {
  200. $values[] = $others_val;
  201. $pers[] = round(($others_val/$total)*100);
  202. $labels[] = "Other";
  203. $colors[] = $this->_random_color();
  204. }
  205. $url = $this->_makeGchartUrl($labels, $pers, $colors);
  206. /*
  207. make view_data array
  208. */
  209. $view_data['source_counts'] = $counts;
  210. $view_data['sources_total'] = $total;
  211. $view_data['chart_url'] = $url;
  212. return $view_data;
  213. }
  214. private function _makeGchartUrl(array $labels, array $values, array $colors)
  215. {
  216. $url = "http://chart.apis.google.com/chart?";
  217. // type
  218. $url .= "&cht=p3";
  219. // size
  220. $url .= "&chs=700x300";
  221. // data
  222. $url .= "&chd=t:".implode(',',$values);
  223. // $url .= "&chds=1,100";
  224. // colors
  225. $url .= "&chco=".implode(',', $colors);
  226. // labels
  227. $url .= "&chl=".implode('|', $labels);
  228. return $url;
  229. }
  230. private function _makeGchartBarUrl(array $xlabels, array $yvals, array $colors, array $terms)
  231. {
  232. $max_x = 100;
  233. $points = count($xlabels);
  234. // echo "<pre>"; echo print_r($xlabels, true); echo "</pre>";
  235. // echo "<pre>"; echo print_r($yvals, true); echo "</pre>";
  236. // echo "<pre>"; echo print_r($terms, true); echo "</pre>";
  237. $interval = round($max_x/($points-1),0);
  238. $xaxis = array();
  239. for ($x=0; $x<$points; $x++) {
  240. if ($interval*$x > $max_x) {
  241. $xaxis[] = $max_x;
  242. } else {
  243. $xaxis[] = $interval*$x;
  244. }
  245. }
  246. $xaxis = implode(',',$xaxis);
  247. $url = "http://chart.apis.google.com/chart?";
  248. // type
  249. $url .= "&cht=bvg";
  250. // size
  251. $url .= "&chs=700x300";
  252. $url .= "&chbh=3,0,12";
  253. // data
  254. foreach($yvals as $yval) {
  255. $x_y = array();
  256. // $x_y['x']=$xaxis;
  257. $x_y['y']=$yval;
  258. $data_sets[] = implode("|", $x_y);
  259. }
  260. $data_string = implode("|", $data_sets);
  261. $url .= "&chd=t:$data_string";
  262. // $url .= "&chd=t:".implode("|",$yvals);
  263. // x-axis labels
  264. $url .= "&chxt=x";
  265. foreach($xlabels as $unixtime) {
  266. $xlbls[] = urlencode(date('ga', $unixtime));
  267. }
  268. $url .= "&chxl=0:|".implode('|', $xlbls);
  269. // colors
  270. $url .= "&chco=".implode(',', $colors);
  271. // legend
  272. $url .= "&chdl=".implode('|', $terms);
  273. $url .= "&chdlp=b";
  274. // gridlines
  275. echo "<pre>"; echo print_r($interval, true); echo "</pre>";
  276. $url .= "&chg=".$interval.",10,1,3";
  277. $url .= "&chf=c,ls,0,FFFFCC,".($interval/100).",FFFFFF,".($interval/100);
  278. // echo "<textarea style='width:95%;height:100px'>$url</textarea>";
  279. return $url;
  280. }
  281. private function _makeGchartLineUrl(array $xlabels, array $yvals, array $colors, array $terms)
  282. {
  283. $max_x = 100;
  284. $points = count($xlabels);
  285. // foreach($yvals as &$yvalstr) {
  286. // $yvals = explode(',', $yvalstr);
  287. // $yvalstr = $this->array_to_extended_encoding($yvals);
  288. // }
  289. // echo "<pre>"; echo print_r($xlabels, true); echo "</pre>";
  290. // echo "<pre>"; echo print_r($yvals, true); echo "</pre>";
  291. // echo "<pre>"; echo print_r($terms, true); echo "</pre>";
  292. $interval = round($max_x/($points-1),2);
  293. $xaxis = array();
  294. for ($x=0; $x<$points; $x++) {
  295. if ($interval*$x > $max_x) {
  296. $xaxis[] = $max_x;
  297. } else {
  298. $xaxis[] = $interval*$x;
  299. }
  300. }
  301. $xaxis = implode(',',$xaxis);
  302. $url = "http://chart.apis.google.com/chart?";
  303. // type
  304. $url .= "&cht=lxy";
  305. // size
  306. $url .= "&chs=700x300";
  307. // $url .= "&chbh=10";
  308. // data
  309. foreach($yvals as $yval) {
  310. $x_y = array();
  311. $x_y['x']=$xaxis;
  312. $x_y['y']=$yval;
  313. $data_sets[] = implode("|", $x_y);
  314. }
  315. $data_string = implode("|", $data_sets);
  316. $url .= "&chd=t:$data_string";
  317. // $url .= "&chd=t:".implode("|",$yvals);
  318. // x-axis labels
  319. $url .= "&chxt=x";
  320. foreach($xlabels as $unixtime) {
  321. $xlbls[] = urlencode(date('ga', $unixtime));
  322. }
  323. $url .= "&chxl=0:|".implode('|', $xlbls);
  324. // colors
  325. $url .= "&chco=".implode(',', $colors);
  326. // legend
  327. $url .= "&chdl=".implode('|', $terms);
  328. $url .= "&chdlp=b";
  329. // gridlines
  330. $url .= "&chg=".$interval.",10,2,2";
  331. // echo "<textarea style='width:95%;height:100px'>$url</textarea>";
  332. return $url;
  333. }
  334. private function _gchartEncodeExtended($string) {
  335. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
  336. $first = floor($value / 64);
  337. $second = $value % 64;
  338. $encoding .= $characters[$first] . $characters[$second];
  339. return $encoding;
  340. }
  341. private function array_to_extended_encoding($array)
  342. {
  343. $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-.';
  344. $encoding = '';
  345. foreach ($array as $value) {
  346. $first = floor($value / 64);
  347. $second = $value % 64;
  348. $encoding .= $characters[$first] . $characters[$second];
  349. // echo "<pre>"; echo print_r($value, true); echo "</pre>";
  350. // echo "<pre>"; echo print_r($encoding, true); echo "</pre>";
  351. }
  352. return $encoding;
  353. }
  354. private function _random_color($type='any'){
  355. mt_srand((double)microtime()*1000000);
  356. $c = '';
  357. while(strlen($c)<6){
  358. if ($type == 'dark') {
  359. $c .= sprintf("%02X", mt_rand(0, 128));
  360. }
  361. elseif ($type == 'light') {
  362. $c .= sprintf("%02X", mt_rand(128, 255));
  363. }
  364. elseif ($type == 'nowhite') {
  365. $c .= sprintf("%02X", mt_rand(0, 223));
  366. }
  367. elseif ($type == 'noblack') {
  368. $c .= sprintf("%02X", mt_rand(32, 255));
  369. } else {
  370. $c .= sprintf("%02X", mt_rand(0, 255));
  371. }
  372. }
  373. return $c;
  374. }
  375. }
  376. /* End of file welcome.php */
  377. /* Location: ./system/application/controllers/welcome.php */