-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTableFormater.cs
40 lines (34 loc) · 1.45 KB
/
TableFormater.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Linq;
using System.Text;
using ConsoleTables;
namespace azure_usage_report
{
public static class TableFormater
{
public static string FormatSummary(OutputFormat format, string title, RegionalUsage[] regionalUsages)
{
var locations = regionalUsages
.Select(u => u.Location)
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToArray();
var columns = new string[] { "resource" }.Concat(locations).Append("total").ToArray();
var table = new ConsoleTable(columns);
foreach (var resourceGroup in regionalUsages.GroupBy(u => u.Resource))
{
var row = new string[] { resourceGroup.Key }
.Concat(locations.Select(l => (resourceGroup.FirstOrDefault(u => u.Location.Equals(l, StringComparison.OrdinalIgnoreCase))?.Current ?? 0).ToString("N0")))
.Append(resourceGroup.Select(v => v.Current).Sum().ToString("N0"))
.ToArray();
table.AddRow(row);
}
var output = new StringBuilder();
if (!string.IsNullOrEmpty(title))
{
output.AppendLine(format == OutputFormat.Markdown ? "# " + title : title);
}
output.AppendLine(format == OutputFormat.Markdown ? table.ToMarkDownString() : table.ToStringAlternative());
return output.ToString();
}
}
}