Example #1
0
 public MainWindowViewModel()
 {
     ActualStatus   = new CPUStatus();
     timer          = new Timer(2_000);
     timer.Elapsed += Timer_Elapsed;
     timer.Start();
 }
Example #2
0
        public void Interpret(Data d, SQLiteConnection conn)
        {
            if (d.Type == ECollectorType.CPUUsage)
            {
                CPUUsageAlert       cpu_alert_level = new CPUUsageAlert();
                double              cpu_alert       = cpu_alert_level.GetValueAsDouble(conn) ?? 80.0;
                CPUUsageAlertCounts alert_counts    = new CPUUsageAlertCounts();
                // Make sure we get at least 1
                int a_counts = Math.Max(alert_counts.GetValueAsInt(conn) ?? 1, 1);

                // The interpretation should be done after the insertion, so the value that was just collected should be in the database already

                // Because we're averaging over the last x readings, we can't just use the single Data record d and have to do a query
                // to get the last y readings

                long device_id = GetDeviceID(d, conn);
                if (device_id >= 0)
                {
                    List <int> all_cpu = new List <int>();

                    // To see how the TimeStamp clause works, check out:
                    // https://www.sqlite.org/lang_datefunc.html
                    //string sql = string.Format("SELECT D.Value FROM Data AS D INNER JOIN Collectors AS C ON D.CollectorID = C.CollectorID WHERE C.DeviceID = {0} AND C.CollectorType = {1} AND TimeStamp >= date('now', '-1 day');",
                    //    device_id, (int)CollectorType.CPUUsage);
                    string sql = $"SELECT D.Value FROM Data AS D INNER JOIN Collectors AS C ON D.CollectorID = C.CollectorID WHERE C.DeviceID = {device_id} AND C.CollectorType = {(int)ECollectorType.CPUUsage} ORDER BY TimeStamp DESC LIMIT {a_counts};";
                    using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                        using (SQLiteDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                //var definition = new { Value = string.Empty };
                                //var val = JsonConvert.DeserializeAnonymousType(reader.GetString(0), definition);
                                var val = JsonConvert.DeserializeObject <ValueClass <string> >(reader.GetString(0));
                                if (int.TryParse(val.Value, out int cpu_usage))
                                {
                                    all_cpu.Add(cpu_usage);
                                }
                            }
                        }

                    // When all_cpu doesn't have anything in it, Average() throws an exception, so we
                    // need to make sure there's something in it. And, we don't want to generate an alert
                    // unless we've gotten all the readings we should have. Given both of those, the
                    // average shouldn't even be calculated unless we have everything we want.
                    if (all_cpu.Count == a_counts)
                    {
                        CPUStatus   cpu_status   = new CPUStatus(cpu_alert);
                        EStatusType alert_status = cpu_status.GetStatus(all_cpu);
                        string      message      = $"{(int)(cpu_status.Average + 0.5)} %";

                        SetDeviceStatus(device_id, alert_status, CPUStatus.Types, message, conn);
                    }
                }
            }
        }
Example #3
0
        public void GenerateStatusTypesProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(90.0);
            List <int> cpu        = new List <int>(new int[] { 1, 1, 1, 1, 1 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(1.0, cpu_status.Average, 1);

            cpu    = new List <int>(new int[] { 90, 90, 90, 90, 90 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
        }
Example #4
0
        public void HandleMaxThresholdsProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(0.0);
            List <int> cpu        = new List <int>(new int[] { 0, 0, 0, 0, 0 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.ExcessiveCPU, status);

            cpu_status = new CPUStatus(100.0);
            status     = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.NormalCPU, status);
            cpu    = new List <int>(new int[] { 100, 100, 100, 100, 100 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
        }
Example #5
0
        public void HandleThresholdTransitionsProperly()
        {
            CPUStatus  cpu_status = new CPUStatus(90.0);
            List <int> cpu        = new List <int>(new int[] { 89, 89, 89, 89, 89 });

            EStatusType status = cpu_status.GetStatus(cpu);

            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(89.0, cpu_status.Average, 1);

            cpu    = new List <int>(new int[] { 89, 90, 90, 90, 90 });
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.NormalCPU, status);
            Assert.Equal(89.8, cpu_status.Average, 1);

            cpu[0] = 90;
            status = cpu_status.GetStatus(cpu);
            Assert.Equal(EStatusType.ExcessiveCPU, status);
            Assert.Equal(90.0, cpu_status.Average, 1);
        }