/// <exception cref="OnlyOneNonProcessedRegistryTakenException"> /// Too few registries obtained, processing is impossible (there's no possibility of determining /// end time of the activity represented by the only registry) /// </exception> /// <exception cref="NoNonProcessedRegistriesException"></exception> /// <exception cref="AllTakenRegistiesBeginActivityException">See description of the exception class</exception> public static RegistriesList GetRegistries(string connectionString, int quantityToTake) { using (var context = new MetricsDataContext(connectionString)) { var registries = context.Registries. Where(r => r.Processed.HasValue && !r.Processed.Value) .Take(quantityToTake) .OrderBy(r => r.Time) .ToList(); foreach (var registry in registries) { registry.IpAddress = context.IpAddresses.Single(ip => ip.Id == registry.Ip); registry.MacAddress = context.MacAddresses.Single(mac => mac.Id == registry.Mac); registry.Username1 = context.Usernames.Single(u => u.Id == registry.Username); } if (registries.Count == 1) { throw new OnlyOneNonProcessedRegistryTakenException(registries[0]); } if (registries.Count == 0) { throw new NoNonProcessedRegistriesException(); } return(MakeRegistriesList(registries)); } }
public static bool AnyNonTransmittedActivitiesRegistries(string connectionString) { using (var context = new MetricsDataContext(connectionString)) { return(context.ActivitiesRegistries.Any(r => !r.Transmitted.Value)); } }
public static ActivitiesRegistry GetFirstNonTransmittedActivitiesRegistry(string connectionString) { using (var context = new MetricsDataContext(connectionString)) { return(context.ActivitiesRegistries.FirstOrDefault(r => !r.Transmitted.Value)); } }
private void OnDataSaving() { using (var context = new MetricsDataContext(_connectionString)) { for (int i = 0; i < _report.Count; i++) { var existingUser = context.Usernames.FirstOrDefault(u => u.Value == _report[i].Username1.Value); if (existingUser != null) { _report[i].Username1 = existingUser; } var existingIp = context.IpAddresses.FirstOrDefault(ip => ip.Value == _report[i].IpAddress.Value); if (existingIp != null) { _report[i].IpAddress = existingIp; } var existingMac = context.MacAddresses.FirstOrDefault(m => m.Value == _report[i].MacAddress.Value); if (existingMac != null) { _report[i].MacAddress = existingMac; } context.Registries.InsertOnSubmit(_report[i]); } context.SubmitChanges(); // TODO transaction _report.Clear(); } }
private static bool DatabaseExists() { using (MetricsDataContext context = new MetricsDataContext(ConfigHelper.GetConnectionString(ConfigFileName, ConnectionName))) { return(context.DatabaseExists()); } }
private static bool AnyRecordsAfter(DateTime d) { using ( MetricsDataContext context = new MetricsDataContext(ConfigHelper.GetConnectionString(ConfigFileName, ConnectionName))) { return(context.Registries.Any(r => r.Time > d)); } }
public static void DeleteProcessedRegistries(string connectionString) { using (var context = new MetricsDataContext(connectionString)) { var registriesToDelete = context.Registries.Where(r => r.Processed.HasValue && r.Processed.Value); context.Registries.DeleteAllOnSubmit(registriesToDelete); context.SubmitChanges(); } }
private static bool AllRecordsAfterOf(DateTime d, CollectionEvent @event) { using ( MetricsDataContext context = new MetricsDataContext(ConfigHelper.GetConnectionString(ConfigFileName, ConnectionName))) { return(!context.Registries.Any(r => r.Time > d && r.Event != (uint)@event)); } }
public static void MarkAsTransmitted(string connectionString, ActivitiesRegistry transmittedRegistry) { using (var context = new MetricsDataContext(connectionString)) { var dbRegistry = context.ActivitiesRegistries.Single(r => r.Id == transmittedRegistry.Id); dbRegistry.Transmitted = true; context.SubmitChanges(); } }
/// <summary> /// Marks 'processed' field with NULL, so that the given registry won't be obtainable /// but also isn't marked as a processed. /// Usings: 1. If only one registry is obtained, there's no more registries in db - cannot be processed /// 2. Filter ban /// </summary> public static void SetProcessedToNull(string connectionString, Registry registry) { using (var context = new MetricsDataContext(connectionString)) { var reg = context.Registries.First(r => r.Id == registry.Id); reg.Processed = null; context.SubmitChanges(); } }
public static void MarkAsProcessed(string connectionString, List <Registry> registries) { using (var context = new MetricsDataContext(connectionString)) { List <Registry> contextRegistries = new List <Registry>(); registries.ForEach(r => contextRegistries.Add(context.Registries.Single(rg => rg.Id == r.Id))); contextRegistries.ForEach(r => r.Processed = true); context.SubmitChanges(); } }
public MonitoringServiceDockerSocket(int removeAfterHours , MetricsDataContext metricsDataContext , ILogger <MonitoringServiceDockerSocket> logger) { _metricsDataContext = metricsDataContext; _logger = logger; _removeAfterHours = removeAfterHours; _dockerClient = new DockerClientConfiguration(new Uri("unix://var/run/docker.sock")).CreateClient(); }
public MonitoringServiceDockerEngineAPI(int removeAfterHours , MetricsDataContext metricsDataContext , ILogger <MonitoringServiceDockerEngineAPI> logger , IDockerEngineAPIClient apiClient) { _apiClient = apiClient; _metricsDataContext = metricsDataContext; _logger = logger; _removeAfterHours = removeAfterHours; }
public static void StoreJsonInActivitiesRegistry(string connectionString, string json) { using (var context = new MetricsDataContext(connectionString)) { context.ActivitiesRegistries.InsertOnSubmit(new ActivitiesRegistry() { Json = json, Transmitted = false }); context.SubmitChanges(); } }
public static void MarkAllAsNonProcessed(string connectionString) { using (var context = new MetricsDataContext(connectionString)) { var registries = context.Registries.ToArray(); for (int i = 0; i < registries.Count(); i++) { registries[i].Processed = false; } context.SubmitChanges(); } }
private static void DeleteDatabase() { using ( MetricsDataContext context = new MetricsDataContext(ConfigHelper.GetConnectionString(ConfigFileName, ConnectionName))) { if (context.DatabaseExists()) { context.DeleteDatabase(); context.SubmitChanges(); } } }
private static DateTime GetLastRecordTime() { using ( MetricsDataContext context = new MetricsDataContext(ConfigHelper.GetConnectionString(ConfigFileName, ConnectionName))) { if (context.DatabaseExists()) { var x = context.Registries.OrderByDescending(r => r.Time).AsEnumerable().FirstOrDefault(); return(x?.Time ?? MinDate); } return(MinDate); } }
public void CreateDatabaseIfNotExists() { DatabaseMutex.WaitOne(); using (var context = new MetricsDataContext(_connectionString)) { if (!context.DatabaseExists()) { context.CreateDatabase(); context.SubmitChanges(); } } DatabaseMutex.ReleaseMutex(); }
public static void MarkRegistriesAsProcessed(string connectionString, IEnumerable <long> ids) { var enumerable = ids as long[] ?? ids.ToArray(); if (!enumerable.Any()) { return; } using (var context = new MetricsDataContext(connectionString)) { var registriesToMark = context.Registries.Where(r => enumerable.Contains(r.Id)).ToArray(); for (int i = 0; i < registriesToMark.Length; i++) { registriesToMark[i].Processed = true; } context.SubmitChanges(); } }
/// <summary> /// Checks if there any more registries in db that stored after given time /// </summary> public static bool AnyMoreRegistriesExist(string connectionString, DateTime after) { using (var context = new MetricsDataContext(connectionString)) return(context.Registries.Any(r => r.Time > after)); }
public static RegistriesList GetRegistries(string connectionString) { using (var context = new MetricsDataContext(connectionString)) { var registries = context.Registries. Where(r => r.Processed.HasValue && !r.Processed.Value) .OrderBy(r => r.Time) .ToList(); if (registries.Any()) { Dictionary <long, string> ips = new Dictionary <long, string>(); Dictionary <long, string> macs = new Dictionary <long, string>(); Dictionary <long, string> usernames = new Dictionary <long, string>(); foreach (var registry in registries) { if (ips.ContainsKey(registry.Ip.Value)) { registry.IpAddress = new IpAddress() { Value = ips[registry.Ip.Value] } } ; else { registry.IpAddress = context.IpAddresses.Single(ip => ip.Id == registry.Ip); ips.Add(registry.Ip.Value, registry.IpAddress.Value); } if (macs.ContainsKey(registry.Mac.Value)) { registry.MacAddress = new MacAddress() { Value = macs[registry.Mac.Value] } } ; else { registry.MacAddress = context.MacAddresses.Single(mac => mac.Id == registry.Mac); macs.Add(registry.Mac.Value, registry.MacAddress.Value); } if (usernames.ContainsKey(registry.Username.Value)) { registry.Username1 = new Username() { Value = usernames[registry.Username.Value] } } ; else { registry.Username1 = context.Usernames.Single(u => u.Id == registry.Username); usernames.Add(registry.Username.Value, registry.Username1.Value); } } return(MakeRegistriesList(registries)); } return(null); } }