Authored by Lixiaodi

点击数统计 实时

... ... @@ -40,4 +40,7 @@ public interface IUnionClickLogsDAO {
List<UnionClickLogs> queryUnionClickLogsByParam(UnionLogsReqBO unionLogsReqBO);
List<UnionClickCount> queryCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
int queryCountInfo(@Param("unionType") Long unionType, @Param("clientType")String clientType, @Param("beginTime") int beginTime,@Param("endTime") int endTime);
}
\ No newline at end of file
... ...
... ... @@ -18,5 +18,8 @@ public interface UnionClickCountDayMapper {
int updateByPrimaryKey(UnionClickCountDay record);
int deleteCountInfoByTime(@Param("beginTime") int beginTime,@Param("endTime") int endTime);
int selectCount(@Param("unionType") String unionType, @Param("clientType") int clientType,
@Param("start") String start, @Param("end") String end);
}
\ No newline at end of file
... ...
... ... @@ -48,4 +48,9 @@
create_time &gt;= #{beginTime,jdbcType=INTEGER}
and create_time &lt; #{endTime,jdbcType=INTEGER}
</delete>
<select id="selectCount">
select sum(num) from union_click_count_day
where union_type = #{unionType,jdbcType=VARCHAR} and client_type = #{clientType,jdbcType=INTEGER} and
day_id &gt;= #{start,jdbcType=VARCHAR} and day_id &lt;= #{end,jdbcType=VARCHAR}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -279,4 +279,11 @@
group by
union_type,client_type;
</select>
<select id="queryCountInfo" resultType="java.lang.Integer">
select count(*) from union_click_logs
where union_type = #{unionType,jdbcType=BIGINT} and client_type = #{clientType,jdbcType=VARCHAR} and
create_time &gt;= #{beginTime,jdbcType=INTEGER}
and create_time &lt; #{endTime,jdbcType=INTEGER}
</select>
</mapper>
\ No newline at end of file
... ...
... ... @@ -43,6 +43,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
... ... @@ -155,6 +156,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
@Autowired
AppAdSourceMonitorAnaMapper appAdSourceMonitorAnaMapper;
@Autowired
private UnionClickCountDayMapper unionClickCountDayMapper;
// 记录付费渠道的redis
private static final String UNION_PAY_CHANNEL_KEY_PRE = "union:pay_channel:";
... ... @@ -176,6 +180,9 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
private static final int DEFAULT_QUEUECAPACITY = 20000;
private SimpleDateFormat formatDay = new SimpleDateFormat("yyyyMMdd");
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public UnionResponse clickUnion(ClickUnionRequestBO request) throws ServiceException {
try {
... ... @@ -716,11 +723,79 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
return pageActivateDeviceIdRspBO;
}
@SuppressWarnings("unchecked")
@Override
public Integer queryActivateCount(ActivateDeviceIdReqBO activateDeviceIdReqBO) {
log.info("enter queryActivateCount. param activateDeviceIdReqBO={}", activateDeviceIdReqBO);
Integer count = 0;
// if(StringUtils.isEmpty(activateDeviceIdReqBO.getActivateTime())){
Integer start = activateDeviceIdReqBO.getActivateTimeBegin();
Integer end = activateDeviceIdReqBO.getActivateTimeEnd();
Long unionType = activateDeviceIdReqBO.getUnionType();
String apptype = activateDeviceIdReqBO.getIsIdfa()>0 ? "ios" : "android";
if (unionType==null||start == null || end == null || start == 0 || end == 0 || end < start) {
log.info("enter queryActivateCount. param is empty ");
return 0;
}
Calendar time = Calendar.getInstance();
time.set(Calendar.HOUR_OF_DAY, 0);
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
int dayStart = (int) (time.getTimeInMillis()/1000);
int now = (int) (System.currentTimeMillis() / 1000);
int e = dayStart;
while (e < now) {
e += 3600;
}
e -= 3600 * 2;
log.info("queryActivateCount dayStart is {}, now is {}, lastH is {}", format.format(new Date(dayStart * 1000L)),
format.format(new Date(now * 1000L)), format.format(new Date(e * 1000L)));
String key = "yh:union:clickdata:" + unionType + ":" + formatDay.format(new Date(now * 1000L));
Map<Integer, Integer> map = redisValueCache.get(key, Map.class);
log.info("queryActivateCount key is {}, getValue is {}", key, map);
if (map == null) {
map = new HashMap<>();
}
boolean changed = false;
while (e >= dayStart) {
Integer c = map.get(e);
if (c == null) {
c = unionClickLogsDAO.queryCountInfo(unionType, apptype, e, e + 3600);
log.info(
"queryActivateCount load unionClickLogsDAO db unionType is {}, apptype is {}, start is {}, count is {}",
unionType, apptype, format.format(new Date(e * 1000L)), c);
} else {
break;
}
map.put(e, c);
changed = true;
e -= 3600;
}
if (changed) {
log.info("queryActivateCount add to redis key is {}, getValue is {}", key, map);
redisValueCache.set(key, map, 24, TimeUnit.HOURS);
}
int count = 0;
// 是否实时模式
if (end > dayStart) {
count += getTodayCount(unionType, apptype, Math.max(dayStart, start), end + 1, map);
}
if (start < dayStart) {
String beginDay = formatDay.format(new Date(start * 1000L));
String endDay = formatDay.format(new Date(Math.min(end, dayStart) * 1000L));
count += getDaysCount(unionType, apptype, beginDay, endDay);
log.info("queryActivateCount get history count unionType is {}, apptype is {}, beginDay is{}, endDay is {}",
unionType, apptype, beginDay, endDay);
}
/*// if(StringUtils.isEmpty(activateDeviceIdReqBO.getActivateTime())){
// activateDeviceIdReqBO.setActivateTimeStr(DateUtils.getTodayZero());
// }else{
// String dateStr = activateDeviceIdReqBO.getActivateTime().replaceAll("-","/");
... ... @@ -738,11 +813,61 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
// count = unionClickLogsDAO.queryActivateImeiCount(activateDeviceIdReqBO);
count = 0;
}
}
}*/
log.info("out queryActivateCount.count={}", count);
return count;
}
private int getDaysCount(Long unionType, String apptype, String beginDay, String endDay) {
int apptypeInt = apptype.equals("android") ? 1 : 2;
return unionClickCountDayMapper.selectCount(Long.toString(unionType), apptypeInt, beginDay, endDay);
}
private int getTodayCount(Long unionType, String apptype, int start, int end, Map<Integer, Integer> map) {
if (start >= end) {
return 0;
}
int max = (int) (System.currentTimeMillis() / 1000);
end = Math.min(max, end);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(start * 1000L);
time.set(Calendar.MINUTE, 0);
time.set(Calendar.SECOND, 0);
time.set(Calendar.MILLISECOND, 0);
int startHour = (int) (time.getTimeInMillis()/1000);
int count = 0;
log.info("queryActivateCount getTodayCount startHour is {}", new Date(startHour * 1000L));
if (startHour != start) {
int e = Math.min(startHour + 3600, end);
log.info("queryActivateCount getTodayCount param is {}, {}", new Date(start * 1000L), new Date(e * 1000L));
count += unionClickLogsDAO.queryCountInfo(unionType, apptype, start, e);
start = e;
}
if (start == end) {
return count;
}
while (start < end) {
Integer c = 0;
if (start + 3600 < end) {
log.info("queryActivateCount getTodayCount from redis param is {}", new Date(start * 1000L));
c = map.get(start);
if (c == null) {
c = 0;
}
} else {
log.info("queryActivateCount getTodayCount from db param is {}, {}", new Date(start * 1000L),
new Date(end * 1000L));
c = unionClickLogsDAO.queryCountInfo(unionType, apptype, start, end);
}
count += c;
start += 3600;
}
return count;
}
@Override
public PageActivateDeviceIdResponseBO queryByDeviceId(ActivateDeviceIdReqBO activateDeviceIdReqBO) {
log.info("enter queryByDeviceId. param activateDeviceIdReqBO={}", activateDeviceIdReqBO);
... ...