Skip to content

Commit

Permalink
RecentCrawledStats endpoint (#7)
Browse files Browse the repository at this point in the history
* RecentCrawledStats endpoint

* Sanitizing user input.

* Adding health endpoint
  • Loading branch information
dgrechka authored Nov 6, 2022
1 parent 54435dd commit bc50cd9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CardIndexRestAPI/CardIndexRestAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
65 changes: 65 additions & 0 deletions CardIndexRestAPI/Controllers/SolrProxyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Web;
using static CardIndexRestAPI.DataSchema.Requests;
using System.Numerics;
using System.Text.RegularExpressions;

namespace SolrAPI.Controllers
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<string, string> requestParams = new Dictionary<string, string>();
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}
}

0 comments on commit bc50cd9

Please sign in to comment.