<commit_before>﻿using AutoMapper;
using Hatfield.EnviroData.Core;
using Hatfield.EnviroData.MVC.Models;
using Hatfield.EnviroData.WQDataProfile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Hatfield.EnviroData.MVC.Controllers.API
{
    public class StationQueryAPIController : ApiController
    {
        private readonly IWQDataRepository _wqDataRepository;
        private readonly ISiteRepository _siteRepository;
        private readonly IWQVariableRepository _variableRepository;
        private readonly IResultRepository _resultRepository;

        public StationQueryAPIController(ISiteRepository siteRepository, IWQVariableRepository variableRepository, IResultRepository resultRepository, IWQDataRepository wqDataRepository)
        {
            _wqDataRepository = wqDataRepository;
            _siteRepository = siteRepository;
            _variableRepository = variableRepository;
            _resultRepository = resultRepository;
        }

        [HttpGet]
        public IEnumerable<SiteViewModel> GetSites()
        {
            var sites = _siteRepository.GetAll();
            var items = Mapper.Map<IEnumerable<SiteViewModel>>(sites);
            return items;
        }

        [HttpGet]
        public SiteViewModel GetSingleSite(int locationId)
        {
            var sites = _siteRepository.GetAll().Where(x => x.SamplingFeatureID == locationId).FirstOrDefault();
            var item = Mapper.Map<SiteViewModel>(sites);
            return item;
        }

        [HttpGet]
        public IEnumerable<VariableViewModel> GetAllAnalytes()
        {
            var sites = _variableRepository.GetAllChemistryVariables().Where(x => x.VariableDefinition != null);
            var items = Mapper.Map<IEnumerable<VariableViewModel>>(sites);
            return items;
        }

        [HttpPost]
        public IEnumerable<StationAnalyteQueryViewModel> FetchStationData(FetchSiteAnalyteQueryViewModel queryViewModel)
        {
            var items = new List<StationAnalyteQueryViewModel>();
            var actions = _wqDataRepository.GetAllWQAnalyteDataActions();

            foreach (var action in actions)
            {
                foreach (var analyte in queryViewModel.SelectedVariables)
                {
                    var result = action.FeatureActions.Where(x => x.SamplingFeatureID == queryViewModel.SelectedSiteID).FirstOrDefault().Results.Where(x => x.VariableID == analyte).FirstOrDefault();
                    if (result != null)
                    {
                        if (result.MeasurementResult != null && result.MeasurementResult.MeasurementResultValues.First().ValueDateTime <= queryViewModel.EndDate && result.MeasurementResult.MeasurementResultValues.First().ValueDateTime >= queryViewModel.StartDate)
                        {
                            var measurementValue = result.MeasurementResult.MeasurementResultValues.FirstOrDefault().DataValue;
                            var resultDateTime = action.BeginDateTime;
                            var unitsName = result.Unit.UnitsName;
                            string prefix = null;
                            if (result.ResultExtensionPropertyValues.Where(x => x.ExtensionProperty.PropertyName == "Prefix").FirstOrDefault().PropertyValue != null)
                            {
                                prefix = result.ResultExtensionPropertyValues.Where(x => x.ExtensionProperty.PropertyName == "Prefix").FirstOrDefault().PropertyValue;
                            }
                            var variable = result.Variable.VariableDefinition;
                            double? detectionLimit = null;
                            if (result.ResultsDataQualities.Count > 0)
                            {
                                detectionLimit = result.ResultsDataQualities.Where(x => x.DataQuality.DataQualityTypeCV == "methodDetectionLimit").FirstOrDefault().DataQuality.DataQualityValue;
                            }
                            items.Add(new StationAnalyteQueryViewModel { DataValue = measurementValue, ResultDateTime = resultDateTime.ToString("MMM-dd-yyyy, HH:mm tt"), UnitsName = unitsName, Variable = variable, MethodDetectionLimit = detectionLimit, Prefix = prefix });<commit_msg>Updated to display latest version of an action<commit_after>﻿using AutoMapper;
using Hatfield.EnviroData.Core;
using Hatfield.EnviroData.MVC.Models;
using Hatfield.EnviroData.WQDataProfile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Hatfield.EnviroData.MVC.Controllers.API
{
    public class StationQueryAPIController : ApiController
    {
        private readonly IWQDefaultValueProvider _wqDefaultValueProvider;
        private readonly IWQDataRepository _wqDataRepository;
        private readonly ISiteRepository _siteRepository;
        private readonly IWQVariableRepository _variableRepository;

        public StationQueryAPIController(IWQDefaultValueProvider wqDefaultValueProvider, ISiteRepository siteRepository, IWQVariableRepository variableRepository, IWQDataRepository wqDataRepository)
        {
            _wqDefaultValueProvider = wqDefaultValueProvider;
            _wqDataRepository = wqDataRepository;
            _siteRepository = siteRepository;
            _variableRepository = variableRepository;
        }

        [HttpGet]
        public IEnumerable<SiteViewModel> GetSites()
        {
            var sites = _siteRepository.GetAll();
            var items = Mapper.Map<IEnumerable<SiteViewModel>>(sites);
            return items;
        }

        [HttpGet]
        public SiteViewModel GetSingleSite(int locationId)
        {
            var sites = _siteRepository.GetAll().Where(x => x.SamplingFeatureID == locationId).FirstOrDefault();
            var item = Mapper.Map<SiteViewModel>(sites);
            return item;
        }

        [HttpGet]
        public IEnumerable<VariableViewModel> GetAllAnalytes()
        {
            var sites = _variableRepository.GetAllChemistryVariables().Where(x => x.VariableDefinition != null);
            var items = Mapper.Map<IEnumerable<VariableViewModel>>(sites);
            return items;
        }

        [HttpPost]
        public IEnumerable<StationAnalyteQueryViewModel> FetchStationData(FetchSiteAnalyteQueryViewModel queryViewModel)
        {
            var items = new List<StationAnalyteQueryViewModel>();
            var actions = _wqDataRepository.GetAllWQAnalyteDataActions();
            var versionHelper = new DataVersioningHelper(_wqDefaultValueProvider);

            foreach (var action in actions)
            {
                foreach (var analyte in queryViewModel.SelectedVariables)
                {
                    var latestAction = versionHelper.GetLatestVersionActionData(action);
                    var result = latestAction.FeatureActions.Where(x => x.SamplingFeatureID == queryViewModel.SelectedSiteID).FirstOrDefault().Results.Where(x => x.VariableID == analyte).FirstOrDefault();

                    
                    

                    if (result != null)
                    {
                        if (result.MeasurementResult != null && result.MeasurementResult.MeasurementResultValues.First().ValueDateTime <= queryViewModel.EndDate && result.MeasurementResult.MeasurementResultValues.First().ValueDateTime >= queryViewModel.StartDate)
                        {
                            var measurementValue = result.MeasurementResult.MeasurementResultValues.FirstOrDefault().DataValue;
                            var resultDateTime = latestAction.BeginDateTime;
                            var unitsName = result.Unit.UnitsName;
                            string prefix = null;
                            if (result.ResultExtensionPropertyValues.Where(x => x.ExtensionProperty.PropertyName == "Prefix").FirstOrDefault().PropertyValue != null)
                            {
                                prefix = result.ResultExtensionPropertyValues.Where(x => x.ExtensionProperty.PropertyName == "Prefix").FirstOrDefault().PropertyValue;
                            }
                            var variable = result.Variable.VariableDefinition;
                            double? detectionLimit = null;
                            if (result.ResultsDataQualities.Count > 0)
                            {
                                detectionLimit = result.ResultsDataQualities.Where(x => x.DataQuality.DataQualityTypeCV == "methodDetectionLimit").FirstOrDefault().DataQuality.DataQualityValue;
                            }
                            items.Add(new StationAnalyteQueryViewModel { DataValue = measurementValue, ResultDateTime = resultDateTime.ToString("MMM-dd-yyyy, HH:mm tt"), UnitsName = unitsName, Variable = variable, MethodDetectionLimit = detectionLimit, Prefix = prefix });
{'fraction_non_alphanumeric': 0.07659811006114509, 'fraction_numerical': 0.00022234574763757643, 'mean_word_length': 2.3195571955719556, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3958817', 'n_tokens_mistral': 2110, 'n_tokens_neox': 2051, 'n_words': 392}