소스 검색

修正在線人數顯示錯誤問題

upon 1 년 전
부모
커밋
d91a632299
1개의 변경된 파일109개의 추가작업 그리고 3개의 파일을 삭제
  1. 109 3
      TEAMModelBI/Controllers/BIHome/OnLineController.cs

+ 109 - 3
TEAMModelBI/Controllers/BIHome/OnLineController.cs

@@ -96,9 +96,115 @@ namespace TEAMModelBI.Controllers.BIHome
             todayTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", addSql, "Base");
             todayStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", addSql, "Base");
 
-            string onStuSql = $"select value(count(c.id)) from c join t in c.loginInfos where array_length(c.loginInfos) > 0 and t.expire > {hour1} and c.pk='Base'";
-            onTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", onStuSql);
-            onStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", onStuSql);
+            //string onStuSql = $"select value(count(c.id)) from c join t in c.loginInfos where array_length(c.loginInfos) > 0 and t.expire > {hour1} and c.pk='Base'";
+            //onTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", onStuSql);
+            //onStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", onStuSql);
+
+            #region 取得在線人數
+            var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
+            var redisClinet = _azureRedis.GetRedisClient(8);
+            var (strDaySt, strDayEt) = TimeHelper.GetUnixToDate(daySt, dayEt, "yyyyMMddHH");
+            Dictionary<long, int> allDays = new();  //所有在线人数
+            Dictionary<long, int> tchDays = new();  //教师在线人数
+            Dictionary<long, int> stuDays = new();  //学生在线人数
+            Dictionary<long, int> tmdDays = new();  //醍摩豆账户学生
+            int tchkey = 0;// 最近時間的key
+
+            // 撈redis記錄的教師在線人數
+            SortedSetEntry[] tchDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:teacher:{dateDay}");
+            if (tchDay.Length > 0)// 如果redis有資料就直接使用
+            {
+                foreach (var item in tchDay)
+                {
+                    int val = ((int)item.Score);
+                    int key = ((int)item.Element);
+                    tchDays.Add(key, val);                   
+                }
+                
+                var tchDaysOrderBy = tchDays.OrderBy(o => o.Key).ToList();
+                tchkey = (int)tchDaysOrderBy[tchDaysOrderBy.Count - 1].Key;
+                onTchCnt = (int)tchDaysOrderBy[tchkey].Value;              
+            }
+            else
+            {// 如果redis沒資料去Azure Storage取
+                string tableSqlTch = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
+                List<HourLogin> hourLoginsTch = await table.QueryWhereString<HourLogin>(tableSqlTch);
+
+                if (hourLoginsTch.Count > 0)
+                {
+                    foreach (var item in hourLoginsTch)
+                    {                       
+                        tchDays.Add(item.Hour, item.Teacher);                       
+                    }
+                    var tchDaysOrderBy = tchDays.OrderBy(o => o.Key).ToList();
+                    tchkey = (int)tchDaysOrderBy[tchDaysOrderBy.Count - 1].Key;
+                    onTchCnt = (int)tchDaysOrderBy[tchkey].Value;
+                }
+            }
+            // 撈redis記錄的學生在線人數
+            SortedSetEntry[] stuDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:student:{dateDay}");
+            if (stuDay.Length > 0)// 如果redis有資料就直接使用
+            {
+                foreach (var item in stuDay)
+                {
+                    int val = ((int)item.Score);
+                    int key = ((int)item.Element);
+                    stuDays.Add(key, val);
+                }
+
+                var stuDaysOrderBy = stuDays.OrderBy(o => o.Key).ToList();
+                onStuCnt = (int)stuDaysOrderBy[stuDaysOrderBy.Count - 2].Value;
+            }
+            else
+            {// 如果redis沒資料去Azure Storage取
+                string tableSqlStu = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
+                List<HourLogin> hourLoginsStu = await table.QueryWhereString<HourLogin>(tableSqlStu);                
+                if (hourLoginsStu.Count > 0)
+                {
+                    foreach (var item in hourLoginsStu)
+                    {
+                        stuDays.Add(item.Hour, item.Student);
+                    }
+                    var stuDaysOrderBy = stuDays.OrderBy(o => o.Key).ToList();
+                    onStuCnt = (int)stuDaysOrderBy[stuDaysOrderBy.Count - 2].Value;             
+                }
+            }
+
+            SortedSetEntry[] tmdDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:tmduser:{dateDay}");
+            if (tmdDay.Length > 0)
+            {
+                foreach (var item in tmdDay)
+                {
+                    int val = (int)item.Score;
+                    int key = (int)item.Element;
+                    tmdDays.Add(key, val);                   
+                }
+                int iValue = 0;
+                if (tmdDays.TryGetValue(tchkey, out iValue)) 
+                {
+                    onStuCnt = onStuCnt + tmdDays[tchkey]; // 用最近時間的key取在線醍摩豆账户学生數
+                }                
+            }
+            else
+            {
+                string tableSqlTmd = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
+                List<HourLogin> hourLoginsTmd = await table.QueryWhereString<HourLogin>(tableSqlTmd);
+                if (hourLoginsTmd.Count > 0)
+                {
+                    foreach (var item in hourLoginsTmd)
+                    {                        
+                        var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, item.Hour, 00, 00)).Hour;
+                        tmdDays.Add(utcTo, item.TmdUser);                       
+                    }
+                    int iValue = 0;
+                    if (tmdDays.TryGetValue(tchkey, out iValue))
+                    {
+                        onStuCnt = onStuCnt + tmdDays[tchkey]; // 用最近時間的key取在線醍摩豆账户学生數
+                    }                    
+                }
+            }
+
+            #endregion
 
             //接口访问量
             List<RecCnt> recCnts = new();