public bool StopCounter(int id)
        {
            PerformanceMonitorRecord recordToUpdate =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == id);

            string performanceCounterReading =
                    _performanceCounterService.GetPerformanceCounterReading(recordToUpdate.CategoryName, recordToUpdate.InstanceName, recordToUpdate.CounterName);

            //threshold check
            bool passedThreshold;
            var passedThresholdValue = 0;
            double countervalue = double.Parse(performanceCounterReading);
            passedThreshold = _performanceMonitorService.CheckThreshold(recordToUpdate.Threshold, recordToUpdate.ThresholdWhen, countervalue, ref passedThresholdValue);

            //calculate the the duration (in ticks)
            DateTime startTime = (DateTime)recordToUpdate.Created;
            TimeSpan durationCount = DateTime.Now.Subtract(startTime);

            DateTime sharedTimeStampValue = DateTime.Now;

            //first write last values to datarecord (then to the monitorrecord for min/max/average)
            PerformanceMonitorDataRecord datarecord = new PerformanceMonitorDataRecord()
            {
                PerformanceMonitorRecord_Id = recordToUpdate.Id,
                //should be finally equal to latest reading in performancemonitorrecord
                Count = performanceCounterReading,
                HeartBeat = sharedTimeStampValue,
                PassedThreshold = passedThreshold,
                PassedThresholdValue = passedThresholdValue,
                //duration count(ticks in nanoseconds from starttime (created)) should be equal to value in performancemonitorrecord
                Ticks = Convert.ToString(durationCount.Ticks)
            };

            var result = _performanceMonitorDataService.WriteDataRecord(datarecord);
            if (result)
            {
                recordToUpdate.LastValue = performanceCounterReading;
                recordToUpdate.DurationCount = Convert.ToString(durationCount.Ticks);
                recordToUpdate.EndTime = sharedTimeStampValue;

                //mean, min and max values
                List<PerformanceMonitorDataRecord> dataRecords = _performanceMonitorDataService.GetDataRecords(id);
                //first convert counted values from string to int
                List<double> countedValues = new List<double>();
                bool conversionResult = false;
                foreach (PerformanceMonitorDataRecord record in dataRecords)
                {
                    double value;
                    bool conversionPassed = double.TryParse(record.Count, out value);
                    if (conversionPassed)
                    {
                        countedValues.Add(value);
                        conversionResult = true;
                    }
                    else
                    {
                        //conversion failed
                        conversionResult = false;
                    }
                }
                double meanValue = 0;
                double minValue = 0;
                double maxValue = 0;

                if (conversionResult)
                {
                    meanValue = Math.Round(countedValues.Average(), 2);
                    minValue = Math.Round(countedValues.Min(), 2);
                    maxValue = Math.Round(countedValues.Max(), 2);
                }
                recordToUpdate.Mean = meanValue; 
                recordToUpdate.Minimum = minValue;
                recordToUpdate.Maximum = maxValue;

                var monitorrecordresult = _performanceMonitorDataService.UpdatePerformanceMonitorRecord(recordToUpdate);
                if (monitorrecordresult)
                {
                    _notifier.Information(T("Counter stopped and all values updated succesfully"));
                }
                else
                {
                    _notifier.Error(T("Error: Failed to stop counter and update the monitor record"));
                }
           }
            else
            {
                _notifier.Error(T("Error: Failed to stop counter and update all records"));
            }
            
            return result;
        }
        public bool EditPost(EditPostInputModel inputModel)
        {
            string performanceCounterReading =
                _performanceCounterService.GetPerformanceCounterReading(inputModel.CategoryName,inputModel.InstanceName,inputModel.CounterName);

            if (performanceCounterReading == String.Empty)
            {
                _notifier.Error(T("Error: Not able to perform a reading on this type of counter"));
                return false;
            }

            PerformanceMonitorRecord record = new PerformanceMonitorRecord()
            {
                Id = inputModel.Id,
                CategoryName = inputModel.CategoryName,
                InstanceName = inputModel.InstanceName,
                CounterName = inputModel.CounterName,
                InitialValue = performanceCounterReading,
                Duration = inputModel.Duration,
                SampleInterval = inputModel.SampleInterval,
                Threshold = inputModel.Threshold,
                ThresholdWhen = inputModel.ThresholdWhen
            };

            var result = _performanceMonitorDataService.SetPerformanceMonitorRecord(record);
            if (result)
            {
                PerformanceMonitorRecord activeRecord =
                    _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.IsEnabled == true);

                //threshold check
                bool passedThreshold;
                var passedThresholdValue = 0;
                double countervalue = double.Parse(activeRecord.InitialValue);

                passedThreshold = _performanceMonitorService.CheckThreshold(activeRecord.Threshold, activeRecord.ThresholdWhen, countervalue, ref passedThresholdValue);

                PerformanceMonitorDataRecord datarecord = new PerformanceMonitorDataRecord() 
                {
                    PerformanceMonitorRecord_Id = activeRecord.Id,
                    Count = activeRecord.InitialValue,
                    HeartBeat = activeRecord.Created,
                    PassedThreshold = passedThreshold,
                    PassedThresholdValue = passedThresholdValue,

                    //duration count(ticks in nanoseconds from starttime (created)) should initially be set to startvalue (zero)
                    Ticks = "0"
                };

                var dataResult = _performanceMonitorDataService.WriteDataRecord(datarecord);
                if (dataResult)
                {
                    _notifier.Information(T("New counter records added succesfully"));
                }
                else
                {
                    _notifier.Warning(T("New counter record added succesfully, but failed to write initial datarecord"));
                }
            }
            else
            {
                _notifier.Error(T("Error: Failed to add new counter"));
            }
            return result;
        }
        public bool WriteDataRecord(PerformanceMonitorDataRecord performanceMonitorDataRecord)
        {
            _performanceMonitorDataRecordRepository.Create(performanceMonitorDataRecord);

            return true;
        }
        public bool DeletePerformanceMonitorDataRecord(PerformanceMonitorDataRecord dataRecordToDelete)
        {
            if (dataRecordToDelete == null)
            {
                return false;
            }
            _performanceMonitorDataRecordRepository.Delete(dataRecordToDelete);

            return true;
        }
        public void HeartBeat()
        {
            //perform and process the counterreading every 'heartbeat'
            PerformanceMonitorRecord activeMonitorRecord =
                _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.IsEnabled == true);

            string performanceCounterReading =
                    _performanceCounterService.GetPerformanceCounterReading(activeMonitorRecord.CategoryName, activeMonitorRecord.InstanceName, activeMonitorRecord.CounterName);

            //calculate the the duration (in ticks)
            DateTime startTime = (DateTime)activeMonitorRecord.Created;
            TimeSpan durationCount = DateTime.Now.Subtract(startTime);

            //threshold check
            bool passedThreshold;
            var passedThresholdValue = 0;
            double countervalue = double.Parse(performanceCounterReading);

            passedThreshold = CheckThreshold(activeMonitorRecord.Threshold, activeMonitorRecord.ThresholdWhen, countervalue, ref passedThresholdValue);

            PerformanceMonitorDataRecord dataRecord = new PerformanceMonitorDataRecord()
            {
                PerformanceMonitorRecord_Id = activeMonitorRecord.Id,
                Count = performanceCounterReading,
                HeartBeat = DateTime.Now,
                PassedThreshold = passedThreshold,
                PassedThresholdValue = passedThresholdValue,
                Ticks = Convert.ToString(durationCount.Ticks)
            };

            _performanceMonitorDataService.WriteDataRecord(dataRecord);
        }