wabbajack/Wabbajack.BuildServer/Controllers/NexusCache.cs

152 lines
5.3 KiB
C#
Raw Normal View History

2020-04-03 22:36:56 +00:00
using System;
using System.Collections.Generic;
2020-04-02 04:50:23 +00:00
using System.Linq;
2020-04-02 21:16:46 +00:00
using System.Threading;
2020-01-08 04:41:50 +00:00
using System.Threading.Tasks;
2020-04-03 22:36:56 +00:00
using Microsoft.AspNetCore.Authorization;
2020-01-08 04:41:50 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
2020-04-02 04:21:19 +00:00
using Newtonsoft.Json;
using Wabbajack.BuildServer.Model.Models;
2020-01-08 04:41:50 +00:00
using Wabbajack.BuildServer.Models;
2020-04-03 22:36:56 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
2020-01-08 04:41:50 +00:00
using Wabbajack.Lib.NexusApi;
namespace Wabbajack.BuildServer.Controllers
{
//[Authorize]
[ApiController]
[Route("/v1/games/")]
public class NexusCache : AControllerBase<NexusCache>
{
2020-04-03 22:36:56 +00:00
private AppSettings _settings;
private static long CachedCount = 0;
private static long ForwardCount = 0;
public NexusCache(ILogger<NexusCache> logger, SqlService sql, AppSettings settings) : base(logger, sql)
2020-01-08 04:41:50 +00:00
{
2020-04-03 22:36:56 +00:00
_settings = settings;
2020-01-08 04:41:50 +00:00
}
2020-01-10 02:06:11 +00:00
2020-01-08 04:41:50 +00:00
/// <summary>
2020-01-10 02:06:11 +00:00
/// Looks up the mod details for a given Gamename/ModId pair. If the entry is not found in the cache it will
/// be requested from the server (using the caller's Nexus API key if provided).
2020-01-08 04:41:50 +00:00
/// </summary>
/// <param name="db"></param>
/// <param name="GameName">The Nexus game name</param>
/// <param name="ModId">The Nexus mod id</param>
/// <returns>A Mod Info result</returns>
[HttpGet]
[Route("{GameName}/mods/{ModId}.json")]
2020-04-02 21:16:46 +00:00
public async Task<ModInfo> GetModInfo(string GameName, long ModId)
2020-01-08 04:41:50 +00:00
{
2020-04-02 21:16:46 +00:00
Utils.Log($"Nexus Mod Info {GameName} {ModId}");
var game = GameRegistry.GetByFuzzyName(GameName).Game;
var result = await SQL.GetNexusModInfoString(game, ModId);
2020-01-08 04:41:50 +00:00
string method = "CACHED";
if (result == null)
{
var api = await NexusApiClient.Get(Request.Headers["apikey"].FirstOrDefault());
2020-04-11 03:16:10 +00:00
result = await api.GetModInfo(game, ModId, false);
await SQL.AddNexusModInfo(game, ModId, DateTime.UtcNow, result);
2020-01-08 04:41:50 +00:00
2020-04-11 03:16:10 +00:00
2020-01-08 04:41:50 +00:00
method = "NOT_CACHED";
2020-04-02 21:16:46 +00:00
Interlocked.Increment(ref ForwardCount);
}
else
{
Interlocked.Increment(ref CachedCount);
2020-01-08 04:41:50 +00:00
}
Response.Headers.Add("x-cache-result", method);
2020-04-02 21:16:46 +00:00
return result;
2020-01-08 04:41:50 +00:00
}
[HttpGet]
[Route("{GameName}/mods/{ModId}/files.json")]
2020-04-02 21:16:46 +00:00
public async Task<NexusApiClient.GetModFilesResponse> GetModFiles(string GameName, long ModId)
2020-01-08 04:41:50 +00:00
{
2020-04-02 21:16:46 +00:00
Utils.Log($"Nexus Mod Files {GameName} {ModId}");
var game = GameRegistry.GetByFuzzyName(GameName).Game;
var result = await SQL.GetModFiles(game, ModId);
2020-01-08 04:41:50 +00:00
string method = "CACHED";
if (result == null)
{
var api = await NexusApiClient.Get(Request.Headers["apikey"].FirstOrDefault());
2020-04-11 03:16:10 +00:00
result = await api.GetModFiles(game, ModId, false);
await SQL.AddNexusModFiles(game, ModId, DateTime.UtcNow, result);
2020-01-08 04:41:50 +00:00
method = "NOT_CACHED";
2020-04-02 21:16:46 +00:00
Interlocked.Increment(ref ForwardCount);
}
else
{
Interlocked.Increment(ref CachedCount);
2020-01-08 04:41:50 +00:00
}
Response.Headers.Add("x-cache-result", method);
2020-04-02 21:16:46 +00:00
return result;
2020-01-08 04:41:50 +00:00
}
2020-04-02 21:16:46 +00:00
private class NexusIngestHeader
{
public List<NexusCacheData<ModInfo>> ModInfos { get; set; }
public List<NexusCacheData<NexusFileInfo>> FileInfos { get; set; }
public List<NexusCacheData<NexusApiClient.GetModFilesResponse>> ModFiles { get; set; }
}
2020-01-08 04:41:50 +00:00
[HttpGet]
2020-04-02 21:16:46 +00:00
[Route("/nexus_cache/ingest")]
[Authorize]
public async Task<IActionResult> IngestNexusFile()
2020-01-08 04:41:50 +00:00
{
2020-04-02 21:16:46 +00:00
long totalRows = 0;
var dataPath = @"nexus_export.json".RelativeTo(_settings.TempPath);
2020-01-08 04:41:50 +00:00
2020-04-02 21:16:46 +00:00
var data = JsonConvert.DeserializeObject<NexusIngestHeader>(await dataPath.ReadAllTextAsync());
2020-01-08 04:41:50 +00:00
2020-04-02 21:16:46 +00:00
foreach (var record in data.ModInfos)
{
2020-04-03 03:57:59 +00:00
await SQL.AddNexusModInfo(GameRegistry.GetByFuzzyName(record.Game).Game, record.ModId,
2020-04-02 21:16:46 +00:00
record.LastCheckedUTC, record.Data);
totalRows += 1;
}
foreach (var record in data.FileInfos)
{
2020-04-03 03:57:59 +00:00
await SQL.AddNexusFileInfo(GameRegistry.GetByFuzzyName(record.Game).Game, record.ModId,
2020-04-02 21:16:46 +00:00
long.Parse(record.FileId),
record.LastCheckedUTC, record.Data);
totalRows += 1;
}
foreach (var record in data.ModFiles)
{
2020-04-03 03:57:59 +00:00
await SQL.AddNexusModFiles(GameRegistry.GetByFuzzyName(record.Game).Game, record.ModId,
2020-04-02 21:16:46 +00:00
record.LastCheckedUTC, record.Data);
totalRows += 1;
2020-01-08 04:41:50 +00:00
}
2020-04-02 21:16:46 +00:00
return Ok(totalRows);
2020-01-08 04:41:50 +00:00
}
2020-04-02 04:21:19 +00:00
2020-04-02 21:16:46 +00:00
[HttpGet]
[Route("/nexus_cache/stats")]
public async Task<IActionResult> NexusCacheStats()
2020-04-02 04:21:19 +00:00
{
2020-04-02 21:16:46 +00:00
return Ok(new ClientAPI.NexusCacheStats
{
CachedCount = CachedCount,
ForwardCount = ForwardCount,
CacheRatio = (double)CachedCount / (ForwardCount == 0 ? 1 : ForwardCount)
});
2020-04-02 04:21:19 +00:00
}
2020-04-02 21:16:46 +00:00
2020-01-08 04:41:50 +00:00
}
}