diff --git a/CardIndexRestAPI/CardIndexRestAPI.csproj b/CardIndexRestAPI/CardIndexRestAPI.csproj index df404d6..47d3ecf 100644 --- a/CardIndexRestAPI/CardIndexRestAPI.csproj +++ b/CardIndexRestAPI/CardIndexRestAPI.csproj @@ -9,7 +9,7 @@ - + diff --git a/CardIndexRestAPI/Controllers/SolrProxyController.cs b/CardIndexRestAPI/Controllers/SolrProxyController.cs index 3513f61..117d381 100644 --- a/CardIndexRestAPI/Controllers/SolrProxyController.cs +++ b/CardIndexRestAPI/Controllers/SolrProxyController.cs @@ -12,6 +12,7 @@ using System.Web; using static CardIndexRestAPI.DataSchema.Requests; using System.Numerics; +using System.Text.RegularExpressions; namespace SolrAPI.Controllers { @@ -148,6 +149,12 @@ public async Task MatchedCardsSearch([FromBody]GetMatchesRequest request) */ + [EnableCors] + [HttpGet("Health")] + public IActionResult Health() { + return Ok("Works!"); + } + [EnableCors] [HttpPost("MatchedImagesSearch")] public async Task MatchedImagesSearch([FromBody] GetMatchesRequest request) @@ -300,5 +307,63 @@ public async Task LatestCards([FromQuery]int maxCardsCount=10, [FromQuery] strin await Response.CompleteAsync(); } } + + [EnableCors] + [HttpGet("RecentCrawledStats/{cardsNamespace}")] + public async Task RecentCrawledStats([FromRoute] string cardsNamespace, [FromQuery] RecentStatsMode mode = RecentStatsMode.Days) + { + // checking input + var match = Regex.Match(cardsNamespace, @"^[0-9A-Za-z\-]+$", RegexOptions.IgnoreCase); + if (!match.Success) + { + throw new FormatException($"cardsNamespace must contain 0-9A-Za-z or '-' characters only"); + } + + string facettingStartStr = mode switch + { + RecentStatsMode.Days => "NOW-7DAY/DAY", + RecentStatsMode.Months => "NOW-12MONTH/MONTH", + _ => throw new NotSupportedException() + }; + string facettingRangeStr = mode switch + { + RecentStatsMode.Days => "+1DAY", + RecentStatsMode.Months => "+1MONTH", + _ => throw new NotSupportedException() + }; + + var safeCardsNamespace = cardsNamespace.Replace(Environment.NewLine, ""); + + Trace.TraceInformation($"Fetching statistics for ns \"{safeCardsNamespace}\" over recent {mode}(s)"); + + Dictionary requestParams = new Dictionary(); + requestParams.Add("q", "*:*"); + requestParams.Add("fl", "id, card_creation_time"); + requestParams.Add("fq", $"id:/{cardsNamespace}.*/"); + requestParams.Add("facet", "true"); + requestParams.Add("facet.range", "card_creation_time"); + requestParams.Add("facet.range.start", facettingStartStr); + requestParams.Add("facet.range.end", "NOW"); + requestParams.Add("facet.range.gap", facettingRangeStr); + + + FormUrlEncodedContent requestContent = new FormUrlEncodedContent(requestParams); + + try + { + await ProxyHttpPost("recent stats", this.solrCardsSelectExpressionsURL, requestContent, Response); + } + catch (Exception err) + { + string errorMsg = $"Exception occurred during recent stats fetch: {err}"; + Trace.TraceError(errorMsg); + Response.StatusCode = 500; + Response.ContentLength = ASCIIEncoding.Unicode.GetByteCount(errorMsg); + await Response.WriteAsync(errorMsg); + await Response.CompleteAsync(); + } + } } + + public enum RecentStatsMode { Days, Months} }