diff --git a/DiscImageChef.Core/Statistics.cs b/DiscImageChef.Core/Statistics.cs index 56c930f2d..e1aec5a99 100644 --- a/DiscImageChef.Core/Statistics.cs +++ b/DiscImageChef.Core/Statistics.cs @@ -51,30 +51,23 @@ using Version = DiscImageChef.Database.Models.Version; namespace DiscImageChef.Core { - /// - /// Handles anonymous usage statistics - /// + /// Handles anonymous usage statistics public static class Statistics { - /// - /// Statistics file semaphore - /// + /// Statistics file semaphore static bool submitStatsLock; - /// - /// Loads saved statistics from disk - /// + /// Loads saved statistics from disk public static void LoadStats() { - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); if(File.Exists(Path.Combine(Settings.Settings.StatsPath, "Statistics.xml"))) try { - Stats allStats = new Stats(); - XmlSerializer xs = new XmlSerializer(allStats.GetType()); - StreamReader sr = - new StreamReader(Path.Combine(Settings.Settings.StatsPath, "Statistics.xml")); + var allStats = new Stats(); + var xs = new XmlSerializer(allStats.GetType()); + var sr = new StreamReader(Path.Combine(Settings.Settings.StatsPath, "Statistics.xml")); allStats = (Stats)xs.Deserialize(sr); sr.Close(); @@ -83,7 +76,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Analyze > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "analyze" && c.Synchronized) ?? - new Command {Name = "analyze", Synchronized = true}; + new Command + { + Name = "analyze", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Analyze; ctx.Commands.Update(command); @@ -92,8 +88,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Benchmark > 0) { Command command = - ctx.Commands.FirstOrDefault(c => c.Name == "benchmark" && c.Synchronized) ?? - new Command {Name = "benchmark", Synchronized = true}; + ctx.Commands.FirstOrDefault(c => c.Name == "benchmark" && c.Synchronized) ?? new Command + { + Name = "benchmark", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Benchmark; ctx.Commands.Update(command); @@ -102,8 +100,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Checksum > 0) { Command command = - ctx.Commands.FirstOrDefault(c => c.Name == "checksum" && c.Synchronized) ?? - new Command {Name = "checksum", Synchronized = true}; + ctx.Commands.FirstOrDefault(c => c.Name == "checksum" && c.Synchronized) ?? new Command + { + Name = "checksum", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Checksum; ctx.Commands.Update(command); @@ -112,7 +112,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Compare > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "compare" && c.Synchronized) ?? - new Command {Name = "compare", Synchronized = true}; + new Command + { + Name = "compare", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Compare; ctx.Commands.Update(command); @@ -122,7 +125,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "convert-image" && c.Synchronized) ?? - new Command {Name = "convert-image", Synchronized = true}; + new Command + { + Name = "convert-image", Synchronized = true + }; command.Count += (ulong)allStats.Commands.ConvertImage; ctx.Commands.Update(command); @@ -132,7 +138,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "create-sidecar" && c.Synchronized) ?? - new Command {Name = "create-sidecar", Synchronized = true}; + new Command + { + Name = "create-sidecar", Synchronized = true + }; command.Count += (ulong)allStats.Commands.CreateSidecar; ctx.Commands.Update(command); @@ -141,7 +150,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Decode > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "decode" && c.Synchronized) ?? - new Command {Name = "decode", Synchronized = true}; + new Command + { + Name = "decode", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Decode; ctx.Commands.Update(command); @@ -151,7 +163,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "device-info" && c.Synchronized) ?? - new Command {Name = "device-info", Synchronized = true}; + new Command + { + Name = "device-info", Synchronized = true + }; command.Count += (ulong)allStats.Commands.DeviceInfo; ctx.Commands.Update(command); @@ -161,7 +176,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "device-report" && c.Synchronized) ?? - new Command {Name = "device-report", Synchronized = true}; + new Command + { + Name = "device-report", Synchronized = true + }; command.Count += (ulong)allStats.Commands.DeviceReport; ctx.Commands.Update(command); @@ -171,7 +189,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "dump-media" && c.Synchronized) ?? - new Command {Name = "dump-media", Synchronized = true}; + new Command + { + Name = "dump-media", Synchronized = true + }; command.Count += (ulong)allStats.Commands.DumpMedia; ctx.Commands.Update(command); @@ -180,7 +201,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Entropy > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "entropy" && c.Synchronized) ?? - new Command {Name = "entropy", Synchronized = true}; + new Command + { + Name = "entropy", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Entropy; ctx.Commands.Update(command); @@ -190,7 +214,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "extract-files" && c.Synchronized) ?? - new Command {Name = "extract-files", Synchronized = true}; + new Command + { + Name = "extract-files", Synchronized = true + }; command.Count += (ulong)allStats.Commands.ExtractFiles; ctx.Commands.Update(command); @@ -199,7 +226,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Formats > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "formats" && c.Synchronized) ?? - new Command {Name = "formats", Synchronized = true}; + new Command + { + Name = "formats", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Formats; ctx.Commands.Update(command); @@ -209,7 +239,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "image-info" && c.Synchronized) ?? - new Command {Name = "image-info", Synchronized = true}; + new Command + { + Name = "image-info", Synchronized = true + }; command.Count += (ulong)allStats.Commands.ImageInfo; ctx.Commands.Update(command); @@ -219,7 +252,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "list-devices" && c.Synchronized) ?? - new Command {Name = "list-devices", Synchronized = true}; + new Command + { + Name = "list-devices", Synchronized = true + }; command.Count += (ulong)allStats.Commands.ListDevices; ctx.Commands.Update(command); @@ -229,7 +265,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "list-encodings" && c.Synchronized) ?? - new Command {Name = "list-encodings", Synchronized = true}; + new Command + { + Name = "list-encodings", Synchronized = true + }; command.Count += (ulong)allStats.Commands.ListEncodings; ctx.Commands.Update(command); @@ -238,7 +277,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Ls > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "ls" && c.Synchronized) ?? - new Command {Name = "ls", Synchronized = true}; + new Command + { + Name = "ls", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Ls; ctx.Commands.Update(command); @@ -248,7 +290,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "media-info" && c.Synchronized) ?? - new Command {Name = "media-info", Synchronized = true}; + new Command + { + Name = "media-info", Synchronized = true + }; command.Count += (ulong)allStats.Commands.MediaInfo; ctx.Commands.Update(command); @@ -258,7 +303,10 @@ namespace DiscImageChef.Core { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "media-scan" && c.Synchronized) ?? - new Command {Name = "media-scan", Synchronized = true}; + new Command + { + Name = "media-scan", Synchronized = true + }; command.Count += (ulong)allStats.Commands.MediaScan; ctx.Commands.Update(command); @@ -267,8 +315,10 @@ namespace DiscImageChef.Core if(allStats.Commands.PrintHex > 0) { Command command = - ctx.Commands.FirstOrDefault(c => c.Name == "printhex" && c.Synchronized) ?? - new Command {Name = "printhex", Synchronized = true}; + ctx.Commands.FirstOrDefault(c => c.Name == "printhex" && c.Synchronized) ?? new Command + { + Name = "printhex", Synchronized = true + }; command.Count += (ulong)allStats.Commands.PrintHex; ctx.Commands.Update(command); @@ -277,7 +327,10 @@ namespace DiscImageChef.Core if(allStats.Commands.Verify > 0) { Command command = ctx.Commands.FirstOrDefault(c => c.Name == "verify" && c.Synchronized) ?? - new Command {Name = "verify", Synchronized = true}; + new Command + { + Name = "verify", Synchronized = true + }; command.Count += (ulong)allStats.Commands.Verify; ctx.Commands.Update(command); @@ -288,16 +341,15 @@ namespace DiscImageChef.Core foreach(OsStats operatingSystem in allStats.OperatingSystems) { if(string.IsNullOrWhiteSpace(operatingSystem.name) || - string.IsNullOrWhiteSpace(operatingSystem.version)) continue; + string.IsNullOrWhiteSpace(operatingSystem.version)) + continue; OperatingSystem existing = ctx.OperatingSystems.FirstOrDefault(c => c.Name == operatingSystem.name && c.Version == operatingSystem.version && c.Synchronized) ?? new OperatingSystem { - Name = operatingSystem.name, - Version = operatingSystem.version, - Synchronized = true + Name = operatingSystem.name, Version = operatingSystem.version, Synchronized = true }; existing.Count += (ulong)operatingSystem.Value; @@ -307,11 +359,14 @@ namespace DiscImageChef.Core if(allStats.Versions != null) foreach(NameValueStats nvs in allStats.Versions) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; - Version existing = - ctx.Versions.FirstOrDefault(c => c.Value == nvs.name && c.Synchronized) ?? - new Version {Value = nvs.name, Synchronized = true}; + Version existing = ctx.Versions.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? + new Version + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.Versions.Update(existing); @@ -320,11 +375,15 @@ namespace DiscImageChef.Core if(allStats.Filesystems != null) foreach(NameValueStats nvs in allStats.Filesystems) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; Filesystem existing = ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? - new Filesystem {Name = nvs.name, Synchronized = true}; + new Filesystem + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.Filesystems.Update(existing); @@ -333,11 +392,15 @@ namespace DiscImageChef.Core if(allStats.Partitions != null) foreach(NameValueStats nvs in allStats.Partitions) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; Partition existing = ctx.Partitions.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? - new Partition {Name = nvs.name, Synchronized = true}; + new Partition + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.Partitions.Update(existing); @@ -346,11 +409,15 @@ namespace DiscImageChef.Core if(allStats.Filesystems != null) foreach(NameValueStats nvs in allStats.Filesystems) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; Filesystem existing = ctx.Filesystems.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? - new Filesystem {Name = nvs.name, Synchronized = true}; + new Filesystem + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.Filesystems.Update(existing); @@ -359,11 +426,15 @@ namespace DiscImageChef.Core if(allStats.MediaImages != null) foreach(NameValueStats nvs in allStats.MediaImages) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; MediaFormat existing = ctx.MediaFormats.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? - new MediaFormat {Name = nvs.name, Synchronized = true}; + new MediaFormat + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.MediaFormats.Update(existing); @@ -372,10 +443,14 @@ namespace DiscImageChef.Core if(allStats.Filters != null) foreach(NameValueStats nvs in allStats.Filters) { - if(string.IsNullOrWhiteSpace(nvs.name)) continue; + if(string.IsNullOrWhiteSpace(nvs.name)) + continue; Filter existing = ctx.Filters.FirstOrDefault(c => c.Name == nvs.name && c.Synchronized) ?? - new Filter {Name = nvs.name, Synchronized = true}; + new Filter + { + Name = nvs.name, Synchronized = true + }; existing.Count += (ulong)nvs.Value; ctx.Filters.Update(existing); @@ -387,22 +462,21 @@ namespace DiscImageChef.Core if(ctx.SeenDevices.Any(d => d.Manufacturer == device.Manufacturer && d.Model == device.Model && d.Revision == device.Revision && - d.Bus == device.Bus)) continue; + d.Bus == device.Bus)) + continue; ctx.SeenDevices.Add(new DeviceStat { - Bus = device.Bus, - Manufacturer = device.Manufacturer, - Model = device.Model, - Revision = device.Revision, - Synchronized = true + Bus = device.Bus, Manufacturer = device.Manufacturer, Model = device.Model, + Revision = device.Revision, Synchronized = true }); } if(allStats.Medias != null) foreach(MediaStats media in allStats.Medias) { - if(string.IsNullOrWhiteSpace(media.type)) continue; + if(string.IsNullOrWhiteSpace(media.type)) + continue; Database.Models.Media existing = ctx.Medias.FirstOrDefault(c => c.Type == media.type && c.Real == media.real && @@ -423,154 +497,152 @@ namespace DiscImageChef.Core // Do not care about it } - if(Settings.Settings.Current.Stats == null) return; + if(Settings.Settings.Current.Stats == null) + return; ctx.OperatingSystems.Add(new OperatingSystem { - Name = DetectOS.GetRealPlatformID().ToString(), - Synchronized = false, - Version = DetectOS.GetVersion(), - Count = 1 + Name = DetectOS.GetRealPlatformID().ToString(), Synchronized = false, Version = DetectOS.GetVersion(), + Count = 1 }); ctx.Versions.Add(new Version { - Value = CommonTypes.Interop.Version.GetVersion(), Synchronized = false, Count = 1 + Name = CommonTypes.Interop.Version.GetVersion(), Synchronized = false, Count = 1 }); + ctx.SaveChanges(); } - /// - /// Saves statistics to disk - /// + /// Saves statistics to disk public static void SaveStats() { - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); ctx.SaveChanges(); - if(Settings.Settings.Current.Stats != null && Settings.Settings.Current.Stats.ShareStats) SubmitStats(); + + if(Settings.Settings.Current.Stats != null && + Settings.Settings.Current.Stats.ShareStats) + SubmitStats(); } - /// - /// Submits statistics to DiscImageChef.Server - /// + /// Submits statistics to DiscImageChef.Server public static void SubmitStats() { - Thread submitThread = new Thread(() => + var submitThread = new Thread(() => { - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + try { - if(submitStatsLock) return; + if(submitStatsLock) + return; submitStatsLock = true; - if(ctx.Commands.Any(c => !c.Synchronized) || ctx.Filesystems.Any(c => !c.Synchronized) || - ctx.Filters.Any(c => !c.Synchronized) || ctx.MediaFormats.Any(c => !c.Synchronized) || - ctx.Partitions.Any(c => !c.Synchronized) || ctx.Medias.Any(c => !c.Synchronized) || - ctx.SeenDevices.Any(c => !c.Synchronized) || ctx.OperatingSystems.Any(c => !c.Synchronized) || + if(ctx.Commands.Any(c => !c.Synchronized) || + ctx.Filesystems.Any(c => !c.Synchronized) || + ctx.Filters.Any(c => !c.Synchronized) || + ctx.MediaFormats.Any(c => !c.Synchronized) || + ctx.Partitions.Any(c => !c.Synchronized) || + ctx.Medias.Any(c => !c.Synchronized) || + ctx.SeenDevices.Any(c => !c.Synchronized) || + ctx.OperatingSystems.Any(c => !c.Synchronized) || ctx.Versions.Any(c => !c.Synchronized)) { - StatsDto dto = new StatsDto(); + var dto = new StatsDto(); if(ctx.Commands.Any(c => !c.Synchronized)) { dto.Commands = new List(); - foreach(string nvs in - ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) + + foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) dto.Commands.Add(new NameValueStats { - name = nvs, - Value = ctx.Commands.LongCount(c => !c.Synchronized && - c.Name == nvs) + name = nvs, Value = ctx.Commands.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.Filesystems.Any(c => !c.Synchronized)) { dto.Filesystems = new List(); - foreach(string nvs in - ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) + + foreach(string nvs in ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) dto.Filesystems.Add(new NameValueStats { - name = nvs, - Value = ctx.Filesystems.LongCount(c => !c.Synchronized && - c.Name == nvs) + name = nvs, Value = ctx.Filesystems.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.Filters.Any(c => !c.Synchronized)) { dto.Filters = new List(); + foreach(string nvs in ctx.Filters.Where(c => !c.Synchronized).Select(c => c.Name).Distinct() ) dto.Filters.Add(new NameValueStats { - name = nvs, - Value = ctx.Filters.LongCount(c => !c.Synchronized && - c.Name == nvs) + name = nvs, Value = ctx.Filters.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.MediaFormats.Any(c => !c.Synchronized)) { dto.MediaFormats = new List(); - foreach(string nvs in - ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) + + foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) dto.MediaFormats.Add(new NameValueStats { - name = nvs, - Value = ctx.MediaFormats.LongCount(c => !c.Synchronized && - c.Name == nvs) + name = nvs, + Value = ctx.MediaFormats.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.Partitions.Any(c => !c.Synchronized)) { dto.Partitions = new List(); - foreach(string nvs in - ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name).Distinct()) + + foreach(string nvs in ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) dto.Partitions.Add(new NameValueStats { - name = nvs, - Value = ctx.Partitions.LongCount(c => !c.Synchronized && - c.Name == nvs) + name = nvs, Value = ctx.Partitions.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.Versions.Any(c => !c.Synchronized)) { dto.Versions = new List(); - foreach(string nvs in - ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Value).Distinct()) + + foreach(string nvs in ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) dto.Versions.Add(new NameValueStats { - name = nvs, - Value = ctx.Versions.LongCount(c => !c.Synchronized && - c.Value == nvs) + name = nvs, Value = ctx.Versions.LongCount(c => !c.Synchronized && c.Name == nvs) }); } if(ctx.Medias.Any(c => !c.Synchronized)) { dto.Medias = new List(); - foreach(string media in ctx.Medias.Where(c => !c.Synchronized).Select(c => c.Type) - .Distinct()) + + foreach(string media in ctx.Medias.Where(c => !c.Synchronized).Select(c => c.Type). + Distinct()) { if(ctx.Medias.Any(c => !c.Synchronized && c.Type == media && c.Real)) dto.Medias.Add(new MediaStats { - real = true, - type = media, - Value = ctx.Medias.LongCount(c => !c.Synchronized && - c.Type == media && c.Real) + real = true, type = media, + Value = ctx.Medias.LongCount(c => !c.Synchronized && c.Type == media && c.Real) }); + if(ctx.Medias.Any(c => !c.Synchronized && c.Type == media && !c.Real)) dto.Medias.Add(new MediaStats { - real = false, - type = media, - Value = ctx.Medias.LongCount(c => !c.Synchronized && - c.Type == media && !c.Real) + real = false, type = media, + Value = ctx.Medias.LongCount(c => !c.Synchronized && c.Type == media && !c.Real) }); } } @@ -578,13 +650,12 @@ namespace DiscImageChef.Core if(ctx.SeenDevices.Any(c => !c.Synchronized)) { dto.Devices = new List(); + foreach(DeviceStat device in ctx.SeenDevices.Where(c => !c.Synchronized)) dto.Devices.Add(new DeviceStats { - Bus = device.Bus, - Manufacturer = device.Manufacturer, - ManufacturerSpecified = !(device.Manufacturer is null), - Model = device.Model, + Bus = device.Bus, Manufacturer = device.Manufacturer, + ManufacturerSpecified = !(device.Manufacturer is null), Model = device.Model, Revision = device.Revision }); } @@ -592,70 +663,74 @@ namespace DiscImageChef.Core if(ctx.OperatingSystems.Any(c => !c.Synchronized)) { dto.OperatingSystems = new List(); - foreach(string osName in ctx.OperatingSystems.Where(c => !c.Synchronized) - .Select(c => c.Name).Distinct()) + + foreach(string osName in ctx. + OperatingSystems.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { - foreach(string osVersion in ctx - .OperatingSystems - .Where(c => !c.Synchronized && c.Name == osName) - .Select(c => c.Version).Distinct()) + foreach(string osVersion in ctx. + OperatingSystems. + Where(c => !c.Synchronized && c.Name == osName). + Select(c => c.Version).Distinct()) dto.OperatingSystems.Add(new OsStats { - name = osName, - version = osVersion, - Value = - ctx.OperatingSystems.LongCount(c => - !c - .Synchronized && - c.Name == - osName && - c.Version == - osVersion) + name = osName, version = osVersion, + Value = ctx.OperatingSystems.LongCount(c => !c.Synchronized && + c.Name == osName && + c.Version == osVersion) }); } } - #if DEBUG + #if DEBUG System.Console.WriteLine("Uploading statistics"); - #else + #else DiscImageChef.Console.DicConsole.DebugWriteLine("Submit stats", "Uploading statistics"); - #endif + #endif + + string json = JsonConvert.SerializeObject(dto, Formatting.Indented, new JsonSerializerSettings + { + NullValueHandling = NullValueHandling.Ignore + }); + + byte[] jsonBytes = Encoding.UTF8.GetBytes(json); + var request = WebRequest.Create("https://www.discimagechef.app/api/uploadstatsv2"); - string json = JsonConvert.SerializeObject(dto, Formatting.Indented, - new JsonSerializerSettings - { - NullValueHandling = NullValueHandling.Ignore - }); - byte[] jsonBytes = Encoding.UTF8.GetBytes(json); - WebRequest request = WebRequest.Create("https://www.discimagechef.app/api/uploadstatsv2"); ((HttpWebRequest)request).UserAgent = $"DiscImageChef {typeof(Version).Assembly.GetName().Version}"; + request.Method = "POST"; request.ContentLength = jsonBytes.Length; request.ContentType = "application/json"; Stream reqStream = request.GetRequestStream(); reqStream.Write(jsonBytes, 0, jsonBytes.Length); + //jsonStream.CopyTo(reqStream); reqStream.Close(); WebResponse response = request.GetResponse(); - if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) return; + if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) + return; - Stream data = response.GetResponseStream(); - StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException()); + Stream data = response.GetResponseStream(); + var reader = new StreamReader(data ?? throw new InvalidOperationException()); string result = reader.ReadToEnd(); data.Close(); response.Close(); - if(result != "ok") return; + if(result != "ok") + return; if(ctx.Commands.Any(c => !c.Synchronized)) - foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name) - .Distinct()) + foreach(string nvs in ctx.Commands.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { Command existing = ctx.Commands.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? - new Command {Name = nvs, Synchronized = true}; + new Command + { + Name = nvs, Synchronized = true + }; existing.Count += (ulong)ctx.Commands.LongCount(c => !c.Synchronized && c.Name == nvs); ctx.Commands.Update(existing); @@ -663,16 +738,21 @@ namespace DiscImageChef.Core } if(ctx.Filesystems.Any(c => !c.Synchronized)) - foreach(string nvs in ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name) - .Distinct()) + foreach(string nvs in ctx.Filesystems.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { Filesystem existing = ctx.Filesystems.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? - new Filesystem {Name = nvs, Synchronized = true}; + new Filesystem + { + Name = nvs, Synchronized = true + }; existing.Count += (ulong)ctx.Filesystems.LongCount(c => !c.Synchronized && c.Name == nvs); + ctx.Filesystems.Update(existing); + ctx.Filesystems.RemoveRange(ctx.Filesystems.Where(c => !c.Synchronized && c.Name == nvs)); } @@ -682,7 +762,10 @@ namespace DiscImageChef.Core ) { Filter existing = ctx.Filters.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? - new Filter {Name = nvs, Synchronized = true}; + new Filter + { + Name = nvs, Synchronized = true + }; existing.Count += (ulong)ctx.Filters.LongCount(c => !c.Synchronized && c.Name == nvs); ctx.Filters.Update(existing); @@ -690,73 +773,95 @@ namespace DiscImageChef.Core } if(ctx.MediaFormats.Any(c => !c.Synchronized)) - foreach(string nvs in ctx.MediaFormats.Where(c => !c.Synchronized).Select(c => c.Name) - .Distinct()) + foreach(string nvs in ctx.MediaFormats.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { MediaFormat existing = ctx.MediaFormats.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? - new MediaFormat {Name = nvs, Synchronized = true}; + new MediaFormat + { + Name = nvs, Synchronized = true + }; existing.Count += (ulong)ctx.MediaFormats.LongCount(c => !c.Synchronized && c.Name == nvs); + ctx.MediaFormats.Update(existing); + ctx.MediaFormats.RemoveRange(ctx.MediaFormats.Where(c => !c.Synchronized && c.Name == nvs)); } if(ctx.Partitions.Any(c => !c.Synchronized)) - foreach(string nvs in ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name) - .Distinct()) + foreach(string nvs in ctx.Partitions.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { Partition existing = - ctx.Partitions.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? - new Partition {Name = nvs, Synchronized = true}; + ctx.Partitions.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? new Partition + { + Name = nvs, Synchronized = true + }; existing.Count += (ulong)ctx.Partitions.LongCount(c => !c.Synchronized && c.Name == nvs); + ctx.Partitions.Update(existing); ctx.Partitions.RemoveRange(ctx.Partitions.Where(c => !c.Synchronized && c.Name == nvs)); } if(ctx.Versions.Any(c => !c.Synchronized)) - foreach(string nvs in ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Value) - .Distinct()) + foreach(string nvs in ctx.Versions.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { - Version existing = ctx.Versions.FirstOrDefault(c => c.Synchronized && c.Value == nvs) ?? - new Version {Value = nvs, Synchronized = true}; + Version existing = ctx.Versions.FirstOrDefault(c => c.Synchronized && c.Name == nvs) ?? + new Version + { + Name = nvs, Synchronized = true + }; - existing.Count += (ulong)ctx.Versions.LongCount(c => !c.Synchronized && c.Value == nvs); + existing.Count += (ulong)ctx.Versions.LongCount(c => !c.Synchronized && c.Name == nvs); ctx.Versions.Update(existing); - ctx.Versions.RemoveRange(ctx.Versions.Where(c => !c.Synchronized && c.Value == nvs)); + ctx.Versions.RemoveRange(ctx.Versions.Where(c => !c.Synchronized && c.Name == nvs)); } if(ctx.Medias.Any(c => !c.Synchronized)) - foreach(string media in ctx.Medias.Where(c => !c.Synchronized).Select(c => c.Type) - .Distinct()) + foreach(string media in ctx.Medias.Where(c => !c.Synchronized).Select(c => c.Type). + Distinct()) { if(ctx.Medias.Any(c => !c.Synchronized && c.Type == media && c.Real)) { Database.Models.Media existing = ctx.Medias.FirstOrDefault(c => c.Synchronized && c.Type == media && c.Real) ?? - new Database.Models.Media {Synchronized = true, Type = media, Real = true}; + new Database.Models.Media + { + Synchronized = true, Type = media, Real = true + }; existing.Count += (ulong)ctx.Medias.LongCount(c => !c.Synchronized && c.Type == media && c.Real); + ctx.Medias.Update(existing); + ctx.Medias.RemoveRange(ctx.Medias.Where(c => !c.Synchronized && c.Type == media && c.Real)); } - if(!ctx.Medias.Any(c => !c.Synchronized && c.Type == media && !c.Real)) continue; + if(!ctx.Medias.Any(c => !c.Synchronized && c.Type == media && !c.Real)) + continue; { Database.Models.Media existing = ctx.Medias.FirstOrDefault(c => c.Synchronized && c.Type == media && !c.Real) ?? - new Database.Models.Media {Synchronized = true, Type = media, Real = false}; + new Database.Models.Media + { + Synchronized = true, Type = media, Real = false + }; existing.Count += (ulong)ctx.Medias.LongCount(c => !c.Synchronized && c.Type == media && !c.Real); + ctx.Medias.Update(existing); + ctx.Medias.RemoveRange(ctx.Medias.Where(c => !c.Synchronized && c.Type == media && !c.Real)); } @@ -770,25 +875,31 @@ namespace DiscImageChef.Core } if(ctx.OperatingSystems.Any(c => !c.Synchronized)) - foreach(string osName in ctx.OperatingSystems.Where(c => !c.Synchronized) - .Select(c => c.Name).Distinct()) + foreach(string osName in ctx. + OperatingSystems.Where(c => !c.Synchronized).Select(c => c.Name). + Distinct()) { - foreach(string osVersion in ctx - .OperatingSystems - .Where(c => !c.Synchronized && c.Name == osName) - .Select(c => c.Version).Distinct()) + foreach(string osVersion in ctx. + OperatingSystems. + Where(c => !c.Synchronized && c.Name == osName). + Select(c => c.Version).Distinct()) { OperatingSystem existing = ctx.OperatingSystems.FirstOrDefault(c => c.Synchronized && c.Name == osName && c.Version == osVersion) ?? - new OperatingSystem {Synchronized = true, Version = osVersion, Name = osName}; + new OperatingSystem + { + Synchronized = true, Version = osVersion, Name = osName + }; existing.Count += (ulong)ctx.OperatingSystems.LongCount(c => !c.Synchronized && c.Name == osName && c.Version == osVersion); + ctx.OperatingSystems.Update(existing); + ctx.OperatingSystems.RemoveRange(ctx.OperatingSystems.Where(c => !c.Synchronized && c.Name == osName && c.Version == @@ -803,13 +914,16 @@ namespace DiscImageChef.Core { // Can't connect to the server, do nothing } + // ReSharper disable once RedundantCatchClause catch { - #if DEBUG + #if DEBUG submitStatsLock = false; - if(Debugger.IsAttached) throw; - #endif + + if(Debugger.IsAttached) + throw; + #endif } IEnumerable statsFiles = @@ -819,25 +933,28 @@ namespace DiscImageChef.Core foreach(string statsFile in statsFiles) try { - if(!File.Exists(statsFile)) continue; + if(!File.Exists(statsFile)) + continue; - Stats stats = new Stats(); + var stats = new Stats(); // This can execute before debug console has been inited - #if DEBUG + #if DEBUG System.Console.WriteLine("Uploading partial statistics file {0}", statsFile); - #else + #else DiscImageChef.Console.DicConsole.DebugWriteLine("Submit stats", "Uploading partial statistics file {0}", statsFile); - #endif + #endif - FileStream fs = new FileStream(statsFile, FileMode.Open, FileAccess.Read); - XmlSerializer xs = new XmlSerializer(stats.GetType()); + var fs = new FileStream(statsFile, FileMode.Open, FileAccess.Read); + var xs = new XmlSerializer(stats.GetType()); xs.Deserialize(fs); // Just to test validity of stats file fs.Seek(0, SeekOrigin.Begin); - WebRequest request = WebRequest.Create("https://www.discimagechef.app/api/uploadstats"); + var request = WebRequest.Create("https://www.discimagechef.app/api/uploadstats"); + ((HttpWebRequest)request).UserAgent = $"DiscImageChef {typeof(CommonTypes.Interop.Version).Assembly.GetName().Version}"; + request.Method = "POST"; request.ContentLength = fs.Length; request.ContentType = "application/xml"; @@ -846,16 +963,19 @@ namespace DiscImageChef.Core reqStream.Close(); WebResponse response = request.GetResponse(); - if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) return; + if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) + return; - Stream data = response.GetResponseStream(); - StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException()); + Stream data = response.GetResponseStream(); + var reader = new StreamReader(data ?? throw new InvalidOperationException()); string responseFromServer = reader.ReadToEnd(); data.Close(); response.Close(); fs.Close(); - if(responseFromServer == "ok") File.Delete(statsFile); + + if(responseFromServer == "ok") + File.Delete(statsFile); } catch(WebException) { @@ -864,133 +984,171 @@ namespace DiscImageChef.Core } catch { - #if DEBUG - if(!Debugger.IsAttached) continue; + #if DEBUG + if(!Debugger.IsAttached) + continue; submitStatsLock = false; + throw; - #endif + #endif } submitStatsLock = false; }); + submitThread.Start(); } - /// - /// Adds the execution of a command to statistics - /// + /// Adds the execution of a command to statistics /// Command public static void AddCommand(string command) { - if(string.IsNullOrWhiteSpace(command)) return; + if(string.IsNullOrWhiteSpace(command)) + return; - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.DeviceStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.DeviceStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + + ctx.Commands.Add(new Command + { + Name = command, Synchronized = false, Count = 1 + }); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); - ctx.Commands.Add(new Command {Name = command, Synchronized = false, Count = 1}); ctx.SaveChanges(); } - /// - /// Adds a new filesystem to statistics - /// + /// Adds a new filesystem to statistics /// Filesystem name public static void AddFilesystem(string filesystem) { - if(string.IsNullOrWhiteSpace(filesystem)) return; + if(string.IsNullOrWhiteSpace(filesystem)) + return; - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.FilesystemStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.FilesystemStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + + ctx.Filesystems.Add(new Filesystem + { + Name = filesystem, Synchronized = false, Count = 1 + }); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); - ctx.Filesystems.Add(new Filesystem {Name = filesystem, Synchronized = false, Count = 1}); ctx.SaveChanges(); } - /// - /// Adds a new partition scheme to statistics - /// + /// Adds a new partition scheme to statistics /// Partition scheme name internal static void AddPartition(string partition) { - if(string.IsNullOrWhiteSpace(partition)) return; + if(string.IsNullOrWhiteSpace(partition)) + return; - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.PartitionStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.PartitionStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + + ctx.Partitions.Add(new Partition + { + Name = partition, Synchronized = false, Count = 1 + }); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); - ctx.Partitions.Add(new Partition {Name = partition, Synchronized = false, Count = 1}); ctx.SaveChanges(); } - /// - /// Adds a new filter to statistics - /// + /// Adds a new filter to statistics /// Filter name public static void AddFilter(string filter) { - if(string.IsNullOrWhiteSpace(filter)) return; + if(string.IsNullOrWhiteSpace(filter)) + return; - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.FilterStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.FilterStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + + ctx.Filters.Add(new Filter + { + Name = filter, Synchronized = false, Count = 1 + }); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); - ctx.Filters.Add(new Filter {Name = filter, Synchronized = false, Count = 1}); ctx.SaveChanges(); } - /// - /// Ads a new media image to statistics - /// + /// Ads a new media image to statistics /// Media image name public static void AddMediaFormat(string format) { - if(string.IsNullOrWhiteSpace(format)) return; + if(string.IsNullOrWhiteSpace(format)) + return; - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.MediaImageStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.MediaImageStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + + ctx.MediaFormats.Add(new MediaFormat + { + Name = format, Synchronized = false, Count = 1 + }); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); - ctx.MediaFormats.Add(new MediaFormat {Name = format, Synchronized = false, Count = 1}); ctx.SaveChanges(); } - /// - /// Adds a new device to statistics - /// + /// Adds a new device to statistics /// Device public static void AddDevice(Device dev) { - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.DeviceStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.DeviceStats) + return; string deviceBus; - if(dev.IsUsb) deviceBus = "USB"; - else if(dev.IsFireWire) deviceBus = "FireWire"; - else deviceBus = dev.Type.ToString(); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); + if(dev.IsUsb) + deviceBus = "USB"; + else if(dev.IsFireWire) + deviceBus = "FireWire"; + else + deviceBus = dev.Type.ToString(); + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); + ctx.SeenDevices.Add(new DeviceStat { - Bus = deviceBus, - Manufacturer = dev.Manufacturer, - Model = dev.Model, - Revision = dev.Revision, + Bus = deviceBus, Manufacturer = dev.Manufacturer, Model = dev.Model, Revision = dev.Revision, Synchronized = false }); + ctx.SaveChanges(); } - /// - /// Adds a new media type to statistics - /// + /// Adds a new media type to statistics /// Media type /// Set if media was found on a real device, otherwise found on a media image public static void AddMedia(MediaType type, bool real) { - if(Settings.Settings.Current.Stats == null || !Settings.Settings.Current.Stats.MediaStats) return; + if(Settings.Settings.Current.Stats == null || + !Settings.Settings.Current.Stats.MediaStats) + return; + + var ctx = DicContext.Create(Settings.Settings.LocalDbPath); - DicContext ctx = DicContext.Create(Settings.Settings.LocalDbPath); ctx.Medias.Add(new Database.Models.Media { Real = real, Synchronized = false, Type = type.ToString(), Count = 1 }); + ctx.SaveChanges(); } }