namespace ZB.MOM.WW.MxGateway.Server.Dashboard.Components;
public static class DashboardDisplay
{
///
/// Formats a nullable date and time value for display.
///
/// The date and time to format.
/// Formatted date and time string or "-" if null.
public static string DateTime(DateTimeOffset? value)
{
return value.HasValue
? value.Value.UtcDateTime.ToString("yyyy-MM-dd HH:mm:ss 'UTC'", System.Globalization.CultureInfo.InvariantCulture)
: "-";
}
///
/// Formats a time span duration for display.
///
/// The duration to format.
/// Formatted duration string.
public static string Duration(TimeSpan value)
{
return value.TotalDays >= 1
? value.ToString(@"d\.hh\:mm\:ss", System.Globalization.CultureInfo.InvariantCulture)
: value.ToString(@"hh\:mm\:ss", System.Globalization.CultureInfo.InvariantCulture);
}
///
/// Formats a nullable text value for display.
///
/// The text to format.
/// Formatted text or "-" if null or empty.
public static string Text(string? value)
{
return string.IsNullOrWhiteSpace(value) ? "-" : value;
}
///
/// Formats a nullable text value for display, shortened to a maximum length.
///
///
/// For table cells bound to text the gateway does not control — fault messages, COM
/// exception text, SQL errors. One multi-line exception otherwise makes a single row
/// several times taller than its neighbours. Call sites keep the full text reachable
/// on the element's title and on the row's detail page.
///
/// The text to format.
/// Maximum characters to render before the ellipsis.
/// Formatted text, ellipsized when longer than , or "-" if null or empty.
public static string Abbreviate(string? value, int maxLength = 80)
{
if (string.IsNullOrWhiteSpace(value))
{
return "-";
}
// Length-checked, never a bare range slice: a value shorter than maxLength
// would throw and take the whole page render down with it.
return value.Length <= maxLength
? value
: string.Concat(value.AsSpan(0, maxLength).TrimEnd(), "…");
}
///
/// Formats a long count value for display with thousands separator.
///
/// The count to format.
/// Formatted count string.
public static string Count(long value)
{
return value.ToString("N0", System.Globalization.CultureInfo.InvariantCulture);
}
///
/// Retrieves a metric value from a snapshot by name and optional dimension.
///
/// Dashboard snapshot.
/// Metric name.
/// Optional metric dimension.
/// Metric value or zero if not found.
public static long MetricValue(DashboardSnapshot snapshot, string name, string? dimension = null)
{
return snapshot.Metrics.FirstOrDefault(metric =>
string.Equals(metric.Name, name, StringComparison.Ordinal)
&& string.Equals(metric.Dimension, dimension, StringComparison.Ordinal))?.Value ?? 0;
}
}