statistics.mod.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function stat_visit_info($type, $time_type, $module = '', $daterange = array(), $is_system_stat = false) {
  8. global $_W;
  9. $result = array();
  10. if (empty($type) || empty($time_type) || !empty($type) && !in_array($type, array('web', 'app', 'api', 'all'))) {
  11. return $result;
  12. }
  13. $stat_visit_table = table('stat_visit');
  14. if ($type != 'all') {
  15. $stat_visit_table->searchWithType($type);
  16. }
  17. if (empty($is_system_stat)) {
  18. $stat_visit_table->searchWithUnacid($_W['uniacid']);
  19. }
  20. if (!empty($module)) {
  21. $stat_visit_table->searchWithModule($module);
  22. }
  23. switch ($time_type) {
  24. case 'today':
  25. $stat_visit_table->searchWithDate(date('Ymd'));
  26. break;
  27. case 'yesterday':
  28. $stat_visit_table->searchWithDate(date('Ymd', strtotime('-1 days')));
  29. break;
  30. case 'week':
  31. $stat_visit_table->searchWithGreaterThenDate(date('Ymd', strtotime('-6 days')));
  32. $stat_visit_table->searchWithLessThenDate(date('Ymd'));
  33. break;
  34. case 'month':
  35. $stat_visit_table->searchWithGreaterThenDate(date('Ymd', strtotime('-29 days')));
  36. $stat_visit_table->searchWithLessThenDate(date('Ymd'));
  37. break;
  38. case 'daterange':
  39. if (empty($daterange)) {
  40. return stat_visit_info($type, 'month', $module, array(), $is_system_stat);
  41. }
  42. $stat_visit_table->searchWithGreaterThenDate(date('Ymd', strtotime($daterange['start'])));
  43. $stat_visit_table->searchWithLessThenDate(date('Ymd', strtotime($daterange['end'])));
  44. break;
  45. }
  46. $visit_info = $stat_visit_table->getall();
  47. if (!empty($visit_info)) {
  48. $result = $visit_info;
  49. }
  50. return $result;
  51. }
  52. function stat_visit_app_byuniacid($time_type, $module = '', $daterange = array(), $is_system_stat = false) {
  53. $result = array();
  54. $visit_info = stat_visit_info('app', $time_type, $module, $daterange, $is_system_stat);
  55. if (empty($visit_info)) {
  56. return $result;
  57. }
  58. foreach ($visit_info as $info) {
  59. if ($is_system_stat) {
  60. if (empty($info['uniacid'])) {
  61. continue;
  62. }
  63. if ($result[$info['uniacid']]['uniacid'] == $info['uniacid']) {
  64. $result[$info['uniacid']]['count'] += $info['count'];
  65. $result[$info['uniacid']]['highest'] = $result[$info['uniacid']]['highest'] >= $info['count'] ? $result[$info['uniacid']]['highest'] : $info['count'];
  66. } else {
  67. $result[$info['uniacid']] = $info;
  68. $result[$info['uniacid']]['highest'] = $info['count'];
  69. }
  70. } else {
  71. if (empty($info['module'])) {
  72. continue;
  73. }
  74. if ($result[$info['module']]['module'] == $info['module']) {
  75. $result[$info['module']]['count'] += $info['count'];
  76. $result[$info['module']]['highest'] = $result[$info['module']]['highest'] >= $info['count'] ? $result[$info['module']]['highest'] : $info['count'];
  77. } else {
  78. $result[$info['module']] = $info;
  79. $result[$info['module']]['highest'] = $info['count'];
  80. }
  81. }
  82. }
  83. $modules = stat_modules_except_system();
  84. $count = count($modules);
  85. foreach ($result as $key => $val) {
  86. $result[$key]['avg'] = round($val['count'] / $count);
  87. }
  88. return $result;
  89. }
  90. function stat_visit_app_bydate($time_type, $module = '', $daterange = array(), $is_system_stat = false) {
  91. $result = array();
  92. $visit_info = stat_visit_info('app', $time_type, $module, $daterange, $is_system_stat);
  93. if (empty($visit_info)) {
  94. return $result;
  95. }
  96. $count = stat_account_count();
  97. foreach ($visit_info as $info) {
  98. if (empty($info['uniacid']) || empty($info['date'])) {
  99. continue;
  100. }
  101. if ($result[$info['date']]['date'] == $info['date']) {
  102. $result[$info['date']]['count'] += $info['count'];
  103. $result[$info['date']]['highest'] = $result[$info['date']]['highest'] >= $info['count'] ? $result[$info['date']]['highest'] : $info['count'];
  104. } else {
  105. unset($info['module'], $info['uniacid']);
  106. $result[$info['date']] = $info;
  107. $result[$info['date']]['highest'] = $info['count'];
  108. }
  109. }
  110. if (empty($result)) {
  111. return $result;
  112. }
  113. foreach ($result as $key => $val) {
  114. $result[$key]['avg'] = round($val['count'] / $count);
  115. }
  116. return $result;
  117. }
  118. function stat_visit_all_bydate($time_type, $daterange = array(), $is_system_stat = false) {
  119. $result = array();
  120. $visit_info = stat_visit_info('all', $time_type, '', $daterange, $is_system_stat);
  121. if (empty($visit_info)) {
  122. return $result;
  123. } else {
  124. foreach ($visit_info as $visit) {
  125. $result['count'][$visit['date']] += $visit['count'];
  126. $result['ip_count'][$visit['date']] += $visit['ip_count'];
  127. }
  128. }
  129. return $result;
  130. }
  131. function stat_visit_web_bydate($time_type, $daterange = array(), $is_system_stat = false) {
  132. $result = array();
  133. $visit_info = stat_visit_info('web', $time_type, '', $daterange, $is_system_stat);
  134. if (empty($visit_info)) {
  135. return $result;
  136. } else {
  137. foreach ($visit_info as $visit) {
  138. $result['count'][$visit['date']] += $visit['count'];
  139. $result['ip_count'][$visit['date']] += $visit['ip_count'];
  140. }
  141. }
  142. return $result;
  143. }
  144. function stat_all_visit_statistics($type, $data) {
  145. if ($type == 'current_account') {
  146. $modules = stat_modules_except_system();
  147. $count = count($modules);
  148. } elseif ($type == 'all_account') {
  149. $count = stat_account_count();
  150. }
  151. $result = array(
  152. 'visit_sum' => 0,
  153. 'visit_highest' => 0,
  154. 'visit_avg' => 0
  155. );
  156. if (empty($data)) {
  157. return $result;
  158. }
  159. foreach ($data as $val) {
  160. $result['visit_sum'] += $val['count'];
  161. if ($result['visit_highest'] < $val['count']) {
  162. $result['visit_highest'] = $val['count'];
  163. }
  164. }
  165. $result['visit_avg'] = round($result['visit_sum'] / $count);
  166. return $result;
  167. }
  168. function stat_modules_except_system() {
  169. $modules = uni_modules();
  170. if (!empty($modules)) {
  171. foreach ($modules as $key => $module) {
  172. if (!empty($module['issystem'])) {
  173. unset($modules[$key]);
  174. }
  175. }
  176. }
  177. return $modules;
  178. }
  179. function stat_account_count() {
  180. $count = 0;
  181. $account_table = table('account');
  182. $account_table->searchWithType(array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH));
  183. $account_table->accountRankOrder();
  184. $account_list = $account_table->searchAccountList();
  185. $count = count($account_list);
  186. return $count;
  187. }
  188. function stat_date_range($start, $end) {
  189. $result = array();
  190. if (empty($start) || empty($end)) {
  191. return $result;
  192. }
  193. $start = strtotime($start);
  194. $end = strtotime($end);
  195. $i = 0;
  196. while(strtotime(end($result)) < $end) {
  197. $result[] = date('Ymd', $start + $i * 86400);
  198. $i++;
  199. }
  200. return $result;
  201. }