private List <MetricValue> readMetricValuesIntoEntity(SIMEntityBase entity, JobTarget jobTarget, string metricName, AppDRESTMetric appDRESTMetric, int timerangeDuration)
        {
            List <MetricValue> metricValues = new List <MetricValue>(appDRESTMetric.metricValues.Count);

            foreach (AppDRESTMetricValue appDRESTMetricValue in appDRESTMetric.metricValues)
            {
                // Populate metrics into the list for output into CSV
                MetricValue metricValue = new MetricValue();

                metricValue.Controller      = jobTarget.Controller;
                metricValue.ApplicationID   = jobTarget.ApplicationID;
                metricValue.ApplicationName = jobTarget.Application;

                if (entity != null)
                {
                    if (entity is SIMMachine)
                    {
                        SIMMachine machine = (SIMMachine)entity;

                        metricValue.EntityID   = machine.MachineID;
                        metricValue.EntityName = machine.MachineName;
                        metricValue.EntityType = machine.MachineType;
                    }
                    else if (entity is SIMMachineNetwork)
                    {
                        SIMMachineNetwork machineNetwork = (SIMMachineNetwork)entity;

                        metricValue.EntityID   = machineNetwork.MachineID;
                        metricValue.EntityName = machineNetwork.NetworkName;
                        metricValue.EntityType = machineNetwork.Speed.ToString();
                    }
                }

                metricValue.EventTimeStampUtc = UnixTimeHelper.ConvertFromUnixTimestamp(appDRESTMetricValue.startTimeInMillis);
                metricValue.EventTimeStamp    = metricValue.EventTimeStampUtc.ToLocalTime();
                metricValue.EventTime         = metricValue.EventTimeStamp;

                metricValue.MetricName = metricName;
                metricValue.MetricID   = appDRESTMetric.metricId;
                switch (appDRESTMetric.frequency)
                {
                case "SIXTY_MIN":
                {
                    metricValue.MetricResolution = 60;
                    break;
                }

                case "TEN_MIN":
                {
                    metricValue.MetricResolution = 10;
                    break;
                }

                case "ONE_MIN":
                {
                    metricValue.MetricResolution = 1;
                    break;
                }

                default:
                {
                    metricValue.MetricResolution = 1;
                    break;
                }
                }

                metricValue.Count       = appDRESTMetricValue.count;
                metricValue.Min         = appDRESTMetricValue.min;
                metricValue.Max         = appDRESTMetricValue.max;
                metricValue.Occurrences = appDRESTMetricValue.occurrences;
                metricValue.Sum         = appDRESTMetricValue.sum;
                metricValue.Value       = appDRESTMetricValue.value;

                metricValues.Add(metricValue);
            }

            return(metricValues);
        }
        private List <MetricValue> readMetricsIntoEntities(
            List <AppDRESTMetric> metricData,
            Dictionary <string, SIMEntityBase> entitiesDictionaryByName,
            JobTarget jobTarget,
            JobTimeRange jobTimeRange)
        {
            SIMEntityBase entity = null;

            int timerangeDuration = (int)(jobTimeRange.To - jobTimeRange.From).Duration().TotalMinutes;

            List <MetricValue> metricValues = new List <MetricValue>(metricData.Count * timerangeDuration);

            foreach (AppDRESTMetric appDRESTMetric in metricData)
            {
                if (appDRESTMetric.metricValues.Count == 0)
                {
                    // No metrics in this chunk
                    continue;
                }

                #region Get metric path components and metric name

                // Analyze metric path returned by the call to controller
                string[] metricPathComponents = appDRESTMetric.metricPath.Split('|');

                if (metricPathComponents.Length == 0)
                {
                    // Metric name was no good
                    logger.Warn("Metric path='{0}' could not be parsed into individual components", appDRESTMetric.metricPath);
                    continue;
                }

                string[] metricNameComponents = appDRESTMetric.metricName.Split('|');

                if (metricNameComponents.Length == 0)
                {
                    // Metric name was no good
                    logger.Warn("Metric name='{0}' could not be parsed into individual components", appDRESTMetric.metricName);
                    continue;
                }

                // Name of the metric is always the last one in the metric path, but we're going to take more then one
                string metricName = String.Format("{0} {1}", metricPathComponents[metricPathComponents.Length - 2], metricPathComponents[metricPathComponents.Length - 1]);

                #endregion

                #region Determine metric entity type, scope and name from metric path

                //"metricPath" : "Application Infrastructure Performance|Root|Containers|Individual Nodes|61b921e747a9|Hardware Resources|CPU|%Busy",
                //"metricPath" : "Application Infrastructure Performance|Root|Containers|Individual Nodes|8404a587a6c4|Hardware Resources|Disks|Reads/sec",
                //"metricPath" : "Application Infrastructure Performance|Root|Containers|Individual Nodes|61859e063c2e|Hardware Resources|Network|Incoming KB/sec",
                //"metricPath" : "Application Infrastructure Performance|Root|Containers|javaresourcehog|Individual Nodes|3c887b73cc52|Hardware Resources|Network|Incoming KB/sec",
                //"metricPath" : "Application Infrastructure Performance|Root|Containers|Individual Nodes|342508d53e0d|Hardware Resources|Memory|Used %",

                for (int i = 0; i < metricPathComponents.Length; i++)
                {
                    if (String.Compare(metricPathComponents[i], "Individual Nodes", true) == 0)
                    {
                        if (entitiesDictionaryByName.TryGetValue(metricPathComponents[i + 1], out entity) == false)
                        {
                            SIMMachine machine = new SIMMachine();
                            machine.MachineID   = -1;
                            machine.MachineName = metricPathComponents[i + 1];
                            entity = machine;
                        }
                        break;
                    }
                }

                #endregion

                List <MetricValue> metricValuesConverted = readMetricValuesIntoEntity(entity, jobTarget, metricName, appDRESTMetric, timerangeDuration);
                metricValues.AddRange(metricValuesConverted);
            }

            return(metricValues);
        }