PlotDirector/PlotLine/Services/WritingContextMetrics.cs
2026-12-02 15:26:20 +00:00

32 lines
2.3 KiB
C#

using PlotLine.Data;
using Microsoft.AspNetCore.Mvc.Rendering;
using PlotLine.Models;
using PlotLine.ViewModels;
namespace PlotLine.Services;
// Timeline's metric rows include SQL-filled defaults. Only recorded SceneMetricValues are evidence.
public static class WritingContextMetrics
{
public static TimelineViewModel Build(TimelineData data,IReadOnlyList<SceneMetricValue> recorded)
{
var scenes=StoryStateService.OrderScenes(data).ToArray();var byId=scenes.ToDictionary(s=>s.SceneID);
return new(){Project=data.Project!,OrderedScenes=scenes,ChapterOptions=data.Chapters.Select(c=>new SelectListItem($"{c.ChapterNumber:0.##} · {c.ChapterTitle}",c.ChapterID.ToString())).ToArray(),MetricGraphs=recorded.OrderBy(p=>p.SortOrder).ThenBy(p=>p.MetricName).Where(p=>byId.ContainsKey(p.SceneID)).GroupBy(p=>p.MetricTypeID).Select(group=>{
var metric=group.First();return new MetricGraphViewModel{MetricTypeID=group.Key,MetricName=metric.MetricName,MinValue=metric.MinValue,MaxValue=metric.MaxValue,
Points=group.Select(p=>new MetricGraphPointViewModel{SceneID=p.SceneID,SceneNumber=byId[p.SceneID].SceneNumber,SceneTitle=byId[p.SceneID].SceneTitle,Value=p.Value}).ToArray()};
}).ToArray()};
}
public static TimelineViewModel BuildWindow(TimelineData data, IReadOnlyList<SceneMetricValue> recorded,
IReadOnlyList<SceneMetricType> types, int sceneId)
{
var model = Build(data, recorded);
var ordered = model.OrderedScenes.ToArray();
var index = Array.FindIndex(ordered, s => s.SceneID == sceneId);
model.OrderedScenes = index < 0 ? [] : ordered.Skip(Math.Max(0, index - 5)).Take(Math.Min(ordered.Length, index + 6) - Math.Max(0, index - 5)).ToArray();
var ids = model.OrderedScenes.Select(s => s.SceneID).ToHashSet();
model.MetricGraphs = types.Where(t => t.IsActive && t.IsEnabledForProject).OrderBy(t => t.SortOrder).ThenBy(t => t.MetricName)
.Select(t => new MetricGraphViewModel { MetricTypeID = t.MetricTypeID, MetricName = t.MetricName,
MinValue = t.MinValue, MaxValue = t.MaxValue,
Points = model.MetricGraphs.FirstOrDefault(g => g.MetricTypeID == t.MetricTypeID)?.Points.Where(p => ids.Contains(p.SceneID)).ToArray() ?? [] }).ToArray();
return model;
}
}