-
Notifications
You must be signed in to change notification settings - Fork 13
/
usagereportingconfig.go
298 lines (260 loc) · 10.7 KB
/
usagereportingconfig.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package config
import (
"os"
"strconv"
"strings"
"time"
"github.com/Axway/agent-sdk/pkg/cmd/properties"
"github.com/Axway/agent-sdk/pkg/util/exception"
"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/gorhill/cronexpr"
)
const (
// DEPRECATE remove old and new env vars as well as checks below
oldUsageReportingPublishEnvVar = "CENTRAL_PUBLISHUSAGE"
oldUsageReportingPublishMetricEnvVar = "CENTRAL_PUBLISHMETRIC"
oldUsageReportingIntervalEnvVar = "CENTRAL_EVENTAGGREGATIONINTERVAL"
newUsageReportingPublishEnvVar = "CENTRAL_USAGEREPORTING_PUBLISH"
newUsageReportingPublishMetricEnvVar = "CENTRAL_USAGEREPORTING_PUBLISHMETRIC"
newUsageReportingIntervalEnvVar = "CENTRAL_USAGEREPORTING_INTERVAL"
// QA EnvVars
qaUsageReportingScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_OFFLINESCHEDULE"
qaUsageReportingOfflineScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_OFFLINEREPORTSCHEDULE"
qaUsageReportingUsageScheduleEnvVar = "QA_CENTRAL_USAGEREPORTING_USAGESCHEDULE"
// Config paths
pathUsageReportingPublish = "central.usagereporting.publish"
pathUsageReportingPublishMetric = "central.usagereporting.publishMetric"
pathUsageReportingUsageSchedule = "central.usagereporting.usageSchedule"
pathUsageReportingInterval = "central.usagereporting.interval"
pathUsageReportingOffline = "central.usagereporting.offline"
pathUsageReportingSchedule = "central.usagereporting.offlineSchedule"
)
// UsageReportingConfig - Interface to get usage reporting config
type UsageReportingConfig interface {
GetURL() string
CanPublishUsage() bool
CanPublishMetric() bool
GetInterval() time.Duration
GetUsageSchedule() string
IsOfflineMode() bool
GetSchedule() string
GetReportSchedule() string
GetReportGranularity() int
UsingQAVars() bool
Validate()
}
// UsageReportingConfiguration - structure to hold all usage reporting settings
type UsageReportingConfiguration struct {
UsageReportingConfig
Publish bool `config:"publish"`
PublishMetric bool `config:"publishMetric"`
Interval time.Duration `config:"interval"`
UsageSchedule string `config:"usageSchedule"`
Offline bool `config:"offline"`
Schedule string `config:"offlineSchedule"`
URL string
reportSchedule string
reportGranularity int
qaVars bool
}
// NewUsageReporting - Creates the default usage reporting config
func NewUsageReporting(platformURL string) UsageReportingConfig {
defaultInterval := 15 * time.Minute
return &UsageReportingConfiguration{
URL: platformURL,
Publish: true,
PublishMetric: true,
Interval: defaultInterval,
UsageSchedule: "@daily",
Offline: false,
Schedule: "@hourly",
reportSchedule: "@monthly",
qaVars: false,
}
}
func (u *UsageReportingConfiguration) validateInterval() {
if val := os.Getenv(newUsageReportingIntervalEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
if val := os.Getenv(oldUsageReportingIntervalEnvVar); val != "" {
if value, err := time.ParseDuration(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingIntervalEnvVar, newUsageReportingIntervalEnvVar)
u.Interval = value
}
}
}
func (u *UsageReportingConfiguration) validatePublish() {
if val := os.Getenv(newUsageReportingPublishEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
val := os.Getenv(oldUsageReportingPublishEnvVar)
if val != "" {
if value, err := strconv.ParseBool(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingPublishEnvVar, newUsageReportingPublishEnvVar)
u.Publish = value
}
}
}
func (u *UsageReportingConfiguration) validatePublishMetric() {
if val := os.Getenv(newUsageReportingPublishMetricEnvVar); val != "" {
return // this env var is set use what has been parsed
}
// check if the old env var had a value
if val := os.Getenv(oldUsageReportingPublishMetricEnvVar); val != "" {
if value, err := strconv.ParseBool(val); err == nil {
log.DeprecationWarningReplace(oldUsageReportingPublishMetricEnvVar, newUsageReportingPublishMetricEnvVar)
u.PublishMetric = value
}
}
}
// Validate -
func (u *UsageReportingConfiguration) Validate() {
u.validateInterval() // DEPRECATE
eventAgg := u.Interval
if eventAgg < 60*time.Second {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingInterval))
}
u.validatePublish() // DEPRECATE
u.validatePublishMetric() // DEPRECATE
if !u.Offline {
u.validateUsageSchedule()
} else {
u.validateOffline()
}
}
func (u *UsageReportingConfiguration) validateUsageSchedule() {
// check fi the qa env var is set
if val := os.Getenv(qaUsageReportingUsageScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingUsageScheduleEnvVar, val)
} else {
log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaUsageReportingUsageScheduleEnvVar, val, u.UsageSchedule)
u.UsageSchedule = val
u.qaVars = true
}
return
}
// Check the cron expressions
cron, err := cronexpr.Parse(u.UsageSchedule)
if err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingUsageSchedule))
}
checks := 5
nextRuns := cron.NextN(time.Now(), uint(checks))
if len(nextRuns) != checks {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingUsageSchedule))
}
for i := 1; i < checks-1; i++ {
delta := nextRuns[i].Sub(nextRuns[i-1])
if delta < (time.Minute * 15) {
log.Tracef("%s must be at least 15 minutes apart", pathUsageReportingUsageSchedule)
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingUsageSchedule))
}
}
}
func (u *UsageReportingConfiguration) validateOffline() {
if _, err := cronexpr.Parse(u.Schedule); err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
// reporting is offline, lets read the QA env vars
if val := os.Getenv(qaUsageReportingScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingScheduleEnvVar, val)
} else {
log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaUsageReportingScheduleEnvVar, val, u.Schedule)
u.Schedule = val
u.qaVars = true
}
}
if val := os.Getenv(qaUsageReportingOfflineScheduleEnvVar); val != "" {
if _, err := cronexpr.Parse(val); err != nil {
log.Tracef("Could not use %s (%s) it is not a proper cron schedule", qaUsageReportingOfflineScheduleEnvVar, val)
} else {
log.Tracef("Using %s (%s) rather than the default (%s) for non-QA", qaUsageReportingOfflineScheduleEnvVar, val, u.reportSchedule)
u.reportSchedule = val
u.qaVars = true
}
}
// Check the cron expressions
cron, err := cronexpr.Parse(u.Schedule)
if err != nil {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
nextTwoRuns := cron.NextN(time.Now(), 2)
if len(nextTwoRuns) != 2 {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
u.reportGranularity = int(nextTwoRuns[1].Sub(nextTwoRuns[0]).Milliseconds())
// if no QA env vars are set then validate the schedule is at least hourly
if nextTwoRuns[1].Sub(nextTwoRuns[0]) < time.Hour && !u.qaVars {
exception.Throw(ErrBadConfig.FormatError(pathUsageReportingSchedule))
}
}
// GetURL - Returns the usage reporting URL
func (u *UsageReportingConfiguration) GetURL() string {
return u.URL
}
// CanPublishUsage - Returns the publish boolean
func (u *UsageReportingConfiguration) CanPublishUsage() bool {
return u.Publish
}
// CanPublishMetric - Returns the publish metric boolean
func (u *UsageReportingConfiguration) CanPublishMetric() bool {
return u.PublishMetric
}
// GetInterval - Returns the publish interval
func (u *UsageReportingConfiguration) GetInterval() time.Duration {
return u.Interval
}
// IsOfflineMode - Returns the offline boolean
func (u *UsageReportingConfiguration) IsOfflineMode() bool {
return u.Offline
}
// GetSchedule - Returns the schedule string
func (u *UsageReportingConfiguration) GetSchedule() string {
return u.Schedule
}
// GetUsageSchedule - Returns the schedule string for publishing reports
func (u *UsageReportingConfiguration) GetUsageSchedule() string {
return u.UsageSchedule
}
// GetReportSchedule - Returns the offline schedule string for creating reports
func (u *UsageReportingConfiguration) GetReportSchedule() string {
return u.reportSchedule
}
// GetReportGranularity - Returns the granularity used in the offline reports
func (u *UsageReportingConfiguration) GetReportGranularity() int {
if u.reportGranularity == 0 {
return int(u.Interval.Milliseconds())
}
return u.reportGranularity
}
// UsingQAVars - Returns the offline boolean
func (u *UsageReportingConfiguration) UsingQAVars() bool {
return u.qaVars
}
// AddUsageReportingProperties - Adds the command properties needed for Uage Reporting Settings
func AddUsageReportingProperties(props properties.Properties) {
props.AddBoolProperty(pathUsageReportingPublish, true, "Indicates if the agent can publish usage events to Amplify platform. Default to true")
props.AddBoolProperty(pathUsageReportingPublishMetric, true, "Indicates if the agent can publish metric events to Amplify platform. Default to true")
props.AddDurationProperty(pathUsageReportingInterval, 15*time.Minute, "The time interval at which usage and metric events will be generated")
props.AddStringProperty(pathUsageReportingUsageSchedule, "@daily", "The schedule at usage events are sent to the platform")
props.AddBoolProperty(pathUsageReportingOffline, false, "Turn this on to save the usage events to disk for manual upload")
props.AddStringProperty(pathUsageReportingSchedule, "@hourly", "The schedule at which usage events are generated, for offline mode only")
}
// ParseUsageReportingConfig - Parses the Usage Reporting Config values from the command line
func ParseUsageReportingConfig(props properties.Properties) UsageReportingConfig {
// Start with the default config
platformURL := strings.TrimRight(props.StringPropertyValue(pathPlatformURL), urlCutSet)
cfg := NewUsageReporting(platformURL).(*UsageReportingConfiguration)
// update the config
cfg.Publish = props.BoolPropertyValue(pathUsageReportingPublish)
cfg.PublishMetric = props.BoolPropertyValue(pathUsageReportingPublishMetric)
cfg.Interval = props.DurationPropertyValue(pathUsageReportingInterval)
cfg.UsageSchedule = props.StringPropertyValue(pathUsageReportingUsageSchedule)
cfg.Offline = props.BoolPropertyValue(pathUsageReportingOffline)
cfg.Schedule = props.StringPropertyValue(pathUsageReportingSchedule)
return cfg
}