-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathMiniLcmImport.cs
More file actions
105 lines (93 loc) · 4.1 KB
/
Copy pathMiniLcmImport.cs
File metadata and controls
105 lines (93 loc) · 4.1 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
using System.Diagnostics;
using FwDataMiniLcmBridge;
using FwLiteProjectSync.Import;
using Humanizer;
using LcmCrdt;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MiniLcm;
using MiniLcm.Models;
using MiniLcm.Project;
using MiniLcm.SyncHelpers;
namespace FwLiteProjectSync;
public class MiniLcmImport(
ILogger<MiniLcmImport> logger,
FwDataFactory fwDataFactory,
CrdtProjectsService crdtProjectsService
) : IProjectImport
{
public async Task<IProjectIdentifier> Import(IProjectIdentifier project)
{
if (project is not FwDataProject fwDataProject) throw new ArgumentException("Project is not a fwdata project");
var startTime = Stopwatch.GetTimestamp();
try
{
using var fwDataApi = fwDataFactory.GetFwDataMiniLcmApi(fwDataProject, false);
var harmonyProject = await crdtProjectsService.CreateProject(new(fwDataProject.Name,
fwDataProject.Name,
SeedNewProjectData: false,
FwProjectId: fwDataApi.ProjectId,
AfterCreate: async (provider, _) =>
{
var crdtApi = provider.GetRequiredService<IMiniLcmApi>();
await ImportProject(crdtApi, fwDataApi, fwDataApi.EntryCount);
}));
var timeSpent = Stopwatch.GetElapsedTime(startTime);
logger.LogInformation("Import of {ProjectName} complete, took {TimeSpend}",
fwDataProject.Name,
timeSpent.Humanize(2));
return harmonyProject;
}
catch
{
logger.LogError("Import of {ProjectName} failed, deleting project", fwDataProject.Name);
throw;
}
}
public async Task ImportProject(IMiniLcmApi importTo, IMiniLcmApi importFrom, int entryCount)
{
using var activity = FwLiteProjectSyncActivitySource.Value.StartActivity();
importTo = new ResumableImportApi(importTo);
await ImportWritingSystems(importTo, importFrom);
await foreach (var partOfSpeech in importFrom.GetPartsOfSpeech())
{
await importTo.CreatePartOfSpeech(partOfSpeech);
logger.LogInformation("Imported part of speech {Id}", partOfSpeech.Id);
}
await foreach (var publication in importFrom.GetPublications())
{
await importTo.CreatePublication(publication);
logger.LogInformation("Imported publication {Id}", publication.Id);
}
await foreach (var complexFormType in importFrom.GetComplexFormTypes())
{
await importTo.CreateComplexFormType(complexFormType);
logger.LogInformation("Imported complex form type {Id}", complexFormType.Id);
}
// Morph types are created automatically for CRDT projects, so we update them instead of creating them
// Optimize this to a simple foreach like above in #2350
var importFromMorphTypes = await importFrom.GetMorphTypes().ToArrayAsync();
var existingMorphTypes = await importTo.GetMorphTypes().ToArrayAsync();
await MorphTypeSync.Sync(existingMorphTypes, importFromMorphTypes, importTo);
logger.LogInformation("Importing semantic domains");
await importTo.BulkImportSemanticDomains(importFrom.GetSemanticDomains());
logger.LogInformation("Importing {Count} entries", entryCount);
await importTo.BulkCreateEntries(importFrom.GetAllEntries());
activity?.SetTag("app.import.entries", entryCount);
logger.LogInformation("Imported {Count} entries", entryCount);
}
internal async Task ImportWritingSystems(IMiniLcmApi importTo, IMiniLcmApi importFrom)
{
var writingSystems = await importFrom.GetWritingSystems();
foreach (var ws in writingSystems.Analysis)
{
await importTo.CreateWritingSystem(ws);
logger.LogInformation("Imported ws {WsId}", ws.WsId);
}
foreach (var ws in writingSystems.Vernacular)
{
await importTo.CreateWritingSystem(ws);
logger.LogInformation("Imported ws {WsId}", ws.WsId);
}
}
}