Skip to content

Commit

Permalink
160 fix plotting (#163)
Browse files Browse the repository at this point in the history
* Add missing ConfigureAwait(false) calls

* Done

* Fix API

---------

Co-authored-by: Vincent Wilms <[email protected]>
  • Loading branch information
Apollo3zehn and Vincent Wilms authored Dec 3, 2024
1 parent 65a9f5d commit d4f48fe
Show file tree
Hide file tree
Showing 10 changed files with 2,614 additions and 2,533 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: "9.0.x"
dotnet-quality: "preview"

- name: Set up Python
uses: actions/setup-python@v3
Expand Down
12 changes: 10 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"x-generator": "NSwag v14.0.8.0 (NJsonSchema v11.0.1.0 (Newtonsoft.Json v13.0.0.0))",
"x-generator": "NSwag v14.1.0.0 (NJsonSchema v11.0.2.0 (Newtonsoft.Json v13.0.0.0))",
"openapi": "3.0.0",
"info": {
"title": "Nexus REST API",
Expand Down Expand Up @@ -534,7 +534,15 @@
},
"responses": {
"200": {
"description": ""
"description": "",
"content": {
"application/octet-stream": {
"schema": {
"type": "string",
"format": "binary"
}
}
}
}
}
}
Expand Down
59 changes: 47 additions & 12 deletions src/Nexus.UI/Charts/AvailabilityChart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Nexus.UI.Charts;
public partial class AvailabilityChart
{
private const float LINE_HEIGHT = 7.0f;

private const float HALF_LINE_HEIGHT = LINE_HEIGHT / 2;

[Inject]
Expand Down Expand Up @@ -43,27 +44,37 @@ private void PaintSurface(SKPaintGLSurfaceEventArgs e)
Color = new SKColor(249, 115, 22, 0x19)
};

using var axisTitleFont = new SKFont
{
Size = 17
};

using var axisTitlePaint = new SKPaint
{
TextSize = 17,
IsAntialias = true,
Color = new SKColor(0x55, 0x55, 0x55),
TextAlign = SKTextAlign.Center
Color = new SKColor(0x55, 0x55, 0x55)
};

using var axisLabelFont = new SKFont
{
Typeface = TypeFaceService.GetTTF("Courier New Bold")
};

using var axisLabelPaint = new SKPaint
{
IsAntialias = true,
Typeface = TypeFaceService.GetTTF("Courier New Bold"),
Color = new SKColor(0x55, 0x55, 0x55)
};

using var axisLabelCenteredFont = new SKFont
{
Typeface = TypeFaceService.GetTTF("Courier New Bold")
};

using var axisLabelCenteredPaint = new SKPaint
{
IsAntialias = true,
Typeface = TypeFaceService.GetTTF("Courier New Bold"),
Color = new SKColor(0x55, 0x55, 0x55),
TextAlign = SKTextAlign.Center
Color = new SKColor(0x55, 0x55, 0x55)
};

using var axisTickPaint = new SKPaint
Expand All @@ -79,12 +90,19 @@ private void PaintSurface(SKPaintGLSurfaceEventArgs e)
using (var canvasRestore = new SKAutoCanvasRestore(canvas))
{
canvas.RotateDegrees(270, xMin, yMin + yRange / 2);
canvas.DrawText("Availability / %", new SKPoint(xMin, yMin + yRange / 2), axisTitlePaint);

canvas.DrawText(
"Availability / %",
new SKPoint(xMin, yMin + yRange / 2),
SKTextAlign.Center,
axisTitleFont,
axisTitlePaint
);
}

xMin += 10;

var widthPerCharacter = axisLabelPaint.MeasureText(" ");
var widthPerCharacter = axisLabelFont.MeasureText(" ");
var desiredYLabelCount = 11;
var maxYLabelCount = yRange / 50;
var ySkip = (int)(desiredYLabelCount / (float)maxYLabelCount) + 1;
Expand All @@ -98,7 +116,7 @@ private void PaintSurface(SKPaintGLSurfaceEventArgs e)
var label = $"{(int)(relative * 100),3:D0}";
var lineOffset = widthPerCharacter * 3;

canvas.DrawText(label, new SKPoint(xMin, y + HALF_LINE_HEIGHT), axisLabelPaint);
canvas.DrawText(label, new SKPoint(xMin, y + HALF_LINE_HEIGHT), axisLabelFont, axisLabelPaint);
canvas.DrawLine(new SKPoint(xMin + lineOffset, y), new SKPoint(xMax, y), axisTickPaint);
}
}
Expand Down Expand Up @@ -137,10 +155,27 @@ private void PaintSurface(SKPaintGLSurfaceEventArgs e)
if ((i + xSkip) % xSkip == 0)
{
var currentBegin = AvailabilityData.Begin.AddDays(i);
canvas.DrawText(currentBegin.ToString("dd.MM"), xMin + (i + 0.5f) * valueWidth, yMax - 20, axisLabelCenteredPaint);

canvas.DrawText(
currentBegin.ToString("dd.MM"),
xMin + (i + 0.5f) * valueWidth,
yMax - 20,
SKTextAlign.Center,
axisLabelCenteredFont,
axisLabelCenteredPaint
);

if (lastBegin.Year != currentBegin.Year)
canvas.DrawText(currentBegin.ToString("yyyy"), xMin + (i + 0.5f) * valueWidth, yMax, axisLabelCenteredPaint);
{
canvas.DrawText(
currentBegin.ToString("yyyy"),
xMin + (i + 0.5f) * valueWidth,
yMax,
SKTextAlign.Center,
axisLabelCenteredFont,
axisLabelCenteredPaint
);
}

lastBegin = currentBegin;
}
Expand Down
66 changes: 52 additions & 14 deletions src/Nexus.UI/Charts/Chart.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ protected override void OnParametersSet()
.GroupBy(lineSeries => lineSeries.Unit)
.ToDictionary(group => GetAxisInfo(group.Key, group), group => group.ToArray());

_skiaView.Invalidate();
if (OperatingSystem.IsBrowser())
_skiaView.Invalidate();
});
}
}
Expand Down Expand Up @@ -189,7 +190,9 @@ public void OnMouseUp()
zoomBox.Height > 0)
{
ApplyZoom(zoomBox);
_skiaView.Invalidate();

if (OperatingSystem.IsBrowser())
_skiaView.Invalidate();
}
}

Expand Down Expand Up @@ -218,7 +221,8 @@ private void OnDoubleClick(MouseEventArgs e)
var relativePosition = JSRuntime.Invoke<Position>("nexus.chart.toRelative", _chartId, e.ClientX, e.ClientY);
DrawAuxiliary(relativePosition);

_skiaView.Invalidate();
if (OperatingSystem.IsBrowser())
_skiaView.Invalidate();
}

private void OnWheel(WheelEventArgs e)
Expand Down Expand Up @@ -250,13 +254,16 @@ private void OnWheel(WheelEventArgs e)
ApplyZoom(zoomBox);
DrawAuxiliary(relativePosition);

_skiaView.Invalidate();
if (OperatingSystem.IsBrowser())
_skiaView.Invalidate();
}

private void ToggleSeriesEnabled(LineSeries series)
{
series.Show = !series.Show;
_skiaView.Invalidate();

if (OperatingSystem.IsBrowser())
_skiaView.Invalidate();
}

#region Draw
Expand Down Expand Up @@ -546,9 +553,13 @@ private void ResetZoom()

private float DrawYAxes(SKCanvas canvas, float xMin, float yMin, float yMax, Dictionary<AxisInfo, LineSeries[]> axesMap)
{
using var axisLabelFont = new SKFont
{
Typeface = TypeFaceService.GetTTF("Courier New Bold")
};

using var axisLabelPaint = new SKPaint
{
Typeface = TypeFaceService.GetTTF("Courier New Bold"),
IsAntialias = true,
Color = new SKColor(0x55, 0x55, 0x55)
};
Expand All @@ -562,7 +573,7 @@ private float DrawYAxes(SKCanvas canvas, float xMin, float yMin, float yMax, Dic
var currentOffset = xMin;
var canvasRange = yMax - yMin;
var maxTickCount = Math.Max(1, (int)Math.Round(canvasRange / 50, MidpointRounding.AwayFromZero));
var widthPerCharacter = axisLabelPaint.MeasureText(" ");
var widthPerCharacter = axisLabelFont.MeasureText(" ");

foreach (var axesEntry in axesMap)
{
Expand Down Expand Up @@ -593,7 +604,7 @@ private float DrawYAxes(SKCanvas canvas, float xMin, float yMin, float yMax, Dic
var localUnitOffset = maxChars - axisInfo.Unit.Length;
var xUnit = currentOffset + localUnitOffset * widthPerCharacter;
var yUnit = yMin;
canvas.DrawText(axisInfo.Unit, new SKPoint(xUnit, yUnit), axisLabelPaint);
canvas.DrawText(axisInfo.Unit, new SKPoint(xUnit, yUnit), axisLabelFont, axisLabelPaint);

/* draw labels and ticks */
for (int i = 0; i < ticks.Length; i++)
Expand All @@ -608,7 +619,7 @@ private float DrawYAxes(SKCanvas canvas, float xMin, float yMin, float yMax, Dic
var x = currentOffset + localLabelOffset * widthPerCharacter;
var y = yMax - (tick - axisInfo.Min) * scaleFactor;

canvas.DrawText(label, new SKPoint(x, y + HALF_LINE_HEIGHT), axisLabelPaint);
canvas.DrawText(label, new SKPoint(x, y + HALF_LINE_HEIGHT), axisLabelFont, axisLabelPaint);

var tickX = currentOffset + textWidth + TICK_MARGIN_LEFT;
canvas.DrawLine(tickX, y, tickX + TICK_SIZE, y, axisTickPaint);
Expand Down Expand Up @@ -710,10 +721,13 @@ private float[] GetYTicks(float min, float max, int maxTickCount)

private void DrawTimeAxis(SKCanvas canvas, float xMin, float yMin, float xMax, float yMax, DateTime begin, DateTime end)
{
using var axisLabelFont = new SKFont
{
Typeface = TypeFaceService.GetTTF("Courier New Bold")
};

using var axisLabelPaint = new SKPaint
{
Typeface = TypeFaceService.GetTTF("Courier New Bold"),
TextAlign = SKTextAlign.Center,
IsAntialias = true,
Color = new SKColor(0x55, 0x55, 0x55)
};
Expand All @@ -727,6 +741,7 @@ private void DrawTimeAxis(SKCanvas canvas, float xMin, float yMin, float xMax, f
var canvasRange = xMax - xMin;
var maxTickCount = Math.Max(1, (int)Math.Round(canvasRange / 130, MidpointRounding.AwayFromZero));
var (config, ticks) = GetTimeTicks(begin, end, maxTickCount);

_timeAxisConfig = config;

var timeRange = (end - begin).Ticks;
Expand All @@ -741,7 +756,14 @@ private void DrawTimeAxis(SKCanvas canvas, float xMin, float yMin, float xMax, f

/* fast tick */
var tickLabel = tick.ToString(config.FastTickLabelFormat);
canvas.DrawText(tickLabel, x, yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP, axisLabelPaint);

canvas.DrawText(
tickLabel,
x, yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP,
SKTextAlign.Center,
axisLabelFont,
axisLabelPaint
);

/* slow tick */
var addSlowTick = IsSlowTickRequired(previousTick, tick, config.SlowTickTrigger);
Expand All @@ -751,13 +773,29 @@ private void DrawTimeAxis(SKCanvas canvas, float xMin, float yMin, float xMax, f
if (config.SlowTickLabelFormat1 is not null)
{
var slowTickLabel1 = tick.ToString(config.SlowTickLabelFormat1);
canvas.DrawText(slowTickLabel1, x, yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP + TIME_FAST_LABEL_OFFSET, axisLabelPaint);

canvas.DrawText(
slowTickLabel1,
x,
yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP + TIME_FAST_LABEL_OFFSET,
SKTextAlign.Center,
axisLabelFont,
axisLabelPaint
);
}

if (config.SlowTickLabelFormat2 is not null)
{
var slowTickLabel2 = tick.ToString(config.SlowTickLabelFormat2);
canvas.DrawText(slowTickLabel2, x, yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP + TIME_FAST_LABEL_OFFSET * 2, axisLabelPaint);

canvas.DrawText(
slowTickLabel2,
x,
yMax + TICK_SIZE + TIME_AXIS_MARGIN_TOP + TIME_FAST_LABEL_OFFSET * 2,
SKTextAlign.Center,
axisLabelFont,
axisLabelPaint
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Nexus.UI/Core/NexusDemoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,12 @@ public Task<IReadOnlyDictionary<string, CatalogItem>> SearchCatalogItemsAsync(IR
throw new NotImplementedException();
}

public void SetMetadata(string catalogId, CatalogMetadata metadata)
public HttpResponseMessage SetMetadata(string catalogId, CatalogMetadata metadata)
{
throw new NotImplementedException();
}

public Task SetMetadataAsync(string catalogId, CatalogMetadata metadata, CancellationToken cancellationToken = default)
public Task<HttpResponseMessage> SetMetadataAsync(string catalogId, CatalogMetadata metadata, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nexus.UI/Nexus.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="9.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
<PackageReference Include="MudBlazor" Version="7.15.0" />
<PackageReference Include="SkiaSharp" Version="2.88.9" />
<PackageReference Include="SkiaSharp.Views.Blazor" Version="2.88.9" />
<PackageReference Include="SkiaSharp" Version="3.116.0" />
<PackageReference Include="SkiaSharp.Views.Blazor" Version="3.116.0" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 5 additions & 4 deletions src/Nexus/API/v1/CatalogsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Nexus.Core;
using Nexus.Core.V1;
Expand Down Expand Up @@ -481,22 +482,22 @@ public Task<ActionResult<CatalogMetadata>>
/// <param name="metadata">The catalog metadata to set.</param>
/// <param name="cancellationToken">A token to cancel the current operation.</param>
[HttpPut("{catalogId}/metadata")]
public async Task<ActionResult<object>>
public async Task<ActionResult<object?>>
SetMetadataAsync(
string catalogId,
[FromBody] CatalogMetadata metadata,
CancellationToken cancellationToken)
{
if (metadata.Overrides?.Id != catalogId)
return (ActionResult<object>)UnprocessableEntity("The catalog ID does not match the ID of the catalog to update.");
return UnprocessableEntity("The catalog ID does not match the ID of the catalog to update.");

catalogId = WebUtility.UrlDecode(catalogId);

var response = await ProtectCatalogAsync<object>(catalogId, ensureReadable: false, ensureWritable: true, async catalogContainer =>
var response = await ProtectCatalogAsync<object?>(catalogId, ensureReadable: false, ensureWritable: true, async catalogContainer =>
{
await catalogContainer.UpdateMetadataAsync(metadata);

return new object();
return default!;

}, cancellationToken);

Expand Down
6 changes: 3 additions & 3 deletions src/Nexus/Core/CatalogContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static CatalogContainer CreateRoot(ICatalogManager catalogManager, IDatab
public async Task<IEnumerable<CatalogContainer>> GetChildCatalogContainersAsync(
CancellationToken cancellationToken)
{
await _semaphore.WaitAsync(cancellationToken);
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

try
{
Expand All @@ -109,7 +109,7 @@ public async Task<IEnumerable<CatalogContainer>> GetChildCatalogContainersAsync(
// TODO: Use Lazy instead?
public async Task<LazyCatalogInfo> GetLazyCatalogInfoAsync(CancellationToken cancellationToken)
{
await _semaphore.WaitAsync(cancellationToken);
await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

try
{
Expand All @@ -127,7 +127,7 @@ public async Task<LazyCatalogInfo> GetLazyCatalogInfoAsync(CancellationToken can

public async Task UpdateMetadataAsync(CatalogMetadata metadata)
{
await _semaphore.WaitAsync();
await _semaphore.WaitAsync().ConfigureAwait(false);

try
{
Expand Down
Loading

0 comments on commit d4f48fe

Please sign in to comment.