From 1d4a2eb1b07a4ef56b563431cd6d8858e9716466 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 24 Apr 2023 16:09:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=B1=E5=83=8F=E5=AD=98=E5=82=A8=E9=A2=84?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.API/appsettings.json | 7 +- .../Service/Common/SystemMonitor.cs | 162 ++++++------------ 2 files changed, 55 insertions(+), 114 deletions(-) diff --git a/IRaCIS.Core.API/appsettings.json b/IRaCIS.Core.API/appsettings.json index 791297540..f142d1f51 100644 --- a/IRaCIS.Core.API/appsettings.json +++ b/IRaCIS.Core.API/appsettings.json @@ -57,9 +57,10 @@ } } }, - "UpdateConfig": { - "test": "123456", - "test1": "12345678" + "IRaCISImageStore": { + "SwitchingMode": "RemainingDiskCapacity", + "SwitchingRatio": 80, + "NotificationEmail": "872297557@qq.com" }, "IRaCISBasicConfig": { "DoctorCodePrefix": "RE", diff --git a/IRaCIS.Core.Application/Service/Common/SystemMonitor.cs b/IRaCIS.Core.Application/Service/Common/SystemMonitor.cs index 80e6973fb..429e4f4b7 100644 --- a/IRaCIS.Core.Application/Service/Common/SystemMonitor.cs +++ b/IRaCIS.Core.Application/Service/Common/SystemMonitor.cs @@ -16,6 +16,7 @@ using System.Text.Json.Nodes; using System.Text.Encodings.Web; using Newtonsoft.Json.Linq; using Newtonsoft.Json; +using IRaCIS.Core.Infrastructure; namespace IRaCIS.Core.Application.Service.Common { @@ -44,136 +45,95 @@ namespace IRaCIS.Core.Application.Service.Common } + + + public string GetBestStoreDisk() { + var json = File.ReadAllText("appsettings.json"); + + JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load }); + + int switchingRatio = 80; + + try + { + switchingRatio = (int)jsonObject["IRaCISImageStore"]["SwitchingRatio"]; + } + catch (Exception e) + { + + throw new BusinessValidationFailedException("解析Json文件配置出现问题"); + } + //默认存储的路径 var defaultStoreRootFolder = Path.Combine((Directory.GetParent(_hostEnvironment.ContentRootPath.TrimEnd('\\'))).IfNullThrowException().FullName, StaticData.Folder.IRaCISDataFolder); - - - + DriveInfo defaultDrive = new DriveInfo(Path.GetPathRoot(defaultStoreRootFolder)); var drives = DriveInfo.GetDrives().Where(t => !t.Name.Contains("C") && !t.Name.Contains("c")) .Where(d => d.DriveType == DriveType.Fixed && d.IsReady) + //剩余空间最多的 .OrderByDescending(d => d.AvailableFreeSpace) + + //存储空间相同,则按照按照总空间从大到小排序 .ThenByDescending(d => d.TotalSize - d.TotalFreeSpace); var bestDrive = drives.FirstOrDefault(); var bestStoreRootFolder = string.Empty; //仅仅只有C 盘 - if (bestDrive == null) + if (bestDrive == null || ((double)(defaultDrive.TotalSize - defaultDrive.TotalFreeSpace) / defaultDrive.TotalSize) * 100 < switchingRatio) { bestStoreRootFolder = defaultStoreRootFolder; } else { - bestStoreRootFolder = drives.FirstOrDefault()?.Name + _hostEnvironment.EnvironmentName; + bestStoreRootFolder = Path.Combine(drives.FirstOrDefault()?.Name, _hostEnvironment.EnvironmentName); } - DriveInfo drive = new DriveInfo(Path.GetPathRoot(defaultStoreRootFolder)); + + //找到最优驱动器 + DriveInfo drive = new DriveInfo(Path.GetPathRoot(bestStoreRootFolder)); //最优盘符使用率超过百分之80 - if (((double)(drive.TotalSize - drive.TotalFreeSpace) / drive.TotalSize) * 100 > 80) + if (((double)(drive.TotalSize - drive.TotalFreeSpace) / drive.TotalSize) * 100 > switchingRatio) { } + if (!Directory.Exists(bestStoreRootFolder)) + { + Directory.CreateDirectory(bestStoreRootFolder); + } return bestStoreRootFolder; } + public class GeneralRule + { + public string Endpoint { get; set; } + public string Period { get; set; } + public int Limit { get; set; } + } + public void UpdateAppSettings(string key, string newValue) { - #region NewtonSoftJson - //// 读取 JSON 文件 - //string jsonFilePath = "appsettings.json"; - //string jsonString = File.ReadAllText(jsonFilePath); - - //// 解析 JSON 文件 - //var jObject = JsonConvert.DeserializeObject(jsonString); - - //// 更新或添加配置项 - //jObject[key] = newValue; - - //// 将更新后的 JSON 字符串写入文件 - //using var streamWriter = new StreamWriter(jsonFilePath); - //using var jsonTextWriter = new JsonTextWriter(streamWriter); - //jsonTextWriter.Formatting = Formatting.Indented; - //jObject.WriteTo(jsonTextWriter); - #endregion - - #region test - //var json = File.ReadAllText("appsettings.json"); - //var jObject = JToken.Parse(json, new JsonLoadSettings { CommentHandling = CommentHandling.Load,DuplicatePropertyNameHandling=DuplicatePropertyNameHandling.Ignore }); - - //// Update or add the property - //jObject[key] = newValue; - - //// Preserve comments - //var settings = new JsonSerializerSettings - //{ - // PreserveReferencesHandling = PreserveReferencesHandling.Objects, - // Formatting = Formatting.Indented, - // StringEscapeHandling = StringEscapeHandling.Default, - - //}; - //var updatedJson = JsonConvert.SerializeObject(jObject, settings); - //File.WriteAllText("appsettings.json", updatedJson); - - //var json = File.ReadAllText("appsettings.json"); - //var jToken = JToken.Parse(json); - - //// Update or add the property - //jToken[key] = newValue; - - //// Preserve comments - //var settings = new JsonSerializerSettings - //{ - // TypeNameHandling = TypeNameHandling.All, - // PreserveReferencesHandling = PreserveReferencesHandling.Objects, - // Formatting = Formatting.Indented, - // StringEscapeHandling = StringEscapeHandling.Default - //}; - //var updatedJson = JsonConvert.SerializeObject(jToken, settings); - //File.WriteAllText("appsettings.json", updatedJson); - - - //string json = @"{ - // // This is a comment - // ""Name"": ""John Smith"", - // ""Age"": 30, - // ""Address"": { - // // This is another comment - // ""Street"": ""123 Main St"", - // ""City"": ""Anytown"", - // ""State"": ""CA"", - // ""Zip"": ""12345"" - // } - //}"; - - //// 使用 Json.NET 库解析 JSON 字符串 - //JObject jObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load }); - - //// 修改属性 - //jObject["Name"] = "Jane Smith"; - - //// 将修改后的 JObject 对象序列化为 JSON 字符串并保留注释 - //string newJson = jObject.ToString(Newtonsoft.Json.Formatting.Indented); - - //// 打印修改后的 JSON 字符串 - //Console.WriteLine(newJson); - - #endregion - + List generalRules = new List() +{ + new GeneralRule() { Endpoint = "*", Period = "1s", Limit = 3 }, + new GeneralRule() { Endpoint = "*", Period = "15m", Limit = 100 }, + new GeneralRule() { Endpoint = "*", Period = "12h", Limit = 1000 }, + new GeneralRule() { Endpoint = "*", Period = "7d", Limit = 10000 } +}; // 读取 Json 文件 string jsonFilePath = "appsettings.json"; @@ -181,37 +141,17 @@ namespace IRaCIS.Core.Application.Service.Common var json = File.ReadAllText("appsettings.json"); JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load }); - // 获取 UpdateConfig 属性所在的节点 - JToken updateConfigNode = jsonObject.SelectToken("UpdateConfig"); - JProperty updateConfigProperty = (JProperty)updateConfigNode.Parent; - JObject updateConfigParent = (JObject)updateConfigProperty.Value; - // 向 updateConfigParent 添加或者属性 - updateConfigParent[key] = newValue; + // 操作UpdateConfig对象 + jsonObject["UpdateConfig"][key] = newValue; + jsonObject["UpdateConfig"]["testArray"] = JArray.FromObject(generalRules); // 将更改保存回 Json 文件 File.WriteAllText(jsonFilePath, jsonObject.ToString()); - //using (var reader = new JsonTextReader(new StringReader(json))) - //using (var writer = new JsonTextWriter(new StreamWriter("appsettings.json"))) - //{ - // while (reader.Read()) - // { - // if (reader.TokenType == JsonToken.PropertyName && (string)reader.Value == key) - // { - // // Write the updated property value - // writer.WritePropertyName(key); - // writer.WriteValue(newValue); - // reader.Read(); - // continue; - // } - // // Write all other tokens - // writer.WriteToken(reader.TokenType, reader.Value); - // } - //}