Authored by Lixiaodi

点击数统计 实时

@@ -40,4 +40,7 @@ public interface IUnionClickLogsDAO { @@ -40,4 +40,7 @@ public interface IUnionClickLogsDAO {
40 List<UnionClickLogs> queryUnionClickLogsByParam(UnionLogsReqBO unionLogsReqBO); 40 List<UnionClickLogs> queryUnionClickLogsByParam(UnionLogsReqBO unionLogsReqBO);
41 41
42 List<UnionClickCount> queryCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime); 42 List<UnionClickCount> queryCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
  43 +
  44 + int queryCountInfo(@Param("unionType") Long unionType, @Param("clientType")String clientType, @Param("beginTime") int beginTime,@Param("endTime") int endTime);
  45 +
43 } 46 }
@@ -19,4 +19,7 @@ public interface UnionClickCountDayMapper { @@ -19,4 +19,7 @@ public interface UnionClickCountDayMapper {
19 19
20 int deleteCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime); 20 int deleteCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
21 21
  22 + int selectCount(@Param("unionType") String unionType, @Param("clientType") int clientType,
  23 + @Param("start") String start, @Param("end") String end);
  24 +
22 } 25 }
@@ -48,4 +48,9 @@ @@ -48,4 +48,9 @@
48 create_time &gt;= #{beginTime,jdbcType=INTEGER} 48 create_time &gt;= #{beginTime,jdbcType=INTEGER}
49 and create_time &lt; #{endTime,jdbcType=INTEGER} 49 and create_time &lt; #{endTime,jdbcType=INTEGER}
50 </delete> 50 </delete>
  51 + <select id="selectCount">
  52 + select sum(num) from union_click_count_day
  53 + where union_type = #{unionType,jdbcType=VARCHAR} and client_type = #{clientType,jdbcType=INTEGER} and
  54 + day_id &gt;= #{start,jdbcType=VARCHAR} and day_id &lt;= #{end,jdbcType=VARCHAR}
  55 + </select>
51 </mapper> 56 </mapper>
@@ -279,4 +279,11 @@ @@ -279,4 +279,11 @@
279 group by 279 group by
280 union_type,client_type; 280 union_type,client_type;
281 </select> 281 </select>
  282 +
  283 + <select id="queryCountInfo" resultType="java.lang.Integer">
  284 + select count(*) from union_click_logs
  285 + where union_type = #{unionType,jdbcType=BIGINT} and client_type = #{clientType,jdbcType=VARCHAR} and
  286 + create_time &gt;= #{beginTime,jdbcType=INTEGER}
  287 + and create_time &lt; #{endTime,jdbcType=INTEGER}
  288 + </select>
282 </mapper> 289 </mapper>
@@ -43,6 +43,7 @@ import org.springframework.stereotype.Service; @@ -43,6 +43,7 @@ import org.springframework.stereotype.Service;
43 43
44 import javax.annotation.Resource; 44 import javax.annotation.Resource;
45 import java.net.URLDecoder; 45 import java.net.URLDecoder;
  46 +import java.text.SimpleDateFormat;
46 import java.util.*; 47 import java.util.*;
47 import java.util.concurrent.ExecutorService; 48 import java.util.concurrent.ExecutorService;
48 import java.util.concurrent.LinkedBlockingQueue; 49 import java.util.concurrent.LinkedBlockingQueue;
@@ -156,6 +157,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -156,6 +157,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
156 @Autowired 157 @Autowired
157 AppAdSourceMonitorAnaMapper appAdSourceMonitorAnaMapper; 158 AppAdSourceMonitorAnaMapper appAdSourceMonitorAnaMapper;
158 159
  160 + @Autowired
  161 + private UnionClickCountDayMapper unionClickCountDayMapper;
  162 +
159 // 记录付费渠道的redis 163 // 记录付费渠道的redis
160 private static final String UNION_PAY_CHANNEL_KEY_PRE = "union:pay_channel:"; 164 private static final String UNION_PAY_CHANNEL_KEY_PRE = "union:pay_channel:";
161 165
@@ -176,6 +180,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -176,6 +180,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
176 180
177 private static final int DEFAULT_QUEUECAPACITY = 20000; 181 private static final int DEFAULT_QUEUECAPACITY = 20000;
178 182
  183 + private SimpleDateFormat formatDay = new SimpleDateFormat("yyyyMMdd");
  184 + private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  185 +
179 @Override 186 @Override
180 public UnionResponse clickUnion(ClickUnionRequestBO request) throws ServiceException { 187 public UnionResponse clickUnion(ClickUnionRequestBO request) throws ServiceException {
181 try { 188 try {
@@ -716,11 +723,79 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -716,11 +723,79 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
716 return pageActivateDeviceIdRspBO; 723 return pageActivateDeviceIdRspBO;
717 } 724 }
718 725
  726 + @SuppressWarnings("unchecked")
719 @Override 727 @Override
720 public Integer queryActivateCount(ActivateDeviceIdReqBO activateDeviceIdReqBO) { 728 public Integer queryActivateCount(ActivateDeviceIdReqBO activateDeviceIdReqBO) {
721 log.info("enter queryActivateCount. param activateDeviceIdReqBO={}", activateDeviceIdReqBO); 729 log.info("enter queryActivateCount. param activateDeviceIdReqBO={}", activateDeviceIdReqBO);
722 - Integer count = 0;  
723 -// if(StringUtils.isEmpty(activateDeviceIdReqBO.getActivateTime())){ 730 + Integer start = activateDeviceIdReqBO.getActivateTimeBegin();
  731 + Integer end = activateDeviceIdReqBO.getActivateTimeEnd();
  732 + Long unionType = activateDeviceIdReqBO.getUnionType();
  733 + String apptype = activateDeviceIdReqBO.getIsIdfa()>0 ? "ios" : "android";
  734 + if (unionType==null||start == null || end == null || start == 0 || end == 0 || end < start) {
  735 + log.info("enter queryActivateCount. param is empty ");
  736 + return 0;
  737 + }
  738 +
  739 + Calendar time = Calendar.getInstance();
  740 + time.set(Calendar.HOUR_OF_DAY, 0);
  741 + time.set(Calendar.MINUTE, 0);
  742 + time.set(Calendar.SECOND, 0);
  743 + time.set(Calendar.MILLISECOND, 0);
  744 +
  745 + int dayStart = (int) (time.getTimeInMillis()/1000);
  746 +
  747 + int now = (int) (System.currentTimeMillis() / 1000);
  748 + int e = dayStart;
  749 + while (e < now) {
  750 + e += 3600;
  751 + }
  752 + e -= 3600 * 2;
  753 +
  754 + log.info("queryActivateCount dayStart is {}, now is {}, lastH is {}", format.format(new Date(dayStart * 1000L)),
  755 + format.format(new Date(now * 1000L)), format.format(new Date(e * 1000L)));
  756 +
  757 + String key = "yh:union:clickdata:" + unionType + ":" + formatDay.format(new Date(now * 1000L));
  758 + Map<Integer, Integer> map = redisValueCache.get(key, Map.class);
  759 +
  760 + log.info("queryActivateCount key is {}, getValue is {}", key, map);
  761 + if (map == null) {
  762 + map = new HashMap<>();
  763 + }
  764 +
  765 + boolean changed = false;
  766 +
  767 + while (e >= dayStart) {
  768 + Integer c = map.get(e);
  769 + if (c == null) {
  770 + c = unionClickLogsDAO.queryCountInfo(unionType, apptype, e, e + 3600);
  771 + log.info(
  772 + "queryActivateCount load unionClickLogsDAO db unionType is {}, apptype is {}, start is {}, count is {}",
  773 + unionType, apptype, format.format(new Date(e * 1000L)), c);
  774 + } else {
  775 + break;
  776 + }
  777 + map.put(e, c);
  778 + changed = true;
  779 + e -= 3600;
  780 + }
  781 + if (changed) {
  782 + log.info("queryActivateCount add to redis key is {}, getValue is {}", key, map);
  783 + redisValueCache.set(key, map, 24, TimeUnit.HOURS);
  784 + }
  785 + int count = 0;
  786 + // 是否实时模式
  787 + if (end > dayStart) {
  788 + count += getTodayCount(unionType, apptype, Math.max(dayStart, start), end + 1, map);
  789 + }
  790 + if (start < dayStart) {
  791 + String beginDay = formatDay.format(new Date(start * 1000L));
  792 + String endDay = formatDay.format(new Date(Math.min(end, dayStart) * 1000L));
  793 + count += getDaysCount(unionType, apptype, beginDay, endDay);
  794 + log.info("queryActivateCount get history count unionType is {}, apptype is {}, beginDay is{}, endDay is {}",
  795 + unionType, apptype, beginDay, endDay);
  796 + }
  797 +
  798 +/*// if(StringUtils.isEmpty(activateDeviceIdReqBO.getActivateTime())){
724 // activateDeviceIdReqBO.setActivateTimeStr(DateUtils.getTodayZero()); 799 // activateDeviceIdReqBO.setActivateTimeStr(DateUtils.getTodayZero());
725 // }else{ 800 // }else{
726 // String dateStr = activateDeviceIdReqBO.getActivateTime().replaceAll("-","/"); 801 // String dateStr = activateDeviceIdReqBO.getActivateTime().replaceAll("-","/");
@@ -738,11 +813,61 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher @@ -738,11 +813,61 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
738 // count = unionClickLogsDAO.queryActivateImeiCount(activateDeviceIdReqBO); 813 // count = unionClickLogsDAO.queryActivateImeiCount(activateDeviceIdReqBO);
739 count = 0; 814 count = 0;
740 } 815 }
741 - } 816 + }*/
742 log.info("out queryActivateCount.count={}", count); 817 log.info("out queryActivateCount.count={}", count);
743 return count; 818 return count;
744 } 819 }
745 820
  821 + private int getDaysCount(Long unionType, String apptype, String beginDay, String endDay) {
  822 + int apptypeInt = apptype.equals("android") ? 1 : 2;
  823 + return unionClickCountDayMapper.selectCount(Long.toString(unionType), apptypeInt, beginDay, endDay);
  824 + }
  825 +
  826 + private int getTodayCount(Long unionType, String apptype, int start, int end, Map<Integer, Integer> map) {
  827 + if (start >= end) {
  828 + return 0;
  829 + }
  830 +
  831 + int max = (int) (System.currentTimeMillis() / 1000);
  832 + end = Math.min(max, end);
  833 +
  834 + Calendar time = Calendar.getInstance();
  835 + time.setTimeInMillis(start * 1000L);
  836 + time.set(Calendar.MINUTE, 0);
  837 + time.set(Calendar.SECOND, 0);
  838 + time.set(Calendar.MILLISECOND, 0);
  839 +
  840 + int startHour = (int) (time.getTimeInMillis()/1000);
  841 + int count = 0;
  842 + log.info("queryActivateCount getTodayCount startHour is {}", new Date(startHour * 1000L));
  843 + if (startHour != start) {
  844 + int e = Math.min(startHour + 3600, end);
  845 + log.info("queryActivateCount getTodayCount param is {}, {}", new Date(start * 1000L), new Date(e * 1000L));
  846 + count += unionClickLogsDAO.queryCountInfo(unionType, apptype, start, e);
  847 + start = e;
  848 + }
  849 + if (start == end) {
  850 + return count;
  851 + }
  852 + while (start < end) {
  853 + Integer c = 0;
  854 + if (start + 3600 < end) {
  855 + log.info("queryActivateCount getTodayCount from redis param is {}", new Date(start * 1000L));
  856 + c = map.get(start);
  857 + if (c == null) {
  858 + c = 0;
  859 + }
  860 + } else {
  861 + log.info("queryActivateCount getTodayCount from db param is {}, {}", new Date(start * 1000L),
  862 + new Date(end * 1000L));
  863 + c = unionClickLogsDAO.queryCountInfo(unionType, apptype, start, end);
  864 + }
  865 + count += c;
  866 + start += 3600;
  867 + }
  868 + return count;
  869 + }
  870 +
746 @Override 871 @Override
747 public PageActivateDeviceIdResponseBO queryByDeviceId(ActivateDeviceIdReqBO activateDeviceIdReqBO) { 872 public PageActivateDeviceIdResponseBO queryByDeviceId(ActivateDeviceIdReqBO activateDeviceIdReqBO) {
748 log.info("enter queryByDeviceId. param activateDeviceIdReqBO={}", activateDeviceIdReqBO); 873 log.info("enter queryByDeviceId. param activateDeviceIdReqBO={}", activateDeviceIdReqBO);