ln.skyscanner/checks/SkyCheckState.cs

114 lines
3.5 KiB
C#

// /**
// * 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 ln.types.odb;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Linq;
using ln.types.odb.attributes;
using System.Collections.Generic;
namespace ln.skyscanner.checks
{
[JsonConverter(typeof(StringEnumConverter))]
public enum CheckState { OK, WARN, CRITICAL, FAIL, ERROR }
public struct CheckStateChange
{
public CheckState NewState;
public DateTime Timestamp;
public CheckStateChange(CheckState checkState)
{
NewState = checkState;
Timestamp = DateTime.Now;
}
}
public class SkyCheckState
{
[DocumentID]
public Guid ID;
public readonly String CheckName;
[ByReference]
public readonly Node Node;
public PerformanceValue[] PerformanceValues => performanceValues.ToArray();
List<PerformanceValue> performanceValues = new List<PerformanceValue>();
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<CheckStateChange> history = new List<CheckStateChange>();
protected SkyCheckState()
{
}
public SkyCheckState(SkyCheck skyCheck,Node node)
{
ID = Guid.NewGuid();
CheckName = skyCheck.Name;
Node = node;
}
public void WritePerformanceValue(String perfName,double value, double wLower = Double.MinValue, double wUpper = Double.MaxValue, double cLower = Double.MinValue, double cUpper = Double.MaxValue)
{
if (performanceValues == null)
performanceValues = new List<PerformanceValue>();
string[] perfPath = new string[] { Node.UniqueIdentity, CheckName, perfName };
perfName = string.Join("/", perfPath);
foreach (PerformanceValue performanceValue in performanceValues)
{
if (performanceValue.PerfName.Equals(perfName))
{
performanceValue.WritePerfValue(value);
return;
}
}
PerformanceValue newPerformanceValue = new PerformanceValue(perfPath, wLower, wUpper, cLower, cUpper);
performanceValues.Add(newPerformanceValue);
newPerformanceValue.WritePerfValue(value);
}
public void CheckHistory()
{
if ((history.Count == 0) || (history[history.Count - 1].NewState != CheckState))
{
history.Add(new CheckStateChange(CheckState));
}
}
}
}