@@ -17,6 +17,11 @@ import (
17
17
//go:embed "report.tmpl"
18
18
var reportTmpl string
19
19
20
+ const (
21
+ // DefaultReportDateFormat is the default date format used in the report.
22
+ DefaultReportDateFormat = "2006-01-02"
23
+ )
24
+
20
25
// ReportColumn defines the interface that all report data columns must implement.
21
26
// This interface ensures that different types of data columns can be used
22
27
// consistently within the report generation process.
@@ -34,62 +39,55 @@ type ReportColumn interface {
34
39
Value () string
35
40
}
36
41
37
- // reportModel struct holds the data that is exposed to the template renderer
38
- // for generating the report. It encapsulates all the information necessary
39
- // to render the report's content, including data, and annotations.
40
- type reportModel struct {
41
- Title string
42
- Date <- chan time.Time
43
- Columns []ReportColumn
44
- Views [][]int
45
- }
46
-
47
42
// Report generates an HTML file containing an interactive chart that
48
43
// visually represents the provided data and annotations.
49
44
//
50
45
// The generated HTML file can be opened in a web browser to explore
51
46
// the data visually, interact with the chart elements, and view
52
47
// the associated annotations.
53
48
type Report struct {
54
- model reportModel
49
+ Title string
50
+ Date <- chan time.Time
51
+ Columns []ReportColumn
52
+ Views [][]int
53
+ DateFormat string
55
54
}
56
55
57
56
// NewReport takes a channel of time as the time axis and returns a new
58
57
// instance of the Report struct. This instance can later be used to
59
58
// add data and annotations and subsequently generate a report.
60
59
func NewReport (title string , date <- chan time.Time ) * Report {
61
60
return & Report {
62
- model : reportModel {
63
- Title : title ,
64
- Date : date ,
65
- Columns : []ReportColumn {},
66
- Views : [][]int {
67
- {},
68
- },
61
+ Title : title ,
62
+ Date : date ,
63
+ Columns : []ReportColumn {},
64
+ Views : [][]int {
65
+ {},
69
66
},
67
+ DateFormat : DefaultReportDateFormat ,
70
68
}
71
69
}
72
70
73
71
// AddChart adds a new chart to the report and returns its unique
74
72
// identifier. This identifier can be used later to refer to the
75
73
// chart and add columns to it.
76
74
func (r * Report ) AddChart () int {
77
- r .model . Views = append (r . model .Views , []int {})
78
- return len (r .model . Views ) - 1
75
+ r .Views = append (r .Views , []int {})
76
+ return len (r .Views ) - 1
79
77
}
80
78
81
79
// AddColumn adds a new data column to the specified charts. If no
82
80
// chart is specified, it will be added to the main chart.
83
81
func (r * Report ) AddColumn (column ReportColumn , charts ... int ) {
84
- r .model . Columns = append (r . model .Columns , column )
85
- columnID := len (r .model . Columns )
82
+ r .Columns = append (r .Columns , column )
83
+ columnID := len (r .Columns )
86
84
87
85
if len (charts ) == 0 {
88
86
charts = append (charts , 0 )
89
87
}
90
88
91
89
for _ , chartID := range charts {
92
- r .model . Views [chartID ] = append (r . model .Views [chartID ], columnID )
90
+ r .Views [chartID ] = append (r .Views [chartID ], columnID )
93
91
}
94
92
}
95
93
@@ -102,7 +100,7 @@ func (r *Report) WriteToWriter(writer io.Writer) error {
102
100
return err
103
101
}
104
102
105
- return tmpl .Execute (writer , r . model )
103
+ return tmpl .Execute (writer , r )
106
104
}
107
105
108
106
// WriteToFile writes the generated report content to a file with
0 commit comments