コード例 #1
0
 private DojStop GetDojStop(ILogger log, Stop stop, string runId)
 {
     try
     {
         log.LogInformation($"Casting Stop to DoJStop: {stop.Id} : {runId}");
         return(_stopService.CastToDojStop(stop));
     }
     catch (Exception ex)
     {
         log.LogError($"Exception: {ex} --> occurred during GetDojStop with id {stop.Id} : {runId}");
         return(null);
     }
 }
コード例 #2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] string officerName, HttpRequest req,
            ILogger log)
        {
            log.LogInformation("CPRA Report Generation Requested");
            try
            {
                if (!RIPAAuthorization.ValidateUserOrAdministratorRole(req, log).ConfigureAwait(false).GetAwaiter().GetResult())
                {
                    return(new UnauthorizedResult());
                }
            }
            catch (Exception ex)
            {
                log.LogError(ex.Message);
                return(new UnauthorizedResult());
            }

            await _blobContainerClient.CreateIfNotExistsAsync();

            byte[] fileBytes;
            string tempPath  = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.csv");
            var    startDate = req.Query["StartDate"];
            var    endDate   = req.Query["EndDate"];
            var    fileName  = $"{startDate}-{endDate}-CPRAReport.csv";
            string stopQueryString;
            string stopSummaryQueryString;

            try
            {
                StopQueryUtility stopQueryUtility = new StopQueryUtility();
                StopQuery        stopQuery        = stopQueryUtility.GetStopQuery(req);
                stopQueryString        = stopQueryUtility.GetStopsQueryString(stopQuery, true, true);
                stopSummaryQueryString = stopQueryUtility.GetStopsSummaryQueryString(stopQuery);
            }
            catch (Exception ex)
            {
                log.LogError("An error occurred while evaluating the stop query.", ex);
                return(new BadRequestObjectResult("An error occurred while evaluating the stop query. Please try again."));
            }

            List <Stop> stopResponse;
            IEnumerable <StopStatusCount> stopStatuses;
            int totalStopCount = 0;

            try
            {
                stopResponse = await _stopCosmosDbService.GetStopsAsync(stopQueryString) as List <Stop>;

                stopStatuses = await _stopCosmosDbService.GetStopStatusCounts(stopSummaryQueryString);

                foreach (var stopStatus in stopStatuses)
                {
                    totalStopCount += stopStatus.Count;
                }
            }
            catch (Exception ex)
            {
                log.LogError("An error occurred getting stops requested.", ex);
                return(new BadRequestObjectResult("An error occurred getting stops requested. Please try again."));
            }

            if (totalStopCount == 0)
            {
                return(new OkObjectResult("No valid stops were found during that date range."));
            }

            var builder = new StringBuilder();

            try
            {
                foreach (var stop in stopResponse)
                {
                    var dojStop = _stopService.CastToDojStop(stop);
                    dojStop.Officer = null;
                    var jsonStop = JsonConvert.SerializeObject(dojStop);
                    if (stop.Location.Beat != null)
                    {
                        jsonStop += $"|{stop.Location.Beat.Codes.Text}";
                    }
                    jsonStop = jsonStop.Replace("\"", "\"\"");
                    builder.AppendLine($"\"{jsonStop}\"");
                }
            }
            catch (Exception ex)
            {
                log.LogError("An error occurred while parsing stops.", ex.Message);
            }

            await File.WriteAllTextAsync(tempPath, builder.ToString(), Encoding.UTF8);

            using (StreamReader sr = new StreamReader(tempPath))
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    sr.BaseStream.CopyTo(ms);
                    fileBytes = ms.ToArray();
                }
            }

            File.Delete(tempPath);

            try
            {
                await blobUtilities.UploadBlobCpraReport(fileBytes, fileName, officerName, _blobContainerClient);
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }

            var result = new CpraResult
            {
                FileName  = $"{officerName}/{fileName}",
                CpraItems = new List <CpraListItem>
                {
                    new CpraListItem()
                    {
                        Level  = 1,
                        Header = "Total stops in date range",
                        Detail = totalStopCount.ToString(),
                    },
                    new CpraListItem()
                    {
                        Level  = 1,
                        Header = "Submitted stops included on report",
                        Detail = stopResponse.Count.ToString(),
                    },
                    new CpraListItem()
                    {
                        Level  = 1,
                        Header = "From Date",
                        Detail = startDate,
                    },
                    new CpraListItem()
                    {
                        Level  = 1,
                        Header = "To Date",
                        Detail = endDate,
                    }
                }
            };

            return(new OkObjectResult(result));
        }