Skip to content

Commit

Permalink
Output tags in csv as columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mivano committed Feb 19, 2024
1 parent 9ecc0d0 commit 5d92388
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/OutputFormatters/CsvOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ public override Task WriteBudgets(BudgetsSettings settings, IEnumerable<BudgetIt

public override Task WriteDailyCost(DailyCostSettings settings, IEnumerable<CostDailyItem> dailyCosts)
{
if (settings.IncludeTags)
{
var dailyCostWithTags = new List<dynamic>();

// Get all the unique tag values first
var tags = dailyCosts.SelectMany(a => a.Tags).Select(a => a.Key).Distinct().OrderBy(a => a).ToList();

// Map the dailyCosts to a dynamic object with the tags as columns
foreach (var dailyCost in dailyCosts)
{
dynamic expando = new ExpandoObject();
expando.Date = dailyCost.Date;
expando.Cost = dailyCost.Cost;
expando.Currency = dailyCost.Currency;
expando.CostUsd = dailyCost.CostUsd;
foreach (var tag in tags)
{
var tagValue = dailyCost.Tags.FirstOrDefault(a => a.Key == tag);
((IDictionary<string, object>)expando)[tag] = tagValue.Value;
}
dailyCostWithTags.Add(expando);
}


return ExportToCsv(settings.SkipHeader, dailyCostWithTags);
}
else
return ExportToCsv(settings.SkipHeader, dailyCosts);
}

Expand Down

0 comments on commit 5d92388

Please sign in to comment.