public async Task SaveDeploymentFromHub(string tenantId, DeploymentServiceModel deploymentModel, List <TwinServiceModel> deviceTwins)
        {
            CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

            foreach (TwinServiceModel deviceTwin in deviceTwins)
            {
                DeploymentHistoryModel modelToSave = new DeploymentHistoryModel
                {
                    DeploymentId           = deploymentModel.Id,
                    DeploymentName         = deploymentModel.Name,
                    DeviceId               = deviceTwin.DeviceId,
                    PreviousFirmwareTwin   = null,
                    LastUpdatedDateTimeUtc = DateTime.UtcNow,
                    Twin = deviceTwin,
                };

                var value = JsonConvert.SerializeObject(
                    modelToSave,
                    Formatting.None,
                    new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                });

                await storageClient.SaveDocumentAsync(string.Format(DeploymentHistoryCollection, deviceTwin.DeviceId), deploymentModel.Id, new ValueServiceModel()
                {
                    Data = value
                }, this.GenerateCollectionLink(tenantId), Guid.NewGuid());
            }
        }
        private async Task SaveDeployment(DeploymentServiceModel deployment, string tenantId)
        {
            CosmosOperations storageClient = await CosmosOperations.GetClientAsync();

            var value = JsonConvert.SerializeObject(
                deployment,
                Formatting.Indented,
                new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore,
            });

            await storageClient.SaveDocumentAsync(DeploymentsCollection, deployment.Id, new ValueServiceModel()
            {
                Data = value, ETag = deployment.ETag
            }, this.GenerateCollectionLink(tenantId));
        }
        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 async Task <List <TwinServiceModel> > GetDeviceProperties(string tenantId, DeploymentServiceModel deploymentDetail, PackageType packageType = PackageType.DeviceConfiguration)
        {
            List <TwinServiceModel> twins = new List <TwinServiceModel>();

            if (packageType == PackageType.EdgeManifest)
            {
                IEnumerable <string> deviceIds = deploymentDetail.DeploymentMetrics.DeviceStatuses != null ? deploymentDetail.DeploymentMetrics.DeviceStatuses.Keys : this.FetchDeviceStatuses(tenantId, deploymentDetail.Id).Result.Keys;
                if (deviceIds != null && deviceIds.Count() > 0)
                {
                    string moduleQuery    = @"deviceId IN [{0}] AND moduleId = '$edgeAgent'";
                    var    deviceIdsQuery = string.Join(",", deviceIds.Select(d => $"'{d}'"));
                    var    query          = string.Format(moduleQuery, deviceIdsQuery);

                    await this.GetModuleTwins(tenantId, query, null, twins);
                }
            }
            else
            {
                string deviceQuery = deploymentDetail.TargetCondition == "*" ? string.Empty : deploymentDetail.TargetCondition;
                await this.GetDeviceTwins(tenantId, deviceQuery, null, twins);
            }

            return(twins);
        }