From 2f841ad7a58ae93fd7b315263b7c4917366fde97 Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Wed, 2 Nov 2022 16:26:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E3=80=90=E6=B5=81=E9=87=8F?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1-=E6=9C=88=E3=80=91=E6=8A=A5=E8=A1=A8?= =?UTF-8?q?=E5=90=8E=E7=AB=AF=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../proxy/server/dal/FlowReportDayMapper.java | 8 ++ .../server/dal/FlowReportMonthMapper.java | 48 ++++++++ .../server/dal/entity/FlowReportDayDO.java | 2 +- .../server/dal/entity/FlowReportMonthDO.java | 72 ++++++++++++ .../server/job/FlowReportForMonthJob.java | 103 ++++++++++++++++++ .../src/main/resources/sql/job_info.data.sql | 4 +- 6 files changed, 235 insertions(+), 2 deletions(-) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportMonthMapper.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportMonthDO.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/job/FlowReportForMonthJob.java diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportDayMapper.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportDayMapper.java index 73d542b6..93f97587 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportDayMapper.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportDayMapper.java @@ -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.FlowReportDayDO; +import java.util.Date; +import java.util.List; + /** * @author: aoshiguchen * @date: 2022/10/28 @@ -45,4 +49,8 @@ public interface FlowReportDayMapper extends SqlMapper { @Delete("delete from flow_report_day where date_str = :dateStr") void deleteByDateStr(@Param("dateStr") String dateStr); + + @ResultType(FlowReportDayDO.class) + @Select("select * from flow_report_day where date >= :startDate and date <= :endDate") + List findListByDateRange(@Param("startDate") Date startDate, @Param("endDate") Date endDate); } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportMonthMapper.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportMonthMapper.java new file mode 100644 index 00000000..7528b1cb --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/FlowReportMonthMapper.java @@ -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.FlowReportMonthDO; + +/** + * @author: aoshiguchen + * @date: 2022/10/28 + */ +@Intercept(ignoreGlobal = true) +@Component +public interface FlowReportMonthMapper extends SqlMapper { + @Select("select * from flow_report_month where license_id = :licenseId and date_str = :dateStr") + FlowReportMonthDO findOne(@Param("licenseId") Integer licenseId, @Param("dateStr") String dateStr); + + @Insert("insert into flow_report_month(`user_id`,`license_id`,`write_bytes`,`read_bytes`,`date`,`date_str`,`create_time`) values(:userId,:licenseId,:writeBytes,:readBytes,:date,:dateStr,:createTime)") + void add(FlowReportMonthDO flowReportMonthDO); + + @Delete("delete from flow_report_month where date_str = :dateStr") + void deleteByDateStr(@Param("dateStr") String dateStr); +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportDayDO.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportDayDO.java index 9e6dee20..7da73929 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportDayDO.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportDayDO.java @@ -62,7 +62,7 @@ public class FlowReportDayDO { private Date date; /** * 报表统计时间 - * yyyy-MM-dd HH + * yyyy-MM-dd */ private String dateStr; /** diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportMonthDO.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportMonthDO.java new file mode 100644 index 00000000..312e0dbb --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/dal/entity/FlowReportMonthDO.java @@ -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_month") +public class FlowReportMonthDO { + @Id + private Integer id; + /** + * 用户ID + */ + private Integer userId; + /** + * licenseId + */ + private Integer licenseId; + /** + * 写入字节数 + */ + private Long writeBytes; + /** + * 读取字节数 + */ + private Long readBytes; + /** + * 报表统计时间 + */ + private Date date; + /** + * 报表统计时间 + * yyyy-MM + */ + private String dateStr; + /** + * 创建时间 + */ + private Date createTime; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/job/FlowReportForMonthJob.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/job/FlowReportForMonthJob.java new file mode 100644 index 00000000..0d440387 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/job/FlowReportForMonthJob.java @@ -0,0 +1,103 @@ +/** + * 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.*; +import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportDayDO; +import fun.asgc.neutrino.proxy.server.dal.entity.FlowReportMonthDO; +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 = "FlowReportForMonthJob", cron = "0 10 0 1 * ?", param = "") +public class FlowReportForMonthJob implements IJobHandler { + @Autowired + private FlowReportService flowReportService; + @Autowired + private LicenseMapper licenseMapper; + @Autowired + private FlowReportMinuteMapper flowReportMinuteMapper; + @Autowired + private FlowReportHourMapper flowReportHourMapper; + @Autowired + private FlowReportDayMapper flowReportDayMapper; + @Autowired + private FlowReportMonthMapper flowReportHourDOList; + + @Override + public void execute(String param) throws Exception { + Date now = new Date(); + String dateStr = DateUtil.format(DateUtil.addDate(now, Calendar.MONTH, -1), "yyyy-MM"); + Date date = DateUtil.parse(dateStr, "yyyy-MM"); + Date startDayDate = DateUtil.getDayBegin(date); + Date endEndDate = DateUtil.getDayEnd(date); + + // 删除原来的记录 + flowReportDayMapper.deleteByDateStr(dateStr); + + // 查询上个月的天级别统计数据 + List flowReportDayList = flowReportDayMapper.findListByDateRange(startDayDate, endEndDate); + if (CollectionUtil.isEmpty(flowReportDayList)) { + return; + } + + // 汇总前一个天的天级别统计数据 + Map map = new HashMap<>(); + for (FlowReportDayDO item : flowReportDayList) { + FlowReportMonthDO report = map.get(item.getLicenseId()); + if (null == report) { + report = new FlowReportMonthDO(); + 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 (FlowReportMonthDO item : map.values()) { + flowReportHourDOList.add(item); + } + } + +} diff --git a/neutrino-proxy-server/src/main/resources/sql/job_info.data.sql b/neutrino-proxy-server/src/main/resources/sql/job_info.data.sql index 67320131..aad12b24 100644 --- a/neutrino-proxy-server/src/main/resources/sql/job_info.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/job_info.data.sql @@ -8,4 +8,6 @@ INSERT INTO job_info(`id`, `desc`, `handler`, `cron`, `param`, `enable`, `create INSERT INTO job_info(`id`, `desc`, `handler`, `cron`, `param`, `enable`, `create_time`, `update_time`) VALUES (4, '流量统计报表-小时', 'FlowReportForHourJob', '0 0 */1 * * ?', '', 1, STRFTIME('%s000', 'NOW'), STRFTIME('%s000', 'NOW')); INSERT INTO job_info(`id`, `desc`, `handler`, `cron`, `param`, `enable`, `create_time`, `update_time`) VALUES -(5, '流量统计报表-天', 'FlowReportForDayJob', '0 0 1 * * ?', '', 1, STRFTIME('%s000', 'NOW'), STRFTIME('%s000', 'NOW')); \ No newline at end of file +(5, '流量统计报表-天', 'FlowReportForDayJob', '0 0 1 * * ?', '', 1, STRFTIME('%s000', 'NOW'), STRFTIME('%s000', 'NOW')); +INSERT INTO job_info(`id`, `desc`, `handler`, `cron`, `param`, `enable`, `create_time`, `update_time`) VALUES +(6, '流量统计报表-月', 'FlowReportForMonthJob', '0 10 0 1 * ?', '', 1, STRFTIME('%s000', 'NOW'), STRFTIME('%s000', 'NOW')); \ No newline at end of file