TestController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Newtonsoft.Json;
  5. using StackExchange.Redis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.SDK.DI;
  14. using TEAMModelOS.SDK.Extension;
  15. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  16. using TEAMModelOS.SDK.Models;
  17. using TEAMModelOS.SDK.Models.Cosmos.Common;
  18. namespace TEAMModelOS.Controllers.XTest
  19. {
  20. [Route("test")]
  21. [ApiController]
  22. public class TestController :ControllerBase
  23. {
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly AzureRedisFactory _azureRedis;
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly DingDing _dingDing;
  28. public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, DingDing dingDing) {
  29. _azureCosmos = azureCosmos;
  30. _azureRedis = azureRedis;
  31. _azureStorage = azureStorage;
  32. _dingDing = dingDing;
  33. }
  34. /// <summary>
  35. /// 测试blob多线程写入同一个文件
  36. /// </summary>
  37. /// <returns></returns>
  38. [ProducesDefaultResponseType]
  39. [HttpPost("blobroot")]
  40. public async Task<IActionResult> MultipleBlob(JsonElement jsonMsg) {
  41. if (jsonMsg.TryGetProperty("name", out JsonElement name) && name.ValueKind == JsonValueKind.String
  42. && jsonMsg.TryGetProperty("root", out JsonElement root) && root.ValueKind == JsonValueKind.String)
  43. {
  44. List<Dictionary<string, double?>> list = new List<Dictionary<string, double?>>();
  45. string u = System.Web.HttpUtility.UrlDecode($"{root}", Encoding.UTF8).Split("/")[0];
  46. var client = _azureStorage.GetBlobContainerClient($"{name}");
  47. var size = await client.GetBlobsSize(u);
  48. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", u);
  49. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", u, size.HasValue ? size.Value : 0);
  50. var scores = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{name}");
  51. double blobsize = 0;
  52. if (scores != default && scores != null)
  53. {
  54. foreach (var score in scores)
  55. {
  56. blobsize = blobsize + score.Score;
  57. list.Add(new Dictionary<string, double?>() { { score.Element.ToString(), score.Score } });
  58. }
  59. }
  60. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{blobsize}"));
  61. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Blob() 容器:{name}使用:{root},文件分类:{list.ToJsonString()}",
  62. GroupNames.成都开发測試群組);
  63. return Ok(list);
  64. }
  65. else {
  66. return Ok();
  67. }
  68. }
  69. /// <summary>
  70. /// 测试redis通配符
  71. /// </summary>
  72. /// <param name="request"></param>
  73. /// <returns></returns>
  74. [ProducesDefaultResponseType]
  75. [HttpGet("test-redis")]
  76. public async Task<IActionResult> TestRedis( ) {
  77. var client = _azureCosmos.GetCosmosClient();
  78. int count = 0;
  79. string sql = $" SELECT value(count(serial)) FROM c join serial in c.serial where c.id ='hbcn' and c.pk='Product' and serial.prodCode='3222C6D2' ";
  80. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<int>(sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Product") }))
  81. {
  82. count = item;
  83. break;
  84. }
  85. return Ok(count) ;
  86. }
  87. /// <summary>
  88. /// 删除
  89. /// </summary>
  90. /// <param name="request"></param>
  91. /// <returns></returns>
  92. [ProducesDefaultResponseType]
  93. //[AuthToken(Roles = "teacher")]
  94. [HttpPost("delete")]
  95. public async Task<IActionResult> Delete(JsonElement request)
  96. {
  97. try
  98. {
  99. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  100. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  101. var client = _azureCosmos.GetCosmosClient();
  102. List<Task> tasksFiles = new List<Task>();
  103. var query = $"select c.id from c ";
  104. if (scope.ToString().Equals("school"))
  105. {
  106. var ids = client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
  107. await foreach (var id in ids)
  108. {
  109. using var json = await JsonDocument.ParseAsync(id.ContentStream);
  110. var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
  111. //批量删除
  112. foreach (var obj in jsonList)
  113. {
  114. tasksFiles.Add(client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
  115. }
  116. }
  117. }
  118. else if (scope.ToString().Equals("teacher")) {
  119. var ids = client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
  120. await foreach (var id in ids)
  121. {
  122. using var json = await JsonDocument.ParseAsync(id.ContentStream);
  123. var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
  124. //批量删除
  125. foreach (var obj in jsonList)
  126. {
  127. //tasksFiles.Add(client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
  128. }
  129. }
  130. }
  131. await Task.WhenAll(tasksFiles);
  132. return Ok(new { code = 1 });
  133. }
  134. catch (Exception e)
  135. {
  136. return BadRequest(e.StackTrace);
  137. }
  138. }
  139. }
  140. }