using System; using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; using Azure.Cosmos; using Azure.Messaging.ServiceBus; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; using StackExchange.Redis; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Helper.Common.CollectionHelper; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Models.Cosmos.Common; namespace TEAMModelFunction { public class MonitorServicesBus { private readonly AzureCosmosFactory _azureCosmos; private readonly DingDing _dingDing; private readonly AzureStorageFactory _azureStorage; private readonly AzureRedisFactory _azureRedis; private readonly AzureServiceBusFactory _serviceBus; public MonitorServicesBus(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus) { _azureCosmos = azureCosmos; _dingDing = dingDing; _azureStorage = azureStorage; _azureRedis = azureRedis; _serviceBus = serviceBus; } [FunctionName("Exam")] public async Task Exam([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "exam", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { try { var json = JsonDocument.Parse(msg); json.RootElement.TryGetProperty("id", out JsonElement id); json.RootElement.TryGetProperty("progress", out JsonElement progress); json.RootElement.TryGetProperty("code", out JsonElement code); //Dictionary keyValuePairs = mySbMsg.ToObject>(); var client = _azureCosmos.GetCosmosClient(); ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(id.ToString(), new PartitionKey($"{code}")); exam.progress = progress.ToString(); await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}")); } catch (Exception ex) { await _dingDing.SendBotMsg($"ServiceBus,ExamBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組); } } [FunctionName("Vote")] public async Task Vote([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "vote", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { try { var jsonMsg = JsonDocument.Parse(msg); jsonMsg.RootElement.TryGetProperty("id", out JsonElement id); jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress); jsonMsg.RootElement.TryGetProperty("code", out JsonElement code); //Dictionary keyValuePairs = mySbMsg.ToObject>(); var client = _azureCosmos.GetCosmosClient(); Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(id.ToString(), new PartitionKey($"{code}")); vote.progress = progress.ToString(); await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}")); } catch (Exception ex) { await _dingDing.SendBotMsg($"ServiceBus,VoteBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組); } } [FunctionName("Survey")] public async Task Survey([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "survey", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { try { var jsonMsg = JsonDocument.Parse(msg); jsonMsg.RootElement.TryGetProperty("id", out JsonElement id); jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress); jsonMsg.RootElement.TryGetProperty("code", out JsonElement code); //Dictionary keyValuePairs = mySbMsg.ToObject>(); var client = _azureCosmos.GetCosmosClient(); Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync(id.ToString(), new PartitionKey($"{code}")); survey.progress = progress.ToString(); await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}")); } catch (Exception ex) { await _dingDing.SendBotMsg($"ServiceBus,SurveyBus()\n{ex.Message}", GroupNames.醍摩豆服務運維群組); } } [FunctionName("Blob")] public async Task Blob([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { try { // await _dingDing.SendBotMsg($"ServiceBus,Blob(){msg}", GroupNames.醍摩豆服務運維群組); var jsonMsg = JsonDocument.Parse(msg); if(jsonMsg.RootElement.TryGetProperty("name", out JsonElement name)&& name.ValueKind==JsonValueKind.String) { var client = _azureStorage.GetBlobContainerClient($"{name}"); var size = await client.GetBlobsCatalogSize(); await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", new RedisValue($"{name}"), new RedisValue($"{long.Parse($"{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); } #if DEBUG await _dingDing.SendBotMsg($"ServiceBus,Blob() 容器:{name}使用:{size.Item1},文件分类:{size.Item2.ToJsonString()}", GroupNames.成都开发測試群組); #endif } } catch (Exception ex) { await _dingDing.SendBotMsg($"ServiceBus,Blob()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組); } } /// /// 完善课程变更,StuListChange, originCode是学校编码 则表示名单是学校自定义名单,如果是tmdid则表示醍摩豆的私有名单,scope=school,private。 /// /// /// CourseChange ///// /// /// [FunctionName("StuList")] public async Task StuList([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "stulist", Connection = "Azure:ServiceBus:ConnectionString")] string msg) { var client = _azureCosmos.GetCosmosClient(); try { await _dingDing.SendBotMsg($"ServiceBus,StuList:{msg}", GroupNames.醍摩豆服務運維群組); var jsonMsg = JsonDocument.Parse(msg); StuListChange stuListChange = msg.ToObject(); //名单变动修改学生课程关联信息 await StuListService.FixStuCourse(client, stuListChange); //Vote投票 Survey问卷 Exam评测 Learn学习活动 Homework作业活动 //名单变动修改学生问卷关联信息 await StuListService.FixActivity(client, stuListChange, "Survey"); //名单变动修改学生投票关联信息 await StuListService.FixActivity(client, stuListChange, "Vote"); //名单变动修改学生评测关联信息 await StuListService.FixActivity(client, stuListChange, "Exam"); //TODO学习活动 //await FixActivity(client, stuListChange, "Learn"); //TODO作业活动 // await FixActivity(client, stuListChange, "Homework"); } catch (Exception ex) { await _dingDing.SendBotMsg($"StuListServiceBus-StuList\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組); } } } }