新增【流量统计-天】报表后端逻辑

This commit is contained in:
aoshiguchen
2022-11-01 10:01:54 +08:00
parent 8fce7d1677
commit aadee74d8e
6 changed files with 241 additions and 6 deletions
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.Param;
import fun.asgc.neutrino.core.aop.Intercept;
import fun.asgc.neutrino.core.db.annotation.Delete;
import fun.asgc.neutrino.core.db.annotation.Insert;
import fun.asgc.neutrino.core.db.annotation.Select;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportDayDO;
/**
* @author: aoshiguchen
* @date: 2022/10/28
*/
@Intercept(ignoreGlobal = true)
@Component
public interface FlowReportDayMapper extends SqlMapper {
@Select("select * from flow_report_day where license_id = :licenseId and date_str = :dateStr")
FlowReportDayDO findOne(@Param("licenseId") Integer licenseId, @Param("dateStr") String dateStr);
@Insert("insert into flow_report_day(`user_id`,`license_id`,`write_bytes`,`read_bytes`,`date`,`date_str`,`create_time`) values(:userId,:licenseId,:writeBytes,:readBytes,:date,:dateStr,:createTime)")
void add(FlowReportDayDO flowReportDayDO);
@Delete("delete from flow_report_day where date_str = :dateStr")
void deleteByDateStr(@Param("dateStr") String dateStr);
}
@@ -26,10 +26,14 @@ import fun.asgc.neutrino.core.annotation.Param;
import fun.asgc.neutrino.core.aop.Intercept;
import fun.asgc.neutrino.core.db.annotation.Delete;
import fun.asgc.neutrino.core.db.annotation.Insert;
import fun.asgc.neutrino.core.db.annotation.ResultType;
import fun.asgc.neutrino.core.db.annotation.Select;
import fun.asgc.neutrino.core.db.mapper.SqlMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportHourDO;
import java.util.Date;
import java.util.List;
/**
* @author: aoshiguchen
* @date: 2022/10/28
@@ -37,12 +41,17 @@ import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportHourDO;
@Intercept(ignoreGlobal = true)
@Component
public interface FlowReportHourMapper extends SqlMapper {
@Select("select * from flow_report_hour where license_id = :licenseId and date = :date")
FlowReportHourDO findOne(@Param("licenseId") Integer licenseId, @Param("date") String date);
@Select("select * from flow_report_hour where license_id = :licenseId and date_str = :dateStr")
FlowReportHourDO findOne(@Param("licenseId") Integer licenseId, @Param("dateStr") String dateStr);
@Insert("insert into flow_report_hour(`user_id`,`license_id`,`write_bytes`,`read_bytes`,`date`,`date_str`,`create_time`) values(:userId,:licenseId,:writeBytes,:readBytes,:date,:dateStr,:createTime)")
void add(FlowReportHourDO flowReportHourDO);
@Delete("delete from flow_report_hour where date_str = :dateStr")
void deleteByDateStr(@Param("dateStr") String dateStr);
@ResultType(FlowReportHourDO.class)
@Select("select * from flow_report_hour where date >= :startDate and date <= :endDate")
List<FlowReportHourDO> findListByDateRange(@Param("startDate") Date startDate, @Param("endDate") Date endDate);
}
@@ -0,0 +1,72 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.dal.entity;
import fun.asgc.neutrino.core.db.annotation.Id;
import fun.asgc.neutrino.core.db.annotation.Table;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.util.Date;
/**
* @author: aoshiguchen
* @date: 2022/10/24
*/
@ToString
@Accessors(chain = true)
@Data
@Table("flow_report_day")
public class FlowReportDayDO {
@Id
private Integer id;
/**
* 用户ID
*/
private Integer userId;
/**
* licenseId
*/
private Integer licenseId;
/**
* 写入字节数
*/
private Long writeBytes;
/**
* 读取字节数
*/
private Long readBytes;
/**
* 报表统计时间
*/
private Date date;
/**
* 报表统计时间
* yyyy-MM-dd HH
*/
private String dateStr;
/**
* 创建时间
*/
private Date createTime;
}
@@ -0,0 +1,104 @@
/**
* Copyright (c) 2022 aoshiguchen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package fun.asgc.neutrino.proxy.server.job;
import fun.asgc.neutrino.core.annotation.Autowired;
import fun.asgc.neutrino.core.annotation.Component;
import fun.asgc.neutrino.core.annotation.NonIntercept;
import fun.asgc.neutrino.core.quartz.IJobHandler;
import fun.asgc.neutrino.core.quartz.annotation.JobHandler;
import fun.asgc.neutrino.core.util.CollectionUtil;
import fun.asgc.neutrino.core.util.DateUtil;
import fun.asgc.neutrino.proxy.server.dal.FlowReportDayMapper;
import fun.asgc.neutrino.proxy.server.dal.FlowReportHourMapper;
import fun.asgc.neutrino.proxy.server.dal.FlowReportMinuteMapper;
import fun.asgc.neutrino.proxy.server.dal.LicenseMapper;
import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportDayDO;
import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportHourDO;
import fun.asgc.neutrino.proxy.server.service.FlowReportService;
import lombok.extern.slf4j.Slf4j;
import java.util.*;
/**
* @author: aoshiguchen
* @date: 2022/10/28
*/
@Slf4j
@NonIntercept
@Component
@JobHandler(name = "FlowReportForDayJob", cron = "0 0 1 * * ?", param = "")
public class FlowReportForDayJob implements IJobHandler {
@Autowired
private FlowReportService flowReportService;
@Autowired
private LicenseMapper licenseMapper;
@Autowired
private FlowReportMinuteMapper flowReportMinuteMapper;
@Autowired
private FlowReportHourMapper flowReportHourMapper;
@Autowired
private FlowReportDayMapper flowReportDayMapper;
@Override
public void execute(String param) throws Exception {
Date now = new Date();
String dateStr = DateUtil.format(DateUtil.addDate(now, Calendar.DATE, -1), "yyyy-MM-dd");
Date date = DateUtil.parse(dateStr, "yyyy-MM-dd");
Date startHourDate = DateUtil.getDayBegin(date);
Date endHourDate = DateUtil.getDayEnd(date);
// 删除原来的记录
flowReportDayMapper.deleteByDateStr(dateStr);
// 查询前一天的小时级别统计数据
List<FlowReportHourDO> flowReportHourDOList = flowReportHourMapper.findListByDateRange(startHourDate, endHourDate);
if (CollectionUtil.isEmpty(flowReportHourDOList)) {
return;
}
// 汇总前一个天的天级别统计数据
Map<Integer, FlowReportDayDO> map = new HashMap<>();
for (FlowReportHourDO item : flowReportHourDOList) {
FlowReportDayDO report = map.get(item.getLicenseId());
if (null == report) {
report = new FlowReportDayDO();
map.put(item.getLicenseId(), report);
}
Long writeBytes = report.getWriteBytes() == null ? 0 : report.getWriteBytes();
Long readBytes = report.getReadBytes() == null ? 0 : report.getReadBytes();
report.setUserId(item.getUserId());
report.setLicenseId(item.getLicenseId());
report.setWriteBytes(writeBytes + item.getWriteBytes());
report.setReadBytes(readBytes + item.getReadBytes());
report.setDate(date);
report.setDateStr(dateStr);
report.setCreateTime(now);
}
for (FlowReportDayDO item : map.values()) {
flowReportDayMapper.add(item);
}
}
}