|
@@ -12,8 +12,10 @@ using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
using TEAMModelOS.SDK.DI;
|
|
|
using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
|
|
|
namespace TEAMModelOS.SDK.Services
|
|
|
{
|
|
@@ -40,84 +42,159 @@ namespace TEAMModelOS.SDK.Services
|
|
|
//如果已经存在,则忽略,不加入队列。
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- public static async Task<UsedBlob> GetBlobUsed(CosmosClient clientc, BlobContainerClient clients, IDatabase clientr, string scope, string containerName)
|
|
|
+ /// <summary>
|
|
|
+ /// 获取学校或者个人的剩余空间
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="name">学校id 或者醍摩豆id</param>
|
|
|
+ /// <param name="scope"></param>
|
|
|
+ /// <param name="_azureCosmos"></param>
|
|
|
+ /// <param name="_azureRedis"></param>
|
|
|
+ public static async Task<(long usedSize, long teach, long total, long surplus, Dictionary<string, double?> catalog)> GetSurplusSpace(string name, string scope,Option _option, AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage,DingDing _dingDing )
|
|
|
{
|
|
|
- UsedBlob result = new UsedBlob();
|
|
|
+ //已经存储的空间
|
|
|
+ long usedSize = 0;
|
|
|
+ //分配给教师的
|
|
|
+ long teach = 0;
|
|
|
+ //总空间
|
|
|
+ long total = 0;
|
|
|
+ // 剩余的
|
|
|
+ long surplus = 0;
|
|
|
+ Dictionary<string, double?> catalog = new Dictionary<string, double?>();
|
|
|
try
|
|
|
{
|
|
|
- //学校已经分配给所有教师的空间大小GB。
|
|
|
- long teach = 0;
|
|
|
- if (scope.Equals("school"))
|
|
|
+ if (!string.IsNullOrWhiteSpace(name))
|
|
|
{
|
|
|
- await foreach (var item in clientc.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
|
|
|
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{containerName}") }))
|
|
|
+
|
|
|
+ if ("school".Equals(scope, StringComparison.OrdinalIgnoreCase))
|
|
|
+ {
|
|
|
+
|
|
|
+ School school = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(name, new PartitionKey("Base"));
|
|
|
+ total = school.size;
|
|
|
+ teach = school.tsize;
|
|
|
+ //用tsize代替
|
|
|
+ //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
|
|
|
+ // requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{name}") }))
|
|
|
+ //{
|
|
|
+ // var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ // foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ // {
|
|
|
+ // if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
|
|
|
+ // {
|
|
|
+ // teach = _size.GetInt32();
|
|
|
+ // break;
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
- foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ Teacher teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>(name, new PartitionKey("Base"));
|
|
|
+ total = teacher.size;
|
|
|
+ foreach (var school in teacher.schools)
|
|
|
{
|
|
|
- if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
|
|
|
+ try
|
|
|
{
|
|
|
- teach = _size.GetInt32();
|
|
|
- break;
|
|
|
+ SchoolTeacher schoolTeacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<SchoolTeacher>(name, new PartitionKey($"Teacher-{school.schoolId}"));
|
|
|
+ total += schoolTeacher.size;
|
|
|
}
|
|
|
+ catch (Exception ex) { }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- //使用blob狀況
|
|
|
- bool getBlobCatalogFromBlob = false; //是否直接去blob取得使用狀況 false:否(Redis) true:是(Blob)
|
|
|
- long blobsize = 0;
|
|
|
- RedisValue value = default;
|
|
|
- value = clientr.HashGet($"Blob:Record", containerName);
|
|
|
- if (value != default && !value.IsNullOrEmpty)
|
|
|
- {
|
|
|
- JsonElement record = value.ToString().ToObject<JsonElement>();
|
|
|
- if (record.TryGetInt64(out blobsize))
|
|
|
+ long blobsize = 0;
|
|
|
+ RedisValue value = default;
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", name);
|
|
|
+ if (value != default && !value.IsNullOrEmpty)
|
|
|
{
|
|
|
+ JsonElement record = value.ToString().ToObject<JsonElement>();
|
|
|
+ if (record.TryGetInt64(out blobsize))
|
|
|
+ {
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- getBlobCatalogFromBlob = true;
|
|
|
+ var client = _azureStorage.GetBlobContainerClient(name);
|
|
|
+ var size = await client.GetBlobsCatalogSize();
|
|
|
+ if (size.Item1 > 0)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (var key in size.Item2.Keys)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
|
|
|
+ }
|
|
|
+ usedSize = size.Item1.Value;
|
|
|
+ //1024 * 1024 * 1024=1073741824 =1G
|
|
|
+ surplus = (total - teach) * 1073741824 - usedSize;
|
|
|
+ catalog=size.Item2;
|
|
|
+ //return (usedSize, teach, total, surplus, catalog = size.Item2);
|
|
|
}
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- getBlobCatalogFromBlob = true;
|
|
|
- }
|
|
|
- Dictionary<string, double?> catalog = new Dictionary<string, double?>();
|
|
|
- SortedSetEntry[] Scores = clientr.SortedSetRangeByScoreWithScores($"Blob:Catalog:{containerName}");
|
|
|
- if (Scores != null)
|
|
|
- {
|
|
|
- foreach (var score in Scores)
|
|
|
+
|
|
|
+ SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{name}");
|
|
|
+ if (Scores != null)
|
|
|
+ {
|
|
|
+ foreach (var score in Scores)
|
|
|
+ {
|
|
|
+ double val = score.Score;
|
|
|
+ string key = score.Element.ToString();
|
|
|
+ catalog.Add(key, val);
|
|
|
+ }
|
|
|
+ usedSize = blobsize;
|
|
|
+ //1024 * 1024 * 1024=1073741824 =1G
|
|
|
+ surplus = (total - teach) * 1073741824 - usedSize;
|
|
|
+ //return (usedSize, teach, total, surplus, catalog);
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- double val = score.Score;
|
|
|
- string key = score.Element.ToString();
|
|
|
- catalog.Add(key, val);
|
|
|
+ //没有缓存
|
|
|
+ var client = _azureStorage.GetBlobContainerClient(name);
|
|
|
+ var size = await client.GetBlobsCatalogSize();
|
|
|
+ if (size.Item1 > 0)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", name, size.Item1);
|
|
|
+ }
|
|
|
+ foreach (var key in size.Item2.Keys)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{name}", key);
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{name}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
|
|
|
+ }
|
|
|
+ usedSize = size.Item1.Value;
|
|
|
+ //1024 * 1024 * 1024=1073741824 =1G
|
|
|
+ //surplus 可能为负数
|
|
|
+ surplus = (total - teach) * 1073741824 - usedSize;
|
|
|
+ catalog = size.Item2;
|
|
|
+ //return (usedSize, teach, total, surplus, catalog = size.Item2);
|
|
|
}
|
|
|
}
|
|
|
- if (!getBlobCatalogFromBlob)
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"IES5,{_option.Location},blob/used-space()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return (usedSize, teach, total, surplus, catalog);
|
|
|
+ }
|
|
|
+ if (usedSize > 0 && total > 0) {
|
|
|
+ double percent= surplus / total * 1073741824 *100;
|
|
|
+ int tag =11;
|
|
|
+ if (percent <= 10 && percent > 5)
|
|
|
{
|
|
|
- result.size = blobsize;
|
|
|
- result.catalog = catalog;
|
|
|
- result.teach = teach;
|
|
|
- return result;
|
|
|
+ tag = 10;
|
|
|
}
|
|
|
- else
|
|
|
+ else if (percent <= 5 && percent > 0)
|
|
|
{
|
|
|
- var size = await clients.GetBlobsCatalogSize();
|
|
|
- result.size = size.Item1;
|
|
|
- result.catalog = size.Item2;
|
|
|
- result.teach = teach;
|
|
|
- return result;
|
|
|
+ tag = 5;
|
|
|
}
|
|
|
+ else if (percent <= 0)
|
|
|
+ {
|
|
|
+ tag = 0;
|
|
|
+ }//如果已经扩容请忽略此通知!
|
|
|
+ //if()
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
- {
|
|
|
- //await _dingDing.SendBotMsg($"CoreAPI2,{_option.Location},Channel/Create()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
- return result;
|
|
|
- }
|
|
|
+ return (usedSize, teach, total, surplus, catalog);
|
|
|
}
|
|
|
|
|
|
+ public class BlobSpace{ }
|
|
|
+
|
|
|
/// <param name="scope">school or private</param>
|
|
|
/// <param name="containerName">school code or tmid</param>
|
|
|
/// <param name="type">doc, res, item... Empty if no need.</param>
|