1
1
component singleton accessors = " true" {
2
2
3
3
property name = " settings" inject = " coldbox:moduleSettings:unleashsdk" ;
4
+ property name = " config" inject = " coldbox:moduleConfig:unleashsdk" ;
4
5
property name = " client" inject = " UnleashHyperClient@unleashsdk" ;
5
6
property name = " log" inject = " logbox:logger:{this}" ;
6
7
property name = " cache" inject = " cachebox:default" ;
@@ -14,10 +15,39 @@ component singleton accessors="true" {
14
15
" applicationHostname" : " ApplicationHostnameStrategy@unleashsdk"
15
16
};
16
17
18
+ function onDIComplete () {
19
+ variables .metricsBucket = newMetricsBucket ();
20
+ }
21
+
22
+ function register () {
23
+ var registrationInfo = {
24
+ " appName" : variables .settings .appName ,
25
+ " instanceId" : variables .settings .instanceId ,
26
+ " sdkVersion" : " coldbox-modules/unleashsdk:#variables .config .version #" ,
27
+ " strategies" : variables .strategies .keyArray (),
28
+ " started" : getIsoTimeString ( now () ),
29
+ " interval" : variables .settings .metricsInterval * 1000
30
+ };
31
+ if ( log .canInfo () ) {
32
+ log .info ( " Registering instance with Unleash" , registrationInfo );
33
+ }
34
+ variables .client .post ( " /client/register" , registrationInfo );
35
+ }
36
+
17
37
public boolean function isEnabled (
18
38
required string name ,
19
39
struct additionalContext = {},
20
40
boolean defaultValue = false
41
+ ) {
42
+ var enabled = checkEnabled ( argumentCollection = arguments );
43
+ countForMetrics ( arguments .name , enabled );
44
+ return enabled ;
45
+ }
46
+
47
+ public boolean function checkEnabled (
48
+ required string name ,
49
+ struct additionalContext = {},
50
+ boolean defaultValue = false
21
51
) {
22
52
var feature = getFeature ( arguments .name );
23
53
if ( isNull ( feature ) ) {
@@ -131,6 +161,34 @@ component singleton accessors="true" {
131
161
return features ;
132
162
}
133
163
164
+ public struct function sendMetrics () {
165
+ var bucketToSend = variables .metricsBucket ;
166
+ variables .metricsBucket = newMetricsBucket ();
167
+ bucketToSend .stop = getIsoTimeString ( now () );
168
+ var metrics = {
169
+ " appName" : variables .settings .appName ,
170
+ " instanceId" : variables .settings .instanceId ,
171
+ " bucket" : bucketToSend
172
+ };
173
+ variables .client .post ( " /client/metrics" , metrics );
174
+ return metrics ;
175
+ }
176
+
177
+ private void function countForMetrics ( required string name , required boolean enabled ) {
178
+ if ( ! variables .metricsBucket .toggles .keyExists ( arguments .name ) ) {
179
+ variables .metricsBucket .toggles [ arguments .name ] = { " yes" : 0 , " no" : 0 };
180
+ }
181
+ variables .metricsBucket .toggles [ arguments .name ][ yesNoFormat ( arguments .enabled ) ]++ ;
182
+ }
183
+
184
+ public struct function newMetricsBucket () {
185
+ return {
186
+ " start" : getIsoTimeString ( now () ),
187
+ " stop" : " " ,
188
+ " toggles" : {}
189
+ };
190
+ }
191
+
134
192
private array function fetchFeatures () {
135
193
return variables .client .get ( " /client/features" ).json ().features ;
136
194
}
@@ -174,4 +232,15 @@ component singleton accessors="true" {
174
232
return javacast ( " null" , " " );
175
233
}
176
234
235
+ private string function getIsoTimeString ( required date datetime , boolean convertToUTC = true ) {
236
+ if ( arguments .convertToUTC ) {
237
+ arguments .datetime = dateConvert ( " local2utc" , arguments .datetime );
238
+ }
239
+
240
+ return dateFormat ( arguments .datetime , " yyyy-mm-dd" ) &
241
+ " T" &
242
+ timeFormat ( arguments .datetime , " HH:mm:ss" ) &
243
+ " Z" ;
244
+ }
245
+
177
246
}
0 commit comments