From 5d92388b8f7cd182a705f32997894e3e9f21da80 Mon Sep 17 00:00:00 2001 From: Michiel van Oudheusden Date: Mon, 19 Feb 2024 16:10:00 +0100 Subject: [PATCH] Output tags in csv as columns --- src/OutputFormatters/CsvOutputFormatter.cs | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/OutputFormatters/CsvOutputFormatter.cs b/src/OutputFormatters/CsvOutputFormatter.cs index b2d3299..1b41269 100644 --- a/src/OutputFormatters/CsvOutputFormatter.cs +++ b/src/OutputFormatters/CsvOutputFormatter.cs @@ -34,6 +34,33 @@ public override Task WriteBudgets(BudgetsSettings settings, IEnumerable dailyCosts) { + if (settings.IncludeTags) + { + var dailyCostWithTags = new List(); + + // 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)expando)[tag] = tagValue.Value; + } + dailyCostWithTags.Add(expando); + } + + + return ExportToCsv(settings.SkipHeader, dailyCostWithTags); + } + else return ExportToCsv(settings.SkipHeader, dailyCosts); }