-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalysis.go
136 lines (120 loc) · 3.33 KB
/
analysis.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package riksbank
import (
"fmt"
"strings"
)
// AnalysisMethod represents the analysis method for comparing values in a period
type AnalysisMethod int
func (am AnalysisMethod) String() string {
if name, ok := AnalysisMethodNames[am]; ok {
return name
}
return ""
}
const (
// Real is the actual value for the period
Real AnalysisMethod = iota + 1
// Mean is the average value of all values in a period
Mean
// Min is the smallest value of all values in a period
Min
// Max is the largest value of all values in a period
Max
// Ultimo is the value of the last banking day in the period
Ultimo
)
var (
// AnalysisMethods are all known analysis methods
AnalysisMethods = map[string]AnalysisMethod{
"real": Real,
"mean": Mean,
"min": Min,
"max": Max,
"ultimo": Ultimo,
}
// AnalysisMethodNames are all known analysis method names
AnalysisMethodNames = map[AnalysisMethod]string{
Real: "real",
Mean: "mean",
Min: "min",
Max: "max",
Ultimo: "ultimo",
}
// DailyAnalysis are the analysis methods allowed for daily aggregation
DailyAnalysis = map[AnalysisMethod]struct{}{
Real: struct{}{},
}
// WeeklyAnalysis are the analysis methods allowed for weekly aggregation
WeeklyAnalysis = map[AnalysisMethod]struct{}{
Mean: struct{}{},
Min: struct{}{},
Max: struct{}{},
}
// MonthlyAnalysis are the analysis methods allowed for monthly aggregation
MonthlyAnalysis = map[AnalysisMethod]struct{}{
Mean: struct{}{},
Min: struct{}{},
Max: struct{}{},
}
// QuarterlyAnalysis are the analysis methods allowed for quarterly aggregation
QuarterlyAnalysis = map[AnalysisMethod]struct{}{
Mean: struct{}{},
Min: struct{}{},
Max: struct{}{},
}
// YearlyAnalysis are the analysis methods allowed for yearly aggregation
YearlyAnalysis = map[AnalysisMethod]struct{}{
Mean: struct{}{},
Min: struct{}{},
Max: struct{}{},
Ultimo: struct{}{},
}
)
// UnknownAnalysisForAggregateError happens when the analysis method cannot be used for the aggregate method
type UnknownAnalysisForAggregateError struct {
anm AnalysisMethod
agm AggregateMethod
}
func (e UnknownAnalysisForAggregateError) Error() string {
return fmt.Sprintf("%s aggregate does not support analysis method: %v", e.agm.Name(), e.anm)
}
// UnknownAnalysisError happens when the analysis method does not exist at all
type UnknownAnalysisError struct {
s string
}
func (e UnknownAnalysisError) Error() string {
return fmt.Sprintf("analysis method does not exist: %s", e.s)
}
// ParseAggregateAnalysis will attempt to parse a string with an aggregate
func ParseAggregateAnalysis(aggregate AggregateMethod, s string) (AnalysisMethod, error) {
analysis, ok := AnalysisMethods[strings.ToLower(s)]
if !ok {
return AnalysisMethod(0), UnknownAnalysisError{s}
}
err := UnknownAnalysisForAggregateError{analysis, aggregate}
switch aggregate {
case Daily:
if _, ok := DailyAnalysis[analysis]; !ok {
return analysis, err
}
case Weekly:
if _, ok := WeeklyAnalysis[analysis]; !ok {
return analysis, err
}
case Monthly:
if _, ok := MonthlyAnalysis[analysis]; !ok {
return analysis, err
}
case Quarterly:
if _, ok := QuarterlyAnalysis[analysis]; !ok {
return analysis, err
}
case Yearly:
if _, ok := YearlyAnalysis[analysis]; !ok {
return analysis, err
}
default:
return analysis, err
}
return analysis, nil
}