Kaynağa Gözat

优化防火墙网关结构,首页统计当天访问次数。

Li 3 yıl önce
ebeveyn
işleme
6986b32a9e

+ 3 - 39
TEAMModelBI/Controllers/BIHome/AnalyseFileController.cs

@@ -10,6 +10,7 @@ using System.Linq;
 using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
+using TEAMModelBI.Models.RecordM;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models.Cosmos.BI;
@@ -62,7 +63,7 @@ namespace TEAMModelBI.Controllers.BIHome
             RecCnt saveCnts = new();
             //var ipGroup = aGInfos.GroupBy(g => g.properties.clientIp).ToDictionary(k => k.Key, k => k.Count()).ToList();
 
-            List<RecGWInfo> recInfo = aGInfos.Select(s => new RecGWInfo { hour = cHour, ip = s.properties.clientIp, api = s.properties.requestUri.Split("?").ToList().Count() > 1 ? s.properties.requestUri.Split("?").ToList()[0] : s.properties.requestUri, hostName = s.properties.hostname }).ToList();
+            List<RecAppGWInfo> recInfo = aGInfos.Select(s => new RecAppGWInfo { hour = cHour, ip = s.properties.clientIp, api = s.properties.requestUri.Split("?").ToList().Count() > 1 ? s.properties.requestUri.Split("?").ToList()[0] : s.properties.requestUri, hostName = s.properties.hostname }).ToList();
 
             List<RecApiCnt> apiCnt = recInfo.GroupBy(a => a.api).Select(g => new RecApiCnt { api = g.Key, count = g.Count(), hour = cHour, hostName = g.Select(h => h.hostName).Distinct().ToList(), ip = g.Select(i => i.ip).Distinct().ToList() }).ToList();
             saveCnts.apiCnt= apiCnt;
@@ -101,44 +102,7 @@ namespace TEAMModelBI.Controllers.BIHome
             return Ok(new { state = 200, recCnts,  apiCon = apiCnt.Count(), apiSum = apiCnt.Select(ap => ap.count).Sum(), ipCount = ipCnt.Count, ipSum = ipCnt.Select(ip => ip.count).Sum(), ipCnt, apiCnt }); ;
 
             //return Ok(new { state = 200, cnt = recInfo.Count, apiCon = apiCnt.Count(), apiSum = apiCnt.Select(ap=> ap.count).Sum(), ipCount = ipCnt.Count, ipSum = ipCnt.Select(ip=>ip.count).Sum(), ipCnt,apiCnt, recInfo   });;
-        }
-
-        public record RecCnt
-        {
-            public List<RecApiCnt> apiCnt { get; set; }
-            public List<RecIpCnt> ipCnt { get; set; }
-        }
-
-        public record RecCntBas
-        {
-            public int count { get; set; }
-            public string hour { get; set; }
-            public List<string> hostName { get; set; }
-        }
-
-        public record RecIpCnt : RecCntBas
-        {
-            public string ip { get; set; }
-            public List<string> api { get; set; }
-        }
-
-        public record RecApiCnt : RecCntBas
-        {
-            public string api { get; set; }
-            public List<string> ip { get; set; }
-        }
-
-        public record RecGWInfo
-        {
-            public string hour { get; set; }
-            public string ip { get; set; }
-            public string api { get; set; }
-            public string hostName { get; set; }
-        }
-
-
-
-
+        }               
 
         public record StatisNameCnt 
         {

+ 27 - 1
TEAMModelBI/Controllers/BIHome/OnLineController.cs

@@ -1,12 +1,17 @@
 using Azure.Cosmos;
+using Azure.Storage.Blobs;
+using Azure.Storage.Blobs.Models;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
+using TEAMModelBI.Models.RecordM;
 using TEAMModelBI.Tool;
 using TEAMModelBI.Tool.Context;
 using TEAMModelOS.SDK.DI;
@@ -40,14 +45,17 @@ namespace TEAMModelBI.Controllers.BIHome
         {
             var cosmosClient = _azureCosmos.GetCosmosClient();
             var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
+            var blobClient = _azureStorage.GetBlobContainerClient($"0-public");
             jsonElement.TryGetProperty("site", out JsonElement site);
             if ($"{site}".Equals(BIConst.GlobalSite)) 
             {
                 cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
                 table = _azureStorage.GetCloudTableClient(BIConst.GlobalSite).GetTableReference("IESLogin");
+                blobClient = _azureStorage.GetBlobContainerClient($"0-public", BIConst.GlobalSite);
             }
 
             DateTimeOffset dateTime = DateTimeOffset.UtcNow;
+            string cDay = dateTime.ToString("yyyyMMdd");
 
             var (daySt, dayEt) = TimeHelper.GetStartOrEnd(dateTime);  //今天开始时间    13位
             var (daySf, dayEf) = TimeHelper.GetStartOrEnd(dateTime, dateLenth: false);  //今天开始时间  10位
@@ -61,6 +69,7 @@ namespace TEAMModelBI.Controllers.BIHome
             int scCnt = 0;  //学校总数
             int tchCnt = 0; //教师总数
             int stuCnt = 0; //学生总数
+            int apiCnt = 0;  //当天接口访问总量
 
             int onStuCnt = 0;  //学生在线人数
             int onTchCnt = 0;  //教师在线人数
@@ -85,6 +94,23 @@ namespace TEAMModelBI.Controllers.BIHome
             onTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", onStuSql, "Base");
             onStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", onStuSql, "Base");
 
+            //接口访问量
+            List<RecCnt> recCnts = new();
+            await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{cDay}"))
+            {
+                BlobClient tempBlobClient = blobClient.GetBlobClient(blobItem.Name);
+                if (await tempBlobClient.ExistsAsync())
+                {
+                    using (var meomoryStream = new MemoryStream())
+                    {
+                        var response = blobClient.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
+                        RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<RecCnt>();
+                        recCnts.Add(recCnt);
+                    }
+                }
+            }
+            apiCnt = recCnts.Select(x => x.apiCnt.Select(s => s.count).Sum()).Sum();
+
             //List<RecOnLine> recStuOnLines = new();
             //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<RecOnLine>(queryText: "select c.id,c.name,c.code,c.loginInfos from c where c.pk='Base' and array_length(c.loginInfos) > 0 ", requestOptions:new QueryRequestOptions() { }))
             //{
@@ -103,7 +129,7 @@ namespace TEAMModelBI.Controllers.BIHome
             ////onTchCnt = (from rs in recTecOnLines from l in rs.loginInfos where l.expire >= hour1 select rs).ToList().Count();  //linq查询  教师查询人数
             //onTchCnt = recTecOnLines.Select(rss => new RecOnLine { id = rss.id,code=rss.code, name =rss.name,loginInfos = new List<Teacher.LoginInfo> { rss.loginInfos.Find(f => f.expire >= hour1) } }).Where(w => w.loginInfos.FirstOrDefault() != null).ToList().Count();  //lambda 表达式查询    教师查询人数
 
-            return Ok(new { state = 200, areaCnt, scCnt, tchCnt, stuCnt, todayScCnt, todayTchCnt, todayStuCnt, onStuCnt, onTchCnt});
+            return Ok(new { state = 200, areaCnt, scCnt, tchCnt, stuCnt, todayScCnt, todayTchCnt, todayStuCnt, onStuCnt, onTchCnt, apiCnt });
         }
 
         /// <summary>

+ 52 - 0
TEAMModelBI/Models/RecordM/RecAppGWInfo.cs

@@ -0,0 +1,52 @@
+using System.Collections.Generic;
+
+namespace TEAMModelBI.Models.RecordM
+{
+    /// <summary>
+    /// 防火墙网关信息
+    /// </summary>
+    public record RecAppGWInfo
+    {
+        public string hour { get; set; }
+        public string ip { get; set; }
+        public string api { get; set; }
+        public string hostName { get; set; }
+    }
+
+    /// <summary>
+    /// 防火墙统计后的信息
+    /// </summary>
+    public record RecCnt
+    {
+        public List<RecApiCnt> apiCnt { get; set; }
+        public List<RecIpCnt> ipCnt { get; set; }
+    }
+
+    /// <summary>
+    /// 防火墙统计通用信息
+    /// </summary>
+    public record RecCntBas
+    {
+        public int count { get; set; }
+        public string hour { get; set; }
+        public List<string> hostName { get; set; }
+    }
+
+    /// <summary>
+    /// 防火墙按照IP统计
+    /// </summary>
+    public record RecIpCnt : RecCntBas
+    {
+        public string ip { get; set; }
+        public List<string> api { get; set; }
+    }
+
+    /// <summary>
+    /// 防火墙按照接口统计
+    /// </summary>
+    public record RecApiCnt : RecCntBas
+    {
+        public string api { get; set; }
+        public List<string> ip { get; set; }
+    }
+}