Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output tags in csv as columns #111

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading