118 lines
4.3 KiB
C#
118 lines
4.3 KiB
C#
using System;
|
|
using ln.application;
|
|
using ln.application.service;
|
|
using System.Threading;
|
|
using ln.skyscanner.perfvalue;
|
|
using ln.perfdb.storage;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
namespace ln.skyscanner.services
|
|
{
|
|
public class PerformanceValueService : ApplicationServiceBase
|
|
{
|
|
public String BasePath { get; private set; }
|
|
|
|
private Dictionary<string, PerfFile> perfFiles = new Dictionary<string, PerfFile>();
|
|
public PerfFile[] PerfFiles => perfFiles.Values.ToArray();
|
|
|
|
SkyScanner.Service coreService;
|
|
public RPC RPCInstance { get; private set; }
|
|
|
|
public PerformanceValueService()
|
|
:base("PerformanceValueService")
|
|
{
|
|
DependOnService<SkyScanner.Service>();
|
|
RPCInstance = new RPC(this);
|
|
}
|
|
|
|
public override void ServiceMain(IApplicationInterface applicationInterface)
|
|
{
|
|
coreService = Dependency<SkyScanner.Service>();
|
|
|
|
BasePath = Path.Combine(
|
|
CurrentApplicationInterface.Arguments["base-path"].Value,
|
|
"perfdb"
|
|
);
|
|
|
|
coreService.RPCContainer.Add("perfValues", RPCInstance);
|
|
|
|
Ready();
|
|
|
|
while (!StopRequested)
|
|
{
|
|
lock (this)
|
|
{
|
|
Monitor.Wait(this);
|
|
}
|
|
}
|
|
|
|
coreService.RPCContainer.Remove(RPCInstance);
|
|
coreService = null;
|
|
}
|
|
|
|
public PerfFile GetPerfFile(params string[] perfPath)
|
|
{
|
|
lock (this)
|
|
{
|
|
string perfName = string.Join("/", perfPath);
|
|
if (!perfFiles.ContainsKey(perfName))
|
|
{
|
|
String perfDirectory = Path.Combine(BasePath, Path.Combine(perfPath.Take(perfPath.Length - 1).ToArray()));
|
|
if (!Directory.Exists(perfDirectory))
|
|
Directory.CreateDirectory(perfDirectory);
|
|
|
|
String perfFileName = Path.Combine(perfDirectory, String.Format("{0}.perf", perfPath[perfPath.Length - 1]));
|
|
|
|
PerfFile perfFile = new PerfFile(perfFileName);
|
|
perfFile.Open();
|
|
|
|
if (perfFile.FirstSection == null)
|
|
{
|
|
PerfFile.PerfFileSection s1 = new PerfFile.PerfFileSection(perfFile, null, TimeSpan.FromDays(28), 60, AggregationMethod.AVERAGE);
|
|
PerfFile.PerfFileSection s2 = new PerfFile.PerfFileSection(perfFile, s1, TimeSpan.FromDays(56), 300, AggregationMethod.AVERAGE);
|
|
PerfFile.PerfFileSection s3 = new PerfFile.PerfFileSection(perfFile, s2, TimeSpan.FromDays(84), 900, AggregationMethod.AVERAGE);
|
|
PerfFile.PerfFileSection s4 = new PerfFile.PerfFileSection(perfFile, s3, TimeSpan.FromDays(168), 1800, AggregationMethod.AVERAGE);
|
|
PerfFile.PerfFileSection s5 = new PerfFile.PerfFileSection(perfFile, s4, TimeSpan.FromDays(750), 3600, AggregationMethod.AVERAGE);
|
|
}
|
|
perfFile.Close();
|
|
perfFiles.Add(perfName, perfFile);
|
|
}
|
|
return perfFiles[perfName];
|
|
}
|
|
}
|
|
|
|
public void WriteValue(PerformanceValue performanceValue,double value)
|
|
{
|
|
performanceValue.LastValue = value;
|
|
|
|
PerfFile perfFile = GetPerfFile(performanceValue.PerfPath);
|
|
lock (perfFile)
|
|
{
|
|
perfFile.EnsureOpen();
|
|
perfFile.Write(DateTimeOffset.Now.ToUnixTimeSeconds(), value);
|
|
perfFile.Close();
|
|
}
|
|
}
|
|
|
|
public class RPC
|
|
{
|
|
PerformanceValueService PerformanceValueService { get; }
|
|
public RPC(PerformanceValueService performanceValueService)
|
|
{
|
|
PerformanceValueService = performanceValueService;
|
|
}
|
|
|
|
public PerfValue[] GetPerfData(string[] perfPath,int timeWindow = 3600)
|
|
{
|
|
PerfFile perfFile = PerformanceValueService.GetPerfFile(perfPath);
|
|
perfFile.EnsureOpen();
|
|
PerfValue[] perfData = perfFile.QueryTime(timeWindow, 0);
|
|
return perfData;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|