//initial
        public bool SetPerformanceMonitorRecord(PerformanceMonitorRecord performanceMonitorRecord)
        {
            if (performanceMonitorRecord == null)
            {
                return false;
            }

            string userName = _workContext.CurrentUser.UserName;
            performanceMonitorRecord.CreatedBy = userName;
            performanceMonitorRecord.Created = DateTime.Now;
            performanceMonitorRecord.StartTime = DateTime.Now;
            performanceMonitorRecord.NodeName = Environment.MachineName.ToUpperInvariant();

            IPAddress localIpAdress = IPAddress.Parse("127.0.0.1");
            performanceMonitorRecord.IpAddress = localIpAdress.ToString();
            performanceMonitorRecord.IsEnabled = true;

            performanceMonitorRecord.EndTime = DateTime.Now.AddHours(Convert.ToDouble(performanceMonitorRecord.Duration));

            //performanceMonitorRecord from string "1-24" to  double value to timespan ticks (nanoseconds)
            TimeSpan durationCount = TimeSpan.FromHours(Convert.ToDouble(performanceMonitorRecord.Duration));
            performanceMonitorRecord.Duration = durationCount.Ticks.ToString();

            _performanceMonitorRecordRepository.Update(performanceMonitorRecord);

            return true;
        }
        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 EditViewModel Edit(EditInputModel inputModel)
        {
            PerformanceMonitorRecord recordToEdit = _performanceMonitorDataService.PerformanceMonitorRecords.FirstOrDefault(r => r.Id == inputModel.Id);
            if (recordToEdit != null)
            {
                //edit an existing record (no implementation in current version)
            }

            //No existing record in the database with the given id, create a new one
            if (recordToEdit == null)
            {
                recordToEdit = new PerformanceMonitorRecord();

                recordToEdit.Id = 0;
                recordToEdit.CategoryName = inputModel.CategoryName;
                if (inputModel.InstanceName != null)
                {
                    recordToEdit.InstanceName = inputModel.InstanceName;
                }

                recordToEdit.CounterName = inputModel.CounterName;

                if (inputModel.SampleInterval <= 0)
                {
                    recordToEdit.SampleInterval = 1;
                }
                else
                {
                    recordToEdit.SampleInterval = inputModel.SampleInterval;
                }

                if (inputModel.Duration == null || Int32.Parse(inputModel.Duration) <= 0)
                {
                    recordToEdit.Duration = "1";
                }
                else
                {
                    recordToEdit.Duration = inputModel.Duration;
                }
                recordToEdit.Threshold = 0;
                //ThresholdWhen default value is 'Above'
                recordToEdit.ThresholdWhen = false;
            }

            string myCatName = inputModel.CategoryName;
            List<string> performanceCounters;
            if (inputModel.InstanceName == "none")
            {
                performanceCounters = _performanceMonitorService.GetCounterNames(myCatName, null);
            }
            else
            {
                string myInstanceName = inputModel.InstanceName;
                performanceCounters = _performanceMonitorService.GetCounterNames(myCatName,myInstanceName);
            }

            var counterList = performanceCounters.Select(c => new SelectListItem
            {
                Text = c.ToString(),
                Value = c.ToString()
            });


            Dictionary<string, string> thresholdWhenValues = new Dictionary<string, string>();
            thresholdWhenValues.Add("false", "Above");
            thresholdWhenValues.Add("true", "Below");

            var thresholdWhenList = thresholdWhenValues.Select(c => new SelectListItem
            {
                Value = c.Key,
                Text = c.Value
            });

            EditViewModel viewModel = new EditViewModel()
            {
                Id = recordToEdit.Id,
                CategoryName = inputModel.CategoryName,
                CounterList = new SelectList(counterList, "Value", "Text", recordToEdit.CounterName),
                ThresholdWhenList = new SelectList(thresholdWhenList, "Value", "Text",recordToEdit.ThresholdWhen),
                Duration = recordToEdit.Duration,
                SampleInterval = recordToEdit.SampleInterval
            };

            return viewModel;
        }
        public bool DeletePerformanceMonitorRecord(PerformanceMonitorRecord recordToDelete)
        {
            if (recordToDelete == null)
            {
                return false;
            }
            _performanceMonitorRecordRepository.Delete(recordToDelete);

            return true;
        }
        //counter stopped : update records
        public bool UpdatePerformanceMonitorRecord(PerformanceMonitorRecord performanceMonitorRecord)
        {
            if (performanceMonitorRecord == null)
            {
                return false;
            }

            performanceMonitorRecord.IsEnabled = false;
            _performanceMonitorRecordRepository.Update(performanceMonitorRecord);

            return true;
        }