162 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			162 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C#
		
	
	
| using IRaCIS.Core.Domain.Share;
 | |
| using Microsoft.AspNetCore.Mvc;
 | |
| using Microsoft.Extensions.Configuration;
 | |
| using Microsoft.Extensions.Hosting;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Linq;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| using Microsoft.AspNetCore.Hosting;
 | |
| using Microsoft.Extensions.Configuration.Json;
 | |
| using IRaCIS.Core.Infrastructure.Extention;
 | |
| using SharpCompress.Common;
 | |
| using System.Text.Json;
 | |
| using System.Text.Json.Nodes;
 | |
| using System.Text.Encodings.Web;
 | |
| using Newtonsoft.Json.Linq;
 | |
| using Newtonsoft.Json;
 | |
| using IRaCIS.Core.Infrastructure;
 | |
| using IRaCIS.Core.Application.Helper;
 | |
| 
 | |
| namespace IRaCIS.Core.Application.Service.Common
 | |
| {
 | |
|     [ApiExplorerSettings(GroupName = "Common")]
 | |
|     public class SystemMonitor : BaseService
 | |
|     {
 | |
| 
 | |
|         public void GetDiskInfo()
 | |
|         {
 | |
|             // Get all drives
 | |
|             var drives = DriveInfo.GetDrives();
 | |
| 
 | |
|             // Loop through each drive and get info
 | |
|             foreach (var drive in drives)
 | |
|             {
 | |
|                 if (drive.IsReady && drive.DriveType == DriveType.Fixed)
 | |
|                 {
 | |
|                     long totalSize = drive.TotalSize;
 | |
|                     long availableSpace = drive.AvailableFreeSpace;
 | |
|                     long usedSpace = totalSize - availableSpace;
 | |
|                     double usedSpacePercent = (double)usedSpace / totalSize * 100;
 | |
| 
 | |
|                     Console.WriteLine($"Drive {drive.Name}: Total size = {totalSize / (1024 * 1024)} MB, Available space = {availableSpace / (1024 * 1024)} MB, Used space = {usedSpace / (1024 * 1024)} MB ({usedSpacePercent:F2}%)");
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
| 
 | |
| 
 | |
|         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)
 | |
|             {
 | |
| 
 | |
|                 //---解析Json文件配置出现问题
 | |
|                 throw new BusinessValidationFailedException(_localizer["SysMon_JsonConfig"]+e.Message);
 | |
|             }
 | |
| 
 | |
|             //默认存储的路径
 | |
|             var defaultStoreRootFolder = FileStoreHelper.GetIRaCISRootDataFolder(_hostEnvironment);
 | |
| 
 | |
| 
 | |
|             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 || ((double)(defaultDrive.TotalSize - defaultDrive.TotalFreeSpace) / defaultDrive.TotalSize) * 100 < switchingRatio)
 | |
|             {
 | |
|                 bestStoreRootFolder = defaultStoreRootFolder;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 bestStoreRootFolder = Path.Combine(bestDrive?.RootDirectory.FullName, _hostEnvironment.EnvironmentName);
 | |
|             }
 | |
| 
 | |
| 
 | |
|             ////找到最优驱动器
 | |
|             //DriveInfo drive = new DriveInfo(Path.GetPathRoot(bestStoreRootFolder));
 | |
| 
 | |
|             ////最优盘符使用率超过百分之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)
 | |
|         {
 | |
| 
 | |
| 
 | |
| 
 | |
|             List<GeneralRule> generalRules = new List<GeneralRule>()
 | |
| {
 | |
|     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";
 | |
| 
 | |
|             var json = File.ReadAllText("appsettings.json");
 | |
| 
 | |
|             JObject jsonObject = JObject.Parse(json, new JsonLoadSettings() { CommentHandling = CommentHandling.Load });
 | |
| 
 | |
| 
 | |
|             // 操作UpdateConfig对象
 | |
|             jsonObject["UpdateConfig"][key] = newValue;
 | |
| 
 | |
|             jsonObject["UpdateConfig"]["testArray"] = JArray.FromObject(generalRules);
 | |
| 
 | |
|             // 将更改保存回 Json 文件
 | |
|             File.WriteAllText(jsonFilePath, jsonObject.ToString());
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
| 
 | |
|         }
 | |
|     }
 | |
| }
 |