forked from openebs-archive/maya
/
usage.go
257 lines (219 loc) · 6.96 KB
/
usage.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
/*
Copyright 2018 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package usage
import (
k8sapi "github.com/openebs/maya/pkg/client/k8s/v1alpha1"
)
// Usage struct represents all information about a usage metric sent to
// Google Analytics with respect to the application
type Usage struct {
// Embedded Event struct as we are currently only sending hits of type
// 'event'
Event
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#an
// use-case: cstor or jiva volume, or m-apiserver application
// Embedded field for application
Application
// Embedded Gclient struct
Gclient
}
// Event is a represents usage of OpenEBS
// Event contains all the query param fields when hits is of type='event'
// Ref: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ec
type Event struct {
// (Required) Event Category, ec
category string
// (Required) Event Action, ea
action string
// (Optional) Event Label, el
label string
// (Optional) Event vallue, ev
// Non negative
value int64
}
// NewEvent returns an Event struct with eventCategory, eventAction,
// eventLabel, eventValue fields
func (u *Usage) NewEvent(c, a, l string, v int64) *Usage {
u.category = c
u.action = a
u.label = l
u.value = v
return u
}
// Application struct holds details about the Application
type Application struct {
// eg. project version
appVersion string
// eg. kubernetes version
appInstallerID string
// Name of the application, usage(OpenEBS/NDM)
appID string
// eg. usage(os-type/architecture) of system or volume's CASType
appName string
}
// Gclient struct represents a Google Analytics hit
type Gclient struct {
// constant tracking-id used to send a hit
trackID string
// anonymous client-id
clientID string
// anonymous campaign source
campaignSource string
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#ds
// (usecase) node-detail
dataSource string
// Document-title property in Google Analytics
// https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#dt
// use-case: uuid of the volume objects or a uuid to anonymously tell objects apart
documentTitle string
}
// New returns an instance of Usage
func New() *Usage {
return &Usage{}
}
// SetDataSource : usage(os-type, kernel)
func (u *Usage) SetDataSource(dataSource string) *Usage {
u.dataSource = dataSource
return u
}
// SetTrackingID Sets the GA-code for the project
func (u *Usage) SetTrackingID(track string) *Usage {
u.trackID = track
return u
}
// SetCampaignSource : source of openebs installater like:
// helm or operator etc. This will have to be configured
// via ENV variable OPENEBS_IO_INSTALLER_TYPE
func (u *Usage) SetCampaignSource(campaignSrc string) *Usage {
u.campaignSource = campaignSrc
return u
}
// SetDocumentTitle : usecase(anonymous-id)
func (u *Usage) SetDocumentTitle(documentTitle string) *Usage {
u.documentTitle = documentTitle
return u
}
// SetApplicationName : usecase(os-type/arch, volume CASType)
func (u *Usage) SetApplicationName(appName string) *Usage {
u.appName = appName
return u
}
// SetApplicationID : usecase(OpenEBS/NDM)
func (u *Usage) SetApplicationID(appID string) *Usage {
u.appID = appID
return u
}
// SetApplicationVersion : usecase(project-version)
func (u *Usage) SetApplicationVersion(appVersion string) *Usage {
u.appVersion = appVersion
return u
}
// SetApplicationInstallerID : usecase(k8s-version)
func (u *Usage) SetApplicationInstallerID(appInstallerID string) *Usage {
u.appInstallerID = appInstallerID
return u
}
// SetClientID sets the anonymous user id
func (u *Usage) SetClientID(userID string) *Usage {
u.clientID = userID
return u
}
// SetCategory sets the category of an event
func (u *Usage) SetCategory(c string) *Usage {
u.category = c
return u
}
// SetAction sets the action of an event
func (u *Usage) SetAction(a string) *Usage {
u.action = a
return u
}
// SetLabel sets the label for an event
func (u *Usage) SetLabel(l string) *Usage {
u.label = l
return u
}
// SetValue sets the value for an event's label
func (u *Usage) SetValue(v int64) *Usage {
u.value = v
return u
}
// Build is a builder method for Usage struct
func (u *Usage) Build() *Usage {
// Default ApplicationID for openebs project is OpenEBS
v := NewVersion()
v.getVersion(false)
u.SetApplicationID(AppName).
SetTrackingID(GAclientID).
SetClientID(v.id).
SetCampaignSource(v.installerType)
// TODO: Add condition for version over-ride
// Case: CAS/Jiva version, etc
return u
}
// Application builder is used for adding k8s&openebs environment detail
// for non install events
func (u *Usage) ApplicationBuilder() *Usage {
v := NewVersion()
v.getVersion(false)
u.SetApplicationVersion(v.openebsVersion).
SetApplicationName(v.k8sArch).
SetApplicationInstallerID(v.k8sVersion).
SetDataSource(v.nodeType)
return u
}
// SetVolumeCapacity sets the storage capacity of the volume for a volume event
func (u *Usage) SetVolumeCapacity(volCapG string) *Usage {
s, _ := toGigaUnits(volCapG)
u.SetValue(s)
return u
}
// Wrapper for setting the default storage-engine for volume-provision event
func (u *Usage) SetVolumeType(volType, method string) *Usage {
if method == VolumeProvision && volType == "" {
// Set the default storage engine, if not specified in the request
u.SetApplicationName(DefaultCASType)
} else {
u.SetApplicationName(volType)
}
return u
}
// Wrapper for setting replica count for volume events
// NOTE: This doesn't get the replica count in a volume de-provision event.
// TODO: Pick the current value of replica-count from the CAS-engine
func (u *Usage) SetReplicaCount(count, method string) *Usage {
if method == VolumeProvision && count == "" {
// Case: When volume-provision the replica count isn't specified
// it is set to three by default by the m-apiserver
u.SetAction(DefaultReplicaCount)
} else {
// Catch all case for volume-deprovision event and
// volume-provision event with an overriden replica-count
u.SetAction(Replica + count)
}
return u
}
// InstallBuilder is a concrete builder for install events
func (u *Usage) InstallBuilder(override bool) *Usage {
v := NewVersion()
clusterSize, _ := k8sapi.NumberOfNodes()
v.getVersion(override)
u.SetApplicationVersion(v.openebsVersion).
SetApplicationName(v.k8sArch).
SetApplicationInstallerID(v.k8sVersion).
SetDataSource(v.nodeType).
SetDocumentTitle(v.id).
SetApplicationID(AppName).
NewEvent(InstallEvent, RunningStatus, EventLabelNode, int64(clusterSize))
return u
}