BlobService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using Azure;
  2. using Azure.Cosmos;
  3. using Azure.Storage.Blobs;
  4. using HTEXLib.COMM.Helpers;
  5. using StackExchange.Redis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. namespace TEAMModelOS.SDK.Services
  15. {
  16. public static class BlobService
  17. {
  18. public static async Task<UsedBlob> GetBlobUsed(CosmosClient clientc, BlobContainerClient clients, IDatabase clientr, string scope, string containerName)
  19. {
  20. UsedBlob result = new UsedBlob();
  21. try
  22. {
  23. //学校已经分配给所有教师的空间大小GB。
  24. long teach = 0;
  25. if (scope.Equals("school"))
  26. {
  27. await foreach (var item in clientc.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  28. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{containerName}") }))
  29. {
  30. var json = await JsonDocument.ParseAsync(item.ContentStream);
  31. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  32. {
  33. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  34. {
  35. teach = _size.GetInt32();
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. //使用blob狀況
  42. bool getBlobCatalogFromBlob = false; //是否直接去blob取得使用狀況 false:否(Redis) true:是(Blob)
  43. long blobsize = 0;
  44. RedisValue value = default;
  45. value = clientr.HashGet($"Blob:Record", containerName);
  46. if (value != default && !value.IsNullOrEmpty)
  47. {
  48. JsonElement record = value.ToString().ToObject<JsonElement>();
  49. if (record.TryGetInt64(out blobsize))
  50. {
  51. }
  52. else
  53. {
  54. getBlobCatalogFromBlob = true;
  55. }
  56. }
  57. else
  58. {
  59. getBlobCatalogFromBlob = true;
  60. }
  61. Dictionary<string, double?> catalog = new Dictionary<string, double?>();
  62. SortedSetEntry[] Scores = clientr.SortedSetRangeByScoreWithScores($"Blob:Catalog:{containerName}");
  63. if (Scores != null)
  64. {
  65. foreach (var score in Scores)
  66. {
  67. double val = score.Score;
  68. string key = score.Element.ToString();
  69. catalog.Add(key, val);
  70. }
  71. }
  72. if (!getBlobCatalogFromBlob)
  73. {
  74. result.size = blobsize;
  75. result.catalog = catalog;
  76. result.teach = teach;
  77. return result;
  78. }
  79. else
  80. {
  81. var size = await clients.GetBlobsCatalogSize();
  82. result.size = size.Item1;
  83. result.catalog = size.Item2;
  84. result.teach = teach;
  85. return result;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. //await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},Channel/Create()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  91. return result;
  92. }
  93. }
  94. /// <param name="scope">school or private</param>
  95. /// <param name="containerName">school code or tmid</param>
  96. /// <param name="type">doc, res, item... Empty if no need.</param>
  97. /// <param name="periodId">Only for school</param>
  98. public static async Task<List<BlobCount>> BloblogCount(CosmosClient clientc, string scope, string containerName, string type, string periodId)
  99. {
  100. List<BlobCount> bloblogcnt = new List<BlobCount>();
  101. try
  102. {
  103. //必須項檢查
  104. if(scope.Equals("school") && string.IsNullOrWhiteSpace(periodId))
  105. {
  106. return bloblogcnt;
  107. }
  108. //資料取得
  109. var queryslt = new StringBuilder();
  110. queryslt.Append($"SELECT COUNT(1) AS count, c.type FROM c ");
  111. if (scope.Equals("school"))
  112. {
  113. queryslt.Append($"JOIN A1 IN c.periodId WHERE A1 IN ('{periodId}') ");
  114. if(!string.IsNullOrWhiteSpace(type)) queryslt.Append($"AND c.type='{type}'");
  115. queryslt.Append("GROUP BY c.type");
  116. await foreach (var item in clientc.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<BlobCount>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{containerName}") }))
  117. {
  118. bloblogcnt.Add(item);
  119. }
  120. }
  121. else if (scope.Equals("private"))
  122. {
  123. if (!string.IsNullOrWhiteSpace(type)) queryslt.Append($"AND c.type='{type}'");
  124. queryslt.Append("GROUP BY c.type");
  125. await foreach (var item in clientc.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<BlobCount>(queryText: queryslt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{containerName}") }))
  126. {
  127. bloblogcnt.Add(item);
  128. }
  129. }
  130. return bloblogcnt;
  131. }
  132. catch (Exception ex)
  133. {
  134. return bloblogcnt;
  135. }
  136. }
  137. public class UsedBlob
  138. {
  139. public long teach { get; set; }
  140. public long? size { get; set; }
  141. public Dictionary<string, double?> catalog { get; set; }
  142. }
  143. public class BlobCount
  144. {
  145. public string type { get; set; }
  146. public int count { get; set; }
  147. }
  148. }
  149. }