Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get FLEx model version from repo and store in metadata #1082

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions backend/LexBoxApi/GraphQL/ProjectMutations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,25 @@ public async Task<IQueryable<Project>> UpdateLangProjectId(string code,
return dbContext.Projects.Where(p => p.Id == projectId);
}

[Error<NotFoundException>]
[Error<DbError>]
[Error<UnauthorizedAccessException>]
[UseMutationConvention]
[UseFirstOrDefault]
[UseProjection]
public async Task<IQueryable<Project>> UpdateFLExModelVersion(string code,
IPermissionService permissionService,
[Service] ProjectService projectService,
LexBoxDbContext dbContext)
{
var projectId = await projectService.LookupProjectId(code);
await permissionService.AssertCanManageProject(projectId);
var project = await dbContext.Projects.FindAsync(projectId);
NotFoundException.ThrowIfNull(project);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is needed as we already know the project exists because we were able to find a project ID from the code. Which means we can skip the project query entirely

await projectService.UpdateFLExModelVersion(projectId);
return dbContext.Projects.Where(p => p.Id == projectId);
}

[Error<NotFoundException>]
[Error<LastMemberCantLeaveException>]
[UseMutationConvention]
Expand Down
16 changes: 16 additions & 0 deletions backend/LexBoxApi/Services/HgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,22 @@
return null;
}

public async Task<int?> GetModelVersionOfFlexProject(ProjectCode code, CancellationToken token = default)
{
var result = await ExecuteHgCommandServerCommand(code, "flexmodelversion", token);
var text = await result.ReadAsStringAsync(token);
try
{
var json = JsonDocument.Parse(text);
return json.RootElement.GetProperty("modelversion").GetInt32();
}
catch
{
if (int.TryParse(text, out var num)) return num;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fallback in case a future FLEx or Chorus update decides to make this a simple text file containing the number instead of JSON. Probably not needed; can remove if you think I should remove it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's remove it as it's planning for something that might never happen

}
return null;
}

public Task RevertRepo(ProjectCode code, string revHash)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -430,7 +446,7 @@
{
var hash = await GetTipHash(code, timeoutSource.Token);
var isEmpty = hash == AllZeroHash;
done = expectedState switch

Check warning on line 449 in backend/LexBoxApi/Services/HgService.cs

View workflow job for this annotation

GitHub Actions / Build API / publish-api

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(LexBoxApi.Services.RepoEmptyState)2' is not covered.
{
RepoEmptyState.Empty => isEmpty,
RepoEmptyState.NonEmpty => !isEmpty
Expand Down
12 changes: 12 additions & 0 deletions backend/LexBoxApi/Services/ProjectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ public async Task UpdateProjectLangProjectId(Guid projectId)
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)
{
// No need for a transaction if we're just saving a single item
Expand Down
1 change: 1 addition & 0 deletions backend/LexCore/Entities/FlexProjectMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class FlexProjectMetadata
/// </summary>
public Guid? LangProjectId { get; set; }
public ProjectWritingSystems? WritingSystems { get; set; }
public int? FlexModelVersion { get; set; }
}

public class ProjectWritingSystems
Expand Down
1 change: 1 addition & 0 deletions backend/LexCore/ServiceInterfaces/IHgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IHgService
Task SoftDeleteRepo(ProjectCode code, string deletedRepoSuffix);
Task<ProjectWritingSystems?> GetProjectWritingSystems(ProjectCode code, CancellationToken token = default);
Task<Guid?> GetProjectIdOfFlexProject(ProjectCode code, CancellationToken token = default);
Task<int?> GetModelVersionOfFlexProject(ProjectCode code, CancellationToken token = default);
BackupExecutor? BackupRepo(ProjectCode code);
Task ResetRepo(ProjectCode code);
Task FinishReset(ProjectCode code, Stream zipFile);
Expand Down
Loading
Loading