예제 #1
0
        private void LoadPluginFromFile(string fileName)
        {
            Assembly assembly;

            try
            {
                assembly = Assembly.LoadFile(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(CultureInfo.CurrentUICulture, "Couldn't load assembly from file {0}{1}{2}", fileName, Environment.NewLine, ex.ToString()), "Plugin Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List <IArchiveWriter> archiveWriters = LoadPlugin <IArchiveWriter>(assembly);

            if (archiveWriters.Count > 0)
            {
                ArchiveWriters.AddRange(archiveWriters);
            }

            List <IMetadataConverter> metadataConverters = LoadPlugin <IMetadataConverter>(assembly);

            if (metadataConverters.Count > 0)
            {
                MetadataConverters.AddRange(metadataConverters);
            }

            List <IMetadataProcessor> metadataProcessors = LoadPlugin <IMetadataProcessor>(assembly);

            if (metadataProcessors.Count > 0)
            {
                MetadataProcessors.AddRange(metadataProcessors);
            }
        }
        public async Task <IActionResult> GetByIdAsync(string id)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    throw new ArgumentNullException("id");
                }

                var metadata = await _repository.GetByIdAsync(id);

                if (metadata == null)
                {
                    return(NotFound());
                }

                return(Ok(MetadataProcessors.ToDetection(metadata)));
            }
            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));
            }
        }
        public async Task <IActionResult> GetSystemMetrics([FromQuery] MetricsFilterDTO queryParameters)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(queryParameters.Timeframe))
                {
                    throw new ArgumentNullException("Timeframe");
                }

                var metrics = new Metrics();

                metrics.Timeframe = queryParameters.Timeframe;

                // Build base queryable
                var queryable = await BuildQueryableAsync(queryParameters.Timeframe);

                // If not metrics to return
                if (queryable == null || queryable.Count() == 0)
                {
                    return(NoContent());
                }

                // Pull reviewed/unreviewed metrics from querable
                var reviewed = MetadataProcessors.GetReviewed(queryable);

                metrics.Reviewed   = reviewed.ReviewedCount;
                metrics.Unreviewed = reviewed.UnreviewedCount;

                // Pull results metrics from queryable
                var results = MetadataProcessors.GetResults(queryable);

                metrics.ConfirmedDetection = results.ConfirmedCount;
                metrics.FalseDetection     = results.FalseCount;
                metrics.UnknownDetection   = results.UnknownCount;

                // Pull comments from queryable
                metrics.ConfirmedComments   = MetadataProcessors.GetComments(queryable, "yes");
                metrics.UnconfirmedComments = MetadataProcessors.GetComments(queryable, "no");
                metrics.UnconfirmedComments.AddRange(MetadataProcessors.GetComments(queryable, "don't know"));

                // Pull tags from queryable
                metrics.Tags = MetadataProcessors.GetTags(queryable);

                return(Ok(metrics));
            }
            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));
            }
        }
        public async Task <IActionResult> Put(string id, [FromBody] DetectionUpdate detectionUpdate)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(id))
                {
                    throw new ArgumentNullException("id");
                }

                if (detectionUpdate == null)
                {
                    throw new ArgumentNullException("postedDetection");
                }

                var metadata = await _repository.GetByIdAsync(id);

                if (metadata == null)
                {
                    return(NotFound());
                }

                metadata.comments      = detectionUpdate.Comments;
                metadata.moderator     = detectionUpdate.Moderator;
                metadata.dateModerated = detectionUpdate.Moderated.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ");
                metadata.reviewed      = detectionUpdate.Reviewed;
                metadata.SRKWFound     = (string.IsNullOrWhiteSpace(detectionUpdate.Found)) ? "no" : detectionUpdate.Found.ToLower();

                // Normalize the tags
                if (!string.IsNullOrWhiteSpace(detectionUpdate.Tags))
                {
                    var working = detectionUpdate.Tags.Replace(",", ";");

                    var tagList = new List <string>();
                    tagList.AddRange(working.Split(';').ToList().Select(x => x.Trim()));
                    metadata.tags = string.Join(";", tagList);
                }
                else
                {
                    metadata.tags = string.Empty;
                }

                await _repository.CommitAsync();

                return(Ok(MetadataProcessors.ToDetection(metadata)));
            }
            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));
            }
        }
        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));
            }
        }