public async Task Handle(MetricQueryRequest <FileSystemUsageWidget> request, CancellationToken cancellationToken) { var fileSystemUsageWidget = request.DataContext; var client = elasticsearchHelper.GetElasticClient(fileSystemUsageWidget); ElasticsearchClient.Objects.Stats.ClusterStatsResponse clusterStatsResponse; if (string.IsNullOrWhiteSpace(fileSystemUsageWidget.NodeId)) { clusterStatsResponse = await client.StatsAsync("nodes.fs", cancellationToken); } else { clusterStatsResponse = await client.StatsAsync("nodes.fs", fileSystemUsageWidget.NodeId, cancellationToken); } if (clusterStatsResponse.IsValid) { if (fileSystemUsageWidget.PercentageType == FileSystemPercentageType.PercentageUsed) { var usedInBytes = clusterStatsResponse.Nodes.FileSystem.TotalInBytes - clusterStatsResponse.Nodes.FileSystem.AvailableInBytes; request.DataContext.Progress = (int)Math.Round((usedInBytes / (double)clusterStatsResponse.Nodes.FileSystem.TotalInBytes) * 100); request.DataContext.Message = $"Used {request.DataContext.Progress}%{Environment.NewLine}" + $"{BytesFormatter.Format(usedInBytes)} used out of {BytesFormatter.Format(clusterStatsResponse.Nodes.FileSystem.TotalInBytes)}"; request.DataContext.State = request.DataContext.Progress >= request.DataContext.ErrorPercentage ? State.Failed : State.Ok; } else { request.DataContext.Progress = (int)Math.Round((clusterStatsResponse.Nodes.FileSystem.AvailableInBytes / (double)clusterStatsResponse.Nodes.FileSystem.TotalInBytes) * 100); request.DataContext.Message = $"Avaliable {request.DataContext.Progress}%{Environment.NewLine}" + $"{BytesFormatter.Format(clusterStatsResponse.Nodes.FileSystem.AvailableInBytes)} available out of {BytesFormatter.Format(clusterStatsResponse.Nodes.FileSystem.TotalInBytes)}"; request.DataContext.State = request.DataContext.Progress <= request.DataContext.ErrorPercentage ? State.Failed : State.Ok; } } else { fileSystemUsageWidget.State = State.Invalid; } }
public static bool IsMemoryHealthy( this NodeInfo nodeInfo, int maxMemoryUsagePercent, out string errorMessage, out int memoryUsagePercent ) { memoryUsagePercent = (int)Math.Round((double)(100 * nodeInfo.UsedMemory) / nodeInfo.MemoryLimit); if (memoryUsagePercent >= maxMemoryUsagePercent) { errorMessage = " memory limit exceeded: " + BytesFormatter.Format(nodeInfo.UsedMemory) + " of " + BytesFormatter.Format(nodeInfo.MemoryLimit) + Environment.NewLine; return(false); } errorMessage = string.Empty; return(true); }
public static bool IsHasEnoughDiskSpace( this NodeInfo nodeInfo, int minFreeDiskSpacePercent, out string errorMessage, out int usedDiskSpacePercent ) { usedDiskSpacePercent = (int)Math.Round((double)(100 * nodeInfo.DiskLimit) / nodeInfo.DiskFree); if (usedDiskSpacePercent >= 100 - minFreeDiskSpacePercent) { errorMessage = " disk free limit excedeed: " + BytesFormatter.Format(nodeInfo.DiskFree) + " of " + BytesFormatter.Format(nodeInfo.DiskLimit) + Environment.NewLine; return(false); } errorMessage = string.Empty; return(true); }
public void BytesFormat() { var varints = new[] { BytesFormatter.Format(0), BytesFormatter.Format(1), BytesFormatter.Format(100), BytesFormatter.Format(1024), BytesFormatter.Format(1025), BytesFormatter.Format(7 * 1024 * 1024), BytesFormatter.Format(58L * 1024 * 1024 * 1024), BytesFormatter.Format(100500L * 1024 * 1024 * 1024), BytesFormatter.Format(1022342342345), BytesFormatter.Format(long.MaxValue), }; Assert.AreEqual("0 bytes", varints[0]); Assert.AreEqual("7 MB", varints[5]); foreach (var v in varints) { Console.WriteLine(v); } }
private static async Task GenerateFile(string[] dictionary, string fileName, ulong maxSize) { var rand = new Random(); ulong lineCount = 1; using (var file = File.Create(fileName, OutFileBuffer, FileOptions.Asynchronous | FileOptions.SequentialScan)) using (var sw = new StreamWriter(file, Encoding.UTF8, OutFileBuffer, true)) using (var stopWatch = new AutoStopwatch("Test file generation")) { while (file.Length < (long)maxSize) { var lines = GenerateLines(dictionary, rand, lineCount, ChunkSize); await sw.WriteAsync(lines); lineCount += ChunkSize; } stopWatch.WorkAmount = file.Length; Console.WriteLine("File '{0}' of total {1} lines of size {2} generated", fileName, lineCount, BytesFormatter.Format(file.Length)); } }
static void Main(string[] args) { var helpVariant = new[] { "/h", "-h", "--help" }; if (args.Any() && helpVariant.Contains(args[0], StringComparer.OrdinalIgnoreCase)) { Console.WriteLine("Test file generator" + Environment.NewLine + "\tUsage DataGenerator [size[MB|GB]] [output file]" + Environment.NewLine + "\t" + string.Format(CultureInfo.InvariantCulture, "Default size: {0} bytes, file: '{1}'", DafaultSize, DafaultOutput) + Environment.NewLine + "\tExample: " + Environment.NewLine + "\tDataGenerator.exe 10MB table.txt" + Environment.NewLine + "\tGenerates 10 MB file" + Environment.NewLine); return; } string outFile = DafaultOutput; ulong outSize = DafaultSize; if (args.Length >= 1) { uint size; var intPart = Regex.Match(args[0], @"\d+").Value; if (intPart.Any() && uint.TryParse(intPart, out size)) { var lowArg = args[0].ToLowerInvariant(); if (lowArg.EndsWith("mb")) { outSize = (ulong)size * Constants.Mb; } else if (lowArg.EndsWith("gb")) { outSize = (ulong)size * Constants.Gb; } else { outSize = size; } } else { Console.WriteLine("Provide file size. Use /h for help."); return; } if (args.Length == 2) { outFile = args[1]; } else { Console.WriteLine("Too many arguments. Use /h for help."); return; } } Console.WriteLine("Generation file {0}", BytesFormatter.Format(outSize)); var appSettings = ConfigurationManager.AppSettings; var fullDictionary = File.ReadAllLines(appSettings["WordsFile"]); GenerateFile(fullDictionary, outFile, outSize).Wait(); }
public override string ToString() { return(BytesFormatter.Format(Convert.ToInt64(Value))); }