Created
September 2, 2024 15:17
-
-
Save pavlovmilen/d71d2e8d80485cf1c2e0085b6e4b0d3e to your computer and use it in GitHub Desktop.
Functions definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using LCI.Services.Abstract; | |
using LCI.Services.Core.DtoModels; | |
using LCI.Services.Core.Services; | |
using Microsoft.Extensions.Logging; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LCI.Services.Utilities | |
{ | |
public class Functions : IFunctions | |
{ | |
private readonly IPoliceApiService _policeApiService; | |
private readonly IPostcodeIoService _postcodeIoService; | |
private readonly ILogger _logger; | |
public Functions(IPoliceApiService policeApiService, IPostcodeIoService postcodeIoService, ILogger<Functions> logger) | |
{ | |
_policeApiService = policeApiService; | |
_postcodeIoService = postcodeIoService; | |
_logger = logger; | |
} | |
public async Task<string> CrimeAtLocationForPostcode(string postcode, int monthsToCheck = 3) | |
{ | |
var postcodeExists = await _postcodeIoService.PostcodeExistsAsync(postcode); | |
if (!postcodeExists) | |
{ | |
return "Invalid postcode"; | |
} | |
DateTime currentDateToCheck = DateTime.Now.AddDays(-45); | |
currentDateToCheck = new DateTime(currentDateToCheck.Year, currentDateToCheck.Month, 1); | |
if(monthsToCheck > 3) | |
{ | |
monthsToCheck = 3; | |
} | |
var tasks = new List<Task<PoliceApiRequestResult>>(); | |
var postcodeDetails = await _postcodeIoService.GetPostcodeDetailsAsync(postcode); | |
try | |
{ | |
for (int i = 0; i < monthsToCheck; i++) | |
{ | |
tasks.Add(_policeApiService.PerformPoliceDataApiRequestAsync(new PostcodeDto { Postcode = postcode, Latitude = postcodeDetails.result.latitude, Longitude = postcodeDetails.result.longitude, ForDate = currentDateToCheck }, currentDateToCheck)); | |
currentDateToCheck = currentDateToCheck.AddMonths(-1); | |
} | |
var datas = (await Task.WhenAll(tasks)).ToList(); | |
return GetStringContentFromCrimeDataAIModel(datas.SelectMany(d => d.CrimeData).ToList(), postcode); | |
} | |
catch (Exception ex) | |
{ | |
_logger.LogError(ex, "Error in CrimeAtLocationForPostcode"); | |
return "Error in CrimeAtLocationForPostcode"; | |
} | |
} | |
private string GetStringContentFromCrimeDataAIModel(List<CrimesAtLocationResponse> results, string postcode) | |
{ | |
if(results == null || !results.Any()) | |
{ | |
return "No data found"; | |
} | |
results = results.OrderBy(x => x.month).ToList(); | |
var stringBuilder = new StringBuilder(); | |
stringBuilder.AppendLine($"Crime data for {postcode} {results[0]?.location?.street?.name ?? ""},columns:CrimeCategory,CrimeOutcome,ForDate;\nData:"); | |
foreach (var result in results) | |
{ | |
foreach (var crimeData in results) | |
{ | |
stringBuilder.Append($",{crimeData.category},{crimeData.outcome_status?.category ?? "no outcome"},{crimeData.month}"); | |
} | |
} | |
return stringBuilder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment