ln.skyscanner/checks/SkyCheckState.cs

114 lines
3.5 KiB
C#
Raw Normal View History

2019-04-05 00:59:04 +02:00
// /**
// * 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;
2019-04-06 09:36:43 +02:00
using ln.types.odb;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
2019-04-08 08:47:12 +02:00
using System.Linq;
using ln.types.odb.attributes;
2019-04-08 13:06:06 +02:00
using System.Collections.Generic;
2019-04-05 00:59:04 +02:00
namespace ln.skyscanner.checks
{
2019-04-06 09:36:43 +02:00
[JsonConverter(typeof(StringEnumConverter))]
2019-04-05 00:59:04 +02:00
public enum CheckState { OK, WARN, CRITICAL, FAIL, ERROR }
2019-04-08 13:06:06 +02:00
public struct CheckStateChange
{
public CheckState NewState;
public DateTime Timestamp;
public CheckStateChange(CheckState checkState)
{
NewState = checkState;
Timestamp = DateTime.Now;
}
}
2019-04-05 00:59:04 +02:00
public class SkyCheckState
{
2019-04-06 09:36:43 +02:00
[DocumentID]
2019-04-12 00:52:55 +02:00
public Guid ID;
2019-04-06 09:36:43 +02:00
2019-04-05 00:59:04 +02:00
public readonly String CheckName;
2019-04-11 08:30:13 +02:00
[ByReference]
public readonly Node Node;
2019-04-05 00:59:04 +02:00
2019-04-12 00:52:55 +02:00
public PerformanceValue[] PerformanceValues => performanceValues.ToArray();
List<PerformanceValue> performanceValues = new List<PerformanceValue>();
2019-04-08 08:47:12 +02:00
2019-04-05 00:59:04 +02:00
public DateTime LastCheckTime { get; set; }
2019-04-12 00:52:55 +02:00
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;
}
}
2019-04-08 13:06:06 +02:00
public CheckStateChange[] History => history.ToArray();
List<CheckStateChange> history = new List<CheckStateChange>();
2019-04-06 09:36:43 +02:00
2019-04-05 00:59:04 +02:00
protected SkyCheckState()
{
}
public SkyCheckState(SkyCheck skyCheck,Node node)
{
2019-04-12 00:52:55 +02:00
ID = Guid.NewGuid();
2019-04-05 00:59:04 +02:00
CheckName = skyCheck.Name;
2019-04-11 08:30:13 +02:00
Node = node;
2019-04-05 00:59:04 +02:00
}
2019-04-08 13:06:06 +02:00
2019-04-12 00:52:55 +02:00
public void WritePerformanceValue(String perfName,double value, double wLower = Double.MinValue, double wUpper = Double.MaxValue, double cLower = Double.MinValue, double cUpper = Double.MaxValue)
2019-04-08 13:06:06 +02:00
{
if (performanceValues == null)
2019-04-12 00:52:55 +02:00
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);
2019-04-08 13:06:06 +02:00
}
2019-04-12 00:52:55 +02:00
public void CheckHistory()
2019-04-08 13:06:06 +02:00
{
2019-04-12 00:52:55 +02:00
if ((history.Count == 0) || (history[history.Count - 1].NewState != CheckState))
{
history.Add(new CheckStateChange(CheckState));
}
2019-04-08 13:06:06 +02:00
}
2019-04-12 00:52:55 +02:00
2019-04-05 00:59:04 +02:00
}
}