internal static async Task ModelBlobExportAsync(IDrxStore store, string name) { var dir = Directory.CreateDirectory(name); var raw = store.GetDocuments(); var documents = from entry in raw orderby entry.Header.TimeStamp descending select entry; var count = 0; foreach (var document in documents) { if (document.Header.Encrypted || document.Header.SecurityLevel > DrxSecurityLevel.TopSecret) { continue; } await document.LoadBodyAsync(); var data = Encoding.UTF8.GetString(document.GetPlainTextBodyAsType(DrxBodyType.Markdown)).Replace(@"\", "\n"); data = Regex.Replace(data, @"^\s+$[\r\n]*", string.Empty, RegexOptions.Multiline); File.WriteAllText($"{dir.FullName}/{document.Id}", data); count++; } Console.WriteLine($"{count} documents exported to DL blob"); }
public static void PrintFlagAverages(IDrxStore store) { var flags = new Dictionary <Guid, int>(); foreach (var document in store.GetDocuments()) { foreach (var flag in document.Header.Flags) { if (!flags.ContainsKey(flag)) { flags[flag] = 0; } flags[flag] += 1; } } var sorted = from entry in flags orderby entry.Value descending select entry; foreach (var flag in sorted) { var resolved = store.ResolveFlag(flag.Key); if (resolved == null) { Console.WriteLine($"???? - {flag.Value}"); continue; } Console.WriteLine($"{resolved?.Tag} {resolved?.Name} - {flag.Value}"); } }
public static void PrintUnresolvableFlags(IDrxStore store) { foreach (var document in store.GetDocuments()) { foreach (var flag in document.Header.Flags) { var resolved = store.ResolveFlag(flag); if (resolved == null) { Console.WriteLine($"Warning: cannot resolve Flag ID {flag}"); continue; } } } }
public static void PrintDocuments(IDrxStore store, Func <DrxDocument, bool> predictate) { var raw = store.GetDocuments().Where(predictate); var documents = from entry in raw orderby entry.Header.TimeStamp descending select entry; var output = new List <DocumentOutputModel>(); foreach (var document in documents) { output.Add(DocumentOutputModel.From(document)); } ConsoleTable .From <DocumentOutputModel>(output) .Write(Format.Default); }
public static void PrintHighestCombinedVrel(IDrxStore store) { var list = new List <(DrxDocument, double)>(); foreach (var document in store.GetDocuments()) { var vrel = document.Header.Vrel; var total = vrel.Vividity + vrel.Remembrance + vrel.Emotion + vrel.Length; list.Add((document, total)); } foreach (var item in list.OrderByDescending(x => x.Item2).ToList()) { Console.WriteLine($"{item.Item2} {item.Item1.ToString()} {(item.Item1.Id)}"); } }
public static void PrintFlags(IDrxStore store, Func <DrxFlag, bool> predictate) { var raw = store.FlagDefinitions.Where(predictate); var flags = from entry in raw orderby entry.Tag ascending select entry; var documents = store.GetDocuments(); var output = new List <FlagOutputModel>(); foreach (var flag in flags) { var total = (from document in documents where document.Header.Flags.Contains(flag.Id) select document).Count(); output.Add(FlagOutputModel.From(flag, total)); } ConsoleTable .From <FlagOutputModel>(output) .Write(Format.Default); }
internal static async Task StaticExportAsync(IDrxStore store, string name) { var dir = Directory.CreateDirectory(name); // the index file var builder = new StringBuilder(); builder.AppendLine("# DRX Store Export"); builder.AppendLine($"Store ID: {store.Id}"); builder.AppendLine($"Export Date: {DateTime.Now.ToString()}"); builder.AppendLine(); var names = new Dictionary <string, int>(); var raw = store.GetDocuments(); var documents = from entry in raw orderby entry.Header.TimeStamp descending select entry; foreach (var document in documents) { var findFlags = from def in store.FlagDefinitions join flag in document.Header.Flags on def.Id equals flag select def.Tag; var flagNames = string.Join(", ", findFlags); int nameIdx = 0; var nameJoin = StringHelper.UrlFriendly(document.Header.Title.Replace(' ', '_')); if (names.ContainsKey(nameJoin)) { nameIdx = names[nameJoin]; nameIdx += 1; } names[nameJoin] = nameIdx; var fileName = nameIdx == 0 ? $"{nameJoin}.md" : $"{nameJoin}-{nameIdx}.md"; if (document.Header.SecurityLevel >= DrxSecurityLevel.Confidential || document.Header.Encrypted) { builder.AppendLine($"- {document.Header.Title} ({document.Header.SecurityLevel}) {flagNames} {document.Header.TimeStamp}"); continue; } else { builder.AppendLine($"- [{document.Header.Title}](index.html#!{fileName}) ({document.Header.SecurityLevel}) {flagNames} {document.Header.TimeStamp}"); } await document.LoadBodyAsync(); var docBuilder = new StringBuilder(); docBuilder.AppendLine($"# {document.Header.Title}"); docBuilder.AppendLine("[Back to Index](index.html#!index.md)"); docBuilder.AppendLine($"ID: {document.Id}"); docBuilder.AppendLine($"VREL: {document.Header.Vrel}"); docBuilder.AppendLine($"Flags: {flagNames}"); docBuilder.AppendLine($"Timestamp: {document.Header.TimeStamp}"); docBuilder.AppendLine($"Security Level: {document.Header.SecurityLevel}"); docBuilder.AppendLine(); docBuilder.Append(Encoding.UTF8.GetString(document.GetPlainTextBodyAsType(DrxBodyType.Markdown)).Replace(@"\", "\n")); File.WriteAllText($"{dir.FullName}/{fileName}", docBuilder.ToString()); } File.WriteAllText($"{dir.FullName}/index.md", builder.ToString()); //ZipFile.CreateFromDirectory(dir.FullName, "export.zip"); }
public static async void PrintSecurityViolations(IDrxStore store, bool correct = false, bool verbose = false) { foreach (var document in store.GetDocuments()) { var violations = new List <string>(); foreach (var flag in document.Header.Flags) { var resolved = store.ResolveFlag(flag); if (resolved == null) { if (verbose) { Console.WriteLine($"Warning: cannot resolve Flag ID {flag}"); } continue; } if (resolved.SecurityLevel > document.Header.SecurityLevel) { if (correct && document.Header.SecurityLevel < resolved.SecurityLevel) { document.Header.SecurityLevel = resolved.SecurityLevel; } violations.Add($"- flag violation: {resolved.Tag} {resolved.Name} **{resolved.SecurityLevel}"); } } await document.LoadBodyAsync(); document.DecryptBodyBytes(); if (document.Header.SecurityLevel >= DrxSecurityLevel.Secret && !document.Header.Encrypted) { if (correct) { document.Header.Encrypted = true; } violations.Add("- document should be encrypted"); } if (violations.Count <= 0) { continue; } if (correct) { document.EncryptBodyBytes(); await document.SaveAsync(); } Console.WriteLine($"{document.ToString()} ({document.Id}) is not compliant: "); foreach (var violation in violations) { Console.WriteLine(violation); } Console.WriteLine(); } }