-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add merge status API to fw-headless (#1294)
Creates a new GET /api/crdt-sync-status endpoint. Returns 404 if the project ID does not match any project in the lexbox database. For all other projects, this returns Syncing if a CRDT sync is currently in progress, NeverSynced if there is no CRDT project yet, and ReadyToSync if the CRDT project exists but is not currently syncing. If the status is ReadyToSync, it will also return a count of commits available in the Lexbox DB that are not yet present in the local CRDT database. (The count is naive, just doing (lexbox commits - local commits), so if the local DB has commits not present in Lexbox the count could even end up negative.)
- Loading branch information
Showing
10 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace FwHeadless.Services; | ||
|
||
public static class HttpHelpers | ||
{ | ||
public static Guid? GetProjectId(this HttpContext? context) | ||
{ | ||
if (context is null) return null; | ||
if (context.Request.Query.TryGetValue("projectId", out var projectIds) && projectIds.FirstOrDefault() is string idStr) | ||
{ | ||
if (Guid.TryParse(idStr, out var projectId)) return projectId; | ||
} | ||
return null; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/FwHeadless/Services/ProjectContextFromIdService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using LcmCrdt; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace FwHeadless.Services; | ||
|
||
// TODO: Pick better name | ||
public class ProjectContextFromIdService(IOptions<FwHeadlessConfig> config, CrdtProjectsService projectsService, ProjectLookupService projectLookupService) | ||
{ | ||
public async Task PopulateProjectContext(HttpContext context, Func<Task> next) | ||
{ | ||
if (context.GetProjectId() is Guid projectId) | ||
{ | ||
var projectCode = await projectLookupService.GetProjectCode(projectId); | ||
if (projectCode is not null) | ||
{ | ||
var projectFolder = Path.Join(config.Value.ProjectStorageRoot, $"{projectCode}-{projectId}"); | ||
var crdtFile = Path.Join(projectFolder, "crdt.sqlite"); | ||
if (File.Exists(crdtFile)) | ||
{ | ||
var project = new CrdtProject("crdt", crdtFile); | ||
projectsService.SetProjectScope(project); | ||
await context.RequestServices.GetRequiredService<CurrentProjectService>().PopulateProjectDataCache(); | ||
} | ||
} | ||
} | ||
await next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Concurrent; | ||
|
||
namespace FwHeadless.Services; | ||
|
||
public class SyncJobStatusService() | ||
{ | ||
private ConcurrentDictionary<Guid, SyncJobStatus> Status { get; init; } = new(); | ||
|
||
public void StartSyncing(Guid projectId) | ||
{ | ||
Status.AddOrUpdate(projectId, (_) => SyncJobStatus.Running, (_, _) => SyncJobStatus.Running); | ||
} | ||
|
||
public void StopSyncing(Guid projectId) | ||
{ | ||
Status.Remove(projectId, out var _); | ||
} | ||
|
||
public SyncJobStatus SyncStatus(Guid projectId) | ||
{ | ||
return Status.TryGetValue(projectId, out var status) ? status : SyncJobStatus.NotRunning; | ||
} | ||
} | ||
|
||
public enum SyncJobStatus | ||
{ | ||
NotRunning, | ||
Running, | ||
} | ||
|
||
public enum ProjectSyncStatusEnum | ||
{ | ||
NeverSynced, | ||
ReadyToSync, | ||
Syncing, | ||
} | ||
|
||
// TODO: Bikeshed this name | ||
public record ProjectSyncStatus( | ||
ProjectSyncStatusEnum status, | ||
int ChangesAvailable) | ||
{ | ||
public static ProjectSyncStatus NeverSynced => new(ProjectSyncStatusEnum.NeverSynced, 0); | ||
public static ProjectSyncStatus Syncing => new(ProjectSyncStatusEnum.Syncing, 0); | ||
public static ProjectSyncStatus ReadyToSync(int changes) | ||
{ | ||
return new(ProjectSyncStatusEnum.ReadyToSync, changes); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
backend/FwHeadless/SendReceiveHelpers.cs → ...FwHeadless/Services/SendReceiveHelpers.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
backend/FwHeadless/SendReceiveService.cs → ...FwHeadless/Services/SendReceiveService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters