-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProjectService.cs
More file actions
372 lines (342 loc) · 15.9 KB
/
Copy pathProjectService.cs
File metadata and controls
372 lines (342 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System.Data.Common;
using System.Globalization;
using System.IO.Compression;
using LexBoxApi.Jobs;
using LexBoxApi.Models.Project;
using LexBoxApi.Services.Email;
using LexCore.Auth;
using LexCore.Config;
using LexCore.Entities;
using LexCore.Exceptions;
using LexCore.ServiceInterfaces;
using LexData;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Path = System.IO.Path; // Resolves ambiguous reference with HotChocolate.Path
namespace LexBoxApi.Services;
public class ProjectService(
LexBoxDbContext dbContext,
IHgService hgService,
IOptions<HgConfig> hgConfig,
IMemoryCache memoryCache,
IEmailService emailService,
FwHeadlessClient fwHeadless)
{
public async Task<Guid> CreateProject(CreateProjectInput input)
{
await using var transaction = await dbContext.Database.BeginTransactionAsync();
var projectId = input.Id ?? Guid.NewGuid();
var theOrg = await dbContext.Orgs.FindAsync(input.OrgId);
/* TODO #737 - Remove this draftProject/isConfidentialIsUntrustworthy stuff and just trust input.IsConfidential */
var draftProject = await dbContext.DraftProjects.FindAsync(projectId);
// There could be draft projects from before we introduced the IsConfidential field. (i.e. where draftProject.IsConfidential is null)
// In those cases we can't trust input.IsConfidential == false, because that is the default, but the user will never have had the chance to pick it.
var isConfidentialIsUntrustworthy = draftProject is not null && draftProject.IsConfidential is null && !input.IsConfidential;
dbContext.Projects.Add(
new Project
{
Id = projectId,
Code = input.Code,
Name = input.Name,
ProjectOrigin = ProjectMigrationStatus.Migrated,
Description = input.Description,
Type = input.Type,
LastCommit = null,
RetentionPolicy = input.RetentionPolicy,
IsConfidential = isConfidentialIsUntrustworthy ? null : input.IsConfidential,
Organizations = theOrg is not null ? [theOrg] : [],
Users = input.ProjectManagerId.HasValue ? [new() { UserId = input.ProjectManagerId.Value, Role = ProjectRole.Manager }] : [],
FlexProjectMetadata = input.Type == ProjectType.FLEx ? new() : null
});
// Also delete draft project, if any
await dbContext.DraftProjects.Where(dp => dp.Id == projectId).ExecuteDeleteAsync();
var manager = input.ProjectManagerId.HasValue ? await dbContext.Users.FindAsync(input.ProjectManagerId.Value) : null;
manager?.UpdateCreateProjectsPermission(ProjectRole.Manager);
await dbContext.SaveChangesAsync();
await hgService.InitRepo(input.Code);
InvalidateProjectOrgIdsCache(projectId);
InvalidateProjectConfidentialityCache(projectId);
InvalidateProjectCodeCache(input.Code);
if (draftProject != null && manager != null)
{
await emailService.SendApproveProjectRequestEmail(manager, input);
}
await transaction.CommitAsync();
return projectId;
}
public async Task UpdateProjectLangTags(Guid projectId)
{
var project = await dbContext.Projects.FindAsync(projectId);
if (project is null || project.Type != ProjectType.FLEx) return;
await dbContext.Entry(project).Reference(p => p.FlexProjectMetadata).LoadAsync();
var langTags = await hgService.GetProjectWritingSystems(project.Code);
if (langTags is null) return;
project.FlexProjectMetadata ??= new FlexProjectMetadata();
project.FlexProjectMetadata.WritingSystems = langTags;
await dbContext.SaveChangesAsync();
}
public async Task UpdateProjectLangProjectId(Guid projectId)
{
var project = await dbContext.Projects.FindAsync(projectId);
if (project is null || project.Type != ProjectType.FLEx) return;
await dbContext.Entry(project).Reference(p => p.FlexProjectMetadata).LoadAsync();
var langProjGuid = await hgService.GetProjectIdOfFlexProject(project.Code);
project.FlexProjectMetadata ??= new FlexProjectMetadata();
project.FlexProjectMetadata.LangProjectId = langProjGuid;
await dbContext.SaveChangesAsync();
}
public async Task UpdateFLExModelVersion(Guid projectId)
{
var project = await dbContext.Projects.FindAsync(projectId);
if (project is null || project.Type != ProjectType.FLEx) return;
await dbContext.Entry(project).Reference(p => p.FlexProjectMetadata).LoadAsync();
var modelVersion = await hgService.GetModelVersionOfFlexProject(project.Code);
if (modelVersion is null) return;
project.FlexProjectMetadata ??= new FlexProjectMetadata();
project.FlexProjectMetadata.FlexModelVersion = modelVersion;
await dbContext.SaveChangesAsync();
}
public async Task<Guid> CreateDraftProject(CreateProjectInput input)
{
var existingProject = await dbContext.Projects.FindAsync(input.Id);
if (existingProject is not null)
{
throw new InvalidOperationException($"Project was already approved ({input.Id}: {existingProject.Code})");
}
// No need for a transaction if we're just saving a single item
var projectId = input.Id ?? Guid.NewGuid();
dbContext.DraftProjects.Add(
new DraftProject
{
Id = projectId,
Code = input.Code,
Name = input.Name,
Description = input.Description,
Type = input.Type,
IsConfidential = input.IsConfidential,
RetentionPolicy = input.RetentionPolicy,
ProjectManagerId = input.ProjectManagerId,
OrgId = input.OrgId
});
await dbContext.SaveChangesAsync();
return projectId;
}
public async Task<bool> ProjectExists(string projectCode)
{
return await dbContext.Projects.AnyAsync(p => p.Code == projectCode);
}
public async ValueTask<Guid?> LookupProjectId(string projectCode)
{
var cacheKey = $"ProjectIdForCode:{projectCode}";
if (memoryCache.TryGetValue(cacheKey, out Guid? projectId)) return projectId;
projectId = await dbContext.Projects
.Where(p => p.Code == projectCode)
.Select(p => (Guid?) p.Id)
.FirstOrDefaultAsync();
memoryCache.Set(cacheKey, projectId, TimeSpan.FromHours(1));
return projectId;
}
public void InvalidateProjectCodeCache(string projectCode)
{
try { memoryCache.Remove($"ProjectIdForCode:{projectCode}"); }
catch (Exception) { }; // Never allow this to throw
}
public async Task<BackupExecutor?> BackupProject(string code)
{
var exists = await dbContext.Projects.Where(p => p.Code == code)
.AnyAsync();
if (!exists) return null;
var backupExecutor = hgService.BackupRepo(code);
return backupExecutor;
}
public async Task ResetProject(ResetProjectByAdminInput input)
{
var projectId = await LookupProjectId(input.Code);
if (projectId is null) throw new NotFoundException($"project {input.Code} not found", nameof(Project));
await using var transaction = await dbContext.Database.BeginTransactionAsync();
var rowsAffected = await dbContext.Projects.Where(p => p.Id == projectId.Value && p.ResetStatus == ResetStatus.None)
.ExecuteUpdateAsync(u => u
.SetProperty(p => p.ResetStatus, ResetStatus.InProgress)
.SetProperty(p => p.RepoSizeInKb, 0)
.SetProperty(p => p.LastCommit, null as DateTimeOffset?));
if (rowsAffected == 0) throw new NotFoundException($"project {input.Code} not ready for reset or already reset", nameof(Project));
await fwHeadless.DeleteRepo(projectId.Value);
await ResetLexEntryCount(input.Code);
await hgService.ResetRepo(input.Code);
await transaction.CommitAsync();
}
public async Task FinishReset(string code, Stream? zipFile = null)
{
var project = await dbContext.Projects.Include(p => p.FlexProjectMetadata).Where(p => p.Code == code).SingleOrDefaultAsync();
if (project is null) throw new NotFoundException($"project {code} not found", nameof(Project));
if (project.ResetStatus != ResetStatus.InProgress) throw ProjectResetException.ResetNotStarted(code);
if (zipFile is not null)
{
await hgService.FinishReset(code, zipFile);
await UpdateProjectMetadata(project);
}
else
{
await hgService.InvalidateDirCache(code);
}
project.ResetStatus = ResetStatus.None;
project.UpdateUpdatedDate();
await dbContext.SaveChangesAsync();
}
public async ValueTask<Guid[]> LookupProjectOrgIds(Guid projectId)
{
var cacheKey = $"ProjectOrgsForId:{projectId}";
if (memoryCache.TryGetValue(cacheKey, out Guid[]? orgIds)) return orgIds ?? [];
orgIds = await dbContext.Projects
.Where(p => p.Id == projectId)
.Select(p => p.Organizations.Select(o => o.Id).ToArray())
.FirstOrDefaultAsync();
memoryCache.Set(cacheKey, orgIds, TimeSpan.FromHours(1));
return orgIds ?? [];
}
public void InvalidateProjectOrgIdsCache(Guid projectId)
{
try { memoryCache.Remove($"ProjectOrgsForId:{projectId}"); }
catch (Exception) { } // Never allow this to throw
}
public async ValueTask<bool?> LookupProjectConfidentiality(Guid projectId)
{
var cacheKey = $"ProjectConfidentiality:{projectId}";
if (memoryCache.TryGetValue(cacheKey, out bool? confidential)) return confidential;
var project = await dbContext.Projects.FindAsync(projectId);
memoryCache.Set(cacheKey, project?.IsConfidential, TimeSpan.FromHours(1));
return project?.IsConfidential;
}
public void InvalidateProjectConfidentialityCache(Guid projectId)
{
try { memoryCache.Remove($"ProjectConfidentiality:{projectId}"); }
catch (Exception) { } // Never allow this to throw
}
public async Task UpdateProjectMetadataForCode(string projectCode)
{
var project = await dbContext.Projects
.Include(p => p.FlexProjectMetadata)
.FirstOrDefaultAsync(p => p.Code == projectCode);
if (project is null) return;
await UpdateProjectMetadata(project);
await dbContext.SaveChangesAsync();
}
public async Task UpdateProjectMetadata(Project project)
{
if (hgConfig.Value.AutoUpdateLexEntryCountOnSendReceive && project is { Type: ProjectType.FLEx } or { Type: ProjectType.WeSay })
{
var count = await hgService.GetLexEntryCount(project.Code, project.Type);
if (project.FlexProjectMetadata is null)
{
project.FlexProjectMetadata = new FlexProjectMetadata { LexEntryCount = count };
}
else
{
project.FlexProjectMetadata.LexEntryCount = count;
}
}
else
{
await hgService.InvalidateDirCache(project.Code);
}
project.LastCommit = await hgService.GetLastCommitTimeFromHg(project.Code);
project.RepoSizeInKb = await hgService.GetRepoSizeInKb(project.Code);
// Caller is responsible for caling dbContext.SaveChangesAsync()
}
public async Task<int?> UpdateRepoSizeInKb(string projectCode)
{
var project = await dbContext.Projects.FirstOrDefaultAsync(p => p.Code == projectCode);
if (project is null) return null;
var size = await hgService.GetRepoSizeInKb(projectCode);
project.RepoSizeInKb = size;
await dbContext.SaveChangesAsync();
return size;
}
public async Task ResetLexEntryCount(string projectCode)
{
var project = await dbContext.Projects
.Include(p => p.FlexProjectMetadata)
.FirstOrDefaultAsync(p => p.Code == projectCode);
if (project is null) return;
if (project.FlexProjectMetadata is not null)
{
project.FlexProjectMetadata.LexEntryCount = null;
await dbContext.SaveChangesAsync();
}
}
public async Task<DirectoryInfo?> ExtractLdmlZip(Project project, string destRoot, CancellationToken token = default)
{
if (project.Type != ProjectType.FLEx) return null;
using var zip = await hgService.GetLdmlZip(project.Code, token);
if (zip is null) return null;
var path = Path.Join(destRoot, project.Id.ToString());
if (Directory.Exists(path)) Directory.Delete(path, true);
var dirInfo = Directory.CreateDirectory(path);
zip.ExtractToDirectory(dirInfo.FullName, true);
return dirInfo;
}
public async Task<string?> PrepareLdmlZip(Quartz.ISchedulerFactory schedulerFactory, CancellationToken token = default)
{
var nowStr = DateTime.UtcNow.ToString("yyyyMMdd-HHmmss", CultureInfo.InvariantCulture);
var path = Path.Join(Path.GetTempPath(), $"sldr-export-{nowStr}");
if (Directory.Exists(path)) Directory.Delete(path, true);
Directory.CreateDirectory(path);
await DeleteTempDirectoryJob.Queue(schedulerFactory, path, TimeSpan.FromHours(4));
var zipRoot = Path.Join(path, "zipRoot");
Directory.CreateDirectory(zipRoot);
await foreach (var project in dbContext.Projects.Where(p => p.Type == ProjectType.FLEx).AsAsyncEnumerable())
{
await ExtractLdmlZip(project, zipRoot, token);
}
var zipFilename = $"sldr-{nowStr}.zip";
var zipFilePath = Path.Join(path, zipFilename);
if (File.Exists(zipFilePath)) File.Delete(zipFilePath);
// If we would create an empty .zip file, just return null instead (will become a 404)
if (!Directory.EnumerateDirectories(zipRoot).Any()) return null;
ZipFile.CreateFromDirectory(zipRoot, zipFilePath, CompressionLevel.Fastest, includeBaseDirectory: false);
return zipFilePath;
}
public async Task<DateTimeOffset?> UpdateLastCommit(string projectCode)
{
var project = await dbContext.Projects.FirstOrDefaultAsync(p => p.Code == projectCode);
if (project is null) return null;
var lastCommitFromHg = await hgService.GetLastCommitTimeFromHg(projectCode);
project.LastCommit = lastCommitFromHg;
await dbContext.SaveChangesAsync();
return lastCommitFromHg;
}
public async Task<int?> UpdateLexEntryCount(string projectCode)
{
var project = await dbContext.Projects.Include(p => p.FlexProjectMetadata).FirstOrDefaultAsync(p => p.Code == projectCode);
if (project?.Type is not (ProjectType.FLEx or ProjectType.WeSay)) return null;
var count = await hgService.GetLexEntryCount(project.Code, project.Type);
if (project.FlexProjectMetadata is null)
{
project.FlexProjectMetadata = new FlexProjectMetadata { LexEntryCount = count };
}
else
{
project.FlexProjectMetadata.LexEntryCount = count;
}
try
{
await dbContext.SaveChangesAsync();
}
catch (DbException e) when (e.SqlState == "23505")
{
// 23505 is "Duplicate key value violates unique constraint", i.e. another process
// already created the FlexProjectMetadata entry with the correct value.
// We'll silently ignore it since the other process has already succeeded
}
return count;
}
public IQueryable<Project> UserProjects(Guid userId)
{
return dbContext.Projects.Where(p => p.Users.Select(u => u.UserId).Contains(userId));
}
public bool IsCrdtProject(Guid projectId)
{
return dbContext.CrdtCommits(projectId).Any();
}
}