Program.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Azure;
  2. using Bogus;
  3. using HTEX.Lib.ETL.Lesson;
  4. using HTEX.Test.Service;
  5. using MathNet.Numerics;
  6. using MathNet.Numerics.LinearAlgebra;
  7. using System;
  8. using System.Configuration;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text.RegularExpressions;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Helper.Common.FileHelper;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelOS.SDK.Models.Cosmos.Student;
  20. using static HTEX.Test.Controllers.LessonRecordController;
  21. using static Microsoft.Azure.Amqp.Serialization.SerializableType;
  22. using static System.Runtime.InteropServices.JavaScript.JSType;
  23. namespace HTEX.Test
  24. {
  25. public class Program
  26. {
  27. public static void Main(string[] args)
  28. {
  29. Test2();
  30. var builder = WebApplication.CreateBuilder(args);
  31. // Add services to the container.
  32. builder.Services.AddControllers();
  33. builder.Services.AddAzureStorage(builder.Configuration.GetValue<string>("Azure:Storage:ConnectionString"));
  34. builder.Services.AddAzureRedis(builder.Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
  35. builder.Services.AddAzureCosmos(builder.Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
  36. builder.Services.AddAzureServiceBus(builder.Configuration.GetValue<string>("Azure:ServiceBus:ConnectionString"));
  37. builder.Services.AddAzureSignalR(builder.Configuration.GetValue<string>("Azure:SignalR:ConnectionString"));
  38. builder.Services.AddSnowflakeId(Convert.ToInt64(builder.Configuration.GetValue<string>("Option:LocationNum")), 1);
  39. builder.Services.AddHttpClient();
  40. var app = builder.Build();
  41. // Configure the HTTP request pipeline.
  42. app.UseHttpsRedirection();
  43. app.UseAuthorization();
  44. app.MapControllers();
  45. app.Run();
  46. }
  47. public static string Test2()
  48. {
  49. string? pathAnalysis = $"F:\\lesson-local\\analysis";
  50. var filesAnalysis = FileHelper.ListAllFiles(pathAnalysis);
  51. List<LessonDataAnalysisMonth> lessonDataAnalysisMonths = new List<LessonDataAnalysisMonth>();
  52. LessonDataAnalysisCluster lessonDataAnalysisCluster = new LessonDataAnalysisCluster();
  53. foreach (var file in filesAnalysis)
  54. {
  55. //读取每月的数据
  56. if (file.EndsWith("-m-analysis.json"))
  57. {
  58. string jsons = System.IO.File.ReadAllText(file);
  59. LessonDataAnalysisMonth lessonDataAnalysisMonth = jsons.ToObject<LessonDataAnalysisMonth>();
  60. lessonDataAnalysisMonths.Add(lessonDataAnalysisMonth);
  61. if (lessonDataAnalysisMonth.task.IsNotEmpty())
  62. {
  63. lessonDataAnalysisCluster.task.AddRange(lessonDataAnalysisMonth.task);
  64. }
  65. if (lessonDataAnalysisMonth.irs.IsNotEmpty())
  66. {
  67. lessonDataAnalysisCluster.irs.AddRange(lessonDataAnalysisMonth.irs);
  68. }
  69. if (lessonDataAnalysisMonth.interactNormal.IsNotEmpty())
  70. {
  71. lessonDataAnalysisCluster.interactNormal.AddRange(lessonDataAnalysisMonth.interactNormal);
  72. }
  73. if (lessonDataAnalysisMonth.pscore.IsNotEmpty())
  74. {
  75. lessonDataAnalysisCluster.pscore.AddRange(lessonDataAnalysisMonth.pscore);
  76. }
  77. if (lessonDataAnalysisMonth.gscore.IsNotEmpty())
  78. {
  79. lessonDataAnalysisCluster.gscore.AddRange(lessonDataAnalysisMonth.gscore);
  80. }
  81. if (lessonDataAnalysisMonth.tscore.IsNotEmpty())
  82. {
  83. lessonDataAnalysisCluster.tscore.AddRange(lessonDataAnalysisMonth.tscore);
  84. }
  85. if (lessonDataAnalysisMonth.stuCowork.IsNotEmpty())
  86. {
  87. lessonDataAnalysisCluster.stuCowork.AddRange(lessonDataAnalysisMonth.stuCowork);
  88. }
  89. if (lessonDataAnalysisMonth.groupCowork.IsNotEmpty())
  90. {
  91. lessonDataAnalysisCluster.groupCowork.AddRange(lessonDataAnalysisMonth.groupCowork);
  92. }
  93. }
  94. }
  95. //标准差偏差N倍,视为异常数据
  96. double thresholdMultiplier =2;
  97. lessonDataAnalysisCluster.pscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.pscore.OrderBy(x => x), thresholdMultiplier);
  98. lessonDataAnalysisCluster.gscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.gscore.OrderBy(x => x), thresholdMultiplier);
  99. lessonDataAnalysisCluster.tscore= LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.tscore.OrderBy(x => x), thresholdMultiplier);
  100. lessonDataAnalysisCluster.irs = LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.irs.OrderBy(x => x), thresholdMultiplier);
  101. lessonDataAnalysisCluster.interactNormal=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.interactNormal.OrderBy(x => x), thresholdMultiplier);
  102. lessonDataAnalysisCluster.stuCowork=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.stuCowork.OrderBy(x => x), thresholdMultiplier);
  103. lessonDataAnalysisCluster.groupCowork=LessonETLService.CleanDataBySDThreshold(lessonDataAnalysisCluster.groupCowork.OrderBy(x => x), thresholdMultiplier);
  104. //超过60 80的
  105. var d = lessonDataAnalysisCluster.interactNormal.Select(x => x).OrderBy(x => x).ToArray();
  106. double n = d.Max()+1;
  107. var clusterInteract = HTEX.Lib.ETL. KMeansService.KMeansOptimized(d,3);
  108. foreach (var item in clusterInteract.OrderBy(x => x.Key))
  109. {
  110. lessonDataAnalysisCluster.clustersInteract.Add(new KeyValuePair<double, List<double>>(item.Value.Average(), item.Value));
  111. Console.WriteLine($"dp:{item.Key} ,avg: {item.Value.Average()}, count: {item.Value.Count}, min:{item.Value.Min()}, max:{item.Value.Max()},weight:{item.Value.Count*1.0/d.Count()}");
  112. }
  113. IEnumerable<double> all = lessonDataAnalysisCluster.clustersInteract.SelectMany(x => x.Value);
  114. int pass = 0;
  115. for (var i = 1; i<n; i++)
  116. {
  117. var p = LessonETLService.GetPersent(all, i);
  118. if (p.persent>=60)
  119. {
  120. pass = i;
  121. break;
  122. }
  123. }
  124. int good = 0;
  125. for (var i = 1; i<n; i++)
  126. {
  127. var p = LessonETLService.GetPersent(all, i);
  128. if (p.persent>=80)
  129. {
  130. good = i;
  131. break;
  132. }
  133. }
  134. double ss = 0;
  135. for (var i = 1; i<n; i++)
  136. {
  137. // KeyValuePair<double, List<int>> curr = new KeyValuePair<double, List<int>>();
  138. var s = lessonDataAnalysisCluster.clustersInteract.FindAll(x => x.Value.Min()<=i && x.Value.Max()>=i).MinBy(x=>x.Key);
  139. var p = LessonETLService.GetPersent(all, i);
  140. var l= i<pass?pass:i<good?pass:good;
  141. var e = (i*1.0/l) *(p.persent) * (s.Value.Count*1.0/d.Count());
  142. ss+= e;
  143. Console.WriteLine($"n: {i},l: {l}, persent: {p.persent},count: {p.count},s-min: {s.Value.Min()},s-max: {s.Value.Max()},value: {e}");
  144. }
  145. //foreach (var s in clusterInteract.OrderBy(x => x.Key))
  146. //{
  147. // clustersDataInteract.Add(s);
  148. //}
  149. //lessonDataAnalysisCluster.clustersInteract= clustersDataInteract;
  150. System.IO.File.WriteAllText(Path.Combine(pathAnalysis, "analysis.json"), lessonDataAnalysisCluster.ToJsonString());
  151. return "";
  152. }
  153. public static string Test()
  154. {
  155. // 创建一个随机数生成器
  156. Random random = new Random();
  157. // 示例数据集
  158. var data = Enumerable.Range(1, 25000).Select(i => Vector<double>.Build.Dense(i % 10, i % 10)).ToArray();
  159. return "Hello World!";
  160. }
  161. static bool[] MarkAnomalies(List<int> array)
  162. {
  163. if (array.Count == 0) return new bool[0];
  164. double average = array.Average();
  165. double variance = array.Average(x => Math.Pow(x - average, 2));
  166. double standardDeviation = Math.Sqrt(variance);
  167. // 定义异常值的阈值,这里使用2倍标准差
  168. double threshold =3* standardDeviation;
  169. bool[] anomalies = new bool[array.Count];
  170. for (int i = 0; i < array.Count; i++)
  171. {
  172. double deviation = Math.Abs(array[i] - average);
  173. anomalies[i] = deviation > threshold;
  174. }
  175. return anomalies;
  176. }
  177. /// <summary>
  178. /// 计算当前元素在集合中超过了多少百分比的值
  179. /// </summary>
  180. /// <param name="nums"></param>
  181. /// <param name="curr"></param>
  182. /// <returns></returns>
  183. public static double GetPersent(IEnumerable<double> nums, int curr)
  184. {
  185. int count = 0;
  186. foreach (var op in nums.OrderBy(x => x))
  187. {
  188. if (op < curr)
  189. {
  190. count++;
  191. }
  192. else if (op == curr)
  193. {
  194. count++;
  195. }
  196. else
  197. {
  198. break;
  199. }
  200. }
  201. return count *1.0/ nums.Count() * 100;
  202. }
  203. public class KeyCount
  204. {
  205. public int count { get; set;}
  206. public int key { get; set;}
  207. }
  208. class WeightedItem
  209. {
  210. public int Value { get; set; }
  211. public double Weight { get; set; }
  212. }
  213. }
  214. }