Browse Source

处理防火墙日志接口记录统计记录。

Li 2 years ago
parent
commit
a2df66924e

+ 23 - 3
TEAMModelBI/Controllers/BITest/TestController.cs

@@ -48,6 +48,9 @@ using StackExchange.Redis;
 using TEAMModelOS.SDK.Models.Service.BI;
 using TEAMModelOS.SDK.Context.BI;
 using TEAMModelOS.SDK.Context.Constant;
+using Azure.Storage.Blobs.Models;
+using Azure.Storage.Blobs;
+using Azure.Storage.Blobs.Specialized;
 
 namespace TEAMModelBI.Controllers.BITest
 {
@@ -1448,11 +1451,28 @@ namespace TEAMModelBI.Controllers.BITest
         public async Task<IActionResult> GetLogAnalyse(JsonElement jsonElement) 
         {
             if (!jsonElement.TryGetProperty("path", out JsonElement path)) return BadRequest();
-            var (an ,saveUrl) = await BILogAnalyseService.GetPathAnalyse(_azureStorage, $"{path}", BIConst.LogGlobal);
 
-            return Ok(new { state = RespondCode.Ok, an, saveUrl });
-        }
+            List<string> filename = new();
+            ////删除过期Blob 文件
+            //var azureClient = _azureStorage.GetBlobContainerClient("0-public");//获取容器连接地址
+            //int expireTime = int.Parse(DateTimeOffset.UtcNow.AddDays(-180).ToString("yyyyMMdd"));
+            //await foreach (var blobItem in azureClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: "visitCnt"))
+            //{
+            //    string[] sub_name = blobItem.Name.Split('/');
+            //    if (sub_name.Length > 2)
+            //    {
+            //        if (int.Parse(sub_name[1]) <= expireTime)
+            //        {
+            //            await azureClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync();
+            //            filename.add(sub_name[1]);
+            //        }
+            //    }
+            //}
+
+            var (an, saveUrl) = await BILogAnalyseService.GetPathAnalyse(_azureStorage, $"{path}", BIConst.LogGlobal);
 
+            return Ok(new { state = RespondCode.Ok, cnt = filename.Count, filename, an, saveUrl });
+        }
 
         public class linqTest
         {

+ 50 - 0
TEAMModelBI/Tool/CosmosBank/stringSuffix.cs

@@ -0,0 +1,50 @@
+using System.Text.RegularExpressions;
+
+namespace TEAMModelBI.Tool.CosmosBank
+{
+    public class stringSuffix
+    {
+        /// <summary>
+        /// 正则表达式,截取两个字符串中间的字符串
+        /// </summary>
+        /// <param name="source">目标字符串</param>
+        /// <param name="startStr">起始字符串</param>
+        /// <param name="endStr">结束字符串</param>
+        /// <returns>截取的结果</returns>
+        public static string CutOutStr(string source, string startStr = "", string endStr = "")
+        {
+            Regex rg;
+            if (endStr.Equals(""))  //提取特定字符串开头的内容
+            {
+                rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*");
+            }
+            else if (startStr.Equals(""))  //提取特定字符串结尾的内容
+            {
+                rg = new Regex(".*?(?=(" + endStr + "))");
+            }
+            else  //提取特定字符串开头与结尾之间的内容
+            {
+                rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))");
+            }
+            return rg.Match(source).Value;
+        }
+
+        /// <summary>
+        /// 获取文件后缀名,如.PDF
+        /// </summary>
+        /// <param name="fileName">是传入的文件名</param>
+        /// <returns></returns>
+        public static string FileSuffixName(string fileName)
+        {
+            string suffixName = null;
+            int suff = fileName.Length - 1;
+            while (suff >= 0 && (fileName[suff] != '.'))
+                suff--;
+            if (suff >= 0)
+            {
+                suffixName = fileName.Remove(0, suff);//获取文件后缀名,如.PDF
+            }
+            return suffixName;
+        }
+    }
+}

+ 5 - 0
TEAMModelOS.SDK/Models/Cosmos/BI/StaticValue.cs

@@ -9,6 +9,11 @@ namespace TEAMModelOS.SDK.Models.Cosmos.BI
         /// </summary>
         public static List<string> activityTypes = new() { "Exam", "Survey", "Vote", "Homework" };
 
+        /// <summary>
+        /// 分析接口时处理后缀
+        /// </summary>
+        public static List<string> suffixName = new() { ".js", ".css", ".ico", ".aspx", ".php", ".aws", ".html" };
+
 
     }
 }

+ 40 - 4
TEAMModelOS.SDK/Models/Service/BI/BILogAnalyseService.cs

@@ -1,5 +1,6 @@
 using Azure.Storage.Blobs;
 using Azure.Storage.Blobs.Models;
+using Azure.Storage.Blobs.Specialized;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -126,7 +127,27 @@ namespace TEAMModelOS.SDK.Models.Service.BI
                     }
 
                     string input = visits.ToString();
-                    List<AGInfo> aGInfos = input.ToObject<List<AGInfo>>();
+                    List<AGInfo> tempAinfos = input.ToObject<List<AGInfo>>();
+
+                    List<AGInfo> tempsert = new List<AGInfo>();
+                    List<AGInfo> aGInfos = new List<AGInfo>();
+
+                    tempAinfos.ForEach(item =>
+                    {
+                        string requestUri = item.properties.requestUri;
+                        var isType = StaticValue.suffixName.Where(k => requestUri.Contains(k)).ToList();
+                        if (isType.Count == 0)
+                            aGInfos.Add(item);
+                    });
+
+                    //foreach (var item in tempAinfos)
+                    //{
+                    //    string requestUri = item.properties.requestUri;
+                    //    var isType = type.Where(k => requestUri.Contains(k)).ToList();
+                    //    if (isType.Count == 0)
+                    //        aGInfos.Add(item);
+                    //}
+
                     DateTimeOffset dtime = DateTimeOffset.UtcNow;
                     string cHour = dtime.ToString("yyyyMMddHH");
                     string cDay = dtime.ToString("yyyyMMdd");
@@ -147,16 +168,31 @@ namespace TEAMModelOS.SDK.Models.Service.BI
                     saveCnts.ipCnt = ipCnt;
                     recCnts.Add(saveCnts);
 
-                    ////保存存至Blob文件
+                    //保存存至Blob文件
                     var url = await _azureStorage.GetBlobContainerClient("0-public").UploadFileByContainer(saveCnts.ToJsonString(), $"visitCnt/{cDay}", $"{cHour}.json");
 
                     urls.Add(url);
                 }
-            } catch (Exception ex) { 
 
-            }
+                var azureClient = _azureStorage.GetBlobContainerClient("0-public");//获取容器连接地址
+                int expireTime = int.Parse(DateTimeOffset.UtcNow.AddDays(-180).ToString("yyyyMMdd"));
+                await foreach (var blobItem in azureClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, prefix: "visitCnt"))
+                {
+                    string[] sub_name = blobItem.Name.Split('/');
+                    if (sub_name.Length > 2)
+                    {
+                        if (int.Parse(sub_name[1]) <= expireTime)
+                        {
+                            await azureClient.GetBlobBaseClient(blobItem.Name).DeleteIfExistsAsync();
+                        }
+                    }
+                }
+
+            } catch (Exception ex) { }
 
             return (recCnts, urls);
         }
+
+
     }
 }