view ServerMonitor/Objects/CheckResult.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents 3142e52cbe69
children 68d7834dc28e
line wrap: on
line source

using ServerMonitorApp.Properties;
using System;
using System.Drawing;

namespace ServerMonitorApp
{
    public class CheckResult
    {
        public const string dateFormat = "yyyy-MM-dd HH:mm:ss.fff";

        public Check Check { get; private set; }

        public CheckStatus CheckStatus { get; private set; }

        public string Message { get; set; }

        public DateTime StartTime { get; set; }

        public DateTime EndTime { get; set; }

        public bool Failed => CheckStatus.In(CheckStatus.Error, CheckStatus.Warning, CheckStatus.Information);

        public FailAction FailAction
        {
            get
            {
                switch (CheckStatus)
                {
                    case CheckStatus.Error: return Settings.Default.ErrorAction;
                    case CheckStatus.Warning: return Settings.Default.WarningAction;
                    case CheckStatus.Information: return Settings.Default.InformationAction;
                    default: return FailAction.None;
                }
            }
        }

        public bool FlashTaskbar => FailAction == FailAction.FlashTaskbar;

        public CheckResult(Check check, CheckStatus status, string message)
        {
            Check = check;
            CheckStatus = status;
            Message = message;
        }

        public string ToLogString()
        {
            return string.Format("{0:00000} {1} {2} {3} {4}",
                Check.Id,
                StartTime.ToString(dateFormat).Replace("T", " "),
                EndTime.ToString(dateFormat).Replace("T", " "),
                CheckStatus,
                Message.ConvertNewlines().Replace("\n", "\\n"));
        }

        public static CheckResult FromLogString(Check check, string logString)
        {
            DateTime startTime = DateTime.Parse(logString.Substring(6, 23));
            DateTime endTime = DateTime.Parse(logString.Substring(30, 23));
            int messageStartPos = logString.IndexOf(' ', 54);
            Enum.TryParse(logString.Substring(54, messageStartPos - 54), out CheckStatus status);
            return new CheckResult(check, status, logString.Substring(messageStartPos + 1)) { StartTime = startTime, EndTime = endTime };
        }
    }
}