Browse Source

在线人数处理异常

Li 3 years ago
parent
commit
48f0c674fb
1 changed files with 60 additions and 31 deletions
  1. 60 31
      TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

+ 60 - 31
TEAMModelOS.FunctionV4/HttpTrigger/IESHttpTrigger.cs

@@ -4,6 +4,7 @@ using HTEXLib.COMM.Helpers;
 using Microsoft.Azure.Cosmos.Table;
 using Microsoft.Azure.Functions.Worker;
 using Microsoft.Azure.Functions.Worker.Http;
+using StackExchange.Redis;
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -229,18 +230,28 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
                         break;
                 }
 
-                await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateDay}", $"{currentHour}", 1);//一天24小时  小时为单位
-                await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateMonth}", $"{currentDay}", 1); //一天的累计   天为单位
+                //天
+                SortedSetEntry[] dayCnt = null;
+                //月
+                SortedSetEntry[] monthCnt = null;
+                try
+                {
+                    await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateDay}", $"{currentHour}", 1);//一天24小时  小时为单位
+                    await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:IES:{scope}:{dateMonth}", $"{currentDay}", 1); //一天的累计   天为单位
 
-                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);  //设置到期时间
+                    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);  //设置到期时间
 
-                //保存当前小时统计
-                var dayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateDay}");
+                    //保存当前小时统计
+                    dayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateDay}");
+                    //保存当前的统计数据
+                    monthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateMonth}");
+                }
+                catch { }
                 if (dayCnt != null && dayCnt.Length > 0)
                 {
                     List<HourLogin> hourLogins = new();
@@ -289,8 +300,6 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
                     }
                     await table.SaveOrUpdateAll(hourLogins);  //保存和更新保存当前小时登录次数
                 }
-                //保存当前的统计数据
-                var monthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:IES:{scope}:{dateMonth}");
                 if (monthCnt != null && monthCnt.Length > 0)
                 {
                     List<DayLogin> dayLogins = new();
@@ -343,25 +352,40 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
                 }
 
                 string tbHourSql = $"PartitionKey eq 'HourLogin' and RowKey le '{delTbHour}'";
-                await table.DeleteStringWhere<HourLogin>(rowKey: tbHourSql);  //删除168小时前的数据
 
                 string tbDaySql = $"PartitionKey eq 'DayLogin' and RowKey le '{delTbDay}'";
-                await table.DeleteStringWhere<DayLogin>(rowKey: tbDaySql);   //删除180天前的数据
+                try
+                {
+                    await table.DeleteStringWhere<HourLogin>(rowKey: tbHourSql);  //删除168小时前的数据
+                    await table.DeleteStringWhere<DayLogin>(rowKey: tbDaySql);   //删除180天前的数据
+                }
+                catch { }
 
                 if (!string.IsNullOrWhiteSpace(school))
                 {
-                    await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateDay}", $"{currentHour}", 1);//当天当前小时在线人加1
-                    await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateMonth}", $"{currentDay}", 1); //当天的在线加1
+                    //天
+                    SortedSetEntry[] scDayCnt = null;
+                    //月
+                    SortedSetEntry[] scMonthCnt = null;
+                    try
+                    {
+                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateDay}", $"{currentHour}", 1);//当天当前小时在线人加1
+                        await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Login:School:{school}:{scope}:{dateMonth}", $"{currentDay}", 1); //当天的在线加1
+
+                        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);  //设置到期时间
 
-                    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);  //设置到期时间
+                        //保存学校当天每小时的
+                        scDayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateDay}");
+                        //学校天峰值
+                        scMonthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateMonth}");
+                    }
+                    catch { }
 
-                    //保存学校当天每小时的
-                    var scDayCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateDay}");
                     if (scDayCnt != null && scDayCnt.Length > 0)
                     {
                         List<HourLoginSchool> hourLoginSchools = new();
@@ -411,8 +435,6 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
                         }
                         await table.SaveOrUpdateAll(hourLoginSchools);
                     }
-                    //学校天峰值
-                    var scMonthCnt = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Login:School:{school}:{scope}:{dateMonth}");
                     if (scMonthCnt != null && scMonthCnt.Length > 0)
                     {
                         List<DayLoginSchool> DayLoginSchools = new();
@@ -467,14 +489,21 @@ namespace TEAMModelOS.FunctionV4.HttpTrigger
                     string tbScHourSql = $"PartitionKey eq 'HourLogin-{school}' and RowKey le '{delTbHour}'";
                     List<HourLogin> scHourLog = await table.QueryWhereString<HourLogin>(tbScHourSql);
                     if (scHourLog.Count > 0)
-                        //await table.DeleteStringWhere<HourLogin>(tbScHourSql); //删除学校168小时前的数据
-                        await table.DeleteAll(scHourLog);
-
+                        try
+                        {
+                            //await table.DeleteStringWhere<HourLogin>(tbScHourSql); //删除学校168小时前的数据
+                            await table.DeleteAll(scHourLog);
+                        }
+                        catch { }
                     string tbScDaySql = $"PartitionKey eq 'DayLogin-{school}' and RowKey le '{delTbDay}'";
                     List<DayLogin> scDayLog = await table.QueryWhereString<DayLogin>(tbScDaySql);
                     if (scDayLog.Count > 0)
-                        await table.DeleteAll(scDayLog);
-                        //await table.DeleteStringWhere<DayLogin>(tbScDaySql); //删除学校180天前的数据
+                        try
+                        {
+                            //await table.DeleteStringWhere<DayLogin>(tbScDaySql); //删除学校180天前的数据
+                            await table.DeleteAll(scDayLog);
+                        }
+                        catch { }
                 }
 
                 await response.WriteAsJsonAsync(new { data = json });