using System; using System.Diagnostics; using Microsoft.Extensions.Logging; using AyaNova.Util; using AyaNova.Models; using Microsoft.EntityFrameworkCore; namespace AyaNova.Biz { /// /// called by Generator to gather server metrics and insert in db /// internal static class CoreJobMetricsSnapshot { private static ILogger log = AyaNova.Util.ApplicationLogging.CreateLogger("CoreJobMetricsSnapshot"); private static TimeSpan tsDataRetention = new TimeSpan(365, 0, 0, 0, 0);//one year private static Process _process = Process.GetCurrentProcess(); private static TimeSpan _oldCPUTime = TimeSpan.Zero; private static DateTime _lastSnapshot = DateTime.UtcNow; // private static DateTime _lastRpsTime = DateTime.UtcNow; private static double _cpu = 0; #if(DEBUG) private static TimeSpan tsOneMinute = new TimeSpan(0, 1, 0); #else private static TimeSpan tsOneMinute = new TimeSpan(0, 1, 0); #endif private static TimeSpan tsOneHour = new TimeSpan(1, 0, 0); private static TimeSpan ts24Hours = new TimeSpan(24, 0, 0); //////////////////////////////////////////////////////////////////////////////////////////////// // DoAsync // public static void DoJob() { //Nothing is gathered less than one minute frequency if (!DateUtil.IsAfterDuration(_lastSnapshot, tsOneMinute)) return; log.LogTrace("Starting metrics snapshot"); ///////////////////////////////////////////// //ONE MINUTE SNAPS // var now = DateTime.UtcNow; _process.Refresh(); //CPU var cpuElapsedTime = now.Subtract(_lastSnapshot).TotalMilliseconds; var newCPUTime = _process.TotalProcessorTime; var elapsedCPU = (newCPUTime - _oldCPUTime).TotalMilliseconds; _cpu = elapsedCPU * 100 / Environment.ProcessorCount / cpuElapsedTime; _oldCPUTime = newCPUTime; //MEMORY // The memory occupied by objects. var Allocated = GC.GetTotalMemory(false);//bigint // The working set includes both shared and private data. The shared data includes the pages that contain all the // instructions that the process executes, including instructions in the process modules and the system libraries. var WorkingSet = _process.WorkingSet64;//bigint // The value returned by this property represents the current size of memory used by the process, in bytes, that // cannot be shared with other processes. var PrivateBytes = _process.PrivateMemorySize64;//bigint // The number of generation 0 collections var Gen0 = GC.CollectionCount(0);//integer // The number of generation 1 collections var Gen1 = GC.CollectionCount(1);//integer // The number of generation 2 collections var Gen2 = GC.CollectionCount(2);//integer //NOTE: CPU percentage is *our* process cpu percentage over timeframe of last captured avg //So it does *not* show the entire server cpu load, only for RAVEN, server stats //need to be captured / viewed independently (digital ocean control panel for example or windows task manager) var CPU = _cpu;// double precision //System.Diagnostics.Debug.WriteLine($"MM Snapshot, cpu: {CPU}"); using (AyContext ct = ServiceProviderProvider.DBContext) { //write to db MetricMM mm = new MetricMM(Allocated, WorkingSet, PrivateBytes, Gen0, Gen1, Gen2, CPU); ct.MetricMM.Add(mm); ct.SaveChanges(); //System.Diagnostics.Debug.WriteLine("MM SAVED"); } ///////////////////////////////////////////// //EVERY HOUR SNAPS // if (DateUtil.IsAfterDuration(_lastSnapshot, tsOneHour)) { //RECORDS IN TABLE // //Only do this once per hour // log.LogTrace("Counting table records"); // //Get a count of important tables in db // List allTableNames = await DbUtil.GetAllTablenamesAsync(); // //Skip some tables as they are internal and / or only ever have one record // List skipTableNames = new List(); // skipTableNames.Add("alicense"); // skipTableNames.Add("aschemaversion"); // foreach (string table in allTableNames) // { // if (!skipTableNames.Contains(table)) // { // //var tags = new MetricTags("TableTagKey", table); // // metrics.Measure.Gauge.SetValue(MetricsRegistry.DBRecordsGauge, tags, await DbUtil.CountOfRecordsAsync(table)); // } // } //JOB COUNTS (DEAD, RUNNING, COMPLETED, SLEEPING) // foreach (JobStatus stat in Enum.GetValues(typeof(JobStatus))) // { // // var jobtag = new MetricTags("JobStatus", stat.ToString()); // // metrics.Measure.Gauge.SetValue(MetricsRegistry.JobsGauge, jobtag, await JobsBiz.GetCountForJobStatusAsync(ct, stat)); // } } ///////////////////////////////////////////// //ONCE A DAY SNAPS AND CLEANUP // if (DateUtil.IsAfterDuration(_lastSnapshot, ts24Hours)) { //FILES ON DISK // log.LogTrace("Files on disk information"); // var UtilFilesInfo = FileUtil.GetUtilityFolderSizeInfo(); // var UserFilesInfo = FileUtil.GetAttachmentFolderSizeInfo(); // var mtag = new MetricTags("File type", "Business object files"); // metrics.Measure.Gauge.SetValue(MetricsRegistry.FileCountGauge, mtag, UserFilesInfo.FileCountWithChildren); // metrics.Measure.Gauge.SetValue(MetricsRegistry.FileSizeGauge, mtag, UserFilesInfo.SizeWithChildren); // mtag = new MetricTags("File type", "OPS files"); // metrics.Measure.Gauge.SetValue(MetricsRegistry.FileCountGauge, mtag, UtilFilesInfo.FileCountWithChildren); // metrics.Measure.Gauge.SetValue(MetricsRegistry.FileSizeGauge, mtag, UtilFilesInfo.SizeWithChildren); ///////////////////////////////// //CLEAR OLD ENTRIES // DateTime ClearDate = DateTime.UtcNow - tsDataRetention; using (AyContext ct = ServiceProviderProvider.DBContext) { ct.Database.ExecuteSqlInterpolated($"delete from ametricmm where t < {ClearDate}"); } } _lastSnapshot = now; } ///////////////////////////////////////////////////////////////////// }//eoc }//eons