|
| 1 | +/* |
| 2 | + * Copyright 2022 MONAI Consortium |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using System.Linq; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Ardalis.GuardClauses; |
| 22 | +using Microsoft.Extensions.Logging; |
| 23 | +using Microsoft.Extensions.Options; |
| 24 | +using Monai.Deploy.WorkflowManager.Common.Database.Options; |
| 25 | +using MongoDB.Bson; |
| 26 | +using MongoDB.Driver; |
| 27 | +using Artifact = Monai.Deploy.Messaging.Common.Artifact; |
| 28 | + |
| 29 | +namespace Monai.Deploy.WorkflowManager.Common.Database.Repositories |
| 30 | +{ |
| 31 | + public class ArtifactReceivedDetails : Artifact |
| 32 | + { |
| 33 | + /// <summary> |
| 34 | + /// Gets or Sets LastReceived. |
| 35 | + /// </summary> |
| 36 | + public DateTime? Received { get; set; } = null; |
| 37 | + |
| 38 | + public static ArtifactReceivedDetails FromArtifact(Artifact artifact) => |
| 39 | + new() |
| 40 | + { |
| 41 | + Received = DateTime.UtcNow, |
| 42 | + Type = artifact.Type, |
| 43 | + Path = artifact.Path |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + public class ArtifactReceivedItems |
| 48 | + { |
| 49 | + public string Id { get; set; } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Gets or Sets WorkflowInstanceId. |
| 53 | + /// </summary> |
| 54 | + public string WorkflowInstanceId { get; set; } = string.Empty; |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets or Sets TaskId. |
| 58 | + /// </summary> |
| 59 | + public string TaskId { get; set; } = string.Empty; |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets or Sets Artifacts. |
| 63 | + /// </summary> |
| 64 | + public List<ArtifactReceivedDetails> Artifacts { get; set; } = new(); |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// The date Time this was received |
| 68 | + /// </summary> |
| 69 | + public DateTime Received { get; set; } = DateTime.UtcNow; |
| 70 | + } |
| 71 | + |
| 72 | + public class ArtifactsRepository : IArtifactsRepository |
| 73 | + { |
| 74 | + private readonly ILogger<ArtifactsRepository> _logger; |
| 75 | + private readonly IMongoCollection<ArtifactReceivedItems> _artifactReceivedItemsCollection; |
| 76 | + |
| 77 | + public ArtifactsRepository( |
| 78 | + IMongoClient client, |
| 79 | + IOptions<WorkloadManagerDatabaseSettings> bookStoreDatabaseSettings, |
| 80 | + ILogger<ArtifactsRepository> logger) |
| 81 | + { |
| 82 | + if (client == null) |
| 83 | + { |
| 84 | + throw new ArgumentNullException(nameof(client)); |
| 85 | + } |
| 86 | + |
| 87 | + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| 88 | + var mongoDatabase = client.GetDatabase(bookStoreDatabaseSettings.Value.DatabaseName); |
| 89 | + _artifactReceivedItemsCollection = mongoDatabase.GetCollection<ArtifactReceivedItems>("ArtifactReceivedItems"); |
| 90 | + EnsureIndex().GetAwaiter().GetResult(); |
| 91 | + } |
| 92 | + private async Task EnsureIndex() |
| 93 | + { |
| 94 | + var indexName = "ArtifactReceivedWorkflowInstanceIdTaskIdIndex"; |
| 95 | + |
| 96 | + var model = new CreateIndexModel<ArtifactReceivedItems>( |
| 97 | + Builders<ArtifactReceivedItems>.IndexKeys.Ascending(s => s.WorkflowInstanceId).Ascending(s => s.TaskId), |
| 98 | + new CreateIndexOptions { Name = indexName } |
| 99 | + ); |
| 100 | + |
| 101 | + await MakeIndex(_artifactReceivedItemsCollection, indexName, model); |
| 102 | + |
| 103 | + indexName = "ReceivedTime"; |
| 104 | + |
| 105 | + model = new CreateIndexModel<ArtifactReceivedItems>( |
| 106 | + Builders<ArtifactReceivedItems>.IndexKeys.Ascending(s => s.Received), |
| 107 | + new CreateIndexOptions { ExpireAfter = TimeSpan.FromDays(7), Name = "ReceivedTime" } |
| 108 | + ); |
| 109 | + |
| 110 | + await MakeIndex(_artifactReceivedItemsCollection, indexName, model); |
| 111 | + } |
| 112 | + private static async Task MakeIndex<T>(IMongoCollection<T> collection, string indexName, CreateIndexModel<T> model) |
| 113 | + { |
| 114 | + Guard.Against.Null(collection, nameof(collection)); |
| 115 | + |
| 116 | + var asyncCursor = (await collection.Indexes.ListAsync()); |
| 117 | + var bsonDocuments = (await asyncCursor.ToListAsync()); |
| 118 | + var indexes = bsonDocuments.Select(_ => _.GetElement("name").Value.ToString()).ToList(); |
| 119 | + |
| 120 | + // If index not present create it else skip. |
| 121 | + if (!indexes.Any(i => i is not null && i.Equals(indexName))) |
| 122 | + { |
| 123 | + await collection.Indexes.CreateOneAsync(model); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public async Task<List<ArtifactReceivedItems>?> GetAllAsync(string workflowInstance, string taskId) |
| 128 | + { |
| 129 | + var result = await _artifactReceivedItemsCollection.FindAsync(a => a.WorkflowInstanceId == workflowInstance && a.TaskId == taskId).ConfigureAwait(false); |
| 130 | + return await result.ToListAsync().ConfigureAwait(false); |
| 131 | + } |
| 132 | + |
| 133 | + public Task AddItemAsync(ArtifactReceivedItems item) |
| 134 | + { |
| 135 | + return _artifactReceivedItemsCollection.InsertOneAsync(item); |
| 136 | + } |
| 137 | + |
| 138 | + public Task AddItemAsync(string workflowInstanceId, string taskId, List<Artifact> artifactsOutputs) |
| 139 | + { |
| 140 | + var artifacts = artifactsOutputs.Select(a => new ArtifactReceivedDetails() |
| 141 | + { |
| 142 | + Path = a.Path, |
| 143 | + Type = a.Type, |
| 144 | + Received = DateTime.UtcNow |
| 145 | + }); |
| 146 | + |
| 147 | + var item = new ArtifactReceivedItems() |
| 148 | + { |
| 149 | + WorkflowInstanceId = workflowInstanceId, |
| 150 | + TaskId = taskId, |
| 151 | + Artifacts = artifacts.ToList() |
| 152 | + }; |
| 153 | + |
| 154 | + return _artifactReceivedItemsCollection.InsertOneAsync(item); |
| 155 | + } |
| 156 | + |
| 157 | + public async Task AddOrUpdateItemAsync(string workflowInstanceId, string taskId, |
| 158 | + IEnumerable<Artifact> artifactsOutputs) |
| 159 | + { |
| 160 | + var artifacts = artifactsOutputs.Select(a => new ArtifactReceivedDetails() |
| 161 | + { |
| 162 | + Path = a.Path, |
| 163 | + Type = a.Type, |
| 164 | + Received = DateTime.UtcNow |
| 165 | + }); |
| 166 | + |
| 167 | + var item = new ArtifactReceivedItems() |
| 168 | + { |
| 169 | + Id = workflowInstanceId + taskId, |
| 170 | + WorkflowInstanceId = workflowInstanceId, |
| 171 | + TaskId = taskId, |
| 172 | + Artifacts = artifacts.ToList() |
| 173 | + }; |
| 174 | + |
| 175 | + var result = await _artifactReceivedItemsCollection |
| 176 | + .FindAsync(a => a.WorkflowInstanceId == workflowInstanceId && a.TaskId == taskId).ConfigureAwait(false); |
| 177 | + var existing = await result.FirstOrDefaultAsync().ConfigureAwait(false); |
| 178 | + |
| 179 | + try |
| 180 | + { |
| 181 | + if (existing == null) |
| 182 | + { |
| 183 | + await _artifactReceivedItemsCollection.InsertOneAsync(item).ConfigureAwait(false); |
| 184 | + } |
| 185 | + else |
| 186 | + { |
| 187 | + item.Artifacts = item.Artifacts.Concat(existing.Artifacts).ToList(); |
| 188 | + var update = Builders<ArtifactReceivedItems>.Update.Set(a => a.Artifacts, item.Artifacts); |
| 189 | + await _artifactReceivedItemsCollection |
| 190 | + .UpdateOneAsync(a => a.WorkflowInstanceId == workflowInstanceId && a.TaskId == taskId, update) |
| 191 | + .ConfigureAwait(false); |
| 192 | + } |
| 193 | + } |
| 194 | + catch (Exception ex) |
| 195 | + { |
| 196 | + |
| 197 | + throw; |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments