statistics.mod.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 (!empty($result[$info['module']]) && $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 (!empty($result[$info['date']]) && $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']] = empty($result['count'][$visit['date']]) ? 0 : $result['count'][$visit['date']];
  126. $result['ip_count'][$visit['date']] = empty($result['ip_count'][$visit['date']]) ? 0 : $result['ip_count'][$visit['date']];
  127. $result['count'][$visit['date']] += $visit['count'];
  128. $result['ip_count'][$visit['date']] += $visit['ip_count'];
  129. }
  130. }
  131. return $result;
  132. }
  133. function stat_visit_web_bydate($time_type, $daterange = array(), $is_system_stat = false) {
  134. $result = array();
  135. $visit_info = stat_visit_info('web', $time_type, '', $daterange, $is_system_stat);
  136. if (empty($visit_info)) {
  137. return $result;
  138. } else {
  139. foreach ($visit_info as $visit) {
  140. $result['count'][$visit['date']] = empty($result['count'][$visit['date']]) ? 0 : $result['count'][$visit['date']];
  141. $result['ip_count'][$visit['date']] = empty($result['ip_count'][$visit['date']]) ? 0 : $result['ip_count'][$visit['date']];
  142. $result['count'][$visit['date']] += $visit['count'];
  143. $result['ip_count'][$visit['date']] += $visit['ip_count'];
  144. }
  145. }
  146. return $result;
  147. }
  148. function stat_all_visit_statistics($type, $data) {
  149. if ($type == 'current_account') {
  150. $modules = stat_modules_except_system();
  151. $count = count($modules);
  152. } elseif ($type == 'all_account') {
  153. $count = stat_account_count();
  154. }
  155. $result = array(
  156. 'visit_sum' => 0,
  157. 'visit_highest' => 0,
  158. 'visit_avg' => 0
  159. );
  160. if (empty($data)) {
  161. return $result;
  162. }
  163. foreach ($data as $val) {
  164. $result['visit_sum'] += $val['count'];
  165. if ($result['visit_highest'] < $val['count']) {
  166. $result['visit_highest'] = $val['count'];
  167. }
  168. }
  169. $result['visit_avg'] = round($result['visit_sum'] / $count);
  170. return $result;
  171. }
  172. function stat_modules_except_system() {
  173. $modules = uni_modules();
  174. if (!empty($modules)) {
  175. foreach ($modules as $key => $module) {
  176. if (!empty($module['issystem'])) {
  177. unset($modules[$key]);
  178. }
  179. }
  180. }
  181. return $modules;
  182. }
  183. function stat_account_count() {
  184. $count = 0;
  185. $account_table = table('account');
  186. $account_table->searchWithType(array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH));
  187. $account_table->accountRankOrder();
  188. $account_list = $account_table->searchAccountList();
  189. $count = count($account_list);
  190. return $count;
  191. }
  192. function stat_date_range($start, $end) {
  193. $result = array();
  194. if (empty($start) || empty($end)) {
  195. return $result;
  196. }
  197. $start = strtotime($start);
  198. $end = strtotime($end);
  199. $i = 0;
  200. while(strtotime(end($result)) < $end) {
  201. $result[] = date('Ymd', $start + $i * 86400);
  202. $i++;
  203. }
  204. return $result;
  205. }
  206. function stat_mc_member() {
  207. $result = array('total' => 0);
  208. $members = table('mc_members')->searchWithAccount()->select('m.uid, a.type')->getall();
  209. if (empty($members)) {
  210. return $result;
  211. }
  212. $result['total'] = count($members);
  213. $account_type = uni_account_type();
  214. foreach ($members as $member) {
  215. foreach ($account_type as $type => $type_info) {
  216. if (!isset($result[$type_info['type_sign']])) {
  217. $result[$type_info['type_sign']] = 0;
  218. }
  219. if ($member['type'] == $type) {
  220. $result[$type_info['type_sign']] += 1;
  221. }
  222. }
  223. }
  224. foreach ($result as $key => &$item) {
  225. if ('total' != $key) {
  226. $item = round($item/$result['total'] * 100, 2);
  227. }
  228. }
  229. return $result;
  230. }
  231. function stat_module() {
  232. $module_support_type = module_support_type();
  233. $result = array('total' => 0, 'account' => 0, 'wxapp' => 0, 'other' => 0);
  234. $select_fields = array_merge(array('name'), array_keys($module_support_type));
  235. $modules = table('modules')->select($select_fields)->where('issystem', 0)->getall();
  236. if (empty($modules)) {
  237. return $result;
  238. }
  239. foreach ($modules as $module) {
  240. foreach ($module_support_type as $type => $type_info) {
  241. if ($module[$type] == $type_info['support']) {
  242. if (in_array($type_info['type'], array_keys($result))) {
  243. $result[$type_info['type']] += 1;
  244. }
  245. if (!in_array($type_info['type'], array_keys($result))) {
  246. $result['other'] += 1;
  247. }
  248. $result['total'] += 1;
  249. }
  250. }
  251. }
  252. foreach ($result as $key => $item) {
  253. if ('total' != $key) {
  254. $result[$key] = round($item/$result['total'] * 100, 2);
  255. }
  256. }
  257. return $result;
  258. }