From 2f9a067c31a23317daee34d2c16f521eda5ef572 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Thu, 9 Jan 2020 15:56:29 -0700 Subject: [PATCH] Add endpoints for getting IndexedVirtualFiles --- .../Controllers/IndexedFiles.cs | 74 +++++++++++++++++++ Wabbajack.BuildServer/GraphQL/Query.cs | 10 --- Wabbajack.Lib/DTOs/IndexedVirtualFile.cs | 15 ++++ 3 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 Wabbajack.BuildServer/Controllers/IndexedFiles.cs create mode 100644 Wabbajack.Lib/DTOs/IndexedVirtualFile.cs diff --git a/Wabbajack.BuildServer/Controllers/IndexedFiles.cs b/Wabbajack.BuildServer/Controllers/IndexedFiles.cs new file mode 100644 index 00000000..e5e8f913 --- /dev/null +++ b/Wabbajack.BuildServer/Controllers/IndexedFiles.cs @@ -0,0 +1,74 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; +using MongoDB.Bson; +using MongoDB.Driver; +using Wabbajack.BuildServer.Models; +using Wabbajack.Common; +using Wabbajack.Lib.DTOs; + +namespace Wabbajack.BuildServer.Controllers +{ + [Route("/indexed_files")] + public class IndexedFiles : AControllerBase + { + public IndexedFiles(ILogger logger, DBContext db) : base(logger, db) + { + } + + [HttpGet] + [Route("{xxHashAsBase64}")] + public async Task GetFile(string xxHashAsBase64) + { + var id = xxHashAsBase64;//.FromHex().ToBase64(); + var query = new[] + { + new BsonDocument("$match", + new BsonDocument("_id", id)), + new BsonDocument("$graphLookup", + new BsonDocument + { + {"from", "indexed_files"}, + {"startWith", "$Children.Hash"}, + {"connectFromField", "Hash"}, + {"connectToField", "_id"}, + {"as", "ChildFiles"}, + {"maxDepth", 8}, + {"restrictSearchWithMatch", new BsonDocument()} + }) + }; + + var result = await Db.IndexedFiles.AggregateAsync(query); + + IndexedVirtualFile Convert(TreeResult t, string Name = null) + { + if (t == null) + return null; + + Dictionary indexed_children= new Dictionary(); + if (t.IsArchive) + indexed_children = t.ChildFiles.ToDictionary(t => t.Hash); + + var file = new IndexedVirtualFile + { + Name = Name, + Size = t.Size, + Hash = t.Hash, + Children = t.IsArchive ? t.Children.Select(child => Convert(indexed_children[child.Hash], child.Name)).ToList() : new List() + }; + return file; + } + + return Convert(result.FirstOrDefault()); + } + + public class TreeResult : IndexedFile + { + public List ChildFiles { get; set; } + } + + + } +} diff --git a/Wabbajack.BuildServer/GraphQL/Query.cs b/Wabbajack.BuildServer/GraphQL/Query.cs index 6e434938..a34b7b16 100644 --- a/Wabbajack.BuildServer/GraphQL/Query.cs +++ b/Wabbajack.BuildServer/GraphQL/Query.cs @@ -54,16 +54,6 @@ namespace Wabbajack.BuildServer.GraphQL return data; }); - Field ("indexedFileTree", - arguments: new QueryArguments( - new QueryArgument {Name = "hash", Description = "Hash of the Job"}), - resolve: context => - { - var hash = context.GetArgument("hash"); - var data = db.IndexedFiles.AsQueryable().Where(j => j.Hash == hash).FirstOrDefault(); - return data; - }); - FieldAsync>("dailyUniqueMetrics", arguments: new QueryArguments( new QueryArgument {Name = "metric_type", Description = "The grouping of metric data to query"} diff --git a/Wabbajack.Lib/DTOs/IndexedVirtualFile.cs b/Wabbajack.Lib/DTOs/IndexedVirtualFile.cs new file mode 100644 index 00000000..72179335 --- /dev/null +++ b/Wabbajack.Lib/DTOs/IndexedVirtualFile.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +namespace Wabbajack.Lib.DTOs +{ + /// + /// Response from the Build server for a indexed file + /// + public class IndexedVirtualFile + { + public string Name { get; set; } + public string Hash { get; set; } + public long Size { get; set; } + public List Children { get; set; } = new List(); + } +}