You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.0 KiB

7 years ago
  1. <?php
  2. /**
  3. * Class Statics_model
  4. * ----------------------------------------------------------
  5. * 통계 관련 모델
  6. */
  7. class Statics_model extends WB_Model
  8. {
  9. function ip_info_update()
  10. {
  11. $this->db->where('sta_country_code', '');
  12. $this->db->where('sta_ip <>', '0');
  13. $this->db->where('sta_ip <>', '2130706433'); // 127.0.0.1
  14. $this->db->group_by('sta_ip');
  15. $result = $this->db->get('statics');
  16. $list = $result->result_array();
  17. if( count($list) > 0 )
  18. {
  19. foreach($list as $row)
  20. {
  21. $ip_info = get_ip_info( long2ip($row['sta_ip']));
  22. $this->db->where('sta_ip', $row['sta_ip']);
  23. $this->db->set('sta_country', $ip_info['country'] );
  24. $this->db->set('sta_country_code', $ip_info['countryCode']);
  25. $this->db->set('sta_addr', $ip_info['addr']);
  26. $this->db->set('sta_org', $ip_info['org']);
  27. $this->db->update('statics');
  28. }
  29. }
  30. }
  31. /**
  32. * 방문통계 목록 구하기
  33. * @param array $param
  34. * @return mixed
  35. */
  36. function visit_list($param=array())
  37. {
  38. $param['select'] = '*, INET_NTOA(sta_ip) AS sta_ip';
  39. $param['from'] = "statics";
  40. $param['limit'] = TRUE;
  41. $param['page_rows'] = element('page_rows', $param, 20);
  42. $param['page'] = element('page', $param, 1);
  43. $param['order_by'] = "sta_idx DESC";
  44. $list = $this->get_list($param);
  45. // 데이타 가공
  46. foreach($list['list'] as &$row)
  47. {
  48. // Internet Explorer인 경우 버젼도 같이 포함해 줍니다.
  49. if( strtolower($row['sta_browser']) == 'internet explorer' )
  50. {
  51. $row['sta_browser'] .= " " . $row['sta_version'];
  52. }
  53. $row['sta_device'] = $row['sta_is_mobile'] == 'Y' ? $row['sta_mobile'] : $row['sta_platform'];
  54. }
  55. return $list;
  56. }
  57. /**
  58. * 시간대별 통계
  59. * @param $startdate
  60. * @param $enddate
  61. */
  62. function statics_times($startdate, $enddate) {
  63. $return = array();
  64. $return['sum'] = 0;
  65. $return['list'] = array();
  66. $return['total'] = 0;
  67. $return['hourlist'] = array();
  68. $return['valuelist'] = array();
  69. for($i= 0; $i<=23; $i++) {
  70. $return['list'][ sprintf('%02d', $i)] = 0;
  71. $return['hourlist'][] = $i;
  72. }
  73. $result =
  74. $this->db->select("DATE_FORMAT(sta_regtime, '%H') as hour, COUNT(*) AS `count`")
  75. ->from('statics')
  76. ->where('sta_regtime >=', $startdate . " 00:00:00")
  77. ->where('sta_regtime <=', $enddate . " 23:59:59")
  78. ->group_by("DATE_FORMAT(sta_regtime, '%H')")
  79. ->order_by("hour ASC")
  80. ->get();
  81. $result_array = $result->result_array();
  82. foreach($result_array as $row)
  83. {
  84. $return['list'][$row['hour']] += $row['count'];
  85. $return['total'] += $row['count'];
  86. }
  87. foreach($return['list'] as $value)
  88. {
  89. $return['valuelist'][] = $value;
  90. }
  91. $return['hourlist'] = json_encode($return['hourlist'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  92. $return['valuelist'] = json_encode($return['valuelist'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  93. return $return;
  94. }
  95. /**
  96. * 기기별 통계
  97. * @param $startdate
  98. * @param $enddate
  99. * @return array
  100. */
  101. function statics_device($startdate, $enddate) {
  102. $return = array(
  103. "sum" => array(
  104. "total" => array(
  105. "count" => 0
  106. ),
  107. "pc" => array(
  108. "count" => 0,
  109. "percent" => 0
  110. ),
  111. "mobile" => array(
  112. "count" => 0,
  113. "percent" => 0
  114. )
  115. ),
  116. "list" => array(),
  117. "device_counts" => array(),
  118. "device_list" => array()
  119. );
  120. $startdate_s = new DateTime($startdate); // 20120101 같은 포맷도 잘됨
  121. $enddate_s = new DateTime($enddate);
  122. $diff = date_diff($startdate_s, $enddate_s);
  123. $diff = $diff->days;
  124. for($i=0; $i<=$diff; $i++)
  125. {
  126. $r_date = date('Y-m-d', strtotime("+{$i} days", strtotime($startdate)));
  127. $return['list'][ $r_date ]['pc'] = 0;
  128. $return['list'][ $r_date ]['mobile'] = 0;
  129. $return['list'][ $r_date ]['total'] = 0;
  130. }
  131. $result =
  132. $this->db->select("DATE_FORMAT(sta_regtime, '%Y-%m-%d') as date, sta_is_mobile, COUNT(*) AS `count`")
  133. ->from('statics')
  134. ->where('sta_regtime >=', $startdate . " 00:00:00")
  135. ->where('sta_regtime <=', $enddate . " 23:59:59")
  136. ->group_by("DATE_FORMAT(sta_regtime, '%Y-%m-%d'), sta_is_mobile")
  137. ->order_by("date ASC")
  138. ->get();
  139. $result_array = $result->result_array();
  140. foreach($result_array as $row)
  141. {
  142. if( $row['sta_is_mobile'] == 'Y' ) {
  143. $return['sum']['mobile']['count'] += (int)$row['count'];
  144. $return['list'][ $row['date'] ]['mobile'] = (int)$row['count'];
  145. }
  146. else {
  147. $return['sum']['pc']['count'] += (int)$row['count'];
  148. $return['list'][ $row['date'] ]['pc'] = (int)$row['count'];
  149. }
  150. $return['sum']['total']['count'] += (int)$row['count'];
  151. $return['list'][ $row['date'] ]['total'] += (int)$row['count'];
  152. }
  153. if( $return['sum']['total']['count'] > 0 )
  154. {
  155. $return['sum']['pc']['percent'] = round($return['sum']['pc']['count'] / $return['sum']['total']['count'] * 100, 2);
  156. $return['sum']['mobile']['percent'] = round($return['sum']['mobile']['count'] / $return['sum']['total']['count'] * 100, 2);
  157. }
  158. // 모바일 기기별
  159. $result =
  160. $this->db->from('statics')
  161. ->select('sta_mobile, COUNT(*) AS count')
  162. ->where('sta_regtime >=', $startdate . " 00:00:00")
  163. ->where('sta_regtime <=', $enddate . " 23:59:59")
  164. ->where('sta_mobile <>', '')
  165. ->group_by('sta_mobile')
  166. ->order_by('count DESC')
  167. ->get();
  168. $list = $result->result_array();
  169. foreach($list as $row)
  170. {
  171. $return['device_list'][] = $row['sta_mobile'];
  172. $return['device_counts'][] = (int)$row['count'];
  173. }
  174. $return['device_list'] = json_encode($return['device_list'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  175. $return['device_counts'] = json_encode($return['device_counts'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  176. return $return;
  177. }
  178. /**
  179. * 그룹별 통계
  180. * @param $column
  181. * @param $startdate
  182. * @param $enddate
  183. * @return mixed
  184. */
  185. function statics_group($column, $startdate, $enddate) {
  186. $result =
  187. $this->db->from('statics')
  188. ->select($column.', COUNT(*) AS count')
  189. ->where('sta_regtime >=', $startdate . " 00:00:00")
  190. ->where('sta_regtime <=', $enddate . " 23:59:59")
  191. ->where("{$column} <>",'')
  192. ->group_by($column)
  193. ->order_by('count DESC')
  194. ->get();
  195. $return['list'] = $result->result_array();
  196. $return[$column] = array();
  197. $return['counts'] = array();
  198. $return['total'] = 0;
  199. foreach($return['list'] as $row)
  200. {
  201. $return[$column][] = $row[$column];
  202. $return['counts'][] = (int)$row['count'];
  203. $return['total'] += (int)$row['count'];
  204. }
  205. $return[$column] = json_encode($return[$column], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  206. $return['counts'] = json_encode($return['counts'], JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
  207. return $return;
  208. }
  209. }