114 lines
6.5 KiB
C#
114 lines
6.5 KiB
C#
using PlotLine.Data;
|
|
using PlotLine.Models;
|
|
using PlotLine.ViewModels;
|
|
|
|
namespace PlotLine.Services;
|
|
|
|
public interface IStoryIntelligenceGlobalProgressService
|
|
{
|
|
Task<StoryIntelligenceGlobalProgressViewModel> GetForCurrentUserAsync();
|
|
Task<StoryIntelligenceGlobalProgressViewModel> GetForUserAsync(int userId);
|
|
}
|
|
|
|
public sealed class StoryIntelligenceGlobalProgressService(
|
|
ICurrentUserService currentUser,
|
|
IStoryIntelligenceResultRepository runs, IOnboardingRepository? onboarding=null, IManuscriptScanPreviewStore? scans=null,
|
|
IOnboardingBuildRepository? builds=null, IBookRepository? bookRepository=null, ICoreImportBasicLocationService? locations=null, IProjectRepository? projects=null) : IStoryIntelligenceGlobalProgressService
|
|
{
|
|
public async Task<StoryIntelligenceGlobalProgressViewModel> GetForCurrentUserAsync()
|
|
{
|
|
if (currentUser.UserId is not int userId)
|
|
{
|
|
return new StoryIntelligenceGlobalProgressViewModel();
|
|
}
|
|
|
|
return await GetForUserAsync(userId);
|
|
}
|
|
|
|
public async Task<StoryIntelligenceGlobalProgressViewModel> GetForUserAsync(int userId)
|
|
{
|
|
var summaries = await runs.ListActiveBookRunSummariesForUserAsync(userId);
|
|
var books = new List<StoryIntelligenceGlobalProgressBookViewModel>();
|
|
foreach (var summary in summaries)
|
|
{
|
|
var bookRuns = summary.BookID.HasValue
|
|
? await runs.ListRunsByBookForUserAsync(summary.BookID.Value, userId)
|
|
: [];
|
|
var relevantRuns = bookRuns
|
|
.Where(run => run.CreatedUtc >= summary.FirstCreatedUtc
|
|
&& run.Status is StoryIntelligenceRunStatuses.Pending
|
|
or StoryIntelligenceRunStatuses.Running
|
|
or StoryIntelligenceRunStatuses.Completed
|
|
or StoryIntelligenceRunStatuses.CompletedWithWarnings
|
|
or StoryIntelligenceRunStatuses.Failed
|
|
or StoryIntelligenceRunStatuses.Cancelled)
|
|
.Select(run => new StoryIntelligenceGlobalProgressRunViewModel
|
|
{
|
|
RunID = run.StoryIntelligenceRunID,
|
|
ProjectID = run.ProjectID,
|
|
BookID = run.BookID,
|
|
BookTitle = string.IsNullOrWhiteSpace(summary.BookDisplayTitle) ? "Book" : summary.BookDisplayTitle,
|
|
Status = string.IsNullOrWhiteSpace(run.Status) ? StoryIntelligenceRunStatuses.Pending : run.Status,
|
|
TotalDetectedScenes = run.TotalDetectedScenes ?? 0,
|
|
CompletedScenes = run.CompletedScenes ?? 0,
|
|
FailedScenes = run.FailedScenes ?? 0,
|
|
TotalDurationMs = run.TotalDurationMs ?? 0,
|
|
UpdatedUtc = run.UpdatedUtc
|
|
})
|
|
.ToList();
|
|
|
|
books.Add(new StoryIntelligenceGlobalProgressBookViewModel
|
|
{
|
|
ProjectID = summary.ProjectID,
|
|
BookID = summary.BookID,
|
|
BookTitle = string.IsNullOrWhiteSpace(summary.BookDisplayTitle) ? "Book" : summary.BookDisplayTitle,
|
|
RunCount = summary.RunCount,
|
|
PendingRunCount = summary.PendingRunCount,
|
|
RunningRunCount = summary.RunningRunCount,
|
|
CompletedRunCount = summary.CompletedRunCount,
|
|
FailedRunCount = summary.FailedRunCount,
|
|
TotalExpectedScenes = summary.TotalExpectedScenes,
|
|
CompletedScenes = summary.CompletedScenes,
|
|
FailedScenes = summary.FailedScenes,
|
|
ActiveScenes = summary.ActiveScenes,
|
|
Status = summary.Status,
|
|
TotalDurationMs = summary.TotalDurationMs,
|
|
UpdatedUtc = summary.UpdatedUtc,
|
|
Runs = relevantRuns
|
|
});
|
|
}
|
|
|
|
return new StoryIntelligenceGlobalProgressViewModel
|
|
{
|
|
Books = books, CoreImport = await CoreImport(userId)
|
|
};
|
|
}
|
|
private async Task<CoreImportGlobalProgress?> CoreImport(int user)
|
|
{
|
|
if(onboarding==null||scans==null||bookRepository==null)return null;
|
|
var state=await onboarding.GetAsync(user);
|
|
if(state==null||state.OnboardingCompletedUtc.HasValue||state.WritingJourney!=WritingJourneyValues.ManuscriptMostlyComplete||state.BookID is not int book)return null;
|
|
if(projects!=null&&state.ProjectID is int project&&(await projects.GetForUserAsync(project,user))?.IsOwner!=true)return null;
|
|
var title=await bookRepository.GetAsync(book);
|
|
if(title==null||title.ProjectID!=state.ProjectID)return null;
|
|
var scan=await scans.GetStateAsync(user,state.UserOnboardingStateID);
|
|
if(scan.PreviewID is Guid scoped && (await scans.GetPreviewAsync(user,scoped))?.BookID!=book)return null;
|
|
CoreImportGlobalProgress Item(string stage,string message,int? percent=null,string url="/onboarding")=>new(book,title.BookDisplayTitle,stage,message,percent,url);
|
|
if(scan.Status==ManuscriptScanStatuses.Running)return Item("Scanning Word",scan.Message??"Reading your manuscript from Word",scan.PercentComplete);
|
|
if(scan.Status==ManuscriptScanStatuses.Failed)return Item("Scan needs attention",scan.Message??"Return to the existing import to review the problem");
|
|
if(scan.PreviewID is not Guid preview)return Item("Continue manuscript setup",state.CurrentStep==OnboardingSteps.WritingSoftware?"Choose your writing software to continue the saved setup":"Return to your saved manuscript setup");
|
|
var progress=await scans.GetBuildProgressAsync(user,preview);
|
|
if(progress?.Status=="Running")return Item("Detecting scenes",progress.Message,progress.PercentComplete);
|
|
if(progress?.Status=="Failed")return Item("Scene detection needs attention",progress.Message);
|
|
var build=builds==null?null:await builds.GetByPreviewAsync(user,preview);
|
|
if(build!=null&&build.BookID==book){
|
|
var done=locations!=null&&await locations.IsCompleteAsync(user,preview);
|
|
return Item(done?"Review characters":"Review locations","Finish the remaining core import review",url:$"/onboarding/{(done?"scan-characters":"scan-locations")}?previewId={preview:D}");
|
|
}
|
|
return scan.ReviewStatus==ManuscriptScanReviewStatuses.ReadyToImport
|
|
?Item("Ready to detect scenes","Continue scene detection using the accepted manuscript")
|
|
:Item("Review chapters","Your Word capture is saved; review the detected chapters",url:$"/onboarding/scan-review?previewId={preview:D}");
|
|
}
|
|
|
|
}
|