瀏覽代碼

优化IES5 登录次数时间

Li 3 年之前
父節點
當前提交
4c1ab1e5e4
共有 1 個文件被更改,包括 207 次插入242 次删除
  1. 207 242
      TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

+ 207 - 242
TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

@@ -1,6 +1,7 @@
 using Azure.Cosmos;
 using Azure.Storage.Blobs.Models;
 using HTEXLib.COMM.Helpers;
+using Microsoft.Azure.Cosmos.Table;
 using Microsoft.Azure.Functions.Worker;
 using Microsoft.Azure.Functions.Worker.Http;
 using System;
@@ -167,29 +168,18 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
             string ip = null;
             int expire = 1;
             if (json.TryGetProperty("school", out JsonElement _school))
-            {
                 school = _school.GetString();
-            }
 
             if (json.TryGetProperty("scope", out JsonElement _scope))
-            {
                 scope = _scope.GetString();
-            }
 
             if (json.TryGetProperty("id", out JsonElement _id))
-            {
                 id = _id.GetString();
-            }
 
             if (json.TryGetProperty("ip", out JsonElement _ip))
-            {
                 ip = _ip.GetString();
-            }
-
             if (json.TryGetProperty("expire", out JsonElement _expire))
-            {
                 expire = _expire.GetInt32();
-            }
 
             var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
             var cosmosClient = _azureCosmos.GetCosmosClient();
@@ -198,13 +188,14 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
             var dateHour = dateTime.ToString("yyyyMMddHH"); //获取当天的小时
             var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
             var dateMonth = dateTime.ToString("yyyyMM");//获取当月的日期
-            var currentHour = dateTime.Hour;
-            var currentDay = dateTime.Day;
+            var currentHour = dateTime.Hour;   //当前小时
+            var currentDay = dateTime.Day;   //当前天
             long Expire = dateTime.AddHours(expire).ToUnixTimeMilliseconds();  //token到期时间
-            long now = dateTime.ToUnixTimeMilliseconds();   //时间戳
-
+            long now = dateTime.ToUnixTimeMilliseconds();   //当前时间戳
             DateTime hour = DateTime.UtcNow.AddHours(25);   //25小时到期
             DateTime month = DateTime.UtcNow.AddDays(32);   //一个月到期
+            var delTbHour = dateTime.AddHours(-168).ToString("yyyyMMddHH");   //168小时前
+            var delTbDay = dateTime.AddDays(-180).ToString("yyyyMMdd");   //180天前            
 
             switch (scope)
             {
@@ -242,136 +233,122 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
 
             var resDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:IES:{scope}:{dateDay}");
             if (resDay == null)
-            {
                 await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:IES:{scope}:{dateDay}", hour);  //设置到期时间
-            }
-
             var rspMonth = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:IES:{scope}:{dateMonth}");
             if (rspMonth == null)
-            {
                 await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:IES:{scope}:{dateMonth}", month);  //设置到期时间
-            }
 
-            ////查询Redis是否有值
-            //var dayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateDay}");
-            //List<dynamic> dayCounts = new();
-            //if (dayCnt != null && dayCnt.Length > 0)
-            //{
-            //    foreach (var hourCnt in dayCnt)
-            //    {
-            //        dayCounts.Add(new { code = hourCnt.Element.ToString(), count = (int)hourCnt.Score });
-            //    }
-            //}
-
-            //var monthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateMonth}");
-            //List<dynamic> monthCounts = new();
-            //if (monthCnt != null && monthCnt.Length > 0)
-            //{
-            //    foreach (var mCnt in monthCnt)
-            //    {
-            //        monthCounts.Add(new { code = mCnt.Element.ToString(), count = (int)mCnt.Score });
-            //    }
-            //}
-
-            //保存当天每小时的峰值
-            List<HourLogin> hourLogins = await table.QueryWhereString<HourLogin>($"PartitionKey eq 'HourLogin' and RowKey eq '{dateHour}'");
-            if (hourLogins.Count > 0)
+            //查询Redis是否有值
+            var dayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateDay}");
+            if (dayCnt != null && dayCnt.Length > 0)
             {
-                foreach (var hourLogin in hourLogins)
+                List<HourLogin> hourLogins = new();
+                foreach (var hourCnt in dayCnt)
                 {
-                    //hourLogin.Hour = currentHour;
-                    if (scope.Equals("teacher"))
-                        hourLogin.Teacher += 1;
-                    else if (scope.Equals("student"))
-                        hourLogin.Student += 1;
-                    else
-                        hourLogin.TmdUser += 1;
+                    if (((int)hourCnt.Element) == currentHour)
+                    {
+                        var tphourLogins = await table.QueryWhereString<HourLogin>($"PartitionKey eq 'HourLogin' and RowKey eq '{dateHour}'");
+                        if (tphourLogins.Count > 0)
+                        {
+                            foreach (var hourLogin in tphourLogins)
+                            {
+                                if (scope.Equals("teacher"))
+                                    hourLogin.Teacher = (int)hourCnt.Score;
+                                else if (scope.Equals("student"))
+                                    hourLogin.Student = (int)hourCnt.Score;
+                                else
+                                    hourLogin.TmdUser = (int)hourCnt.Score;
+                                hourLogins.Add(hourLogin);
+                            }
+                        }
+                        else
+                        {
+                            HourLogin hourLogin = new() { PartitionKey = $"HourLogin", RowKey = dateHour, Hour = currentHour };
+                            if (scope.Equals("teacher"))
+                            {
+                                hourLogin.Teacher = 1;
+                                hourLogin.Student = 0;
+                                hourLogin.TmdUser = 0;
+                            }
+                            else if (scope.Equals("student"))
+                            {
+                                hourLogin.Teacher = 0;
+                                hourLogin.Student = 1;
+                                hourLogin.TmdUser = 0;
+                            }
+                            else
+                            {
+                                hourLogin.Teacher = 0;
+                                hourLogin.Student = 0;
+                                hourLogin.TmdUser = 1;
+                            }
+                            hourLogins.Add(hourLogin);
+                        }
+                    }
                 }
 
-                try
-                {
-                    await table.SaveOrUpdateAll(hourLogins);
-                }
-                catch { }
-            }
-            else
-            {
-                HourLogin hourLogin = new() { PartitionKey = $"HourLogin", RowKey = dateHour, Hour = currentHour };
-                if (scope.Equals("teacher"))
-                {
-                    hourLogin.Teacher = 1;
-                    hourLogin.Student = 0;
-                    hourLogin.TmdUser = 0;
-                }
-                else if (scope.Equals("student"))
-                {
-                    hourLogin.Teacher = 0;
-                    hourLogin.Student = 1;
-                    hourLogin.TmdUser = 0;
-                }
-                else
-                {
-                    hourLogin.Teacher = 0;
-                    hourLogin.Student = 0;
-                    hourLogin.TmdUser = 1;
-                }
-                try
-                {
-                    await table.SaveOrUpdate<HourLogin>(hourLogin);//保存在线数据
-                }
-                catch { }
+                await table.SaveOrUpdateAll(hourLogins);  //保存和更新保存当前小时登录次数
             }
 
-            //保存当天的峰值
-            List<DayLogin> dayLogins = await table.QueryWhereString<DayLogin>($"PartitionKey eq 'DayLogin' and RowKey eq '{dateDay}'");
-            if (dayLogins.Count > 0)
+            var monthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateMonth}");
+            if (monthCnt != null && monthCnt.Length > 0)
             {
-                foreach (var dayLogin in dayLogins)
+                List<DayLogin> dayLogins = new();
+                foreach (var mCnt in monthCnt)
                 {
-                    //dayLogin.Day = currentDay;
-                    if (scope.Equals("teacher"))
-                        dayLogin.Teacher += 1;
-                    else if (scope.Equals("student"))
-                        dayLogin.Student += 1;
-                    else
-                        dayLogin.TmdUser += 1;
+                    if (((int)mCnt.Element) == currentDay)
+                    {
+                        //保存当天的峰值
+                        var tbDays = await table.QueryWhereString<DayLogin>($"PartitionKey eq 'DayLogin' and RowKey eq '{dateDay}'");
+                        if (tbDays.Count > 0)
+                        {
+                            foreach (var dayLogin in tbDays)
+                            {
+                                if (scope.Equals("teacher"))
+                                    dayLogin.Teacher = (int)mCnt.Score;
+                                else if (scope.Equals("student"))
+                                    dayLogin.Student = (int)mCnt.Score;
+                                else
+                                    dayLogin.TmdUser = (int)mCnt.Score;
+                                dayLogins.Add(dayLogin);
+                            }
+                        }
+                        else
+                        {
+                            //保存当月每天的峰值
+                            DayLogin dayLogin = new() { PartitionKey = $"DayLogin", RowKey = dateDay, Day = currentDay };
+                            if (scope.Equals("teacher"))
+                            {
+                                dayLogin.Teacher = 1;
+                                dayLogin.Student = 0;
+                                dayLogin.TmdUser = 0;
+                            }
+                            else if (scope.Equals("student"))
+                            {
+                                dayLogin.Teacher = 0;
+                                dayLogin.Student = 1;
+                                dayLogin.TmdUser = 0;
+                            }
+                            else
+                            {
+                                dayLogin.Teacher = 0;
+                                dayLogin.Student = 0;
+                                dayLogin.TmdUser = 1;
+                            }
+                            dayLogins.Add(dayLogin);
+                        }
+                    }
                 }
 
-                try
-                {
-                    await table.SaveOrUpdateAll(dayLogins);
-                }
-                catch { }
-            }
-            else
-            {
-                //保存当月每天的峰值
-                DayLogin dayLogin = new() { PartitionKey = $"DayLogin", RowKey = dateDay, Day = currentDay };
-                if (scope.Equals("teacher"))
-                {
-                    dayLogin.Teacher = 1;
-                    dayLogin.Student = 0;
-                    dayLogin.TmdUser = 0;
-                }
-                else if (scope.Equals("student"))
-                {
-                    dayLogin.Teacher = 0;
-                    dayLogin.Student = 1;
-                    dayLogin.TmdUser = 0;
-                }
-                else
-                {
-                    dayLogin.Teacher = 0;
-                    dayLogin.Student = 0;
-                    dayLogin.TmdUser = 1;
-                }
-                try
-                {
-                    await table.SaveOrUpdate<DayLogin>(dayLogin);//保存当月每天在线数据
-                }
-                catch { }
+                await table.SaveOrUpdateAll(dayLogins);// 保存当月每天在线数据
             }
 
+            string tbHourSql = $"PartitionKey eq 'HourLogin' and RowKey le '{delTbHour}'";
+            await table.DeleteStringWhere<BIOptLog>(rowKey: tbHourSql);  //删除168小时前的数据
+
+            string tbDaySql = $"PartitionKey eq 'DayLogin' and RowKey le '{delTbDay}'";
+            await table.DeleteStringWhere<BIOptLog>(rowKey: tbDaySql);   //删除180天前的数据
+
             if (!string.IsNullOrWhiteSpace(school))
             {
                 await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateDay}", $"{currentHour}", 1);//当天当前小时在线人加1
@@ -379,137 +356,125 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
 
                 var reScDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:School:{school}:{scope}:{dateDay}");
                 if (reScDay == null)
-                {
                     await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:School:{school}:{scope}:{dateDay}", hour);  //设置到期时间
-                }
-
                 var reScMonth = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"Login:School:{school}:{scope}:{dateMonth}");
                 if (reScMonth == null)
-                {
                     await _azureRedis.GetRedisClient(8).KeyExpireAsync($"Login:School:{school}:{scope}:{dateMonth}", month);  //设置到期时间
-                }
-
-                ////查询Redis是否有值
-                //var scDayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateDay}");
-                //List<dynamic> scDayCounts = new();
-                //if (scDayCnt != null && scDayCnt.Length > 0)
-                //{
-                //    foreach (var itemHour in scDayCnt)
-                //    {
-                //        scDayCounts.Add(new { code = itemHour.Element.ToString(), count = (int)itemHour.Score });
-                //    }
-                //}
-                //var ScMonth = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateMonth}");
-                //List<dynamic> scMonthCount = new();
-                //if (ScMonth != null && ScMonth.Length > 0)
-                //{
-                //    foreach (var count in ScMonth)
-                //    {
-                //        scMonthCount.Add(new { code = count.Element.ToString(), count = (int)count.Score });
-                //    }
-                //}
-
-                //保存学校当天每小时的
-                List<HourLoginSchool> hourLoginSchools = await table.QueryWhereString<HourLoginSchool>($"PartitionKey eq 'HourLogin-{school}' and RowKey eq '{dateHour}'");
-                if (hourLoginSchools.Count > 0)
-                {
-                    foreach (var hLoginSc in hourLoginSchools)
-                    {
-                        //hLoginSc.Hour = currentHour;
-                        if (scope.Equals("teacher"))
-                            hLoginSc.Teacher += 1;
-                        else if (scope.Equals("student"))
-                            hLoginSc.Student += 1;
-                        else
-                            hLoginSc.TmdUser += 1;
-                    }
 
-                    try
-                    {
-                        await table.SaveOrUpdateAll(hourLoginSchools);
-                    }
-                    catch { }
-                }
-                else
+                //查询Redis是否有值
+                var scDayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateDay}");
+                List<dynamic> scDayCounts = new();
+                if (scDayCnt != null && scDayCnt.Length > 0)
                 {
-                    //学校小时峰值
-                    HourLoginSchool hourLoginSc = new() { PartitionKey = $"HourLogin-{school}", RowKey = dateHour, Hour = currentHour, School = school };
-                    if (scope.Equals("teacher"))
-                    {
-                        hourLoginSc.Teacher = 1;
-                        hourLoginSc.Student = 0;
-                        hourLoginSc.TmdUser = 0;
-                    }
-                    else if (scope.Equals("student"))
-                    {
-                        hourLoginSc.Teacher = 0;
-                        hourLoginSc.Student = 1;
-                        hourLoginSc.TmdUser = 0;
-                    }
-                    else
+                    //保存学校当天每小时的
+                    List<HourLoginSchool> hourLoginSchools = new();
+                    foreach (var itemHour in scDayCnt)
                     {
-                        hourLoginSc.Teacher = 0;
-                        hourLoginSc.Student = 0;
-                        hourLoginSc.TmdUser = 1;
+                        if (((int)itemHour.Element) == currentHour)
+                        {
+                            var tmpHour = await table.QueryWhereString<HourLoginSchool>($"PartitionKey eq 'HourLogin-{school}' and RowKey eq '{dateHour}'");
+                            if (tmpHour.Count > 0)
+                            {
+                                foreach (var hLoginSc in tmpHour)
+                                {
+                                    if (scope.Equals("teacher"))
+                                        hLoginSc.Teacher = (int)itemHour.Score;
+                                    else if (scope.Equals("student"))
+                                        hLoginSc.Student = (int)itemHour.Score;
+                                    else
+                                        hLoginSc.TmdUser = (int)itemHour.Score;
+                                    hourLoginSchools.Add(hLoginSc);
+                                }
+                            }
+                            else
+                            {
+                                //学校小时峰值
+                                HourLoginSchool hourLoginSc = new() { PartitionKey = $"HourLogin-{school}", RowKey = dateHour, Hour = currentHour, School = school };
+                                if (scope.Equals("teacher"))
+                                {
+                                    hourLoginSc.Teacher = 1;
+                                    hourLoginSc.Student = 0;
+                                    hourLoginSc.TmdUser = 0;
+                                }
+                                else if (scope.Equals("student"))
+                                {
+                                    hourLoginSc.Teacher = 0;
+                                    hourLoginSc.Student = 1;
+                                    hourLoginSc.TmdUser = 0;
+                                }
+                                else
+                                {
+                                    hourLoginSc.Teacher = 0;
+                                    hourLoginSc.Student = 0;
+                                    hourLoginSc.TmdUser = 1;
+                                }
+                                hourLoginSchools.Add(hourLoginSc);
+                            }
+                        }
                     }
 
-                    try
-                    {
-                        await table.SaveOrUpdate<HourLoginSchool>(hourLoginSc);//保存在线数据
-                    }
-                    catch { }
+                    await table.SaveOrUpdateAll(hourLoginSchools);
                 }
 
-                //学校天峰值
-                List<DayLoginSchool> DayLoginSchools = await table.QueryWhereString<DayLoginSchool>($"PartitionKey eq 'DayLogin-{school}' and RowKey eq '{dateDay}'");
-                if (DayLoginSchools.Count > 0)
-                {
-                    foreach (var dLoginSc in DayLoginSchools)
-                    {
-                        //dLoginSc.Day = currentDay;
-                        if (scope.Equals("teacher"))
-                            dLoginSc.Teacher += 1;
-                        else if (scope.Equals("student"))
-                            dLoginSc.Student += 1;
-                        else
-                            dLoginSc.TmdUser += 1;
-                    }
-
-                    try
-                    {
-                        await table.SaveOrUpdateAll(DayLoginSchools);
-                    }
-                    catch { }
-                }
-                else
+                var ScMonth = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateMonth}");
+                if (ScMonth != null && ScMonth.Length > 0)
                 {
                     //学校天峰值
-                    DayLoginSchool dayLoginSc = new() { PartitionKey = $"DayLogin-{school}", RowKey = dateDay, Day = currentDay, School = school };
-                    if (scope.Equals("teacher"))
+                    List<DayLoginSchool> DayLoginSchools = new();
+                    foreach (var count in ScMonth)
                     {
-                        dayLoginSc.Teacher = 1;
-                        dayLoginSc.Student = 0;
-                        dayLoginSc.TmdUser = 0;
-                    }
-                    else if (scope.Equals("student"))
-                    {
-                        dayLoginSc.Teacher = 0;
-                        dayLoginSc.Student = 1;
-                        dayLoginSc.TmdUser = 0;
-                    }
-                    else
-                    {
-                        dayLoginSc.Teacher = 0;
-                        dayLoginSc.Student = 0;
-                        dayLoginSc.TmdUser = 1;
-                    }
+                        if (((int)count.Element) == currentDay)
+                        {
+                            var tempDays = await table.QueryWhereString<DayLoginSchool>($"PartitionKey eq 'DayLogin-{school}' and RowKey eq '{dateDay}'");
+                            if (tempDays.Count > 0)
+                            {
+                                foreach (var dLoginSc in tempDays)
+                                {
+                                    if (scope.Equals("teacher"))
+                                        dLoginSc.Teacher = (int)count.Score;
+                                    else if (scope.Equals("student"))
+                                        dLoginSc.Student = (int)count.Score;
+                                    else
+                                        dLoginSc.TmdUser = (int)count.Score;
+                                    DayLoginSchools.Add(dLoginSc);
+                                }
+                            }
+                            else
+                            {
+                                //学校天峰值
+                                DayLoginSchool dayLoginSc = new() { PartitionKey = $"DayLogin-{school}", RowKey = dateDay, Day = currentDay, School = school };
+                                if (scope.Equals("teacher"))
+                                {
+                                    dayLoginSc.Teacher = 1;
+                                    dayLoginSc.Student = 0;
+                                    dayLoginSc.TmdUser = 0;
+                                }
+                                else if (scope.Equals("student"))
+                                {
+                                    dayLoginSc.Teacher = 0;
+                                    dayLoginSc.Student = 1;
+                                    dayLoginSc.TmdUser = 0;
+                                }
+                                else
+                                {
+                                    dayLoginSc.Teacher = 0;
+                                    dayLoginSc.Student = 0;
+                                    dayLoginSc.TmdUser = 1;
+                                }
 
-                    try
-                    {
-                        await table.SaveOrUpdate<DayLoginSchool>(dayLoginSc);//保存在线数据
+                                DayLoginSchools.Add(dayLoginSc);
+                            }
+                        }
                     }
-                    catch { }
+
+                    await table.SaveOrUpdateAll(DayLoginSchools);//保存学校当月在线数据
                 }
+
+                string tbScHourSql = $"PartitionKey eq 'HourLogin-{school}' and RowKey le '{delTbHour}'";
+                await table.DeleteStringWhere<BIOptLog>(rowKey: tbHourSql); //删除学校168小时前的数据
+
+                string tbScDaySql = $"PartitionKey eq 'DayLogin-{school}' and RowKey le '{delTbDay}'";
+                await table.DeleteStringWhere<BIOptLog>(rowKey: tbDaySql); //删除学校180天前的数据
             }
 
             await response.WriteAsJsonAsync(new { data = json });