Example #1
0
        public IActionResult Get(string patientId, string code)
        {
            var observation = _aggregator.Run(patientId, code, null);

            //TODO: this may be refactored to the same response of observation controller
            return(Ok(observation));
        }
        /// <summary>
        /// Get Alert Level
        /// </summary>
        /// <param name="alert">Alert Input</param>
        /// <param name="patientId">Patient Id</param>
        /// <returns>0: No Alert, 1: Low Priority Alert, 2: Medium Priority Alert, 3: High Priority Alert</returns>
        public async Task <AlertLevel> GetAlertLevel(IAlertInput alert, string patientId)
        {
            if (alert.TargetValueSource.ToLower() == ObservationType)
            {
                if (!alert.TargetValueNumeric)
                {
                    throw new InvalidCastException("Target value should be numeric");
                }

                try
                {
                    var observations = await _dataProxy.Get <PDObservation>(10, 0, String.Format("{{patientid:\"{0}\",datefrom:\"{2}\",dateto:\"{3}\",codeid:\"{1}\",aggr:\"total\"}}", patientId, alert.TargetValueSource, (DateTime.Now.AddDays(-alert.AggregationPeriodDays).ToUnixTimestampMilli()), (DateTime.Now.ToUnixTimestampMilli())), null);

                    var value = observations.Select(e => e.Value).DefaultIfEmpty(0).Average();
                    return(ApplyFilter(alert, value));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    throw ex;
                }
            }
            else if (alert.TargetValueSource.ToLower() == MetaObservationType)
            {
                //Get Aggregated observation
                var aggrObservation = await _aggregator.Run(patientId, alert.TargetValueCode, DateTime.Now.AddDays(-alert.AggregationPeriodDays));

                var value = aggrObservation.Select(e => e.Value).Average();
                return(ApplyFilter(alert, value));
            }

            else if (alert.TargetValueSource.ToLower() == ClinicalInfoType)
            {
                //Clinical Info Input
                try
                {
                    var patient = await _dataProxy.Get <PDPatient>(patientId);

                    var clinicalInfoList = patient.GetClinicalInfoList();
                    var clinicalInfo     = clinicalInfoList.FirstOrDefault(e => e.Code.ToLower() == alert.TargetValueCode.ToLower());

                    return(ApplyFilter(alert, clinicalInfo.Value));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else if (alert.TargetValueSource.ToLower() == DSSInfoType)
            {
                //DSS Input
                try
                {
                    var observations = await _dssRunner.Run(patientId, _dssDefinitionProvider.GetJsonConfigFromCode(alert.TargetValueCode));

                    var value = observations.Where(e => e.Code == alert.TargetValueCode).Select(e => e.Value).FirstOrDefault();
                    return(ApplyFilter(alert, value));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                    throw ex;
                }
            }
            else
            {
                throw new NotSupportedException($"Source type  not supported from PDManager Alert Evaluator");
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="patientId"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        private async Task <IEnumerable <DSSValue> > GetInputValues(string patientId, DSSConfig config)
        {
            List <DSSValue> values = new List <DSSValue>();
            //Codes from observations
            List <string> observationCodes = new List <string>();

            //Codes from Meta o
            List <string> metaObservationCodes = new List <string>();

            //Codes from Patient Clinical Info
            List <string> clinicalInfoCodes = new List <string>();

            foreach (var c in config.Input)
            {
                if (string.IsNullOrEmpty(c.Source))
                {
                    //TODO: Add some error to response
                    continue;
                }


                if (c.Source.ToLower() == ObservationType)
                {
                    if (!observationCodes.Contains(c.Code))
                    {
                        observationCodes.Add(c.Code);
                    }
                }
                else if (c.Source.ToLower() == MetaObservationType)
                {
                    if (!metaObservationCodes.Contains(c.Code))
                    {
                        metaObservationCodes.Add(c.Code);
                    }
                }
                else if (c.Source.ToLower() == ClinicalInfoType)
                {
                    if (!clinicalInfoCodes.Contains(c.Code))
                    {
                        clinicalInfoCodes.Add(c.Code);
                    }
                }
                else
                {
                    throw new NotSupportedException($"Source type {c.Source} not supported from PDManager DSS");
                }
            }

            #region Observation Codes


            //TODO: Join observations codes in a single request in case

            foreach (var code in observationCodes)
            {
                try
                {
                    var observations = await _dataProxy.Get <PDObservation>(10, 0, String.Format("{{patientid:\"{0}\",deviceid:\"\",datefrom:{2},dateto:{3},codeid:\"{1}\",aggr:\"total\"}}", patientId, code, (DateTime.Now.AddDays(-config.AggregationPeriodDays).ToUnixTimestampMilli()), (DateTime.Now.ToUnixTimestampMilli())), null);


                    foreach (var observation in config.Input.Where(e => e.Code == code))
                    {
                        values.Add(new DSSValue()
                        {
                            Name = observation.Name, Code = observation.Code, Value = observations.Select(e => e.Value).DefaultIfEmpty(0).Average().ToString()
                        });
                    }

                    // }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            #endregion Meta Observation Codes

            #region Meta Observation Codes


            foreach (var code in metaObservationCodes)
            {
                try
                {
                    //Get Aggregated observation
                    var aggrObservation = await _aggregator.Run(patientId, code, DateTime.Now.AddDays(-config.AggregationPeriodDays));

                    //Find Corresponding observation in config


                    foreach (var metaObservation in config.Input.Where(e => e.Code == code))
                    {
                        //Meta Observations are only numeric
                        values.Add(new DSSValue()
                        {
                            Name = metaObservation.Name, Code = metaObservation.Code, Value = aggrObservation.Select(e => e.Value).DefaultIfEmpty(0).Average().ToString()
                        });
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            #endregion Observation Codes


            #region Clinical Info

            try
            {
                var patient = await _dataProxy.Get <PDPatient>(patientId);

                var clinicalInfoList = GetClinicalInfoList(patient.ClinicalInfo);
                foreach (var c in clinicalInfoList)
                {
                    var clinicalInfo = config.Input.FirstOrDefault(e => e.Code.ToLower() == c.Code.ToLower());

                    if (clinicalInfo != null)
                    {
                        values.Add(new DSSValue()
                        {
                            Name = clinicalInfo.Name, Code = clinicalInfo.Code, Value = c.Value
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            #endregion Clinical Info


            return(values);
        }
Example #4
0
        public async Task <IActionResult> Evaluate(string code, string patientId)
        {
            var res = await _aggregator.Run(patientId, code, null);

            return(Ok(res));
        }