BlobService.cs 6.5 KB

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