// /** // * File: SkyCheckState.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using ln.skyscanner.entities; using System.Collections.Generic; using ln.skyscanner.perfvalue; using ln.skyscanner.services; using ln.json.attributes; namespace ln.skyscanner.checks { public enum CheckState { OK, WARN, CRITICAL, FAIL, ERROR } public class CheckStateChange { public CheckState NewState; public DateTime Timestamp; private CheckStateChange() { } public CheckStateChange(CheckState checkState) { NewState = checkState; Timestamp = DateTime.Now; } } public class SkyCheckState { public Guid ID; public readonly String CheckName; public PerformanceValue[] PerformanceValues => performanceValues.ToArray(); List performanceValues = new List(); public DateTime LastCheckTime { get; set; } public TimeSpan UnchangedTime => history.Count > 0 ? DateTime.Now - history[history.Count - 1].Timestamp : TimeSpan.FromSeconds(0); public CheckState BaseCheckState { get; set; } = CheckState.OK; public CheckState CheckState { get { CheckState checkState = BaseCheckState; foreach (PerformanceValue performanceValue in performanceValues) if (performanceValue.CheckState > checkState) checkState = performanceValue.CheckState; return checkState; } set { BaseCheckState = value; } } public CheckStateChange[] History => history.ToArray(); List history = new List(); public string Message { get; set; } protected SkyCheckState() { } public SkyCheckState(SkyCheck skyCheck,Node node) { ID = Guid.NewGuid(); CheckName = skyCheck.Name; } public void CheckHistory() { if ((history.Count == 0) || (history[history.Count - 1].NewState != CheckState)) { history.Add(new CheckStateChange(CheckState)); if (history.Count > 10) history.RemoveAt(0); } } public void AddPerformanceValue(PerformanceValue performanceValue) { performanceValues.Add(performanceValue); } public PerformanceValue GetPerformanceValue(string perfName) { foreach (PerformanceValue performanceValue in performanceValues) { if (performanceValue.PerfName.Equals(perfName)) return performanceValue; } return null; } public bool ContainsPerformanceValue(string perfName) { foreach (PerformanceValue performanceValue in performanceValues) { if (performanceValue.PerfName.Equals(perfName)) return true; } return false; } } }