ln.skyscanner/checks/SkyCheckState.cs

108 lines
3.1 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-08 13:06:06 +02:00
using System.Collections.Generic;
2019-08-29 13:14:52 +02:00
using ln.skyscanner.perfvalue;
using ln.skyscanner.services;
using ln.json.attributes;
2019-04-05 00:59:04 +02:00
namespace ln.skyscanner.checks
{
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-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-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-08 13:06:06 +02:00
2019-08-29 13:14:52 +02:00
public void CheckHistory()
2019-04-08 13:06:06 +02:00
{
2019-08-29 13:14:52 +02:00
if ((history.Count == 0) || (history[history.Count - 1].NewState != CheckState))
{
history.Add(new CheckStateChange(CheckState));
}
}
2019-04-12 00:52:55 +02:00
2019-08-29 13:14:52 +02:00
public void AddPerformanceValue(PerformanceValue performanceValue)
{
performanceValues.Add(performanceValue);
}
public PerformanceValue GetPerformanceValue(string perfName)
{
2019-04-12 00:52:55 +02:00
foreach (PerformanceValue performanceValue in performanceValues)
{
if (performanceValue.PerfName.Equals(perfName))
2019-08-29 13:14:52 +02:00
return performanceValue;
2019-04-12 00:52:55 +02:00
}
2019-08-29 13:14:52 +02:00
return null;
2019-04-08 13:06:06 +02:00
}
2019-08-29 13:14:52 +02:00
public bool ContainsPerformanceValue(string perfName)
2019-04-08 13:06:06 +02:00
{
2019-08-29 13:14:52 +02:00
foreach (PerformanceValue performanceValue in performanceValues)
2019-04-12 00:52:55 +02:00
{
2019-08-29 13:14:52 +02:00
if (performanceValue.PerfName.Equals(perfName))
return true;
2019-04-12 00:52:55 +02:00
}
2019-08-29 13:14:52 +02:00
return false;
2019-04-08 13:06:06 +02:00
}
2019-04-12 00:52:55 +02:00
2019-04-05 00:59:04 +02:00
}
}