ln.perfdb/PerfDB.cs

75 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using ln.types;
namespace ln.perfdb
{
public class PerfDB
{
public TimeWindow[] DefaultTimeWindowDefinition { get; set; } = new TimeWindow[]
{
new TimeWindow(TimeSpan.FromDays(28), 60),
new TimeWindow(TimeSpan.FromDays(56), 300),
new TimeWindow(TimeSpan.FromDays(84), 900),
new TimeWindow(TimeSpan.FromDays(168), 1800),
new TimeWindow(TimeSpan.FromDays(750), 3600)
};
public DirectoryInfo BasePath { get; }
Dictionary<Regex, TimeWindow[]> timeWindowDefinitions = new Dictionary<Regex, TimeWindow[]>();
Dictionary<string, NewPerfFile> perfFiles = new Dictionary<string, NewPerfFile>();
public PerfDB(String path)
{
BasePath = new DirectoryInfo(path);
}
public void SetTimeWindowDefintion(String regex, TimeWindow[] timeWindows) => SetTimeWindowDefintion(new Regex(regex), timeWindows);
public void SetTimeWindowDefintion(Regex regex,TimeWindow[] timeWindows)
{
timeWindowDefinitions[regex] = timeWindows;
}
public TimeWindow[] LookupTimeWindowDefinition(string perfName)
{
foreach (Regex regex in timeWindowDefinitions.Keys)
{
if (regex.IsMatch(perfName))
return timeWindowDefinitions[regex];
}
return DefaultTimeWindowDefinition;
}
public NewPerfFile GetPerfFile(string perfName)
{
string[] perfPath = perfName.Split('/');
lock (this)
{
if (!perfFiles.ContainsKey(perfName))
{
string perfDirectory = Path.Combine(BasePath.FullName, Path.Combine(perfPath.Slice(0, -1)));
if (!Directory.Exists(perfDirectory))
{
Directory.CreateDirectory(perfDirectory);
}
string perfFilePath = Path.Combine(BasePath.FullName, Path.Combine(perfPath));
if (!File.Exists(perfFilePath))
{
TimeWindow[] timeWindows = LookupTimeWindowDefinition(perfName);
perfFiles[perfName] = NewPerfFile.Create(perfFilePath, timeWindows);
}
else
{
perfFiles[perfName] = new NewPerfFile(perfFilePath);
}
}
}
return perfFiles[perfName];
}
}
}