Skip to content

Instantly share code, notes, and snippets.

@recalde
Created November 10, 2024 23:20
Show Gist options
  • Save recalde/fb28c78eb86363aa1be66a16c00b9176 to your computer and use it in GitHub Desktop.
Save recalde/fb28c78eb86363aa1be66a16c00b9176 to your computer and use it in GitHub Desktop.
StatusBadge
using Microsoft.AspNetCore.Mvc;
using SkiaSharp;
[Route("api/status/badge")]
[ApiController]
public class StatusBadgeController : ControllerBase
{
[HttpGet]
public IActionResult GetStatusBadge()
{
// Example data - replace with actual metrics
string requestsPerHr = "1000";
string processTimePerHr = "200ms";
string podCount = "5";
string imageVersion = "v1.0.0";
int percentUtilization = 75;
using var bitmap = new SKBitmap(200, 50);
using var canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.White);
var textPaint = new SKPaint
{
Color = SKColors.Black,
TextSize = 12,
IsAntialias = true
};
// Draw the text metrics
canvas.DrawText($"Req/hr: {requestsPerHr}", 5, 20, textPaint);
canvas.DrawText($"ProcTime/hr: {processTimePerHr}", 5, 35, textPaint);
canvas.DrawText($"PodCount: {podCount}", 110, 20, textPaint);
canvas.DrawText($"Version: {imageVersion}", 110, 35, textPaint);
// Determine color based on percent utilization
SKColor barColor = percentUtilization < 50 ? SKColors.Green
: percentUtilization < 80 ? SKColors.Yellow
: SKColors.Red;
// Draw a utilization bar
var barWidth = (int)(180 * (percentUtilization / 100.0));
var barRect = new SKRect(5, 40, 5 + barWidth, 45);
using var barPaint = new SKPaint
{
Color = barColor,
IsAntialias = true
};
canvas.DrawRect(barRect, barPaint);
// Add percentage text next to the bar
canvas.DrawText($"{percentUtilization}%", 190, 45, textPaint);
using var image = SKImage.FromBitmap(bitmap);
using var data = image.Encode(SKEncodedImageFormat.Png, 100);
return File(data.ToArray(), "image/png");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment