SystemService.cs 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. using Azure.Storage.Blobs.Models;
  2. using DocumentFormat.OpenXml.Bibliography;
  3. using DocumentFormat.OpenXml.Office2010.Excel;
  4. using DocumentFormat.OpenXml.Wordprocessing;
  5. using HTEXLib;
  6. using HTEXLib.COMM.Helpers;
  7. using Microsoft.Azure.Amqp.Framing;
  8. using Newtonsoft.Json.Linq;
  9. using OpenXmlPowerTools;
  10. using StackExchange.Redis;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Diagnostics.PerformanceData;
  15. using System.IdentityModel.Tokens.Jwt;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Text.Json;
  19. using System.Text.RegularExpressions;
  20. using System.Threading.Tasks;
  21. using TEAMModelOS.SDK.DI;
  22. using TEAMModelOS.SDK.Extension;
  23. using TEAMModelOS.SDK.Models.Cosmos.Common;
  24. using TEAMModelOS.SDK.Models.Dtos;
  25. using static Azure.Core.HttpHeader;
  26. using static OpenXmlPowerTools.RevisionProcessor;
  27. using static TEAMModelOS.SDK.Models.Service.SystemService;
  28. namespace TEAMModelOS.SDK.Models.Service
  29. {
  30. public static class SystemService
  31. {
  32. static string cn_wb = @"
  33. IES晚报:
  34. 尊敬的{tmdname}您好,以下是您的今日个人IES教学汇总报告,截至{sendTime},您已发布{examCount}次评测任务,{homeworkCount}次作业任务,并使用HiTeach教师端开设了{lessonCount}节课堂教学活动。
  35. 以下是报告的具体详细信息。
  36. 评测任务提交详情:
  37. {examList}
  38. 课堂教学出席详情:
  39. {lessonList}
  40. 个人课程名单变化详情:
  41. {cn_grouplistJoin}
  42. {cn_grouplistLeave}
  43. 如有布置作业任务,将于次日8点通过早报方式发送。
  44. ";
  45. static string cn_examList = "{examName}已经有{submitCount}位学生提交,仍有{unsubmitCount}位学生未提交。";
  46. static string cn_lessonList = "{lessonName}课例应出席人数{memberCount},实际出席人数{attendCount},出席率{attendRate}。";
  47. static string cn_grouplistJoin = "{grouplistName}已有总人数{memberCount},有{joinCount}加入。";
  48. static string cn_grouplistLeave = "{grouplistName}已有总人数{memberCount},有{joinCount}离开。";
  49. static string cn_zb = @"
  50. IES早报:
  51. 尊敬的{tmdname}您好,以下是您发布过的作业任务汇总报告。
  52. 以下是报告的具体详细信息。
  53. 作业任务提交详情:
  54. {cn_homeworkList}
  55. ";
  56. static string cn_homeworkList = "{homeworkName}已经有{submitCount}位学生提交,仍有{unsubmitCount}位学生未提交。";
  57. public static async Task AccumulateDaily(AzureRedisFactory _azureRedis,AzureCosmosFactory _azureCosmos,CoreAPIHttpService coreAPIHttpService, DingDing dingDing)
  58. {
  59. DateTimeOffset now = DateTimeOffset.UtcNow;
  60. string day = now.ToString("yyyyMMdd");
  61. string homeworkYesterday = now.AddDays(-1).ToString("yyyyMMdd");
  62. List<string> keys = new List<string>();
  63. //当天评测发布的。
  64. //当天的个人评测提交数
  65. Dictionary<string, double?> exam_submit = new();
  66. SortedSetEntry[] exam_goingScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:private:exam-going:{day}");
  67. // Dictionary<string, double?> exam_going = new();
  68. if (exam_goingScores != null)
  69. {
  70. foreach (var score in exam_goingScores)
  71. {
  72. double val = score.Score;
  73. string key = score.Element.ToString();
  74. // exam_going.Add(key, val);
  75. exam_submit.Add(key, val);
  76. keys.Add(key);
  77. }
  78. }
  79. ///需要再次获取 没有任何人提交的作业和评测任务。
  80. SortedSetEntry[] exam_submitScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:private:exam-submit:{day}");
  81. if (exam_submitScores != null)
  82. {
  83. foreach (var score in exam_submitScores)
  84. {
  85. double val = score.Score;
  86. string key = score.Element.ToString();
  87. exam_submit.Add(key, val);
  88. keys.Add(key);
  89. }
  90. }
  91. //昨天发布的作业
  92. SortedSetEntry[] homework_goingScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:homework-going:{homeworkYesterday}");
  93. Dictionary<string, double?> homework_submit = new();
  94. //Dictionary<string, double?> homework_going = new();
  95. if (homework_goingScores != null)
  96. {
  97. foreach (var score in homework_goingScores)
  98. {
  99. double val = score.Score;
  100. string key = score.Element.ToString();
  101. //homework_going.Add(key, val);
  102. homework_submit.Add(key, val);
  103. keys.Add(key);
  104. }
  105. }
  106. //昨天的个人作业提交数
  107. SortedSetEntry[] homework_submitScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:homework-submit:{homeworkYesterday}");
  108. if (homework_submitScores != null)
  109. {
  110. foreach (var score in homework_submitScores)
  111. {
  112. double val = score.Score;
  113. string key = score.Element.ToString();
  114. homework_submit.Add(key, val);
  115. keys.Add(key);
  116. }
  117. }
  118. //当天的个人名单加入人数
  119. Dictionary<string, double?> grouplist = new();
  120. SortedSetEntry[] grouplistScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:grouplist:{day}");
  121. if (grouplistScores != null)
  122. {
  123. foreach (var score in grouplistScores)
  124. {
  125. double val = score.Score;
  126. string key = score.Element.ToString();
  127. grouplist.Add(key, val);
  128. keys.Add(key);
  129. }
  130. }
  131. //当天教师的开课数量Accumulate:Daily:teacher:lesson-create:20240527
  132. Dictionary<string, double?> lessoncreate = new();
  133. SortedSetEntry[] llessoncreateScores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Accumulate:Daily:teacher:lesson-create:{day}");
  134. if (llessoncreateScores != null)
  135. {
  136. foreach (var score in llessoncreateScores)
  137. {
  138. double val = score.Score;
  139. string key = score.Element.ToString();
  140. lessoncreate.Add(key, val);
  141. keys.Add(key);
  142. }
  143. }
  144. List<string> ids = new List<string>();
  145. keys.ForEach(x => {
  146. ids.Add(x.Split("::")[0]);
  147. });
  148. List<Teacher> teachers= new List<Teacher>();
  149. if (ids.IsNotEmpty())
  150. {
  151. string sql = $"select value c from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))})";
  152. var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetList<Teacher>(sql, "Base");
  153. if (result.list.IsNotEmpty())
  154. {
  155. teachers.AddRange(result.list);
  156. }
  157. }
  158. foreach (var teacher in teachers)
  159. {
  160. int examCount = 0,homeworkCount=0,lessonCount=0;
  161. StringBuilder examSB= new StringBuilder();
  162. foreach (var exam in exam_submit)
  163. {
  164. int sendTime = 20;
  165. string[] ks = exam.Key.Split("::");
  166. string tid = ks[0];
  167. string examId = ks[1];
  168. string examName = ks[3];
  169. if (tid.Equals(teacher.id)) {
  170. var tzt = now.GetGMTTime((int)teacher.timezone);
  171. if (tzt.Hour==sendTime)
  172. {
  173. string sql = $"select c.status from c where c.examId='{examId}' and c.pk='ExamClassResult'";
  174. var result = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Common).GetList<ExamClassResult>("", $"ExamClassResult-{tid}");
  175. if (result.list.IsNotEmpty()) {
  176. var unsubmit = result.list.SelectMany(x => x.status).Where(x => x==1);
  177. var submit = result.list.SelectMany(x => x.status).Where(x => x==0);
  178. examCount++;
  179. examSB.Append(cn_examList.Replace("{examName}",examName).Replace("{submitCount}",$"{submit}").Replace("{unsubmitCount}",$"{unsubmit}"));
  180. }
  181. }
  182. }
  183. }
  184. foreach (var homework in homework_submit)
  185. {
  186. int sendTime = 8;
  187. string[] ks = homework.Key.Split("::");
  188. string tid = ks[0];
  189. string examId = ks[1];
  190. string examName = ks[3];
  191. if (tid.Equals(teacher.id))
  192. {
  193. var tzt = now.GetGMTTime((int)teacher.timezone);
  194. if (tzt.Hour==sendTime)
  195. {
  196. string sql = $"select c.status from c where c.examId='{examId}' and c.pk='ExamClassResult'";
  197. }
  198. }
  199. }
  200. }
  201. }
  202. public static async Task RecordAccumulateData(AzureRedisFactory azureRedis, DingDing dingDing, Accumulate accumulate)
  203. {
  204. if (!string.IsNullOrWhiteSpace(accumulate.key) && !string.IsNullOrWhiteSpace(accumulate.target) &&
  205. !string.IsNullOrWhiteSpace(accumulate.id) && !string.IsNullOrWhiteSpace(accumulate.name) &&
  206. !string.IsNullOrWhiteSpace(accumulate.scope) && !string.IsNullOrWhiteSpace(accumulate.client))
  207. {
  208. await RecordAccumulateData(azureRedis, accumulate.key, accumulate.target, accumulate.id, accumulate.name, accumulate.scope, accumulate.client, accumulate.count);
  209. }
  210. else
  211. {
  212. await dingDing.SendBotMsg($"IES累计数据变更统计参数异常,{accumulate.ToJsonString()}", GroupNames.成都开发測試群組);
  213. }
  214. }
  215. /// <summary>
  216. /// 记录累计数据
  217. /// </summary>
  218. public static async Task RecordAccumulateData(AzureRedisFactory azureRedis, string key, string target, string id, string name, string scope, string client, int count)
  219. {
  220. if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(target) &&
  221. !string.IsNullOrWhiteSpace(id) && !string.IsNullOrWhiteSpace(name) &&
  222. !string.IsNullOrWhiteSpace(scope)&& !string.IsNullOrWhiteSpace(client))
  223. {
  224. //处理UTC时差
  225. var nowTime = DateTimeOffset.UtcNow.GetGMTTime();
  226. int difference = (DayOfWeek.Sunday - nowTime.DayOfWeek + 7) % 7; //1-7的结果0-6
  227. var day = nowTime.ToString("yyyyMMdd");
  228. string redisKey = $"Accumulate:Daily:{scope}:{key}:{day}";
  229. string member = $"{target}::{id}::{client}::{name}";
  230. await azureRedis.GetRedisClient(8).SortedSetIncrementAsync(redisKey, member, count);
  231. await azureRedis.GetRedisClient(8).KeyExpireAsync(redisKey, new TimeSpan((difference+1)*24, 10, 0));
  232. //if (key.Equals("lesson") || key.StartsWith("login_"))
  233. //{
  234. // redisKey = $"Accumulate:Daily:ies:{key}:{day}";
  235. // if (scope.Equals("school"))
  236. // {
  237. // member = $"ies::{id}::{name}";
  238. // }
  239. // else {
  240. // member = $"ies::ies::{name}";
  241. // }
  242. // await azureRedis.GetRedisClient(8).SortedSetIncrementAsync(redisKey, member, 1);
  243. //}
  244. }
  245. }
  246. static double GetUserDuration(List<long> times )
  247. {
  248. if (times.IsNotEmpty())
  249. {
  250. if (times.Count>=2)
  251. {
  252. double totalDuration = 0;
  253. // DateTime lastTime = DateTimeOffset.FromUnixTimeMilliseconds((long)jsonData[0]["time"]).UtcDateTime;
  254. long ltime = times[0];
  255. for (int i = 1; i < times.Count; i++)
  256. {
  257. long ctime = times[i];
  258. //DateTime currentTime = DateTimeOffset.FromUnixTimeMilliseconds((long)jsonData[i]["time"]).UtcDateTime;
  259. long timeDifference = ctime - ltime;
  260. ///如果一小时内连续操作,则按真实时间累计
  261. if (timeDifference < 3600000)
  262. {
  263. totalDuration += timeDifference;
  264. }
  265. else
  266. { ///如果一小时内没有连续操作,则按象征性加10秒,以确保数据有效性。 如果因为一小时没有操作,则表示没有新的接口再次请求,当即就已经退出系统。否则后续仍然会有接口进入,并且带有时间戳
  267. totalDuration += 10000;
  268. }
  269. ltime = ctime;
  270. }
  271. return totalDuration;
  272. }
  273. else { return 10000; }
  274. }
  275. else {
  276. return 0;
  277. }
  278. }
  279. public static List<SchoolStick> CountSchoolStickiness(IEnumerable<IGrouping<string,ApiVisit>> schoolApiVisits, IEnumerable<ApiVisit> apiVisits)
  280. {
  281. List<SchoolStick> schoolStickiness = new List<SchoolStick>();
  282. foreach (var chinaSchoolKey in schoolApiVisits)
  283. {
  284. SchoolStick stickiness = new SchoolStick()
  285. {
  286. id = chinaSchoolKey.Key
  287. };
  288. var teachers = chinaSchoolKey.ToList().Where(x => !string.IsNullOrWhiteSpace(x.userId) && !string.IsNullOrWhiteSpace(x.scope) && x.scope.Equals("teacher")).GroupBy(x => x.userId);
  289. double hitaTime = 0, hiteachTime = 0, ies5TchTime = 0, otherTchTime = 0, ies5StuTime = 0, otherStuTime = 0;
  290. int hitaTch= 0, hiteachTch = 0, ies5Tch = 0, otherTch = 0, ies5Stu = 0, otherStu = 0;
  291. double hitaCount = 0, hiteachCount = 0, ies5TchCount = 0, otherTchCount = 0, ies5StuCount = 0, otherStuCount = 0;
  292. double lessonCount= chinaSchoolKey.ToList().Where(x => x.path.Contains("hiteach/create-lesson")).Count() ;
  293. int teacherCount= teachers.Count() ;
  294. foreach (var user in teachers)
  295. {
  296. TchStick tchStick = new TchStick()
  297. {
  298. id = user.Key,
  299. school= chinaSchoolKey.Key
  300. };
  301. //HiTA
  302. var teacherHitaVisit = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.userId) && x.userId.Equals(user.Key) && x.client.Equals("hita"));
  303. var teacherHitaVisitCount = teacherHitaVisit.Count();
  304. if (teacherHitaVisitCount>0)
  305. { hitaTch+=1; }
  306. var teacherHitaVisitTime = GetUserDuration(teacherHitaVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  307. hitaCount+=teacherHitaVisitCount;
  308. hitaTime+=teacherHitaVisitTime;
  309. tchStick.hita.count.value=teacherHitaVisitCount;
  310. tchStick.hita.duration.value=teacherHitaVisitTime;
  311. //Other
  312. var teacherOtherVisit = user.Where(x => !x.client.Equals("hita")&&!x.client.Equals("hiteach")&&!x.client.Equals("ies5"));
  313. var teacherOtherVisitCount= teacherOtherVisit.Count();
  314. var teacherOtherVisitTime= GetUserDuration(teacherOtherVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  315. if (teacherOtherVisitCount>0)
  316. { otherTch+=1; }
  317. otherTchCount+=teacherOtherVisitCount;
  318. otherTchTime+=teacherOtherVisitTime;
  319. tchStick.otherTch.count.value=teacherOtherVisitCount;
  320. tchStick.otherTch.duration.value=teacherOtherVisitTime;
  321. //HiTeach
  322. //教师访问hiteach 不带学校信息的
  323. var teacherHiteachVisit = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.userId) && x.userId.Equals(user.Key) && x.client.Equals("hiteach") && string.IsNullOrWhiteSpace(x.school));
  324. //教师访问hiteach 包含学校信息的
  325. var teacherHiteachSchoolVisit = user.Where(x => x.client.Equals("hiteach"));
  326. //教师所有的Hiteach访问
  327. List<ApiVisit> teacherHiteachAllVisit = new List<ApiVisit>();
  328. teacherHiteachAllVisit.AddRange(teacherHiteachVisit);
  329. teacherHiteachAllVisit.AddRange(teacherHiteachSchoolVisit);
  330. var teacherHiteachAllVisitCount = teacherHiteachAllVisit.Count();
  331. if (teacherHiteachAllVisitCount>0)
  332. { hiteachTch+=1; }
  333. var teacherHiteachAllVisitTime = GetUserDuration(teacherHiteachAllVisit.OrderBy(x => x.time).Select(x => x.time).ToList());
  334. hiteachCount+=teacherHiteachAllVisitCount;
  335. hiteachTime+=teacherHiteachAllVisitTime;
  336. tchStick.hiteach.count.value=teacherHiteachAllVisitCount;
  337. tchStick.hiteach.duration.value=teacherHiteachAllVisitTime;
  338. //IES5
  339. var teacherIes5Visit = user.Where(x => x.client.Equals("ies5"));
  340. var teacherIes5VisitCount= teacherIes5Visit.Count();
  341. if (teacherIes5VisitCount>0)
  342. { ies5Tch+=1; }
  343. var teacherIes5VisitTime = GetUserDuration(teacherIes5Visit.OrderBy(x => x.time).Select(x => x.time).ToList());
  344. ies5TchCount+=teacherIes5VisitCount;
  345. ies5TchTime+=teacherIes5VisitTime;
  346. tchStick.ies5Tch.count.value=teacherIes5VisitCount;
  347. tchStick.ies5Tch.duration.value=teacherIes5VisitTime;
  348. double lessonCountTch = chinaSchoolKey.ToList().Where(x => x.path.Contains("hiteach/create-lesson") && x.userId.Equals(user.Key)).Count();
  349. tchStick.lesson.value=lessonCountTch;
  350. tchStick.tmd.count.value=tchStick.hita.count.value+tchStick.hiteach.count.value+tchStick.ies5Tch.count.value+tchStick.otherTch.count.value;
  351. tchStick.tmd.duration.value=tchStick.hita.duration.value+tchStick.hiteach.duration.value+tchStick.ies5Tch.duration.value+tchStick.otherTch.duration.value;
  352. tchStick.hita.userCount=1;
  353. tchStick.hiteach.userCount=1;
  354. tchStick.ies5Tch.userCount=1;
  355. tchStick.otherTch.userCount=1;
  356. tchStick.tmd.userCount=1;
  357. if (tchStick.hita.count.value>0 && tchStick.hita.duration.value>0)
  358. {
  359. tchStick.hita.stick.value= (tchStick.hita.duration.value/1000/tchStick.hita.count.value);
  360. }
  361. if (tchStick.ies5Tch.count.value>0 && tchStick.ies5Tch.duration.value>0)
  362. {
  363. tchStick.ies5Tch.stick.value= (tchStick.ies5Tch.duration.value/1000/tchStick.ies5Tch.count.value);
  364. }
  365. if (tchStick.otherTch.count.value>0 && tchStick.otherTch.duration.value>0)
  366. {
  367. tchStick.otherTch.stick.value= (tchStick.otherTch.duration.value/1000/tchStick.otherTch.count.value);
  368. }
  369. if (tchStick.hiteach.count.value>0 && tchStick.hiteach.duration.value>0)
  370. {
  371. tchStick.hiteach.stick.value= (tchStick.hiteach.duration.value/1000/tchStick.hiteach.count.value)+tchStick.lesson.value;
  372. }
  373. if (tchStick.tmd.count.value>0 && tchStick.tmd.duration.value>0)
  374. {
  375. tchStick.tmd.stick.value= (tchStick.tmd.duration.value/1000/tchStick.tmd.count.value)+ tchStick.lesson.value;
  376. }
  377. stickiness.tchSticks.Add(tchStick);
  378. }
  379. var student = chinaSchoolKey.ToList().Where(x => !string.IsNullOrWhiteSpace(x.userId) && !string.IsNullOrWhiteSpace(x.scope) && (x.scope.Equals("student")||x.scope.Equals("tmduser"))).GroupBy(x => x.userId);
  380. var studentCount = student.Count();
  381. foreach (var user in student)
  382. {
  383. StuStick stuStick = new StuStick
  384. {
  385. id=user.Key,
  386. school=chinaSchoolKey.Key
  387. };
  388. var studentOtherVisit = user.Where(x => !x.client.Equals("ies5"));
  389. var studentOtherVisitCount = studentOtherVisit.Count();
  390. if (studentOtherVisitCount>0)
  391. { otherStu+=1; }
  392. double studentOtherVisitTime = GetUserDuration(studentOtherVisit.OrderBy(x => x.time).Select(x => x.time).ToList()) ;
  393. var studentIes5Visit = user.Where(x => x.client.Equals("ies5"));
  394. var studentIes5VisitCount = studentIes5Visit.Count();
  395. if (studentIes5VisitCount>0)
  396. { ies5Stu+=1; }
  397. double studentIes5VisitTime = GetUserDuration(studentIes5Visit.OrderBy(x => x.time).Select(x => x.time).ToList());
  398. ies5StuCount+=studentIes5VisitCount;
  399. ies5StuTime+=studentIes5VisitTime;
  400. otherStuCount+=studentOtherVisitCount;
  401. otherStuTime+=studentOtherVisitTime;
  402. stuStick.ies5Stu.count.value=studentIes5VisitCount;
  403. stuStick.ies5Stu.duration.value=studentIes5VisitTime;
  404. stuStick.otherStu.count.value=studentOtherVisitCount;
  405. stuStick.otherStu.duration.value=studentOtherVisitTime;
  406. stuStick.tmd.count.value= stuStick.ies5Stu.count.value+stuStick.otherStu.count.value;
  407. stuStick.tmd.duration.value= stuStick.ies5Stu.duration.value+stuStick.otherStu.duration.value;
  408. stuStick.ies5Stu.userCount=1;
  409. stuStick.otherStu.userCount=1;
  410. stuStick.tmd.userCount=1;
  411. if (stuStick.ies5Stu.count.value>0 && stuStick.ies5Stu.duration.value>0)
  412. {
  413. stuStick.ies5Stu.stick.value= (stuStick.ies5Stu.duration.value/1000/stuStick.ies5Stu.count.value);
  414. }
  415. if (stuStick.otherStu.count.value>0 && stuStick.otherStu.duration.value>0)
  416. {
  417. stuStick.otherStu.stick.value=(stuStick.otherStu.duration.value/1000/stuStick.otherStu.count.value);
  418. }
  419. if (stuStick.tmd.count.value>0 && stuStick.tmd.duration.value>0)
  420. {
  421. stuStick.tmd.stick.value=(stuStick.tmd.duration.value/1000/stuStick.tmd.count.value);
  422. }
  423. stickiness.stuSticks.Add(stuStick);
  424. }
  425. stickiness.hita.duration.value=hitaTime;
  426. stickiness.hiteach.duration.value=hiteachTime;
  427. stickiness.otherTch.duration.value=otherTchTime;
  428. stickiness.ies5Tch.duration.value=ies5TchTime;
  429. stickiness.tmd.duration.value=hitaTime+hiteachTime+otherTchTime+ies5TchTime+otherStuTime+ies5StuTime;
  430. stickiness.hita.userCount=hitaTch;
  431. stickiness.hiteach.userCount=hiteachTch;
  432. stickiness.ies5Tch.userCount=ies5Tch;
  433. stickiness.otherTch.userCount=otherTch;
  434. stickiness.ies5Stu.userCount=ies5Stu;
  435. stickiness.otherStu.userCount=otherStu;
  436. stickiness.hita.count.value=hitaCount;
  437. stickiness.hiteach.count.value=hiteachCount;
  438. stickiness.ies5Tch.count.value=ies5TchCount;
  439. stickiness.otherTch.count.value=otherTchCount;
  440. stickiness.ies5Stu.count.value=ies5StuCount;
  441. stickiness.otherStu.count.value=otherStuCount;
  442. stickiness.tmd.count.value=hitaCount+hiteachCount+ies5TchCount+otherTchCount+ies5StuCount+otherStuCount;
  443. stickiness.lesson.value=lessonCount;
  444. stickiness.tmd.userCount=teacherCount+studentCount;
  445. if(stickiness.hita.count.value>0 && stickiness.hita.duration.value>0)
  446. {
  447. stickiness.hita.stick.value=(stickiness.hita.userCount* stickiness.hita.count.value)/(stickiness.hita.duration.value/1000/stickiness.hita.count.value);
  448. }
  449. if (stickiness.ies5Stu.count.value>0 && stickiness.ies5Stu.duration.value>0)
  450. {
  451. stickiness.ies5Stu.stick.value=(stickiness.ies5Stu.userCount* stickiness.ies5Stu.count.value)/(stickiness.ies5Stu.duration.value/1000/stickiness.ies5Stu.count.value);
  452. }
  453. if (stickiness.ies5Tch.count.value>0 && stickiness.ies5Tch.duration.value>0)
  454. {
  455. stickiness.ies5Tch.stick.value=(stickiness.ies5Tch.userCount* stickiness.ies5Tch.count.value)/(stickiness.ies5Tch.duration.value/1000/stickiness.ies5Tch.count.value);
  456. }
  457. if (stickiness.otherStu.count.value>0 && stickiness.otherStu.duration.value>0)
  458. {
  459. stickiness.otherStu.stick.value=(stickiness.otherStu.userCount* stickiness.otherStu.count.value)/(stickiness.otherStu.duration.value/1000/stickiness.otherStu.count.value);
  460. }
  461. if (stickiness.otherTch.count.value>0 && stickiness.otherTch.duration.value>0)
  462. {
  463. stickiness.otherTch.stick.value=(stickiness.otherTch.userCount* stickiness.otherTch.count.value)/(stickiness.otherTch.duration.value/1000/stickiness.otherTch.count.value);
  464. }
  465. if (stickiness.hiteach.count.value>0 && stickiness.hiteach.duration.value>0)
  466. {
  467. stickiness.hiteach.stick.value=(stickiness.hiteach.userCount* stickiness.hiteach.count.value)/(stickiness.hiteach.duration.value/1000/stickiness.hiteach.count.value)+stickiness.lesson.value;
  468. }
  469. if (stickiness.tmd.count.value>0 && stickiness.tmd.duration.value>0)
  470. {
  471. stickiness.tmd.stick.value=(stickiness.tmd.userCount* stickiness.tmd.count.value)/(stickiness.tmd.duration.value/1000/stickiness.tmd.count.value)+ stickiness.lesson.value;
  472. }
  473. stickiness.tchSticks= OrderByTchStick(stickiness.tchSticks);
  474. stickiness.stuSticks= OrderByStuStick(stickiness.stuSticks);
  475. schoolStickiness.Add(stickiness);
  476. }
  477. return OrderBySchoolStick(schoolStickiness);
  478. }
  479. private static List<StuStick> OrderByStuStick(List<StuStick> stuStickiness)
  480. {
  481. // 假设visit已经填充了数据
  482. #region
  483. var rank_tmd_count = stuStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  484. var rank_ies5Stu_count = stuStickiness.Select(x => x.ies5Stu.count.value).Distinct().OrderByDescending(v => v).ToList();
  485. var rank_otherStu_count = stuStickiness.Select(x => x.otherStu.count.value).Distinct().OrderByDescending(v => v).ToList();
  486. #endregion
  487. #region
  488. var rank_tmd_duration = stuStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  489. var rank_ies5Stu_duration = stuStickiness.Select(x => x.ies5Stu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  490. var rank_otherStu_duration = stuStickiness.Select(x => x.otherStu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  491. #endregion
  492. #region
  493. var rank_tmd_stick = stuStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  494. var rank_ies5Stu_stick = stuStickiness.Select(x => x.ies5Stu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  495. var rank_otherStu_stick = stuStickiness.Select(x => x.otherStu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  496. #endregion
  497. stuStickiness.ForEach(x =>
  498. {
  499. #region
  500. if (x.tmd.count.value>0)
  501. {
  502. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  503. if (index_tmd_count!=-1)
  504. {
  505. x.tmd.count.rank = index_tmd_count + 1;
  506. }
  507. }
  508. if (x.ies5Stu.count.value>0)
  509. {
  510. int index_ies5Stu_count = rank_ies5Stu_count.FindIndex(i => i==x.ies5Stu.count.value);
  511. if (index_ies5Stu_count!=-1)
  512. {
  513. x.ies5Stu.count.rank = index_ies5Stu_count + 1;
  514. }
  515. }
  516. if (x.otherStu.count.value>0)
  517. {
  518. int index_otherStu_count = rank_otherStu_count.FindIndex(i => i==x.otherStu.count.value);
  519. if (index_otherStu_count!=-1)
  520. {
  521. x.otherStu.count.rank = index_otherStu_count + 1;
  522. }
  523. }
  524. #endregion
  525. #region
  526. if (x.tmd.duration.value> 0) {
  527. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  528. if (index_tmd_duration!=-1)
  529. {
  530. x.tmd.duration.rank = index_tmd_duration + 1;
  531. }
  532. }
  533. if (x.ies5Stu.duration.value>0) {
  534. int index_ies5Stu_duration = rank_ies5Stu_duration.FindIndex(i => i==x.ies5Stu.duration.value);
  535. if (index_ies5Stu_duration!=-1)
  536. {
  537. x.ies5Stu.duration.rank = index_ies5Stu_duration + 1;
  538. }
  539. }
  540. if (x.otherStu.duration.value>0) {
  541. int index_otherStu_duration = rank_otherStu_duration.FindIndex(i => i==x.otherStu.duration.value);
  542. if (index_otherStu_duration!=-1)
  543. {
  544. x.otherStu.duration.rank = index_otherStu_duration + 1;
  545. }
  546. }
  547. #endregion
  548. #region
  549. if (x.tmd.stick.value>0)
  550. {
  551. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  552. if (index_tmd_stick!=-1)
  553. {
  554. x.tmd.stick.rank = index_tmd_stick + 1;
  555. }
  556. }
  557. if (x.ies5Stu.stick.value>0)
  558. {
  559. int index_ies5Stu_stick = rank_ies5Stu_stick.FindIndex(i => i==x.ies5Stu.stick.value);
  560. if (index_ies5Stu_stick!=-1)
  561. {
  562. x.ies5Stu.stick.rank = index_ies5Stu_stick + 1;
  563. }
  564. }
  565. if (x.otherStu.stick.value>0)
  566. {
  567. int index_otherStu_stick = rank_otherStu_stick.FindIndex(i => i==x.otherStu.stick.value);
  568. if (index_otherStu_stick!=-1)
  569. {
  570. x.otherStu.stick.rank = index_otherStu_stick + 1;
  571. }
  572. }
  573. #endregion
  574. });
  575. return stuStickiness;
  576. }
  577. private static List<TchStick> OrderByTchStick(List<TchStick> tchStickiness)
  578. {
  579. // 假设visit已经填充了数据
  580. #region
  581. var rank_tmd_count = tchStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  582. var rank_hita_count = tchStickiness.Select(x => x.hita.count.value).Distinct().OrderByDescending(v => v).ToList();
  583. var rank_hiteach_count = tchStickiness.Select(x => x.hiteach.count.value).Distinct().OrderByDescending(v => v).ToList();
  584. var rank_ies5Tch_count = tchStickiness.Select(x => x.ies5Tch.count.value).Distinct().OrderByDescending(v => v).ToList();
  585. var rank_otherTch_count = tchStickiness.Select(x => x.otherTch.count.value).Distinct().OrderByDescending(v => v).ToList();
  586. var rank_lesson_count = tchStickiness.Select(x => x.lesson.value).Distinct().OrderByDescending(v => v).ToList();
  587. #endregion
  588. #region
  589. var rank_tmd_duration = tchStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  590. var rank_hita_duration = tchStickiness.Select(x => x.hita.duration.value).Distinct().OrderByDescending(v => v).ToList();
  591. var rank_hiteach_duration = tchStickiness.Select(x => x.hiteach.duration.value).Distinct().OrderByDescending(v => v).ToList();
  592. var rank_ies5Tch_duration = tchStickiness.Select(x => x.ies5Tch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  593. var rank_otherTch_duration = tchStickiness.Select(x => x.otherTch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  594. #endregion
  595. #region
  596. var rank_tmd_stick = tchStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  597. var rank_hita_stick = tchStickiness.Select(x => x.hita.stick.value).Distinct().OrderByDescending(v => v).ToList();
  598. var rank_hiteach_stick = tchStickiness.Select(x => x.hiteach.stick.value).Distinct().OrderByDescending(v => v).ToList();
  599. var rank_ies5Tch_stick = tchStickiness.Select(x => x.ies5Tch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  600. var rank_otherTch_stick = tchStickiness.Select(x => x.otherTch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  601. #endregion
  602. tchStickiness.ForEach(x => {
  603. #region
  604. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  605. if (index_tmd_count!=-1)
  606. {
  607. x.tmd.count.rank = index_tmd_count + 1;
  608. }
  609. int index_hita_count = rank_hita_count.FindIndex(i => i==x.hita.count.value);
  610. if (index_hita_count!=-1)
  611. {
  612. x.hita.count.rank = index_hita_count + 1;
  613. }
  614. int index_hiteach_count = rank_hiteach_count.FindIndex(i => i==x.hiteach.count.value);
  615. if (index_hiteach_count!=-1)
  616. {
  617. x.hiteach.count.rank = index_hiteach_count + 1;
  618. }
  619. int index_ies5Tch_count = rank_ies5Tch_count.FindIndex(i => i==x.ies5Tch.count.value);
  620. if (index_ies5Tch_count!=-1)
  621. {
  622. x.ies5Tch.count.rank = index_ies5Tch_count + 1;
  623. }
  624. int index_otherTch_count = rank_otherTch_count.FindIndex(i => i==x.otherTch.count.value);
  625. if (index_otherTch_count!=-1)
  626. {
  627. x.otherTch.count.rank = index_otherTch_count + 1;
  628. }
  629. int index_lesson_count = rank_lesson_count.FindIndex(i => i==x.lesson.value);
  630. if (index_lesson_count!=-1)
  631. {
  632. x.lesson.rank = index_lesson_count + 1;
  633. }
  634. #endregion
  635. #region
  636. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  637. if (index_tmd_duration!=-1)
  638. {
  639. x.tmd.duration.rank = index_tmd_duration + 1;
  640. }
  641. int index_hita_duration = rank_hita_duration.FindIndex(i => i==x.hita.duration.value);
  642. if (index_hita_duration!=-1)
  643. {
  644. x.hita.duration.rank = index_hita_duration + 1;
  645. }
  646. int index_hiteach_duration = rank_hiteach_duration.FindIndex(i => i==x.hiteach.duration.value);
  647. if (index_hiteach_duration!=-1)
  648. {
  649. x.hiteach.duration.rank = index_hiteach_duration + 1;
  650. }
  651. int index_ies5Tch_duration = rank_ies5Tch_duration.FindIndex(i => i==x.ies5Tch.duration.value);
  652. if (index_ies5Tch_duration!=-1)
  653. {
  654. x.ies5Tch.duration.rank = index_ies5Tch_duration + 1;
  655. }
  656. int index_otherTch_duration = rank_otherTch_duration.FindIndex(i => i==x.otherTch.duration.value);
  657. if (index_otherTch_duration!=-1)
  658. {
  659. x.otherTch.duration.rank = index_otherTch_duration + 1;
  660. }
  661. #endregion
  662. #region
  663. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  664. if (index_tmd_stick!=-1)
  665. {
  666. x.tmd.stick.rank = index_tmd_stick + 1;
  667. }
  668. int index_hita_stick = rank_hita_stick.FindIndex(i => i==x.hita.stick.value);
  669. if (index_hita_stick!=-1)
  670. {
  671. x.hita.stick.rank = index_hita_stick + 1;
  672. }
  673. int index_hiteach_stick = rank_hiteach_stick.FindIndex(i => i==x.hiteach.stick.value);
  674. if (index_hiteach_stick!=-1)
  675. {
  676. x.hiteach.stick.rank = index_hiteach_stick + 1;
  677. }
  678. int index_ies5Tch_stick = rank_ies5Tch_stick.FindIndex(i => i==x.ies5Tch.stick.value);
  679. if (index_ies5Tch_stick!=-1)
  680. {
  681. x.ies5Tch.stick.rank = index_ies5Tch_stick + 1;
  682. }
  683. int index_otherTch_stick = rank_otherTch_stick.FindIndex(i => i==x.otherTch.stick.value);
  684. if (index_otherTch_stick!=-1)
  685. {
  686. x.otherTch.stick.rank = index_otherTch_stick + 1;
  687. }
  688. #endregion
  689. });
  690. return tchStickiness;
  691. }
  692. private static List<SchoolStick> OrderBySchoolStick(List<SchoolStick> schoolStickiness)
  693. {
  694. // 假设visit已经填充了数据
  695. #region
  696. var rank_tmd_count = schoolStickiness.Select(x => x.tmd.count.value).Distinct().OrderByDescending(v => v).ToList();
  697. var rank_hita_count = schoolStickiness.Select(x => x.hita.count.value).Distinct().OrderByDescending(v => v).ToList();
  698. var rank_hiteach_count = schoolStickiness.Select(x => x.hiteach.count.value).Distinct().OrderByDescending(v => v).ToList();
  699. var rank_ies5Tch_count = schoolStickiness.Select(x => x.ies5Tch.count.value).Distinct().OrderByDescending(v => v).ToList();
  700. var rank_otherTch_count = schoolStickiness.Select(x => x.otherTch.count.value).Distinct().OrderByDescending(v => v).ToList();
  701. var rank_ies5Stu_count = schoolStickiness.Select(x => x.ies5Stu.count.value).Distinct().OrderByDescending(v => v).ToList();
  702. var rank_otherStu_count = schoolStickiness.Select(x => x.otherStu.count.value).Distinct().OrderByDescending(v => v).ToList();
  703. var rank_lesson_count = schoolStickiness.Select(x => x.lesson.value).Distinct().OrderByDescending(v => v).ToList();
  704. #endregion
  705. #region
  706. var rank_tmd_duration = schoolStickiness.Select(x => x.tmd.duration.value).Distinct().OrderByDescending(v => v).ToList();
  707. var rank_hita_duration = schoolStickiness.Select(x => x.hita.duration.value).Distinct().OrderByDescending(v => v).ToList();
  708. var rank_hiteach_duration = schoolStickiness.Select(x => x.hiteach.duration.value).Distinct().OrderByDescending(v => v).ToList();
  709. var rank_ies5Tch_duration = schoolStickiness.Select(x => x.ies5Tch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  710. var rank_otherTch_duration = schoolStickiness.Select(x => x.otherTch.duration.value).Distinct().OrderByDescending(v => v).ToList();
  711. var rank_ies5Stu_duration = schoolStickiness.Select(x => x.ies5Stu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  712. var rank_otherStu_duration = schoolStickiness.Select(x => x.otherStu.duration.value).Distinct().OrderByDescending(v => v).ToList();
  713. #endregion
  714. #region
  715. var rank_tmd_stick = schoolStickiness.Select(x => x.tmd.stick.value).Distinct().OrderByDescending(v => v).ToList();
  716. var rank_hita_stick = schoolStickiness.Select(x => x.hita.stick.value).Distinct().OrderByDescending(v => v).ToList();
  717. var rank_hiteach_stick = schoolStickiness.Select(x => x.hiteach.stick.value).Distinct().OrderByDescending(v => v).ToList();
  718. var rank_ies5Tch_stick = schoolStickiness.Select(x => x.ies5Tch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  719. var rank_otherTch_stick = schoolStickiness.Select(x => x.otherTch.stick.value).Distinct().OrderByDescending(v => v).ToList();
  720. var rank_ies5Stu_stick = schoolStickiness.Select(x => x.ies5Stu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  721. var rank_otherStu_stick = schoolStickiness.Select(x => x.otherStu.stick.value).Distinct().OrderByDescending(v => v).ToList();
  722. #endregion
  723. schoolStickiness.ForEach(x => {
  724. #region
  725. if (x.tmd.count.value>0)
  726. {
  727. int index_tmd_count = rank_tmd_count.FindIndex(i => i==x.tmd.count.value);
  728. if (index_tmd_count!=-1)
  729. {
  730. x.tmd.count.rank = index_tmd_count + 1;
  731. }
  732. }
  733. if (x.hita.count.value>0)
  734. {
  735. int index_hita_count = rank_hita_count.FindIndex(i => i==x.hita.count.value);
  736. if (index_hita_count!=-1)
  737. {
  738. x.hita.count.rank = index_hita_count + 1;
  739. }
  740. }
  741. if (x.hiteach.count.value>0)
  742. {
  743. int index_hiteach_count = rank_hiteach_count.FindIndex(i => i==x.hiteach.count.value);
  744. if (index_hiteach_count!=-1)
  745. {
  746. x.hiteach.count.rank = index_hiteach_count + 1;
  747. }
  748. }
  749. if (x.ies5Tch.count.value>0)
  750. {
  751. int index_ies5Tch_count = rank_ies5Tch_count.FindIndex(i => i==x.ies5Tch.count.value);
  752. if (index_ies5Tch_count!=-1)
  753. {
  754. x.ies5Tch.count.rank = index_ies5Tch_count + 1;
  755. }
  756. }
  757. if (x.otherTch.count.value>0)
  758. {
  759. int index_otherTch_count = rank_otherTch_count.FindIndex(i => i==x.otherTch.count.value);
  760. if (index_otherTch_count!=-1)
  761. {
  762. x.otherTch.count.rank = index_otherTch_count + 1;
  763. }
  764. }
  765. if (x.ies5Stu.count.value>0)
  766. {
  767. int index_ies5Stu_count = rank_ies5Stu_count.FindIndex(i => i==x.ies5Stu.count.value);
  768. if (index_ies5Stu_count!=-1)
  769. {
  770. x.ies5Stu.count.rank = index_ies5Stu_count + 1;
  771. }
  772. }
  773. if (x.otherStu.count.value>0)
  774. {
  775. int index_otherStu_count = rank_otherStu_count.FindIndex(i => i==x.otherStu.count.value);
  776. if (index_otherStu_count!=-1)
  777. {
  778. x.otherStu.count.rank = index_otherStu_count + 1;
  779. }
  780. }
  781. if (x.lesson.value>0)
  782. {
  783. int index_lesson_count = rank_lesson_count.FindIndex(i => i==x.lesson.value);
  784. if (index_lesson_count!=-1)
  785. {
  786. x.lesson.rank = index_lesson_count + 1;
  787. }
  788. }
  789. #endregion
  790. #region
  791. if (x.tmd.duration.value>0)
  792. {
  793. int index_tmd_duration = rank_tmd_duration.FindIndex(i => i==x.tmd.duration.value);
  794. if (index_tmd_duration!=-1)
  795. {
  796. x.tmd.duration.rank = index_tmd_duration + 1;
  797. }
  798. }
  799. if (x.hita.duration.value>0)
  800. {
  801. int index_hita_duration = rank_hita_duration.FindIndex(i => i==x.hita.duration.value);
  802. if (index_hita_duration!=-1)
  803. {
  804. x.hita.duration.rank = index_hita_duration + 1;
  805. }
  806. }
  807. if (x.hiteach.duration.value>0)
  808. {
  809. int index_hiteach_duration = rank_hiteach_duration.FindIndex(i => i==x.hiteach.duration.value);
  810. if (index_hiteach_duration!=-1)
  811. {
  812. x.hiteach.duration.rank = index_hiteach_duration + 1;
  813. }
  814. }
  815. if (x.ies5Tch.duration.value>0) {
  816. int index_ies5Tch_duration = rank_ies5Tch_duration.FindIndex(i => i==x.ies5Tch.duration.value);
  817. if (index_ies5Tch_duration!=-1)
  818. {
  819. x.ies5Tch.duration.rank = index_ies5Tch_duration + 1;
  820. }
  821. }
  822. if (x.otherTch.duration.value>0)
  823. {
  824. int index_otherTch_duration = rank_otherTch_duration.FindIndex(i => i==x.otherTch.duration.value);
  825. if (index_otherTch_duration!=-1)
  826. {
  827. x.otherTch.duration.rank = index_otherTch_duration + 1;
  828. }
  829. }
  830. if (x.ies5Stu.duration.value>0)
  831. {
  832. int index_ies5Stu_duration = rank_ies5Stu_duration.FindIndex(i => i==x.ies5Stu.duration.value);
  833. if (index_ies5Stu_duration!=-1)
  834. {
  835. x.ies5Stu.duration.rank = index_ies5Stu_duration + 1;
  836. }
  837. }
  838. if (x.otherStu.duration.value>0)
  839. {
  840. int index_otherStu_duration = rank_otherStu_duration.FindIndex(i => i==x.otherStu.duration.value);
  841. if (index_otherStu_duration!=-1)
  842. {
  843. x.otherStu.duration.rank = index_otherStu_duration + 1;
  844. }
  845. }
  846. #endregion
  847. #region
  848. if (x.tmd.stick.value>0)
  849. {
  850. int index_tmd_stick = rank_tmd_stick.FindIndex(i => i==x.tmd.stick.value);
  851. if (index_tmd_stick!=-1)
  852. {
  853. x.tmd.stick.rank = index_tmd_stick + 1;
  854. }
  855. }
  856. if (x.hita.stick.value>0)
  857. {
  858. int index_hita_stick = rank_hita_stick.FindIndex(i => i==x.hita.stick.value);
  859. if (index_hita_stick!=-1)
  860. {
  861. x.hita.stick.rank = index_hita_stick + 1;
  862. }
  863. }
  864. if (x.hiteach.stick.value>0)
  865. {
  866. int index_hiteach_stick = rank_hiteach_stick.FindIndex(i => i==x.hiteach.stick.value);
  867. if (index_hiteach_stick!=-1)
  868. {
  869. x.hiteach.stick.rank = index_hiteach_stick + 1;
  870. }
  871. }
  872. if (x.ies5Tch.stick.value>0)
  873. {
  874. int index_ies5Tch_stick = rank_ies5Tch_stick.FindIndex(i => i==x.ies5Tch.stick.value);
  875. if (index_ies5Tch_stick!=-1)
  876. {
  877. x.ies5Tch.stick.rank = index_ies5Tch_stick + 1;
  878. }
  879. }
  880. if (x.otherTch.stick.value>0)
  881. {
  882. int index_otherTch_stick = rank_otherTch_stick.FindIndex(i => i==x.otherTch.stick.value);
  883. if (index_otherTch_stick!=-1)
  884. {
  885. x.otherTch.stick.rank = index_otherTch_stick + 1;
  886. }
  887. }
  888. if (x.ies5Stu.stick.value>0)
  889. {
  890. int index_ies5Stu_stick = rank_ies5Stu_stick.FindIndex(i => i==x.ies5Stu.stick.value);
  891. if (index_ies5Stu_stick!=-1)
  892. {
  893. x.ies5Stu.stick.rank = index_ies5Stu_stick + 1;
  894. }
  895. }
  896. if (x.otherStu.stick.value>0)
  897. {
  898. int index_otherStu_stick = rank_otherStu_stick.FindIndex(i => i==x.otherStu.stick.value);
  899. if (index_otherStu_stick!=-1)
  900. {
  901. x.otherStu.stick.rank = index_otherStu_stick + 1;
  902. }
  903. }
  904. #endregion
  905. });
  906. return schoolStickiness;
  907. }
  908. /// <summary>
  909. /// 结算访问日志
  910. /// </summary>
  911. public static async Task<(List<SchoolStick> chinaSchoolStickiness, List<SchoolStick> globalSchoolStickiness)> VisitSettle(List<string> times ,AzureStorageFactory _azureStorage,AzureRedisFactory _azureRedis, Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator, IPSearcher _ipSearcher) {
  912. List<SchoolStick> chinaSchoolStickiness = new List<SchoolStick>();
  913. List<SchoolStick> globalSchoolStickiness = new List<SchoolStick>();
  914. foreach (var timeDate in times)
  915. {
  916. List<ApiVisit> apiVisits = new List<ApiVisit>();
  917. if (DateTimeOffset.TryParse(timeDate, out DateTimeOffset date))
  918. {
  919. BlobDownloadResult result = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobClient($"http-log/{date:yyyy}/{date:MM}/{date:dd}/index.log").DownloadContentAsync();
  920. var content = result.Content.ToString();
  921. content= content.Substring(0, content.Length-2);
  922. if (content.EndsWith("}"))
  923. {
  924. content=$"[{content}]";
  925. }
  926. else
  927. {
  928. content=$"[{content}}}]";
  929. }
  930. apiVisits.AddRange(content.ToObject<List<ApiVisit>>());
  931. var dateNow = DateTimeOffset.Now.GetGMTTime(8);
  932. if ($"{dateNow:yyyy-MM-dd}".Equals($"{date:yyyy-MM-dd}"))
  933. { //当前小时一致的
  934. BlobDownloadResult resultHour = await _azureStorage.GetBlobContainerClient("0-service-log").GetBlobClient($"http-log/{date:yyyy}/{date:MM}/{date:dd}/{date:HH}.log").DownloadContentAsync();
  935. var contentHour = resultHour.Content.ToString();
  936. contentHour= contentHour.Substring(0, contentHour.Length-2);
  937. if (contentHour.EndsWith("}"))
  938. {
  939. contentHour=$"[{contentHour}]";
  940. }
  941. else
  942. {
  943. contentHour=$"[{contentHour}}}]";
  944. }
  945. var httpLog = contentHour.ToObject<List<HttpLog>>();
  946. (ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo) = await SystemService.ConvertHttpLog(httpLog, _azureRedis, _ipSearcher, _longitudeLatitudeTranslator, dateNow, false);
  947. apiVisits.AddRange(visits);
  948. }
  949. }
  950. //大陆学校
  951. var chinaApiVisits = apiVisits.Where(x => x.hostName.Equals("大陆站"));
  952. var chinaSchoolVisits = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.school) && x.hostName.Equals("大陆站")).GroupBy(x => x.school);
  953. List<SchoolStick> chinaStickiness = CountSchoolStickiness(chinaSchoolVisits, chinaApiVisits);
  954. chinaSchoolStickiness.AddRange(chinaStickiness);
  955. //国际学校
  956. var globalApiVisits = apiVisits.Where(x => x.hostName.Equals("国际站"));
  957. var globalSchoolVisits = apiVisits.Where(x => !string.IsNullOrWhiteSpace(x.school) && x.hostName.Equals("国际站")).GroupBy(x => x.school);
  958. List<SchoolStick> globalStickiness = CountSchoolStickiness(globalSchoolVisits, globalApiVisits);
  959. globalSchoolStickiness.AddRange(globalStickiness);
  960. }
  961. return (chinaSchoolStickiness, globalSchoolStickiness);
  962. }
  963. public static async Task<(ConcurrentBag<ApiVisit> visits, ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo)>
  964. ConvertHttpLog(List<HttpLog> logs, AzureRedisFactory _azureRedis, IPSearcher _ipSearcher, Region2LongitudeLatitudeTranslator _longitudeLatitudeTranslator, DateTimeOffset gmt8Time, bool test = false)
  965. {
  966. ConcurrentBag<ApiVisit> visits = new ConcurrentBag<ApiVisit>();
  967. ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)> uuidInfo = new ConcurrentBag<(string uuid, HttpLog httpLog, List<string> tmdid, List<string> school)>();
  968. object lockObj = new object();
  969. await Parallel.ForEachAsync(logs, async (log, _) =>
  970. {
  971. if (!log.path.Contains("."))
  972. {
  973. string uuid = Guid.NewGuid().ToString();
  974. List<string> schoolMatch = new List<string>();
  975. List<string> useridMatch = new List<string>();
  976. var vist = new ApiVisit()
  977. {
  978. id=uuid,
  979. ip=log.ip,
  980. tid=log.tid,
  981. time= log.time,
  982. userId= log.id,
  983. school= log.school,
  984. tname= log.name,
  985. path = log.path,
  986. client=log.p.Equals("os", StringComparison.OrdinalIgnoreCase) ? "ies5" : log.p,
  987. scope=log.scope,
  988. host= log.host,
  989. hostName=log.hostName,
  990. l=log.l,
  991. year = $"{gmt8Time:yyyy}",
  992. month = $"{gmt8Time:MM}",
  993. day = $"{gmt8Time:dd}",
  994. hour =$"{gmt8Time:HH}"
  995. };
  996. if (test)
  997. {
  998. var time = DateTimeOffset.FromUnixTimeMilliseconds(log.time);
  999. //本地调试时间
  1000. if (vist.ip.Equals("0.0.0.1"))
  1001. {
  1002. time= time.AddHours(8);
  1003. }
  1004. vist.year = $"{time:yyyy}";
  1005. vist.month = $"{time:MM}";
  1006. vist.day = $"{time:dd}";
  1007. vist.hour =$"{time:HH}";
  1008. }
  1009. if (string.IsNullOrWhiteSpace(vist.userId))
  1010. {
  1011. //var jsonPathContext = new JsonPathContext();
  1012. //jsonPathContext.ValueSystem= new JsonTextValueSystem();
  1013. string path = "$..['id_token','idToken','idtoken','tmdid','id','teacherId','teacher','tid','tId','userid','userId','code','studentId','student','studentid']";
  1014. if (!log.param.ValueKind.Equals(JsonValueKind.Null)) {
  1015. JObject jsonObject = JObject.Parse(log.param.GetRawText());
  1016. //var nodes_path = jsonPathContext.SelectNodes(log.param, path);
  1017. var nodes_path = jsonObject.SelectTokens(path);
  1018. foreach (var node in nodes_path)
  1019. {
  1020. // 只获取是字符串的
  1021. if (node.Type.Equals(JTokenType.String))
  1022. {
  1023. switch (true)
  1024. {
  1025. case bool when node.Path.Contains("id_token")||node.Path.Contains("idToken")||node.Path.Contains("idtoken"):
  1026. {
  1027. try
  1028. {
  1029. var jwt = new JwtSecurityToken($"{node}");
  1030. string id = jwt.Payload.Sub;
  1031. var name = jwt.Claims.FirstOrDefault(claim => claim.Type.Equals("name"))?.Value;
  1032. if (!string.IsNullOrWhiteSpace(id) && long.TryParse(id, out long _id))
  1033. {
  1034. vist.userId=id;
  1035. vist.scope="teacher";
  1036. vist.tname=name;
  1037. useridMatch.Add(id);
  1038. }
  1039. }
  1040. catch (Exception ex) { }
  1041. break;
  1042. }
  1043. case bool when node.Path.Contains("tmdid")||node.Path.Contains("id")||node.Path.Contains("teacherId")
  1044. ||node.Path.Contains("teacher")||node.Path.Contains("tid")||node.Path.Contains("tId")||node.Path.Contains("userid")
  1045. ||node.Path.Contains("userId"):
  1046. {
  1047. if (!string.IsNullOrWhiteSpace($"{node}") && long.TryParse($"{node}", out long _id))
  1048. {
  1049. vist.userId=$"{node}";
  1050. vist.scope="teacher";
  1051. useridMatch.Add($"{node}");
  1052. }
  1053. break;
  1054. }
  1055. case bool when node.Path.Contains("studentId")||node.Path.Contains("student")||node.Path.Contains("studentid"):
  1056. {
  1057. if (!string.IsNullOrWhiteSpace($"{node}") && long.TryParse($"{node}", out long _id))
  1058. {
  1059. vist.userId=$"{node}";
  1060. vist.scope="student";
  1061. useridMatch.Add($"{node}");
  1062. }
  1063. break;
  1064. }
  1065. case bool when node.Path.Contains("code"):
  1066. {
  1067. if (!string.IsNullOrWhiteSpace($"{node}"))
  1068. {
  1069. if (long.TryParse($"{node}", out long _id))
  1070. {
  1071. vist.userId=$"{node}";
  1072. useridMatch.Add($"{node}");
  1073. }
  1074. else
  1075. {
  1076. string[] codes = $"{node}".Split("-");
  1077. foreach (var _code in codes)
  1078. {
  1079. if (long.TryParse(_code, out long _codeid))
  1080. {
  1081. vist.userId=$"{node}";
  1082. useridMatch.Add($"{_code}");
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. }
  1093. if (log.path.Contains("process-notify", StringComparison.OrdinalIgnoreCase))
  1094. {
  1095. string s = log.param.ToJsonString().Replace("\\", "");
  1096. Regex regextmdid = new Regex("\"tmdid\":\"(\\d+)\"");
  1097. Match matchtmdid = regextmdid.Match(s);
  1098. if (matchtmdid.Success)
  1099. {
  1100. var t = matchtmdid.Groups[1].Value;
  1101. if (!string.IsNullOrWhiteSpace(t))
  1102. {
  1103. vist.userId=t;
  1104. vist.scope="teacher";
  1105. useridMatch.Add(t);
  1106. }
  1107. }
  1108. Regex regexname = new Regex("\"tmdname\":\"(.+?)\"");
  1109. Match matchname = regexname.Match(s);
  1110. if (matchname.Success)
  1111. {
  1112. var t = matchname.Groups[1].Value;
  1113. if (!string.IsNullOrWhiteSpace(t))
  1114. {
  1115. vist.tname=t;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. else
  1122. {
  1123. if (string.IsNullOrWhiteSpace($"{vist.scope}"))
  1124. {
  1125. vist.scope="teacher";
  1126. uuidInfo.Add((uuid, log, new List<string>() { vist.userId }, new List<string>()));
  1127. }
  1128. }
  1129. if (string.IsNullOrWhiteSpace(vist.school) || vist.school.Equals("true") || vist.school.Equals("false"))
  1130. {
  1131. vist.school="";
  1132. string path = "$..['school','id','schoolId','schoolid','schoolCode','school_code','schoolcode','code']";
  1133. if (!log.param.ValueKind.Equals(JsonValueKind.Null)) {
  1134. JObject jsonObject = JObject.Parse(log.param.GetRawText());
  1135. var nodes_path = jsonObject.SelectTokens(path);
  1136. foreach (var node in nodes_path)
  1137. {
  1138. // 只获取是字符串的
  1139. if (node.Type.Equals(JTokenType.String))
  1140. {
  1141. switch (true)
  1142. {
  1143. case bool when node.Path.Contains("school")||node.Path.Contains("id")||node.Path.Contains("schoolId")
  1144. ||node.Path.Contains("schoolid")||node.Path.Contains("schoolCode")||node.Path.Contains("school_code")||node.Path.Contains("schoolcode"):
  1145. {
  1146. if (!$"{node}".Contains("-")&& $"{node}".Length<=8 && $"{node}".Length>=1)
  1147. {
  1148. vist.school=$"{node}";
  1149. schoolMatch.Add($"{node}");
  1150. }
  1151. break;
  1152. }
  1153. case bool when node.Path.Contains("code"):
  1154. {
  1155. if (!$"{node}".Contains("-")&& $"{node}".Length<=8&& $"{node}".Length>=1)
  1156. {
  1157. vist.school=$"{node}";
  1158. schoolMatch.Add($"{node}");
  1159. }
  1160. else
  1161. {
  1162. string[] codes = $"{node}".Split("-");
  1163. foreach (var _code in codes)
  1164. {
  1165. if ($"{_code}".Length<=8 && $"{_code}".Length>=1)
  1166. {
  1167. vist.school=$"{_code}";
  1168. schoolMatch.Add($"{_code}");
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. if (log.path.Contains("process-notify", StringComparison.OrdinalIgnoreCase))
  1179. {
  1180. string s = log.param.ToJsonString().Replace("\\", "");
  1181. Regex regexname = new Regex("\"schoolId\":\"(.+?)\"");
  1182. Match matchname = regexname.Match(s);
  1183. if (matchname.Success)
  1184. {
  1185. var t = matchname.Groups[1].Value;
  1186. if (!string.IsNullOrWhiteSpace(t))
  1187. {
  1188. vist.school=t;
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. //处理 client
  1195. {
  1196. if (!string.IsNullOrWhiteSpace(log.client))
  1197. {
  1198. if (log.client.Equals("IES", StringComparison.OrdinalIgnoreCase))
  1199. {
  1200. vist.client="ies5";
  1201. }
  1202. if (log.client.Equals("AClassONE", StringComparison.OrdinalIgnoreCase))
  1203. {
  1204. vist.client="aclassone";
  1205. }
  1206. if (log.client.Equals("BB", StringComparison.OrdinalIgnoreCase))
  1207. {
  1208. vist.client="habb";
  1209. }
  1210. if (log.client.Equals("HiTool", StringComparison.OrdinalIgnoreCase) ||log.client.Equals("HiTools", StringComparison.OrdinalIgnoreCase))
  1211. {
  1212. vist.client="hiscan";
  1213. }
  1214. if (log.client.Equals("HiTA", StringComparison.OrdinalIgnoreCase))
  1215. {
  1216. vist.client="hita";
  1217. vist.scope="teacher";
  1218. }
  1219. if (log.client.Equals("HiTeachCC", StringComparison.OrdinalIgnoreCase))
  1220. {
  1221. vist.client="hiteachcc";
  1222. }
  1223. if (log.client.Equals("HiTeach", StringComparison.OrdinalIgnoreCase))
  1224. {
  1225. vist.scope="teacher";
  1226. vist.client="hiteach";
  1227. }
  1228. if (log.client.Equals("Open", StringComparison.OrdinalIgnoreCase))
  1229. {
  1230. vist.client="open";
  1231. }
  1232. }
  1233. }
  1234. //补全站点
  1235. {
  1236. if (
  1237. (log.host.Equals("wwww.teammodel.net")||log.host.Equals("ies5-rc.teammodel.net")))
  1238. {
  1239. vist.hostName="国际站";
  1240. vist.host="www.teammodel.net";
  1241. vist.client="ies5";
  1242. vist.l="Global";
  1243. }
  1244. else if (
  1245. log.host.Equals("teammodelos-yx.chinacloudsites.cn")||log.host.Equals("teammodelos.chinacloudsites.cn")
  1246. ||log.host.Equals("yx.teammodel.cn")||log.host.Equals("teammodelos-rc.chinacloudsites.cn")||log.host.Equals("rc.teammodel.cn")|| log.host.Equals("www.teammodel.cn"))
  1247. {
  1248. vist.hostName="大陆站";
  1249. vist.host="www.teammodel.cn";
  1250. vist.client="ies5"; vist.l="China";
  1251. }
  1252. else if ((log.host.Contains("localhost") && log.p.Equals("os"))|| log.host.Equals("teammodelos-test.chinacloudsites.cn") ||log.host.Equals("test.teammodel.cn"))
  1253. {
  1254. vist.hostName="测试站";
  1255. vist.host="test.teammodel.cn";
  1256. vist.client="ies5";
  1257. vist.l="China";
  1258. }
  1259. else if (
  1260. (log.host.Equals("scyx.teammodel.cn") ||log.host.Equals("jinniu.teammodel.cn")))
  1261. {
  1262. vist.hostName="研修2.0";
  1263. vist.host="scyx.teammodel.cn";
  1264. vist.client="ability"; vist.l="China";
  1265. }
  1266. else if (
  1267. log.host.Equals("open.teammodel.cn")||log.host.Equals("open-test.teammodel.cn") ||log.host.Equals("zhiyin-test.teammodel.cn"))
  1268. {
  1269. vist.hostName="开放平台";
  1270. vist.host="open.teammodel.cn";
  1271. vist.client="open";
  1272. vist.l="China";
  1273. }
  1274. else if ((log.host.Equals("bi-rc.teammodel.net") || log.host.Equals("bi.teammodel.net")))
  1275. {
  1276. vist.hostName="国际站";
  1277. vist.host="bi.teammodel.net";
  1278. vist.client="bi";
  1279. vist.scope="admin";
  1280. vist.l="Global";
  1281. }
  1282. else if (log.host.Equals("teammodelbi.chinacloudsites.cn") ||log.host.Equals("bi.teammodel.cn"))
  1283. {
  1284. vist.hostName="大陆站";
  1285. vist.host="bi.teammodel.cn";
  1286. vist.client="bi";
  1287. vist.scope="admin";
  1288. vist.l="China";
  1289. }
  1290. else if (log.host.Equals("bitest.teammodel.cn")||log.host.Equals("teammodelbi-test.chinacloudsites.cn")||(log.host.Contains("localhost") && (log.p.Equals("bi"))))
  1291. {
  1292. vist.hostName="测试站";
  1293. vist.host="testbi.teammodel.cn";
  1294. vist.client="bi";
  1295. vist.scope="admin";
  1296. vist.l="China";
  1297. }
  1298. else if (
  1299. log.host.Equals("teamcontest.chinacloudsites.cn")||log.host.Equals("contest.chinacloudsites.cn")||log.host.Equals("contest-test.chinacloudsites.cn"))
  1300. {
  1301. vist.hostName="大陆站";
  1302. vist.host="contest.teammodel.cn";
  1303. vist.client="contest"; vist.l="China";
  1304. }
  1305. else if (
  1306. log.host.Equals("hiteachcc.chinacloudsites.cn"))
  1307. {
  1308. vist.hostName="正式站";
  1309. vist.host="hiteachcc.teammodel.cn";
  1310. vist.client="hiteachcc";
  1311. vist.l="China";
  1312. }
  1313. else if (log.host.Equals("appraisal.chinacloudsites.cn"))
  1314. {
  1315. vist.hostName="大陆站";
  1316. vist.host="appraisal.teammodel.cn";
  1317. vist.client="appraisal";
  1318. vist.l="China";
  1319. }
  1320. else if ((log.host.Contains("localhost") && log.p.Equals("appraisal"))||log.host.Equals("appraisal-test.teammodel.cn") ||log.host.Equals("appraisal-test.chinacloudsites.cn"))
  1321. {
  1322. vist.hostName="测试站";
  1323. vist.host="appraisal-test.teammodel.cn";
  1324. vist.client="appraisal";
  1325. vist.l="China";
  1326. }
  1327. else
  1328. {
  1329. if (!string.IsNullOrWhiteSpace(log.p))
  1330. {
  1331. vist.client=log.p.Equals("os", StringComparison.OrdinalIgnoreCase) ? "ies5" : log.p;
  1332. }
  1333. else
  1334. {
  1335. vist.client="other";
  1336. }
  1337. if (log.host.EndsWith(".cn"))
  1338. {
  1339. vist.hostName="大陆站";
  1340. vist.l="China";
  1341. }
  1342. else if (log.host.EndsWith(".net"))
  1343. {
  1344. vist.hostName="国际站";
  1345. vist.l="Global";
  1346. }
  1347. else
  1348. {
  1349. vist.hostName="其他";
  1350. vist.l="其他";
  1351. }
  1352. }
  1353. }
  1354. //补全产品端
  1355. {
  1356. //研修2.0
  1357. if (log.path.Contains("research") || log.path.Contains("study") || log.path.Contains("standard-file"))
  1358. {
  1359. vist.client="ability";
  1360. }
  1361. if (log.path.Contains("teacher"))
  1362. {
  1363. vist.scope="teacher";
  1364. }
  1365. if (log.path.Contains("tmduser"))
  1366. {
  1367. vist.scope=Constant.ScopeTmdUser;
  1368. }
  1369. if (log.path.Contains("student/login"))
  1370. {
  1371. vist.scope=Constant.ScopeStudent;
  1372. }
  1373. if (log.path.StartsWith("/activity"))
  1374. {
  1375. vist.client="contest";
  1376. }
  1377. //小程序
  1378. if (log.path.Contains("aclassone"))
  1379. {
  1380. vist.client="aclassone";
  1381. }
  1382. // /// <summary>
  1383. /// ExamInfo qamode 書面問答類型 0:書面問答 1:紙本測驗 2:艺术评测
  1384. /// </summary>
  1385. //艺术评测
  1386. if (log.path.Contains("art") ||log.path.Contains("aclassone/find-children-activity") ||log.path.Contains("aclassone/find-teacher-activity") ||log.path.Contains("aclassone/find-summary-activity") ||log.path.Contains("aclassone/upload-all") ||log.path.Contains("aclassone/delete"))
  1387. {
  1388. vist.client="art";
  1389. }
  1390. if (log.path.Contains("common/exam/upsert-record"))
  1391. {
  1392. JObject jobject = JObject.Parse(log.param.GetRawText());
  1393. var jtokens = jobject.SelectTokens("$..artId");
  1394. if (jtokens!=null && jtokens.Count()>0)
  1395. {
  1396. vist.client="art";
  1397. vist.scope=Constant.ScopeStudent;
  1398. }
  1399. }
  1400. if (log.path.Contains("habb"))
  1401. {
  1402. vist.client="habb";
  1403. }
  1404. //阅卷客户端
  1405. if (log.path.Contains("hiscan"))
  1406. {
  1407. vist.client="hiscan";
  1408. }
  1409. if (log.path.Contains("hita"))
  1410. {
  1411. vist.client="hita";
  1412. }
  1413. if (log.path.Contains("hiteachcc"))
  1414. {
  1415. vist.client="hiteachcc";
  1416. }
  1417. if (log.path.Contains("sokrate"))
  1418. {
  1419. vist.client="sokrate";
  1420. }
  1421. if (log.path.Contains("sokrate") || log.path.Contains("score"))
  1422. {
  1423. vist.client="sokrate";
  1424. }
  1425. if (log.path.Contains("hiteach"))
  1426. {
  1427. vist.client="hiteach";
  1428. }
  1429. ///IES开放平台
  1430. if (log.path.Contains("business") || log.path.Contains("biz") || log.path.Contains("openapi-config") || log.path.Contains("open-api"))
  1431. {
  1432. vist.client="open";
  1433. }
  1434. //从token的role 能否获取 开放平台
  1435. //单点登录及第三方接口
  1436. if (log.path.Contains("lepei") || log.path.Contains("sc/") || log.path.Contains("/sso") || log.path.Contains("sc-init")|| log.path.Contains("moofen") || log.path.Contains("data-push") || log.path.Contains("xkw")|| log.path.Contains("tianbo")
  1437. || log.path.Contains("oauth/check-bind")|| log.path.Contains("dingding") || log.path.Contains("wechat")
  1438. )
  1439. {
  1440. vist.client="sso-third";
  1441. }
  1442. }
  1443. //处理IP转地区
  1444. var region = await _ipSearcher.SearchIpAsync(vist.ip);
  1445. if (string.IsNullOrWhiteSpace(region))
  1446. {
  1447. region="未知IP·未知IP·未知IP";
  1448. }
  1449. if (!string.IsNullOrWhiteSpace(region))
  1450. {
  1451. region= region.Replace("省·", "·").Replace("市·", "·").Replace("特别行政区·", "·").Replace("藏族羌族自治州·", "·");
  1452. var regions = region.Split("·");
  1453. if (regions.Length==4)
  1454. {
  1455. vist.area= regions[0];
  1456. vist.province = regions[1];
  1457. vist.city = regions[2];
  1458. }
  1459. if (regions.Length==3)
  1460. {
  1461. vist.area= regions[0];
  1462. vist.province = regions[1];
  1463. }
  1464. if (regions.Length==2)
  1465. {
  1466. vist.area= regions[0];
  1467. vist.province = regions[1];
  1468. }
  1469. if (regions.Length==1)
  1470. {
  1471. vist.area= regions[0];
  1472. }
  1473. }
  1474. //处理地区转经纬度
  1475. {
  1476. IEnumerable<JToken> tokens = default;
  1477. if (!string.IsNullOrWhiteSpace(vist.city) && !string.IsNullOrWhiteSpace(vist.province) && !string.IsNullOrWhiteSpace(vist.area))
  1478. {
  1479. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i && @.city=~ /.*{vist.city}/i)]");
  1480. if (!(tokens.Any() && tokens.Count()>0))
  1481. {
  1482. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i && @.city=~ /.*{vist.province}/i)]");
  1483. if (!(tokens.Any() && tokens.Count()>0))
  1484. {
  1485. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i || @.city=~ /.*{vist.city}/i)]");
  1486. }
  1487. if (!(tokens.Any() && tokens.Count()>0))
  1488. {
  1489. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i||@.city=~ /.*{vist.province}/i)]");
  1490. }
  1491. }
  1492. }
  1493. else if (string.IsNullOrWhiteSpace(vist.city) && !string.IsNullOrWhiteSpace(vist.province) && !string.IsNullOrWhiteSpace(vist.area))
  1494. {
  1495. if (vist.area.Equals("中国"))
  1496. {
  1497. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.province}/i)]");
  1498. if (!(tokens.Any() && tokens.Count()>0))
  1499. {
  1500. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i)]");
  1501. }
  1502. }
  1503. else
  1504. {
  1505. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i || @.province=~ /.*{vist.province}/i)]");
  1506. }
  1507. }
  1508. else if (!string.IsNullOrWhiteSpace(vist.city) && string.IsNullOrWhiteSpace(vist.province)&& !string.IsNullOrWhiteSpace(vist.area))
  1509. {
  1510. if (vist.area.Equals("中国"))
  1511. {
  1512. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.province=~ /.*{vist.city}/i)]");
  1513. if (!(tokens.Any() && tokens.Count()>0))
  1514. {
  1515. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.province}/i)]");
  1516. }
  1517. }
  1518. else
  1519. {
  1520. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.city=~ /.*{vist.city}/i || @.city=~ /.*{vist.province}/i)]");
  1521. }
  1522. }
  1523. else if (string.IsNullOrWhiteSpace(vist.city) && string.IsNullOrWhiteSpace(vist.province)&& !string.IsNullOrWhiteSpace(vist.area))
  1524. {
  1525. tokens= _longitudeLatitudeTranslator.regionJson.SelectTokens($"$..[?(@.country=~ /.*{vist.area}/i && @.m=='1')]");
  1526. }
  1527. if (tokens!=null && tokens.Any())
  1528. {
  1529. List<RegionLngLat> regionLngLats = new List<RegionLngLat>();
  1530. foreach (JToken token in tokens)
  1531. {
  1532. regionLngLats.Add(token.ToString().ToObject<RegionLngLat>());
  1533. }
  1534. var points = regionLngLats.FindAll(x => string.IsNullOrWhiteSpace(x.area));
  1535. if (!points.IsNotEmpty())
  1536. {
  1537. points= regionLngLats.FindAll(x => !string.IsNullOrWhiteSpace(x.m) && x.m.Equals("1"));
  1538. }
  1539. if (string.IsNullOrWhiteSpace(vist.city))
  1540. {
  1541. vist.city=points?.FirstOrDefault()?.city;
  1542. if (!string.IsNullOrWhiteSpace(vist.city) && vist.city.EndsWith("市"))
  1543. {
  1544. vist.city=vist.city.Replace("市", "");
  1545. }
  1546. }
  1547. vist.lat=points?.FirstOrDefault()?.lat;
  1548. vist.lng=points?.FirstOrDefault()?.lng;
  1549. }
  1550. else
  1551. {
  1552. if (vist.area.Equals("内网IP"))
  1553. {
  1554. if (vist.host.Contains(".cn") || vist.host.Contains("localhost"))
  1555. {
  1556. vist.lat="30.655821878416408";
  1557. vist.lng="104.08153351042463";
  1558. vist.area="中国";
  1559. vist.province="四川";
  1560. vist.city="成都";
  1561. }
  1562. else
  1563. {
  1564. vist.lat="25.044332";
  1565. vist.lng="121.509062";
  1566. vist.area="中国";
  1567. vist.province="台湾";
  1568. vist.city="台湾";
  1569. }
  1570. }
  1571. else
  1572. {
  1573. vist.lat="30.655821878416408";
  1574. vist.lng="104.08153351042463";
  1575. vist.area="中国";
  1576. vist.province="四川";
  1577. vist.city="成都";
  1578. }
  1579. }
  1580. }
  1581. uuidInfo.Add((uuid, log, useridMatch, schoolMatch));
  1582. visits.Add(vist);
  1583. }
  1584. }
  1585. );
  1586. List<(string tmd, bool exists, string scope)> tmdexists = new List<(string tmd, bool exists, string scope)>();
  1587. List<(string sch, bool exists)> schexists = new List<(string sch, bool exists)>();
  1588. var tmds = uuidInfo.SelectMany(x => x.tmdid).ToHashSet();
  1589. if (tmds.Any())
  1590. {
  1591. foreach (var tmd in tmds)
  1592. {
  1593. var exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:Catalog:{tmd}");
  1594. if (exists)
  1595. {
  1596. tmdexists.Add((tmd, exists, "teacher"));
  1597. }
  1598. else
  1599. {
  1600. exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Login:School:hbcn:student:{tmd}");
  1601. tmdexists.Add((tmd, exists, "student"));
  1602. }
  1603. }
  1604. }
  1605. var schs = uuidInfo.SelectMany(x => x.school).ToHashSet();
  1606. if (schs.Any())
  1607. {
  1608. foreach (var sch in schs)
  1609. {
  1610. var exists = await _azureRedis.GetRedisClient(8).KeyExistsAsync($"Blob:Catalog:{sch}");
  1611. schexists.Add((sch, exists));
  1612. }
  1613. }
  1614. foreach (var item in uuidInfo)
  1615. {
  1616. foreach (var tmd in item.tmdid)
  1617. {
  1618. var tmdext = tmdexists.Find(x => x.tmd.Equals(tmd));
  1619. if (tmdext.tmd!= null && tmdext.exists)
  1620. {
  1621. var vist = visits.Where(x => x.id.Equals(item.uuid));
  1622. foreach (var vi in vist)
  1623. {
  1624. vi.userId=tmd;
  1625. if (string.IsNullOrWhiteSpace(vi.scope))
  1626. {
  1627. vi.scope=tmdext.scope;
  1628. }
  1629. }
  1630. break;
  1631. }
  1632. }
  1633. foreach (var sch in item.school)
  1634. {
  1635. var schext = schexists.Find(x => x.sch.Equals(sch));
  1636. if (schext.sch!= null && schext.exists)
  1637. {
  1638. var vist = visits.Where(x => x.id.Equals(item.uuid));
  1639. foreach (var vi in vist)
  1640. {
  1641. vi.school=sch;
  1642. }
  1643. break;
  1644. }
  1645. }
  1646. }
  1647. return (visits, uuidInfo);
  1648. }
  1649. public class ApiVisit
  1650. {
  1651. public string id { get; set; }
  1652. public string path { get; set; }
  1653. /// <summary>
  1654. /// 细分
  1655. /// ies5 ,hiteach,hita,cc,bi,contest,open,aclassone,sokrates,ability,art
  1656. /// 产品端
  1657. /// </summary>
  1658. public string client { get; set; }
  1659. /// <summary>
  1660. /// 具体功能
  1661. /// </summary>
  1662. // public string func { get; set; }
  1663. public string userId { get; set; }
  1664. public string scope { get; set; }
  1665. public string tname { get; set; }
  1666. public string school { get; set; }
  1667. public string ip { get; set; }
  1668. //public string region { get; set; }
  1669. public string area { get; set; }
  1670. public string province { get; set; }
  1671. public string city { get; set; } = "";
  1672. public long time { get; set; }
  1673. /// <summary>
  1674. ///
  1675. /// </summary>
  1676. public string host { get; set; }
  1677. public string hostName { get; set; } = "其他";
  1678. /// <summary>
  1679. /// tokenid
  1680. /// </summary>
  1681. public string tid { get; set; }
  1682. public string year { get; set; }
  1683. public string month { get; set; }
  1684. public string day { get; set; }
  1685. public string hour { get; set; }
  1686. //public RegionLngLat point { get; set; }
  1687. public string lat { get; set; }
  1688. public string lng { get; set; }
  1689. public string l { get; set; }
  1690. }
  1691. public class HttpLog
  1692. {
  1693. public string ip { get; set; }
  1694. public long time { get; set; }
  1695. public string host { get; set; }
  1696. public string hostName = "其他";
  1697. public string tid { get; set; }
  1698. public string path { get; set; }
  1699. public string client { get; set; }
  1700. public JsonElement param { get; set; }
  1701. public string id { get; set; }
  1702. public string name { get; set; }
  1703. public string school { get; set; }
  1704. public string p { get; set; }
  1705. // public string ua { get; set; }
  1706. // public string referer { get; set; }
  1707. public string scope { get; set; }
  1708. public string year { get; set; }
  1709. public string month { get; set; }
  1710. public string day { get; set; }
  1711. public string hour { get; set; }
  1712. public string l { get; set; }
  1713. }
  1714. public record RegionLngLat
  1715. {
  1716. public string country { get; set; }
  1717. public string province { get; set; }
  1718. public string city { get; set; }
  1719. public string lat { get; set; }
  1720. public string lng { get; set; }
  1721. public string area { get; set; }
  1722. public string m { get; set; } = "0";
  1723. }
  1724. public class CountData
  1725. {
  1726. public string pk { get; set; } = "";
  1727. public string sk { get; set; } = "";
  1728. public string sp { get; set; } = "";
  1729. public int count { get; set; }
  1730. }
  1731. public class SchoolStick
  1732. {
  1733. /// <summary>
  1734. /// 名字
  1735. /// </summary>
  1736. public string name { get; set; }
  1737. public string picture { get; set; }
  1738. /// <summary>
  1739. /// 编码
  1740. /// </summary>
  1741. public string id { get; set; }
  1742. public string last { get; set; }
  1743. #region tmd
  1744. public ClientStick tmd { get; set; } = new ClientStick();
  1745. #endregion
  1746. #region hiteach
  1747. public ClientStick hiteach { get; set; } = new ClientStick();
  1748. public Indicator lesson { get; set; } = new Indicator();
  1749. #endregion
  1750. #region hita
  1751. public ClientStick hita { get; set; } = new ClientStick();
  1752. #endregion
  1753. #region ies5Teacher
  1754. public ClientStick ies5Tch { get; set; } = new ClientStick();
  1755. #endregion
  1756. #region otherTeacher
  1757. public ClientStick otherTch { get; set; } = new ClientStick();
  1758. #endregion
  1759. #region ies5Student
  1760. public ClientStick ies5Stu { get; set; } = new ClientStick();
  1761. #endregion
  1762. #region otherStudent
  1763. public ClientStick otherStu { get; set; } = new ClientStick();
  1764. #endregion
  1765. public List<TchStick> tchSticks { get; set; } = new List<TchStick>();
  1766. public List<StuStick> stuSticks { get; set; } = new List<StuStick>();
  1767. }
  1768. public class TchStick {
  1769. public string school { get; set; }
  1770. /// <summary>
  1771. /// 名字
  1772. /// </summary>
  1773. public string name { get; set; }
  1774. public string picture { get; set; }
  1775. /// <summary>
  1776. /// 编码
  1777. /// </summary>
  1778. public string id { get; set; }
  1779. public string last { get; set; }
  1780. #region tmd
  1781. public ClientStick tmd { get; set; } = new ClientStick();
  1782. #endregion
  1783. #region hiteach
  1784. public ClientStick hiteach { get; set; } = new ClientStick();
  1785. public Indicator lesson { get; set; } = new Indicator();
  1786. #endregion
  1787. #region hita
  1788. public ClientStick hita { get; set; } = new ClientStick();
  1789. #endregion
  1790. #region ies5Teacher
  1791. public ClientStick ies5Tch { get; set; } = new ClientStick();
  1792. #endregion
  1793. #region otherTeacher
  1794. public ClientStick otherTch { get; set; } = new ClientStick();
  1795. #endregion
  1796. }
  1797. public class StuStick
  1798. {
  1799. public string school { get; set; }
  1800. /// <summary>
  1801. /// 名字
  1802. /// </summary>
  1803. public string name { get; set; }
  1804. public string picture { get; set; }
  1805. /// <summary>
  1806. /// 编码
  1807. /// </summary>
  1808. public string id { get; set; }
  1809. public string last { get; set; }
  1810. #region tmd
  1811. public ClientStick tmd { get; set; } = new ClientStick();
  1812. #endregion
  1813. #region ies5Student
  1814. public ClientStick ies5Stu { get; set; } = new ClientStick();
  1815. #endregion
  1816. #region otherStudent
  1817. public ClientStick otherStu { get; set; } = new ClientStick();
  1818. #endregion
  1819. }
  1820. public class ClientStick
  1821. {
  1822. public int userCount { get; set; }
  1823. public Indicator stick { get; set; } = new Indicator();
  1824. public Indicator duration { get; set; } = new Indicator();
  1825. public Indicator count { get; set; } = new Indicator();
  1826. }
  1827. public class Indicator
  1828. {
  1829. /// <summary>
  1830. /// 值
  1831. /// </summary>
  1832. public double value { get; set; } = 0;
  1833. /// <summary>
  1834. /// 值的增减浮动
  1835. /// </summary>
  1836. public double range { get; set; } = 0;
  1837. /// <summary>
  1838. /// 排名
  1839. /// </summary>
  1840. public double rank { get; set; } = 0;
  1841. /// <summary>
  1842. /// 排名的上升下降
  1843. /// </summary>
  1844. public double updown { get; set; } = 0;
  1845. }
  1846. }
  1847. }