Authored by unknown

Merge branch 'dev_log_day'

@@ -71,6 +71,9 @@ @@ -71,6 +71,9 @@
71 <value>/UnionRest/queryUnionOrders</value> 71 <value>/UnionRest/queryUnionOrders</value>
72 <value>/getProdcutDetail</value> 72 <value>/getProdcutDetail</value>
73 <value>/pushArticle</value> 73 <value>/pushArticle</value>
  74 + <value>/UnionClickCountDayRest/delete</value>
  75 + <value>/UnionClickCountDayRest/count</value>
  76 +
74 </list> 77 </list>
75 </property> 78 </property>
76 <property name="excludeMethods"> 79 <property name="excludeMethods">
@@ -4,6 +4,7 @@ package com.yoho.unions.dal; @@ -4,6 +4,7 @@ package com.yoho.unions.dal;
4 import com.yoho.service.model.union.request.ActivateDeviceIdReqBO; 4 import com.yoho.service.model.union.request.ActivateDeviceIdReqBO;
5 import com.yoho.service.model.union.request.UnionLogsReqBO; 5 import com.yoho.service.model.union.request.UnionLogsReqBO;
6 import com.yoho.service.model.union.response.ActivateDeviceIdRspBO; 6 import com.yoho.service.model.union.response.ActivateDeviceIdRspBO;
  7 +import com.yoho.unions.dal.model.UnionClickCount;
7 import com.yoho.unions.dal.model.UnionClickLogs; 8 import com.yoho.unions.dal.model.UnionClickLogs;
8 import org.apache.ibatis.annotations.Param; 9 import org.apache.ibatis.annotations.Param;
9 10
@@ -37,4 +38,6 @@ public interface IUnionClickLogsDAO { @@ -37,4 +38,6 @@ public interface IUnionClickLogsDAO {
37 int queryUnionClickLogsCountByParam(UnionLogsReqBO unionLogsReqBO); 38 int queryUnionClickLogsCountByParam(UnionLogsReqBO unionLogsReqBO);
38 39
39 List<UnionClickLogs> queryUnionClickLogsByParam(UnionLogsReqBO unionLogsReqBO); 40 List<UnionClickLogs> queryUnionClickLogsByParam(UnionLogsReqBO unionLogsReqBO);
  41 +
  42 + List<UnionClickCount> queryCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
40 } 43 }
  1 +package com.yoho.unions.dal;
  2 +
  3 +import java.util.List;
  4 +
  5 +import org.apache.ibatis.annotations.Param;
  6 +
  7 +import com.yoho.unions.dal.model.UnionClickCountDay;
  8 +
  9 +public interface UnionClickCountDayMapper {
  10 + int deleteByPrimaryKey(Integer id);
  11 +
  12 + int insert(UnionClickCountDay record);
  13 +
  14 + UnionClickCountDay selectByPrimaryKey(Integer id);
  15 +
  16 + List<UnionClickCountDay> selectAll();
  17 +
  18 + int updateByPrimaryKey(UnionClickCountDay record);
  19 +
  20 + int deleteCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
  21 +
  22 +}
  1 +package com.yoho.unions.dal.model;
  2 +import lombok.Data;
  3 +import lombok.ToString;
  4 +
  5 +@Data
  6 +@ToString
  7 +public class UnionClickCount {
  8 + private String unionType;
  9 + private String clientType;
  10 + private Integer num;
  11 +}
  1 +package com.yoho.unions.dal.model;
  2 +
  3 +public class UnionClickCountDay {
  4 + private Integer id;
  5 +
  6 + private String dayId;
  7 +
  8 + private String unionType;
  9 +
  10 + private Integer clientType;
  11 +
  12 + private Integer num;
  13 +
  14 + private String comb;
  15 +
  16 + private Integer createTime;
  17 +
  18 + public Integer getId() {
  19 + return id;
  20 + }
  21 +
  22 + public void setId(Integer id) {
  23 + this.id = id;
  24 + }
  25 +
  26 + public String getDayId() {
  27 + return dayId;
  28 + }
  29 +
  30 + public void setDayId(String dayId) {
  31 + this.dayId = dayId;
  32 + }
  33 +
  34 + public String getUnionType() {
  35 + return unionType;
  36 + }
  37 +
  38 + public void setUnionType(String unionType) {
  39 + this.unionType = unionType;
  40 + }
  41 +
  42 + public Integer getClientType() {
  43 + return clientType;
  44 + }
  45 +
  46 + public void setClientType(Integer clientType) {
  47 + this.clientType = clientType;
  48 + }
  49 +
  50 + public Integer getNum() {
  51 + return num;
  52 + }
  53 +
  54 + public void setNum(Integer num) {
  55 + this.num = num;
  56 + }
  57 +
  58 + public String getComb() {
  59 + return comb;
  60 + }
  61 +
  62 + public void setComb(String comb) {
  63 + this.comb = comb;
  64 + }
  65 +
  66 + public Integer getCreateTime() {
  67 + return createTime;
  68 + }
  69 +
  70 + public void setCreateTime(Integer createTime) {
  71 + this.createTime = createTime;
  72 + }
  73 +}
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3 +<mapper namespace="com.yoho.unions.dal.UnionClickCountDayMapper">
  4 + <resultMap id="BaseResultMap" type="com.yoho.unions.dal.model.UnionClickCountDay">
  5 + <id column="id" jdbcType="INTEGER" property="id" />
  6 + <result column="day_id" jdbcType="VARCHAR" property="dayId" />
  7 + <result column="union_type" jdbcType="VARCHAR" property="unionType" />
  8 + <result column="client_type" jdbcType="INTEGER" property="clientType" />
  9 + <result column="num" jdbcType="INTEGER" property="num" />
  10 + <result column="comb" jdbcType="VARCHAR" property="comb" />
  11 + <result column="create_time" jdbcType="INTEGER" property="createTime" />
  12 + </resultMap>
  13 + <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
  14 + delete from union_click_count_day
  15 + where id = #{id,jdbcType=INTEGER}
  16 + </delete>
  17 + <insert id="insert" parameterType="com.yoho.unions.dal.model.UnionClickCountDay">
  18 + insert into union_click_count_day (id, day_id, union_type,
  19 + client_type, num, comb,
  20 + create_time)
  21 + values (#{id,jdbcType=INTEGER}, #{dayId,jdbcType=VARCHAR}, #{unionType,jdbcType=VARCHAR},
  22 + #{clientType,jdbcType=INTEGER}, #{num,jdbcType=INTEGER}, #{comb,jdbcType=VARCHAR},
  23 + #{createTime,jdbcType=INTEGER})
  24 + </insert>
  25 + <update id="updateByPrimaryKey" parameterType="com.yoho.unions.dal.model.UnionClickCountDay">
  26 + update union_click_count_day
  27 + set day_id = #{dayId,jdbcType=VARCHAR},
  28 + union_type = #{unionType,jdbcType=VARCHAR},
  29 + client_type = #{clientType,jdbcType=INTEGER},
  30 + num = #{num,jdbcType=INTEGER},
  31 + comb = #{comb,jdbcType=VARCHAR},
  32 + create_time = #{createTime,jdbcType=INTEGER}
  33 + where id = #{id,jdbcType=INTEGER}
  34 + </update>
  35 + <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  36 + select id, day_id, union_type, client_type, num, comb, create_time
  37 + from union_click_count_day
  38 + where id = #{id,jdbcType=INTEGER}
  39 + </select>
  40 + <select id="selectAll" resultMap="BaseResultMap">
  41 + select id, day_id, union_type, client_type, num, comb, create_time
  42 + from union_click_count_day
  43 + </select>
  44 + <delete id="deleteCountInfoByTime">
  45 + delete
  46 + from union_click_count_day
  47 + where
  48 + create_time &gt;= #{beginTime,jdbcType=INTEGER}
  49 + and create_time &lt; #{endTime,jdbcType=INTEGER}
  50 + </delete>
  51 +</mapper>
@@ -271,4 +271,12 @@ @@ -271,4 +271,12 @@
271 and imei = #{imei} 271 and imei = #{imei}
272 </if> 272 </if>
273 </select> 273 </select>
  274 + <select id="queryCountInfoByTime" resultType="com.yoho.unions.dal.model.UnionClickCount" >
  275 + select union_type as unionType,client_type as clientType,count(*) as num from union_click_logs
  276 + where
  277 + create_time &gt;= #{beginTime,jdbcType=INTEGER}
  278 + and create_time &lt; #{endTime,jdbcType=INTEGER}
  279 + group by
  280 + union_type,client_type;
  281 + </select>
274 </mapper> 282 </mapper>
  1 +package com.yoho.unions.server.restapi;
  2 +
  3 +import javax.annotation.Resource;
  4 +
  5 +import org.slf4j.Logger;
  6 +import org.slf4j.LoggerFactory;
  7 +import org.springframework.stereotype.Controller;
  8 +import org.springframework.web.bind.annotation.RequestMapping;
  9 +import org.springframework.web.bind.annotation.ResponseBody;
  10 +
  11 +import com.yoho.unions.server.task.UnionClickCountDayTask;
  12 +
  13 +@Controller
  14 +@RequestMapping("/UnionClickCountDayRest")
  15 +public class UnionClickCountDayRest {
  16 +
  17 + private static Logger log = LoggerFactory.getLogger(UnionClickCountDayRest.class);
  18 +
  19 + @Resource
  20 + private UnionClickCountDayTask task;
  21 +
  22 + @RequestMapping(value = "/count")
  23 + @ResponseBody
  24 + public String count(Integer start, Integer end) {
  25 + log.info("enter UnionClickCountDayRest.count with start is {}, end is {}", start, end);
  26 + try {
  27 + return task.exeTask(start, end);
  28 + } catch (Exception e) {
  29 + return "error : " + e.getMessage();
  30 + }
  31 + }
  32 +
  33 + @RequestMapping(value = "/delete")
  34 + @ResponseBody
  35 + public String delete(Integer start, Integer end) {
  36 + log.info("enter UnionClickCountDayRest.delete with start is {}, end is {}", start, end);
  37 + try {
  38 + return task.deleteData(start, end);
  39 + } catch (Exception e) {
  40 + return "error : " + e.getMessage();
  41 + }
  42 + }
  43 +}
  1 +package com.yoho.unions.server.task;
  2 +
  3 +import java.text.SimpleDateFormat;
  4 +import java.util.Calendar;
  5 +import java.util.Date;
  6 +import java.util.HashMap;
  7 +import java.util.List;
  8 +import java.util.Map;
  9 +import java.util.concurrent.locks.Lock;
  10 +import java.util.concurrent.locks.ReentrantLock;
  11 +
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.dao.QueryTimeoutException;
  16 +import org.springframework.scheduling.annotation.Scheduled;
  17 +import org.springframework.stereotype.Component;
  18 +
  19 +import com.yoho.unions.dal.IUnionClickLogsDAO;
  20 +import com.yoho.unions.dal.UnionClickCountDayMapper;
  21 +import com.yoho.unions.dal.model.UnionClickCount;
  22 +import com.yoho.unions.dal.model.UnionClickCountDay;
  23 +
  24 +@Component
  25 +public class UnionClickCountDayTask {
  26 +
  27 + private Logger logger = LoggerFactory.getLogger(UnionClickCountDayTask.class);
  28 +
  29 + @Autowired
  30 + private UnionClickCountDayMapper unionClickCountDayMapper;
  31 +
  32 + @Autowired
  33 + private IUnionClickLogsDAO unionClickLogsDAO;
  34 +
  35 + private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  36 + private SimpleDateFormat formatDay = new SimpleDateFormat("yyyyMMdd");
  37 +
  38 + // 防止交错执行
  39 + private Lock lock = new ReentrantLock();
  40 +
  41 + // 每天00:05分执行
  42 + @Scheduled(cron = "* 05 0 * * ?")
  43 + //@Scheduled(cron = "* 0/2 * * * ?")
  44 + public void run() {
  45 +
  46 + Calendar time = Calendar.getInstance();
  47 + time.set(Calendar.HOUR_OF_DAY, 0);
  48 + time.set(Calendar.MINUTE, 0);
  49 + time.set(Calendar.SECOND, 0);
  50 + time.set(Calendar.MILLISECOND, 0);
  51 + int dayStart = (int) (time.getTimeInMillis() / 1000);
  52 + int begin = dayStart - 24 * 60 * 60;
  53 + int end = dayStart;
  54 +
  55 + exeTask(begin, end);
  56 + }
  57 +
  58 + public String exeTask(int begin, int end) {
  59 +
  60 + if (!lock.tryLock()) {
  61 + logger.warn("UnionClickCountDayTask run time={} a job has running", format.format(new Date()));
  62 + return "a job has running";
  63 + }
  64 +
  65 + try {
  66 +
  67 + if(end - begin > 24 * 60 * 60) {
  68 + logger.warn("UnionClickCountDayTask run time={} begin to end over a day", format.format(new Date()));
  69 + return "begin to end over a day";
  70 + }
  71 +
  72 + if(format.format(new Date(begin * 1000L)).equals(format.format(new Date(end * 1000L - 1)))) {
  73 + logger.warn("UnionClickCountDayTask run time={} begin and end not in same day", format.format(new Date()));
  74 + return "begin and end not in same day";
  75 + }
  76 +
  77 + int queryAcross = 5 * 60;
  78 + int timeoutRetryCount = 3;
  79 + logger.info("UnionClickCountDayTask run start={} , end={} , across={} , retry={}",
  80 + format.format(new Date(begin * 1000L)), format.format(new Date(end * 1000L - 1)), queryAcross,
  81 + timeoutRetryCount);
  82 + Map<String, UnionClickCount> map = new HashMap<>();
  83 +
  84 + long s = System.currentTimeMillis();
  85 + int count = 0;
  86 + // 当小于结束时间一直执行
  87 + while (begin < end) {
  88 + List<UnionClickCount> data = null;
  89 + for (int i = 0; i < timeoutRetryCount; i++) {
  90 + try {
  91 +
  92 + logger.info("UnionClickCountDayTask run start={} , end={} , across={} , now_retry={}",
  93 + format.format(new Date(begin * 1000L)), format.format(new Date(end * 1000L - 1)),
  94 + queryAcross, i);
  95 +
  96 + long exeStart = System.currentTimeMillis();
  97 + data = queryCountInfoByTime(begin, Math.min(begin + queryAcross, end));
  98 + begin += queryAcross;
  99 + long exeEnd = System.currentTimeMillis();
  100 + long use = exeEnd - exeStart;
  101 +
  102 + logger.info("UnionClickCountDayTask run start={} , end={} , across={} , use={}",
  103 + format.format(new Date(begin * 1000L)), format.format(new Date(end * 1000L - 1)),
  104 + queryAcross, use);
  105 + if (use < 700) {
  106 + logger.info("UnionClickCountDayTask change across from {} to {}", queryAcross,
  107 + queryAcross + 60);
  108 + queryAcross += 60;
  109 + }
  110 + if (use > 800) {
  111 + logger.info("UnionClickCountDayTask change across from {} to {}", queryAcross,
  112 + queryAcross - 60);
  113 + queryAcross -= 60;
  114 + }
  115 + // 执行成功,则不再重试
  116 + break;
  117 + } catch (QueryTimeoutException e) {
  118 + logger.warn("Exe queryCountInfoByTime start={}, end={} timeout!", begin,
  119 + Math.min(begin + queryAcross, end));
  120 + if (queryAcross <= 2) {
  121 + logger.warn("Exe queryCountInfoByTime queryAcross={} return", queryAcross);
  122 + return "queryCountInfoByTime queryAcross < 2";
  123 + }
  124 + queryAcross >>>= 2;
  125 + }
  126 + }
  127 +
  128 + if (data == null) {
  129 + logger.warn("Exe queryCountInfoByTime sleep start={}, end={} timeout!", begin,
  130 + Math.min(begin + queryAcross, end));
  131 + try {
  132 + Thread.sleep(1000);
  133 + } catch (InterruptedException e) {
  134 + }
  135 + } else {
  136 + logger.info("Exe queryCountInfoByTime data.size={} ", data.size());
  137 + addDataToMap(data, map);
  138 + }
  139 + }
  140 + // 把统计结果放到表里
  141 + logger.info("UnionClickCountDayTask run start to insert map.size={}", map.size());
  142 + String dayId = formatDay.format(new Date(begin * 1000L));
  143 + int successCount = 0;
  144 + int failedCount = 0;
  145 + for (Map.Entry<String, UnionClickCount> entry : map.entrySet()) {
  146 +
  147 + try {
  148 + UnionClickCount info = entry.getValue();
  149 + UnionClickCountDay bean = new UnionClickCountDay();
  150 + bean.setDayId(dayId);
  151 + bean.setUnionType(info.getUnionType());
  152 + bean.setClientType(getClientType(info.getClientType()));
  153 + bean.setNum(info.getNum());
  154 + bean.setCreateTime((int) (System.currentTimeMillis() / 1000));
  155 + bean.setComb(dayId + "_" + bean.getUnionType() + "_" + bean.getClientType());
  156 +
  157 + logger.info("UnionClickCountDayTask insert bean={}", bean);
  158 + unionClickCountDayMapper.insert(bean);
  159 + successCount++;
  160 + count += info.getNum();
  161 + } catch (Exception e) {
  162 + logger.warn("UnionClickCountDayTask insert error={}", e.getMessage());
  163 + failedCount++;
  164 + }
  165 + }
  166 + long e = System.currentTimeMillis();
  167 + long u = e - s;
  168 + logger.info("UnionClickCountDayTask run over data count={}, insert over! success={}, error={}, timeUse={}", count, successCount, failedCount, u);
  169 + return "UnionClickCountDayTask run over data count=" + count + ", insert success=" + successCount + ", error=" + failedCount+", timseUse="+u;
  170 + } finally {
  171 + lock.unlock();
  172 + }
  173 + }
  174 +
  175 + public String deleteData(int begin, int end) {
  176 +
  177 + if (!lock.tryLock()) {
  178 + logger.warn("UnionClickCountDayTask deleteData time={} a job has running", format.format(new Date()));
  179 + return "a job has running";
  180 + }
  181 +
  182 + try {
  183 +
  184 + if (end - begin > 24 * 60 * 60) {
  185 + logger.warn("UnionClickCountDayTask deleteData time={} begin to end over a day",
  186 + format.format(new Date()));
  187 + return "begin to end over a day";
  188 + }
  189 +
  190 + if (format.format(new Date(begin * 1000L)).equals(format.format(new Date(end * 1000L - 1)))) {
  191 + logger.warn("UnionClickCountDayTask deleteData time={} begin and end not in same day",
  192 + format.format(new Date()));
  193 + return "begin and end not in same day";
  194 + }
  195 +
  196 + int queryAcross = 5 * 60;
  197 + int timeoutRetryCount = 3;
  198 + logger.info("UnionClickCountDayTask deleteData start={} , end={} , across={} , retry={}",
  199 + format.format(new Date(begin * 1000L)), format.format(new Date(end * 1000L - 1)), queryAcross,
  200 + timeoutRetryCount);
  201 + Integer count = null;
  202 + try {
  203 + count = deleteCountInfoByTime(begin, end);
  204 + } catch (Exception e) {
  205 + logger.info("UnionClickCountDayTask deleteData error! msg={}", e.getMessage());
  206 + return "Error : " + e.getMessage();
  207 + }
  208 + logger.info("UnionClickCountDayTask deleteData over! deleteCount={}", count);
  209 + return "deleteData over, deleteCount=" + count;
  210 + } finally {
  211 + lock.unlock();
  212 + }
  213 + }
  214 +
  215 + private void addDataToMap(List<UnionClickCount> data, Map<String, UnionClickCount> map) {
  216 + for (UnionClickCount d : data) {
  217 + String key = d.getUnionType() + "_" + d.getClientType();
  218 + UnionClickCount store = map.get(key);
  219 + if (store == null) {
  220 + map.put(key, d);
  221 + } else {
  222 + store.setNum(store.getNum() + d.getNum());
  223 + }
  224 + }
  225 + }
  226 +
  227 + private List<UnionClickCount> queryCountInfoByTime(int start, int end) {
  228 + return unionClickLogsDAO.queryCountInfoByTime(start, end);
  229 + }
  230 +
  231 + private Integer deleteCountInfoByTime(int start, int end) {
  232 + return unionClickCountDayMapper.deleteCountInfoByTime(start, end);
  233 + }
  234 +
  235 + private Integer getClientType(String clientType) {
  236 + if (clientType == null) {
  237 + return 0;
  238 + }
  239 + if (clientType.equals("android")) {
  240 + return 1;
  241 + }
  242 + if (clientType.equals("ios")) {
  243 + return 2;
  244 + }
  245 + return 0;
  246 + }
  247 +}
@@ -68,6 +68,7 @@ datasources: @@ -68,6 +68,7 @@ datasources:
68 - com.yoho.unions.dal.IChannelReportFormDAO 68 - com.yoho.unions.dal.IChannelReportFormDAO
69 - com.yoho.unions.dal.IMktReportFormDAO 69 - com.yoho.unions.dal.IMktReportFormDAO
70 - com.yoho.unions.dal.MktUnionCodeMapper 70 - com.yoho.unions.dal.MktUnionCodeMapper
  71 + - com.yoho.unions.dal.UnionClickCountDayMapper
71 72
72 73
73 bigdata_yh_unions: 74 bigdata_yh_unions:
@@ -59,6 +59,7 @@ datasources: @@ -59,6 +59,7 @@ datasources:
59 - com.yoho.unions.dal.IChannelReportFormDAO 59 - com.yoho.unions.dal.IChannelReportFormDAO
60 - com.yoho.unions.dal.IMktReportFormDAO 60 - com.yoho.unions.dal.IMktReportFormDAO
61 - com.yoho.unions.dal.MktUnionCodeMapper 61 - com.yoho.unions.dal.MktUnionCodeMapper
  62 + - com.yoho.unions.dal.UnionClickCountDayMapper
62 63
63 64
64 bigdata_yh_unions: 65 bigdata_yh_unions: