Commit be574645 authored by 廖旭伟's avatar 廖旭伟

优化设备点击事件统计结果,通过缓存方式获取

parent 9dd2b875
......@@ -33,4 +33,6 @@ public class RedisKey {
*/
public static final String KEY_CP_LIST_CACHE = "cpData";
public static final String KEY_DEVICE_EVENT_CLICK_CACHE = "device:event:click";
}
package com.mortals.xhx.daemon.task;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.module.page.service.PageEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 设备页面点击事件汇总缓存
*/
@Slf4j
@Service("PageClickEventCensusTask")
public class PageClickEventCensusTaskImpl implements ITaskExcuteService {
@Autowired
private PageEventService pageEventService;
@Override
public void excuteTask(ITask task) throws AppException {
log.info("刷新设备页面点击事件汇总缓存...");
pageEventService.doPageClickEventCensusCache();
log.info("完成刷新设备页面点击事件汇总缓存....");
}
@Override
public void stopTask(ITask task) throws AppException {
}
}
......@@ -55,4 +55,10 @@ public interface PageEventDao extends ICRUDDao<PageEventEntity,Long>{
*/
List<UsageCensusVo> getUsageCensus(PageCensusPdu pdu);
/**
* 统计设备点击事件
* @param pdu
* @return
*/
List<PageEventCensusVo> getPageClickEventCensus(PageCensusPdu pdu);
}
......@@ -51,5 +51,10 @@ public class PageEventDaoImpl extends BaseCRUDDaoMybatis<PageEventEntity,Long> i
return this.getSqlSession().selectList(this.getSqlId("getUsageCensus"), pdu);
}
@Override
public List<PageEventCensusVo> getPageClickEventCensus(PageCensusPdu pdu) {
return this.getSqlSession().selectList(this.getSqlId("getPageClickEventCensus"), pdu);
}
}
......@@ -26,4 +26,6 @@ public class PageEventCensusVo {
private Float takeTimeAvg;
private Integer total;
/*** 产品id */
private Long productId;
}
......@@ -5,7 +5,6 @@ import com.mortals.xhx.module.page.model.PageEventEntity;
import com.mortals.xhx.module.page.model.pdu.BuryPointPdu;
import com.mortals.xhx.module.page.model.pdu.PageCensusPdu;
import com.mortals.xhx.module.page.model.vo.PageEventCensusVo;
import com.mortals.xhx.module.page.model.vo.ProductHotCensusVo;
import com.mortals.xhx.module.page.model.vo.UsageCensusVo;
import java.util.List;
......@@ -51,4 +50,11 @@ public interface PageEventService extends ICRUDService<PageEventEntity,Long>{
*/
List<UsageCensusVo> getUsageCensus(PageCensusPdu pdu) throws AppException;
/**
* 统计设备点击事件写入内存
* @return
* @throws AppException
*/
List<PageEventCensusVo> doPageClickEventCensusCache() throws AppException;
}
\ No newline at end of file
package com.mortals.xhx.module.page.service.impl;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
import com.mortals.xhx.feign.device.IDeviceFeign;
import com.mortals.xhx.module.page.dao.PageAccessDepthDao;
import com.mortals.xhx.module.page.dao.PageEventDao;
import com.mortals.xhx.module.page.model.PageEventEntity;
import com.mortals.xhx.module.page.model.PageInfoEntity;
import com.mortals.xhx.module.page.model.PageInfoQuery;
import com.mortals.xhx.module.page.model.pdu.BuryPointPdu;
......@@ -9,16 +21,12 @@ import com.mortals.xhx.module.page.model.vo.AccessTrendCensusVo;
import com.mortals.xhx.module.page.model.vo.PageEventCensusVo;
import com.mortals.xhx.module.page.model.vo.ProductHotCensusVo;
import com.mortals.xhx.module.page.model.vo.UsageCensusVo;
import com.mortals.xhx.module.page.service.PageEventService;
import com.mortals.xhx.module.page.service.PageInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.page.dao.PageEventDao;
import com.mortals.xhx.module.page.model.PageEventEntity;
import com.mortals.xhx.module.page.service.PageEventService;
import org.springframework.util.ObjectUtils;
import java.text.DecimalFormat;
import java.text.ParseException;
......@@ -41,6 +49,11 @@ public class PageEventServiceImpl extends AbstractCRUDServiceImpl<PageEventDao,
@Autowired
private PageAccessDepthDao pageAccessDepthDao;
@Autowired
private IDeviceFeign deviceFeign;
@Autowired
private ICacheService cacheService;
@Override
protected void validData(PageEventEntity entity, Context context) throws AppException {
......@@ -52,6 +65,33 @@ public class PageEventServiceImpl extends AbstractCRUDServiceImpl<PageEventDao,
}
}
@Override
protected void saveAfter(PageEventEntity entity, Context context) throws AppException {
super.saveAfter(entity, context);
DevicePdu devicePdu = new DevicePdu();
devicePdu.setDeviceCode(entity.getDeviceNum());
Rest<RespData<List<DevicePdu>>> deviceRest = deviceFeign.list(devicePdu);
if (YesNoEnum.YES.getValue() == deviceRest.getCode()) {
List<DevicePdu> devicePduList = deviceRest.getData().getData();
if (!ObjectUtils.isEmpty(devicePduList)) {
DevicePdu device = devicePduList.get(0);
String key = device.getSiteId()+"_"+device.getProductId();
PageEventCensusVo pageEventCensusVo = cacheService.hget(RedisKey.KEY_DEVICE_EVENT_CLICK_CACHE,key,PageEventCensusVo.class);
if(ObjectUtils.isEmpty(pageEventCensusVo)){
pageEventCensusVo = new PageEventCensusVo();
pageEventCensusVo.setProductId(entity.getProductId());
pageEventCensusVo.setEventCode("Click");
pageEventCensusVo.setEventName("点击");
pageEventCensusVo.setTotal(1);
cacheService.hset(RedisKey.KEY_DEVICE_EVENT_CLICK_CACHE, key, pageEventCensusVo);
}else {
pageEventCensusVo.setTotal(pageEventCensusVo.getTotal()+1);
cacheService.hset(RedisKey.KEY_DEVICE_EVENT_CLICK_CACHE, key, pageEventCensusVo);
}
}
}
}
@Override
public PageEventEntity saveByPdu(BuryPointPdu pdu) {
if(pdu.getEventInfo()==null){
......@@ -244,4 +284,37 @@ public class PageEventServiceImpl extends AbstractCRUDServiceImpl<PageEventDao,
return list;
}
@Override
public List<PageEventCensusVo> doPageClickEventCensusCache() throws AppException {
DevicePdu devicePdu = new DevicePdu();
devicePdu.setSize(-1);
Rest<RespData<List<DevicePdu>>> deviceRest = deviceFeign.list(devicePdu);
if (YesNoEnum.YES.getValue() == deviceRest.getCode()) {
List<DevicePdu> devicePduList = deviceRest.getData().getData();
if (!ObjectUtils.isEmpty(devicePduList)) {
Map<String,List<String>> group = new HashMap<>();
for(DevicePdu device:devicePduList){
String key = device.getSiteId()+"_"+device.getProductId();
if(group.containsKey(key)){
group.get(key).add(device.getDeviceCode());
}else {
List<String> codeList = new ArrayList<>();
codeList.add(device.getDeviceCode());
group.put(key,codeList);
}
}
for (Map.Entry<String, List<String>> entry : group.entrySet()) {
PageCensusPdu query = new PageCensusPdu();
query.setDeviceNumList(entry.getValue());
List<PageEventCensusVo> list = dao.getPageClickEventCensus(query);
if(CollectionUtils.isNotEmpty(list)) {
cacheService.hset(RedisKey.KEY_DEVICE_EVENT_CLICK_CACHE, entry.getKey(), list.get(0));
}
}
}
}
return null;
}
}
\ No newline at end of file
......@@ -2,8 +2,10 @@ package com.mortals.xhx.module.page.web;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.key.RedisKey;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.device.DevicePdu;
import com.mortals.xhx.feign.device.IDeviceFeign;
......@@ -40,6 +42,9 @@ public class PageCensusController extends BaseJsonBodyController {
private PageAccessService pageAccessService;
@Autowired
private IDeviceFeign deviceFeign;
@Autowired
private ICacheService cacheService;
/**
* 事件分析
......@@ -328,4 +333,43 @@ public class PageCensusController extends BaseJsonBodyController {
ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
return ret;
}
/**
* 获取设备点击事件统计结果
* @param query
* @return
*/
@PostMapping({"getPageClickCensusCache"})
@UnAuth
public Rest<Object> getPageClickEventCensusCache(@RequestBody PageCensusPdu query) {
Rest<Object> ret = new Rest();
Map<String, Object> model = new HashMap();
String busiDesc = "获取设备点击事件统计结果";
int code=1;
try {
if(query.getSiteId()!=null&&query.getProductId()!=null) {
String key = query.getSiteId()+"_"+query.getProductId();
PageEventCensusVo pageEventCensusVo = cacheService.hget(RedisKey.KEY_DEVICE_EVENT_CLICK_CACHE,key,PageEventCensusVo.class);
if(null == pageEventCensusVo){
pageEventCensusVo = new PageEventCensusVo();
pageEventCensusVo.setProductId(query.getProductId());
pageEventCensusVo.setEventCode("Click");
pageEventCensusVo.setEventName("点击");
pageEventCensusVo.setTotal(0);
}
model.put("data", pageEventCensusVo);
model.put("message_info", busiDesc + "成功");
}
this.recordSysLog(this.request, busiDesc + " 【成功】");
} catch (Exception var9) {
code = -1;
this.doException(this.request, busiDesc, model, var9);
}
ret.setCode(code);
ret.setData(model);
ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
return ret;
}
}
......@@ -170,4 +170,27 @@
t.businessCode,
t.businessName
</select>
<!-- 设备点击事件统计 -->
<select id="getPageClickEventCensus" parameterType="com.mortals.xhx.module.page.model.pdu.PageCensusPdu" resultType="com.mortals.xhx.module.page.model.vo.PageEventCensusVo">
SELECT
t.productId,
t.eventCode,
t.eventName,
count(id) AS total
FROM
mortals_xhx_page_event t
WHERE
t.eventCode = 'Click'
<if test="deviceNumList!=null and deviceNumList.size() > 0">
AND t.deviceNum in
<foreach collection="deviceNumList" open="(" close=")" index="index" item="item" separator=",">
#{item}
</foreach>
</if>
GROUP BY
t.productId,
t.eventCode,
t.eventName
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment