ソースを参照

:feat:增加游戏监控日志

zhangxianyu 1 年間 前
コミット
5585f90861

+ 6 - 0
game-data/game-data-serve/pom.xml

@@ -133,6 +133,12 @@
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
         </dependency>
+
+        <!--sms集成-->
+        <dependency>
+            <groupId>com.zanxiang.module</groupId>
+            <artifactId>zx-sms</artifactId>
+        </dependency>
     </dependencies>
 
     <build>

+ 14 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/controller/GameMonitorAlarmController.java

@@ -1,6 +1,7 @@
 package com.zanxiang.game.data.serve.controller;
 
 import com.zanxiang.game.data.serve.service.IGameMonitorAlarmService;
+import com.zanxiang.game.data.serve.service.IOrderCostMonitorAlarmService;
 import com.zanxiang.module.util.pojo.ResultVO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -9,6 +10,8 @@ import org.springframework.web.bind.annotation.PutMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.annotation.Resource;
+
 /**
  * @author tianhua
  * @version 1.0
@@ -22,6 +25,8 @@ public class GameMonitorAlarmController {
 
     @Autowired
     private IGameMonitorAlarmService gameMonitorAlarmService;
+    @Resource
+    private IOrderCostMonitorAlarmService orderCostMonitorAlarmService;
 
     @ApiOperation(value = "发送钉钉消息")
     @PutMapping("/sendMsg")
@@ -29,4 +34,13 @@ public class GameMonitorAlarmController {
         return ResultVO.ok(gameMonitorAlarmService.sendMsgToUser());
     }
 
+
+    @ApiOperation(value = "监控")
+    @PutMapping("/monitor")
+    public ResultVO<Boolean> monitor() {
+        orderCostMonitorAlarmService.testSendSms("测试短信");
+//        orderCostMonitorAlarmService.monitorTencentCostStatus();
+//        orderCostMonitorAlarmService.monitorDataStatus();
+        return ResultVO.ok();
+    }
 }

+ 22 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/pojo/properties/SmsProperties.java

@@ -0,0 +1,22 @@
+package com.zanxiang.game.data.serve.pojo.properties;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+import java.util.Set;
+
+@ConfigurationProperties(
+        prefix = "ali-sms"
+)
+@Data
+@Configuration
+public class SmsProperties {
+
+    public static final String CONFIG_PREFIX = "ali-sms";
+    private String accessKeyId;
+    private String accessKeySecret;
+    private String defaultSignName;
+    private String defaultStstemErrorTemplate;
+
+}

+ 12 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/IOrderCostMonitorAlarmService.java

@@ -7,4 +7,16 @@ public interface IOrderCostMonitorAlarmService {
      * 监控数据状态
      */
     void monitorDataStatus();
+
+    /**
+     * 监控头条广告消耗表
+     */
+    void monitorHeadCostStatus();
+
+    /**
+     * 监控腾讯广告消耗表
+     */
+    void monitorTencentCostStatus();
+
+     boolean testSendSms(String content);
 }

+ 191 - 0
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/impl/OrderCostMonitorAlarmBySmsServiceImpl.java

@@ -0,0 +1,191 @@
+package com.zanxiang.game.data.serve.service.impl;
+
+import com.zanxiang.game.data.serve.pojo.properties.SmsProperties;
+import com.zanxiang.game.data.serve.service.IOrderCostMonitorAlarmService;
+import com.zanxiang.game.data.serve.utils.RedisUtil;
+import com.zanxiang.module.sms.pojo.SendResult;
+import com.zanxiang.module.sms.service.impl.AliSmsService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.nutz.dao.Cnd;
+import org.nutz.dao.Dao;
+import org.nutz.dao.Sqls;
+import org.nutz.dao.sql.Criteria;
+import org.nutz.dao.sql.Sql;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.util.*;
+
+@Service
+@Slf4j
+public class OrderCostMonitorAlarmBySmsServiceImpl implements IOrderCostMonitorAlarmService {
+
+    @Resource
+    private Dao dao;
+
+    @Resource
+    private RedisUtil<String> redisUtil;
+
+    @Autowired
+    private AliSmsService aliSmsService;
+
+    @Resource
+    private SmsProperties smsProperties;
+
+    private final String modelName = "game_data_serve";
+
+    private final String HEADLINE_COST_COUNT = modelName + ":" + "headline_cost_count";
+
+    private final String TENCENT_COST_COUNT = modelName + ":" + "tencent_cost_count";
+
+    @Value("${phoneNumber}")
+    private String phoneNumber;
+
+    /**
+     * 10分钟扫一次,每次扫当前时间-半小时
+     */
+    @Override
+    public void monitorDataStatus() {
+        //获取当前时间前半小时的时间
+        LocalDateTime date = LocalDateTime.now().minusMinutes(30);
+        //查询订单表
+        Sql orderSql = getSql(getOrderCountSql(date));
+        Integer count = orderSql.getInt(0);
+        //如果为0直接告警
+        if (count == 0) {
+            String msg = "前半小时订单表数据为空,请检查";
+            log.info(msg);
+            if(!sendSms(msg)){
+                throw new RuntimeException("短信发送失败");
+            }
+        }
+    }
+
+    @Override
+    public void monitorHeadCostStatus() {
+        //获取当前时间前半小时的时间
+        LocalDateTime date = LocalDateTime.now().minusMinutes(30);
+        //查询头条广告表
+        Sql headCostSql = getSql(getheadCostCountSql(date));
+        Integer newHeadCostCount = headCostSql.getInt(0);
+        //从redis中取旧的消耗值
+        String oldHeadCostCount = redisUtil.getCache(HEADLINE_COST_COUNT);
+        if(StringUtils.isNotBlank(oldHeadCostCount)){
+            //如果不为空,则比较
+            if (newHeadCostCount - Integer.parseInt(oldHeadCostCount) == 0) {
+                //如果没期间没消耗就告警
+                String msg = "前半小时头条广告数据为空,请检查";
+                log.info(msg);
+                if(!sendSms(msg)){
+                    throw new RuntimeException("短信发送失败");
+                }
+            }
+        }
+        //将新数据存入redis
+        redisUtil.setCache(HEADLINE_COST_COUNT, newHeadCostCount.toString(), 60 * 30);
+
+    }
+
+    @Override
+    public void monitorTencentCostStatus() {
+        //获取当前时间前半小时的时间
+        LocalDateTime date = LocalDateTime.now().minusMinutes(30);
+        //查询腾讯广告表
+        Sql tencentCostSql = getSql(getTencentCostSqlSql(date));
+        Integer newTencentCostCount = tencentCostSql.getInt(0);
+        //从redis中取旧的消耗值
+        String oldTencentCostCount = redisUtil.getCache(TENCENT_COST_COUNT);
+        if(StringUtils.isNotBlank(oldTencentCostCount)){
+            if (newTencentCostCount - Integer.parseInt(oldTencentCostCount) == 0) {
+                String msg = "前半小时腾讯广告数据为空,请检查";
+                log.info(msg);
+                if(!sendSms(msg)){
+                    throw new RuntimeException("短信发送失败");
+                }
+            }
+        }
+        //将新数据存入redis
+        redisUtil.setCache(TENCENT_COST_COUNT, newTencentCostCount.toString(), 60 * 30);
+    }
+
+
+
+    /**
+     * 发送短信
+     * @param content
+     * @return
+     */
+    private Boolean sendSms(String content) {
+        Date date = new Date();
+        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Map<String, String> params = new HashMap<>();
+        params.put("msg", content);
+        params.put("time",formatter.format(date));
+        //存储在配置文件
+        Set<String> numberList = new HashSet<>(Arrays.asList(phoneNumber.split(",")));
+
+        Map<String, SendResult> resultMap = aliSmsService.send(smsProperties.getDefaultSignName(), smsProperties.getDefaultStstemErrorTemplate(), params, numberList);
+        for (SendResult next : resultMap.values()) {
+            if (!next.isSuccess()) {
+                log.error("短信发送失败,失败原因:{}", next.getMsg());
+                return false;
+            }
+        }
+        return true;
+    }
+
+
+    public boolean testSendSms(String content) {
+        System.out.println(phoneNumber);
+        return false;
+//        return sendSms(content);
+    }
+
+
+
+    private Sql getSql(String sqlStr) {
+        Sql sql = Sqls.create(sqlStr);
+        sql.setCallback(Sqls.callback.integer());
+        dao.execute(sql);
+        return sql;
+    }
+
+    private String getTencentCostSqlSql(LocalDateTime date) {
+        Criteria cri = Cnd.cri();
+        cri.where().and("day", ">=", date.toLocalDate());
+        cri.where().and("hour", "=", date.getHour());
+        String sql = """
+                select sum(cost) from ods_ad_tencent_data_game.t_gdt_adgroups_data_hour
+                """ + cri;
+        return sql;
+    }
+
+    private String getheadCostCountSql(LocalDateTime date) {
+        Criteria cri = Cnd.cri();
+        cri.where().and("day", ">=", date.toLocalDate());
+        cri.where().and("hour", "=", date.getHour());
+        String sql = """
+                select sum(cost) from ods_ad_byte_data_game.t_ad_data_hour
+                """ + cri;
+        return sql;
+    }
+
+    private String getOrderCountSql(LocalDateTime localDateTime) {
+        Criteria criteria = Cnd.cri();
+        criteria.where().and("create_time", ">=", localDateTime);
+        criteria.where().and("source_system", "=", "ZX_ONE");
+        String sql = """
+                  select count(*) from dm_game_order.t_game_order
+                """ + criteria;
+        return sql;
+    }
+
+
+
+
+}

+ 0 - 40
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/service/impl/OrderCostMonitorAlarmServiceImpl.java

@@ -1,40 +0,0 @@
-package com.zanxiang.game.data.serve.service.impl;
-
-import com.zanxiang.game.data.serve.service.IOrderCostMonitorAlarmService;
-import com.zanxiang.game.data.serve.utils.RedisUtil;
-import org.nutz.dao.Dao;
-import org.springframework.stereotype.Service;
-
-import javax.annotation.Resource;
-import java.math.BigDecimal;
-
-@Service
-public class OrderCostMonitorAlarmServiceImpl implements IOrderCostMonitorAlarmService {
-
-    @Resource
-    private Dao dao;
-
-    @Resource
-    private RedisUtil<BigDecimal> redisUtil;
-
-    private final String modelName = "GAME_DATA_SERVE";
-
-    private final String HeadLine_Redis_Key = modelName+"-"+"HeadLine_COST_COUNT_Redis_Key";
-
-
-
-    /**
-     * 10分钟扫一次,每次扫当前时间-半小时
-     */
-    @Override
-    public void monitorDataStatus() {
-        //查询订单表
-
-
-        //查询头条广告表
-
-        //查询腾讯广告表
-
-
-    }
-}

+ 16 - 4
game-data/game-data-serve/src/main/java/com/zanxiang/game/data/serve/task/OrderCostMonitorAlarmTask.java

@@ -2,6 +2,7 @@ package com.zanxiang.game.data.serve.task;
 
 import com.zanxiang.game.data.serve.service.IOrderCostMonitorAlarmService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
@@ -21,17 +22,28 @@ public class OrderCostMonitorAlarmTask {
     @Resource
     private IOrderCostMonitorAlarmService  orderCostMonitorAlarmService;
 
+    @Value("${sys-config.task_is_run}")
+    private boolean run;
+
     /**
      * 任务每10分钟运行一次
      */
-//    @Scheduled(cron = "0 0/10 * * * ? ")
+    @Scheduled(cron = "0 0/10 * * * ? ")
     public void run() {
-        log.error("订单与消耗监控告警定时任务开始.");
+        if(!run){
+            return;
+        }
+        log.info("订单与消耗监控告警定时任务开始.");
         try {
-            //监控数据状态
+            //监控订单表
             orderCostMonitorAlarmService.monitorDataStatus();
+            //监控头条广告消耗表
+            orderCostMonitorAlarmService.monitorHeadCostStatus();
+            //监控腾讯广告消耗表
+            orderCostMonitorAlarmService.monitorTencentCostStatus();
+            log.info("订单与消耗监控告警定时任务结束.");
         } catch (Exception e) {
-            log.error("订单与消耗监控告警定时任务开始", e);
+            log.error("定时任务订单与消耗监控告警出错", e);
         }
     }
 }