public async Task <DeploymentServiceModel> UpdateAndSaveDeployment(string tenantId, DeploymentServiceModel deployment, Configuration configuration)
        {
            var devicesStatuses = this.GetDeviceStatuses(tenantId, configuration);

            if (devicesStatuses != null && devicesStatuses.Keys.Count > 0)
            {
                deployment.DeploymentMetrics.DeviceStatuses = devicesStatuses;
                deployment.DeploymentMetrics.DeviceMetrics  = this.CalculateDeviceMetrics(devicesStatuses);
            }

            await this.SaveDeployment(deployment, tenantId);

            if (devicesStatuses != null && devicesStatuses.Keys.Count > 0)
            {
                var deviceTwins = await this.GetDeviceProperties(tenantId, devicesStatuses.Keys);

                if (deviceTwins != null && deviceTwins.Count > 0)
                {
                    await this.SaveDeviceProperties(tenantId, deployment.Id, deviceTwins);
                }

                if (ConfigurationsHelper.IsEdgeDeployment(configuration))
                {
                    var moduleTwins = await this.GetDeviceProperties(tenantId, devicesStatuses.Keys, PackageType.EdgeManifest);

                    await this.StoreModuleTwinsInStorage(tenantId, moduleTwins, deployment.Id);
                }
            }

            return(deployment);
        }
        private IDictionary <string, DeploymentStatus> GetDeviceStatuses(string tenantId, Configuration deployment)
        {
            var deviceWithStatus = new Dictionary <string, DeploymentStatus>();
            IDictionary <string, long> metrics = deployment.SystemMetrics?.Results;

            bool appliedCountKeyExists = false;
            long appliedCount          = 0;

            if (metrics.ContainsKey("appliedCount"))
            {
                appliedCountKeyExists = true;
                appliedCount          = metrics["appliedCount"];
            }

            if ((appliedCountKeyExists && appliedCount > 0) || !appliedCountKeyExists)
            {
                string deploymentType = null;
                if (ConfigurationsHelper.IsEdgeDeployment(deployment))
                {
                    deploymentType = PackageType.EdgeManifest.ToString();
                }
                else
                {
                    deploymentType = PackageType.DeviceConfiguration.ToString();
                }

                deployment.Labels.TryGetValue(ConfigurationsHelper.ConfigTypeLabel, out string configType);
                var queries = DeviceStatusQueries.GetQueries(deploymentType, configType);

                string deploymentId = deployment.Id;

                var appliedDevices = this.GetDevicesInQuery(queries[QueryType.APPLIED], deploymentId, tenantId);

                if (!ConfigurationsHelper.IsEdgeDeployment(deployment) && !configType.Equals(ConfigType.Firmware.ToString()))
                {
                    foreach (var devices in appliedDevices)
                    {
                        deviceWithStatus.Add(devices, DeploymentStatus.Unknown);
                    }

                    return(deviceWithStatus);
                }

                if (appliedDevices != null && appliedDevices.Count > 0)
                {
                    // Get reported status from custom Metrics if available otherwise use default queries
                    var successfulDevices = this.GetDevicesInQuery(deployment.Metrics.Queries.ContainsKey(SuccessQueryName) ? deployment.Metrics.Queries[SuccessQueryName] : queries[QueryType.SUCCESSFUL], deploymentId, tenantId);
                    var failedDevices     = this.GetDevicesInQuery(deployment.Metrics.Queries.ContainsKey(FailedQueryName) ? deployment.Metrics.Queries[FailedQueryName] : queries[QueryType.FAILED], deploymentId, tenantId);

                    foreach (var device in appliedDevices)
                    {
                        if (successfulDevices.Contains(device))
                        {
                            deviceWithStatus.Add(device, DeploymentStatus.Succeeded);
                        }
                        else if (failedDevices.Contains(device))
                        {
                            deviceWithStatus.Add(device, DeploymentStatus.Failed);
                        }
                        else
                        {
                            deviceWithStatus.Add(device, DeploymentStatus.Pending);
                        }
                    }
                }
            }

            return(deviceWithStatus);
        }