Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
Never
using System;
using System.Collections.Generic;

public class ErrorInfoManager
{
    private Dictionary<int, (int Min, int Max, string Result)> errorInfoMap = new Dictionary<int, (int, int, string)>();
    private List<(int ErrorType, DateTime Timestamp)> activeWarnings = new List<(int, DateTime)>();

    public void AddErrorInfo(int errorType, int min, int max, string result)
    {
        errorInfoMap[errorType] = (min, max, result);
    }

    public string GetResultForValue(int value)
    {
        foreach (var kvp in errorInfoMap)
        {
            int errorType = kvp.Key;
            var errorInfo = kvp.Value;

            if (value >= errorInfo.Min && value <= errorInfo.Max)
            {
                // Check if there is an active warning for this error type in the last 10 seconds
                if (!IsWarningActive(errorType))
                {
                    // Add a new active warning with the current timestamp
                    activeWarnings.Add((errorType, DateTime.Now));

                    // Return the result
                    return $"{errorInfo.Result} (Error Type: {errorType})";
                }
            }
        }

        return "Default Error Message"; // Modify as needed
    }

    private bool IsWarningActive(int errorType)
    {
        // Check if there is an active warning for the specified error type in the last 10 seconds
        DateTime tenSecondsAgo = DateTime.Now.AddSeconds(-10);
        return activeWarnings.Exists(warning => warning.ErrorType == errorType && warning.Timestamp > tenSecondsAgo);
    }

    public void CleanupWarnings()
    {
        // Remove warnings that are older than 10 seconds
        DateTime tenSecondsAgo = DateTime.Now.AddSeconds(-10);
        activeWarnings.RemoveAll(warning => warning.Timestamp <= tenSecondsAgo);
    }
}

class Program
{
    static void Main()
    {
        var errorInfoManager = new ErrorInfoManager();

        // Add error information to the manager
        errorInfoManager.AddErrorInfo(1, 10, 20, "Warning 1");
        errorInfoManager.AddErrorInfo(2, 30, 40, "Error 1");
        errorInfoManager.AddErrorInfo(3, 50, 60, "Warning 2");

        // Simulate processing data every 50 ms for a certain value
        for (int i = 0; i < 10; i++)
        {
            int valueToCheck = 15 + i * 5; // Simulating different values
            string result = errorInfoManager.GetResultForValue(valueToCheck);
            Console.WriteLine($"Value: {valueToCheck}, Result: {result}");

            // Simulate waiting 50 ms for the next data point
            System.Threading.Thread.Sleep(50);
        }

        // Wait for more than 10 seconds to see cleanup in action
        System.Threading.Thread.Sleep(11000);

        // Perform cleanup to remove older warnings
        errorInfoManager.CleanupWarnings();
    }
}