Browse Source

统计数据转存部分增加对客户端上报日志的转存代码.

王刚 3 years ago
parent
commit
468e3edd1f

+ 22 - 22
CSserver/DataTransfer/Program.cs

@@ -18,7 +18,7 @@ namespace DataTransfer
         {
             Console.WriteLine("言灵数据传输开始........");
 
-            Thread t1 = new Thread(transfer);         
+            Thread t1 = new Thread(transfer);
             t1.Start();
 
             //dataSav();
@@ -39,14 +39,14 @@ namespace DataTransfer
             while (true)
             {
                 try
-                {                               
-                    int hour = DateTime.Now.Hour;            
+                {
+                    int hour = DateTime.Now.Hour;
                     if (hour == 2 && mask == 0)//正式的是晚上2点进行数据传输
                     {
                         Console.WriteLine("数据传输的时间到了开始执行程序.......");
                         //进行数据传输
-                        mask = 1;                        
-                        dataTransfer();                      
+                        mask = 1;
+                        dataTransfer();
                     }
 
                     if (hour != 2 && mask != 0)
@@ -61,7 +61,7 @@ namespace DataTransfer
                         Console.WriteLine("统计信息程序开始执行............");
                         //进行数据传输
                         mask_lose = 1;
-                        UserStatistics.DataDeal();                     
+                        UserStatistics.DataDeal();
                     }
 
                     if (hour != 3 && mask_lose != 0)
@@ -71,12 +71,12 @@ namespace DataTransfer
                     #endregion                   
                 }
                 catch (Exception ex)
-                {                  
+                {
                     Console.WriteLine(ex.Message);  // 提示套接字监听异常     
                     break;
                 }
-            }                 
-        } 
+            }
+        }
 
         public static void dataTransfer()
         {
@@ -111,23 +111,23 @@ namespace DataTransfer
                 }
                 else
                 {
-                    Console.WriteLine(day+"-没有玩家登录,以玩家为单位的数据不做传输");
+                    Console.WriteLine(day + "-没有玩家登录,以玩家为单位的数据不做传输");
                 }
-            }       
-            
+            }
+
         }
-       
+
         /// <summary>
         /// redis数据传输到mysql中
         /// </summary>
         /// <param name="uid"></param>
         /// <param name="zoneid"></param>
-        public static void redisToSql(string uid,int zoneid)
+        public static void redisToSql(string uid, int zoneid)
         {
-            Console.WriteLine("以玩家为单位数据传输开始..........:" );           
-            TaskcompleteTsrecord_RedisToSql(uid,zoneid);
+            Console.WriteLine("以玩家为单位数据传输开始..........:");
+            TaskcompleteTsrecord_RedisToSql(uid, zoneid);
         }
-       
+
         const string Userbaseparams_TableName = "tab_userbaseparams";
 
         const string TaskcompleteTsrecord_TableName = "tab_taskcompleteTsrecord";
@@ -192,7 +192,7 @@ namespace DataTransfer
         /// <param name="uid"></param>
         /// <param name="zoneid"></param>
         public static void TaskcompleteTsrecord_RedisToSql(string uid, int zoneid)
-        {            
+        {
             var mem = Redis.Ins.GetDatabase(0);
             string key = MemKey_Statistics.SelfTaskDataCollect(uid, zoneid);
 
@@ -206,9 +206,9 @@ namespace DataTransfer
                 foreach (var item in ctxList)
                 {
                     num += 1;
-            
+
                     string[] sList = item.ToString().Split('-');
-                    string taskId = sList[0];                              
+                    string taskId = sList[0];
                     string time = sList[1];
 
                     var sql = $" Insert Into {TaskcompleteTsrecord_TableName} (`uid`, `zoneid`, `taskId`, `time`) " +
@@ -268,7 +268,7 @@ namespace DataTransfer
             else
             {
                 Console.WriteLine("玩家任务进度redis-key不存在所以不在数据传输处理");
-            }          
-        }  
+            }
+        }
     }
 }

+ 87 - 65
CSserver/Lib1/Config.cs

@@ -1,88 +1,110 @@
-using MySql.Data.MySqlClient;
+using System.IO;
+using MySql.Data.MySqlClient;
 using StackExchange.Redis;
 
- 
-   public class Config
-    {
 
-        public const bool GameOnline = true;  
+public class Config
+{
+
+    public const bool GameOnline = false;
 
-        public ConfigurationOptions redis;
+    public ConfigurationOptions redis;
 
-        public MySqlConnectionStringBuilder mysql;
-        /// <summary>
-        /// 结束倒计时
-        /// </summary>
-        public int CountDownTimes => GameOnline ? 10 * 60 : 2 * 60;
+    public MySqlConnectionStringBuilder mysql;
+    /// <summary>
+    /// 结束倒计时
+    /// </summary>
+    public int CountDownTimes => GameOnline ? 10 * 60 : 2 * 60;
 
-        /// <summary>
-        /// 结算地址
-        /// </summary>
-        public string SettleUrl
+    /// <summary>
+    /// 结算地址
+    /// </summary>
+    public string SettleUrl
+    {
+        get
         {
-            get
+            var host = "192.168.10.87";
+            if (GameOnline)
             {
-                var host = "192.168.10.87";
-                if (GameOnline)
-                {
-                    host = "115.159.121.129";
-                }
-                return $"http://{host}/ylsj2019/Gameserver/Amfphp/service_call/InquireApi/WorldBosSettle.php";
+                host = "115.159.121.129";
             }
+            return $"http://{host}/ylsj2019/Gameserver/Amfphp/service_call/InquireApi/WorldBosSettle.php";
         }
+    }
 
-        private Config() { }
 
-        static private Config _ins;
-        public static Config Ins
+    public string OutDir
+    {
+        get
+        {
+            var dir = "/data/stat_out/";           
+            VerifyFolder(dir);
+            return dir;
+        }
+    }
+    /// <summary>
+    /// 确保路径存在
+    /// </summary>
+    /// <param name="dir"></param>
+    internal static void VerifyFolder(string dir)
+    {
+        if (!Directory.Exists(dir))
+        {
+            Directory.CreateDirectory(dir);
+        }
+    }
+
+    private Config() { }
+
+    static private Config _ins;
+    public static Config Ins
+    {
+        get
         {
-            get
-            {
 
-                if (null == _ins)
+            if (null == _ins)
+            {
+                if (GameOnline)
                 {
-                    if (GameOnline)
+                    var host = "127.0.0.1";
+                    var port = 6379;
+                    var pwd = "wanggang1985";
+                    _ins = new Config()
                     {
-                        var host = "127.0.0.1";
-                        var port = 6379;
-                        var pwd = "wanggang1985";
-                        _ins = new Config()
+                        redis = ConfigurationOptions.Parse($"{host}:{port},password={pwd},connectTimeout=2000"),
+                        mysql = new MySqlConnectionStringBuilder
                         {
-                            redis = ConfigurationOptions.Parse($"{host}:{port},password={pwd},connectTimeout=2000"),
-                            mysql = new MySqlConnectionStringBuilder
-                            {
-                                Server = "127.0.0.1",
-                                UserID = "gwang",
-                                Password = "wanggang1985",
-                                Port = 3306,
-                                Database = "ylsj2019_pay",
-                                CharacterSet = "utf8"
-                            }
-                        };
-                    }
-                    else
+                            Server = "127.0.0.1",
+                            UserID = "gwang",
+                            Password = "wanggang1985",
+                            Port = 3306,
+                            Database = "ylsj2019_pay",
+                            CharacterSet = "utf8"
+                        }
+                    };
+                }
+                else
+                {
+                    var host = "192.168.10.16";
+                    var port = 6004;
+                    var pwd = "wanggang1985";
+                    _ins = new Config()
                     {
-                        var host = "192.168.10.16";
-                        var port = 6004;
-                        var pwd = "wanggang1985";
-                        _ins = new Config()
+                        redis = ConfigurationOptions.Parse($"{host}:{port},password={pwd},connectTimeout=2000"),
+                        mysql = new MySqlConnectionStringBuilder
                         {
-                            redis = ConfigurationOptions.Parse($"{host}:{port},password={pwd},connectTimeout=2000"),
-                            mysql = new MySqlConnectionStringBuilder
-                            {
-                                Server = "192.168.10.16",
-                                UserID = "gwang",
-                                Password = "wanggang1985",
-                                Port = 3306,
-                                Database = "ylsj2019_pay",
-                                CharacterSet = "utf8"
-                            }
-                        };
-                    }
+                            Server = "192.168.10.16",
+                            UserID = "gwang",
+                            Password = "wanggang1985",
+                            Port = 3306,
+                            Database = "ylsj2019_pay",
+                            CharacterSet = "utf8"
+                        }
+                    };
                 }
-                return _ins;
             }
+            return _ins;
         }
-
     }
- 
+
+}

+ 17 - 1
CSserver/Lib1/MemKey_User.cs

@@ -1,4 +1,4 @@
-
+using System.Collections.Generic;
 public class MemKey_Cfg {
     /// <summary>
     /// GM号列表
@@ -193,4 +193,20 @@ public class MemKey_Statistics
     {
         return "TargetStatistics-" + zoneid;
     }
+
+    public static List<string>  ReportedLogs() {
+        return new List<string> {
+            "stat-reported-log-e_gate",
+            "stat-reported-log-e_task",
+            "stat-reported-log-e_k_1",
+            "stat-reported-log-e_k_2",
+            "stat-reported-log-e_k_3",
+            "stat-reported-log-e_k_4",
+            "stat-reported-log-e_k_5",
+            "stat-reported-log-e_k_6",
+            "stat-reported-log-e_k_7",
+            "stat-reported-log-e_k_8",
+            "stat-reported-log-e_k_9",
+        };
+    }
 }

+ 2 - 1
CSserver/StatisticsTransfer/Program.cs

@@ -19,7 +19,8 @@ namespace StatisticsTransfer {
         {
             Console.WriteLine("FunDistribute................");
 
-            TargetStatistics.TargetValToExecl();
+            TargetStatistics.TargetValToExecl();                     // 统计数据
+            ReportedLogs.TransLog();                                 // 客户端埋点日志  
         }
     }
 

+ 119 - 0
CSserver/StatisticsTransfer/tongji/ReportedLogs.cs

@@ -0,0 +1,119 @@
+//using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using NPOI.HSSF.UserModel;
+using StackExchange.Redis;
+using Newtonsoft.Json.Linq;
+
+namespace StatisticsTransfer.tongji
+{
+    internal class ReportedLogs
+    {
+
+        const int OneTransMaxItem = 1000;
+        /// <summary>
+        /// 导出客户端埋点日志
+        /// </summary>
+        public static void TransLog()
+        {
+
+            var workbook = new HSSFWorkbook();
+            var mem = Redis.Ins.GetDatabase(0);
+
+            MemKey_Statistics.ReportedLogs().ForEach(k =>
+            {
+
+                var sheet = workbook.CreateSheet(GetSheetName(k));                // 创建工作表            
+                var rowInfo = mem.ListRange(k, 0, 0);
+                if (rowInfo.Length > 0)
+                {
+                    var strJS = rowInfo[0].ToString() ?? "{}";
+                    Console.WriteLine(strJS);
+                    var json = JObject.Parse(strJS);
+                    int i = 0, y = 0;
+                    var head_row = sheet.CreateRow(y++);                            // 标题行   
+                    foreach (var kv in json)
+                    {
+                        head_row.CreateCell(i++).SetCellValue(kv.Key);              // 设置标题
+                    }
+                    var start = 0L;
+                    var end = 0L;
+                    while (mem.ListLength(k) > 0 && end < mem.ListLength(k))
+                    {
+                        if (OneTransMaxItem < mem.ListLength(k) - start)
+                        {
+                            end = start + OneTransMaxItem - 1;
+                        }
+                        else
+                        {
+                            end = mem.ListLength(k);
+                        }
+
+                        var arr = mem.ListRange(k, start, end);
+                        arr.ToList().ForEach(x =>
+                        {
+                            var row = sheet.CreateRow(y++);
+                            var strJS = x.ToString() ?? "{}";
+                            var json = JObject.Parse(strJS);
+                            i = 0;                                                 // 重置索引
+                            foreach (var kv in json)
+                            {
+                                var v = kv.Value?.ToString() ?? "-";
+                                row.CreateCell(i++).SetCellValue(v);               // 设置列值 
+                            }
+
+                        });
+                        start = end;
+                        //  mem.ListTrim(k, OneTransMaxItem - 1, mem.ListLength(k) - OneTransMaxItem); // 清除已导数据
+                    }
+                }
+                 
+            });                                                                   // 遍历日志
+
+            OutPut(workbook);                                                     // 保存到文件
+            Console.WriteLine("客户端埋点日志转存完毕!");
+        }
+        /// <summary>
+        /// 将excel写入文件
+        /// </summary>
+        /// <param name="workbook"></param>
+        private static void OutPut(HSSFWorkbook workbook)
+        {
+
+            string txtName = "客户端埋点上报日志表_" + DateTime.Now.ToString("yyyyMMdd");
+
+            string filename = Config.Ins.OutDir + txtName + ".xls";
+            if (File.Exists(filename))
+            {
+                File.Delete(filename);
+            }
+            using var file = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
+            workbook.Write(file);
+
+        }
+        /// <summary>
+        /// redis中的key到 中文工作表名称的映射
+        /// </summary>
+        /// <param name="k"></param>
+        /// <returns></returns>
+        private static string GetSheetName(string k) => k switch
+        {
+            "stat-reported-log-e_gate" => "关卡记录",
+            "stat-reported-log-e_task" => "任务记录",
+            "stat-reported-log-e_k_1" => "资源更新完毕",
+            "stat-reported-log-e_k_2" => "手机号注册完毕(龙游)",
+            "stat-reported-log-e_k_3" => "实名认证完毕(龙游)",
+            "stat-reported-log-e_k_4" => "用户登录完成(已加载对应区的玩家数据)",
+            "stat-reported-log-e_k_5" => "进入新手引导场景",
+            "stat-reported-log-e_k_6" => "离开新手引导场景",
+            "stat-reported-log-e_k_7" => "接取了第一个主线剧情(任务)",
+            "stat-reported-log-e_k_8" => "e_k_8-不知道",
+            "stat-reported-log-e_k_9" => "e_k_9-不知道",
+            _ => "-"                                                           // 默认值
+        };
+
+
+    }
+}

+ 12 - 15
CSserver/StatisticsTransfer/tongji/TargetStatistics.cs

@@ -33,24 +33,19 @@ namespace StatisticsTransfer.tongji
 
             foreach (var item in ctxList)
             {
-                string[] sList = item.ToString().Split(':');
-                string type = sList[0];
-                int num = int.Parse(sList[1]);
-
+                string type = item.Name;
+                int num = int.Parse(item.Value);
                 string[] clist = type.Split('-');
                 string nType = targetType(clist[0]);
-                //Console.WriteLine("nType-----.." + nType);
+
                 if (dic.ContainsKey(nType))
                 {
-                    //Console.WriteLine("type-----..." + type);
                     dic[nType].Add(type, num);
                 }
                 else
                 {
-                    Dictionary<string, int> itemDic = new Dictionary<string, int>();
-                    itemDic.Add(type, num);
+                    var itemDic = new Dictionary<string, int> { { type, num } };
                     dic[nType] = itemDic;
-                    //Console.WriteLine("type-----..." + type);
                 }
             }
 
@@ -97,12 +92,14 @@ namespace StatisticsTransfer.tongji
 
             }
 
-            TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1);
-            float curday = MathF.Floor((long)ts.TotalSeconds / 24 / 60 / 60);
-
-            string txtName = "指标累计信息表_" + "zone" + zoneid + "-day-" + curday;
+            string txtName = "指标累计信息表_" + "zone" + zoneid + "-day-" + DateTime.Now.ToString("yyyyMMdd");
 
-            FileStream file = new FileStream(@"F:\excel\" + txtName + ".xls", FileMode.CreateNew, FileAccess.Write);
+            string filename = Config.Ins.OutDir + txtName + ".xls";
+            if (File.Exists(filename))
+            {
+                File.Delete(filename);
+            }
+            FileStream file = new FileStream(filename, FileMode.CreateNew, FileAccess.Write);
             workbook.Write(file);
             file.Dispose();
 
@@ -355,7 +352,7 @@ namespace StatisticsTransfer.tongji
 
             return type;
         }
-        
+
 
 
     }