public async Task <IActionResult> GetUnknownsAsync([FromQuery] DetectionQueryParameters queryParameters)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(queryParameters.Timeframe))
                {
                    throw new ArgumentNullException("Timeframe");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.SortBy))
                {
                    throw new ArgumentNullException("SortBy");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.SortOrder))
                {
                    throw new ArgumentNullException("SortOrder");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.Location))
                {
                    throw new ArgumentNullException("Location");
                }

                if (queryParameters.Page == 0)
                {
                    throw new ArgumentNullException("Page");
                }

                if (queryParameters.RecordsPerPage == 0)
                {
                    throw new ArgumentNullException("RecordsPerPage");
                }

                // start with all records
                var queryable = (await _repository.GetAllAsync()).AsQueryable();

                // apply desired status
                MetadataFilters.ApplyReviewedFilter(ref queryable, true);

                // apply desired found state
                MetadataFilters.ApplyFoundFilter(ref queryable, "don't know");

                // apply timeframe filter
                MetadataFilters.ApplyTimeframeFilter(ref queryable, queryParameters.Timeframe);

                // apply location filter
                if (queryParameters.Location.ToLower() != "all")
                {
                    MetadataFilters.ApplyLocationFilter(ref queryable, queryParameters.Location);
                }

                // If no detections found
                if (queryable == null || queryable.Count() == 0)
                {
                    return(NoContent());
                }

                // total number of records
                double recordCount = queryable.Count();

                // apply sort filter
                if (queryParameters.SortBy.ToLower() == "confidence")
                {
                    MetadataFilters.ApplyConfidenceSortFilter(ref queryable, queryParameters.SortOrder);
                }

                else if (queryParameters.SortBy.ToLower() == "timestamp")
                {
                    MetadataFilters.ApplyTimestampSortFilter(ref queryable, queryParameters.SortOrder);
                }

                // apply pagination filter
                MetadataFilters.ApplyPaginationFilter(ref queryable, queryParameters.Page, queryParameters.RecordsPerPage);

                // set page count headers
                SetHeaderCounts(recordCount,
                                (queryParameters.RecordsPerPage > 0 ? queryParameters.RecordsPerPage :
                                 MetadataFilters.DefaultRecordsPerPage));

                // map to returnable data type and return
                return(Ok(queryable.Select(x => MetadataProcessors.ToDetection(x)).ToList()));
            }
            catch (ArgumentNullException ex)
            {
                var details = new ProblemDetails()
                {
                    Detail = ex.Message
                };
                return(BadRequest(details));
            }
            catch (Exception ex)
            {
                var details = new ProblemDetails()
                {
                    Title  = ex.GetType().ToString(),
                    Detail = ex.Message
                };

                return(StatusCode(StatusCodes.Status500InternalServerError, details));
            }
        }
Exemple #2
0
        public IActionResult GetUnreviewed([FromQuery] DetectionQueryParameters queryParameters)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(queryParameters.Timeframe))
                {
                    throw new ArgumentNullException("Timeframe");
                }

                if (queryParameters.DateFrom > queryParameters.DateTo)
                {
                    throw new Exception("From Date should be less than To date");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.SortBy))
                {
                    throw new ArgumentNullException("SortBy");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.SortOrder))
                {
                    throw new ArgumentNullException("SortOrder");
                }

                if (string.IsNullOrWhiteSpace(queryParameters.Location))
                {
                    throw new ArgumentNullException("Location");
                }

                if (queryParameters.Page == 0)
                {
                    throw new ArgumentNullException("Page");
                }

                if (queryParameters.RecordsPerPage == 0)
                {
                    throw new ArgumentNullException("RecordsPerPage");
                }

                // start with all records
                var queryable = _repository.GetAll();

                // apply reviewed status
                MetadataFilters.ApplyReviewedFilter(ref queryable, false);

                // apply location filter
                if (queryParameters.Location.ToLower() != "all")
                {
                    MetadataFilters.ApplyLocationFilter(ref queryable, queryParameters.Location);
                }

                // apply timeframe filter
                MetadataFilters.ApplyTimeframeFilter(ref queryable, queryParameters.Timeframe, queryParameters.DateFrom, queryParameters.DateTo);

                // If no detections found
                if (queryable == null || queryable.Count() == 0)
                {
                    return(NoContent());
                }

                // total number of records
                double recordCount = queryable.Count();

                var results = queryable
                              .Select(x => DetectionProcessors.ToDetection(x)).ToList();

                // NOTE: Have to apply SortBy timestamp and pagination filter after
                //       executing the select because of how EF for Cosmos deals with DateTime.
                //       Had to convert from string (how stored in Cosmos) to DateTime in order to apply the
                //       select, but that messed up the SortBy since Cosmos is expecting a string.

                // apply sort filter
                if (queryParameters.SortBy.ToLower() == "confidence")
                {
                    DetectionFilters.ApplyConfidenceSortFilter(ref results, queryParameters.SortOrder);
                }
                else if (queryParameters.SortBy.ToLower() == "timestamp")
                {
                    DetectionFilters.ApplyTimestampSortFilter(ref results, queryParameters.SortOrder);
                }

                // apply pagination filter
                DetectionFilters.ApplyPaginationFilter(ref results, queryParameters.Page, queryParameters.RecordsPerPage);


                // set page count headers
                SetHeaderCounts(recordCount,
                                (queryParameters.RecordsPerPage > 0 ? queryParameters.RecordsPerPage :
                                 MetadataFilters.DefaultRecordsPerPage));

                // map to returnable data type and return
                return(Ok(results));
            }
            catch (ArgumentNullException ex)
            {
                var details = new ProblemDetails()
                {
                    Detail = ex.Message
                };
                return(BadRequest(details));
            }
            catch (Exception ex)
            {
                var details = new ProblemDetails()
                {
                    Title  = ex.GetType().ToString(),
                    Detail = ex.Message
                };

                return(StatusCode(StatusCodes.Status500InternalServerError, details));
            }
        }