33
44package com .microsoft .applicationinsights .agent .internal .profiler .config ;
55
6+ import com .azure .json .JsonReader ;
67import com .azure .json .JsonSerializable ;
8+ import com .azure .json .JsonToken ;
79import com .azure .json .JsonWriter ;
810import com .fasterxml .jackson .databind .util .StdDateFormat ;
11+ import com .microsoft .applicationinsights .agent .internal .profiler .util .TimestampContract ;
912import com .microsoft .applicationinsights .alerting .aiconfig .AlertingConfig ;
1013import java .io .IOException ;
1114import java .text .ParseException ;
1619public class ProfilerConfiguration implements JsonSerializable <ProfilerConfiguration > {
1720
1821 public static final Date DEFAULT_DATE ;
19- private Date lastModified ;
20- private boolean enabled ;
21- private String collectionPlan ;
22- private String cpuTriggerConfiguration ;
23- private String memoryTriggerConfiguration ;
24- private String defaultConfiguration ;
25- private List <AlertingConfig .RequestTrigger > requestTriggerConfiguration ;
22+ // TODO find an alternative to com.fasterxml.jackson.databind.util.StdDateFormat
23+ private static final StdDateFormat STD_DATE_FORMAT ;
2624
2725 static {
26+ STD_DATE_FORMAT = new StdDateFormat ();
2827 Date defaultDate ;
2928 try {
30- defaultDate = new StdDateFormat () .parse ("0001-01-01T00:00:00+00:00" );
29+ defaultDate = STD_DATE_FORMAT .parse ("0001-01-01T00:00:00+00:00" );
3130 } catch (ParseException e ) {
32- // will not happen
3331 defaultDate = null ;
3432 }
3533 DEFAULT_DATE = defaultDate ;
3634 }
3735
36+ private String id ;
37+ private Date lastModified ;
38+ private Date enabledLastModified ;
39+ private boolean enabled ;
40+ private String collectionPlan ;
41+ private String cpuTriggerConfiguration ;
42+ private String memoryTriggerConfiguration ;
43+ private String defaultConfiguration ;
44+ private List <AlertingConfig .RequestTrigger > requestTriggerConfiguration ;
45+
3846 public boolean hasBeenConfigured () {
3947 return getLastModified ().compareTo (DEFAULT_DATE ) != 0 ;
4048 }
4149
50+ public String id () {
51+ return id ;
52+ }
53+
54+ public ProfilerConfiguration setId (String id ) {
55+ this .id = id ;
56+ return this ;
57+ }
58+
4259 public Date getLastModified () {
4360 return lastModified ;
4461 }
@@ -48,6 +65,15 @@ public ProfilerConfiguration setLastModified(Date lastModified) {
4865 return this ;
4966 }
5067
68+ public Date getEnabledLastModified () {
69+ return enabledLastModified ;
70+ }
71+
72+ public ProfilerConfiguration setEnabledLastModified (Date enabledLastModified ) {
73+ this .enabledLastModified = enabledLastModified ;
74+ return this ;
75+ }
76+
5177 public boolean isEnabled () {
5278 return enabled ;
5379 }
@@ -111,7 +137,10 @@ public ProfilerConfiguration setRequestTriggerConfiguration(
111137 @ Override
112138 public JsonWriter toJson (JsonWriter jsonWriter ) throws IOException {
113139 jsonWriter .writeStartObject ();
114- jsonWriter .writeLongField ("lastModified" , lastModified .getTime ());
140+ jsonWriter .writeStringField ("id" , id );
141+ jsonWriter .writeStringField ("lastModified" , STD_DATE_FORMAT .format (lastModified ));
142+ jsonWriter .writeStringField (
143+ "enabledLastModified" , TimestampContract .timestampToString (enabledLastModified ));
115144 jsonWriter .writeBooleanField ("enabled" , enabled );
116145 jsonWriter .writeStringField ("collectionPlan" , collectionPlan );
117146 jsonWriter .writeStringField ("cpuTriggerConfiguration" , cpuTriggerConfiguration );
@@ -125,4 +154,51 @@ public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
125154 jsonWriter .writeEndObject ();
126155 return jsonWriter ;
127156 }
157+
158+ public static ProfilerConfiguration fromJson (JsonReader jsonReader ) throws IOException {
159+ return jsonReader .readObject (
160+ reader -> {
161+ ProfilerConfiguration deserializedProfilerConfiguration = new ProfilerConfiguration ();
162+ while (reader .nextToken () != JsonToken .END_OBJECT ) {
163+ reader .nextToken ();
164+ String fieldName = reader .getFieldName ();
165+ if ("id" .equals (fieldName )) {
166+ String id = reader .getString ();
167+ deserializedProfilerConfiguration .setId (id );
168+ } else if ("lastModified" .equals (fieldName )) {
169+ String lastModified = reader .getString ();
170+ try {
171+ deserializedProfilerConfiguration .setLastModified (
172+ new StdDateFormat ().parse (lastModified ));
173+ } catch (ParseException ignored ) {
174+ deserializedProfilerConfiguration .setLastModified (DEFAULT_DATE );
175+ }
176+ } else if ("enabledLastModified" .equals (fieldName )) {
177+ String enabledLastModified = reader .getString ();
178+ try {
179+ deserializedProfilerConfiguration .setEnabledLastModified (
180+ STD_DATE_FORMAT .parse (enabledLastModified ));
181+ } catch (ParseException ignored ) {
182+ deserializedProfilerConfiguration .setEnabledLastModified (DEFAULT_DATE );
183+ }
184+ } else if ("enabled" .equals (fieldName )) {
185+ deserializedProfilerConfiguration .setEnabled (reader .getBoolean ());
186+ } else if ("collectionPlan" .equals (fieldName )) {
187+ deserializedProfilerConfiguration .setCollectionPlan (reader .getString ());
188+ } else if ("cpuTriggerConfiguration" .equals (fieldName )) {
189+ deserializedProfilerConfiguration .setCpuTriggerConfiguration (reader .getString ());
190+ } else if ("memoryTriggerConfiguration" .equals (fieldName )) {
191+ deserializedProfilerConfiguration .setMemoryTriggerConfiguration (reader .getString ());
192+ } else if ("defaultConfiguration" .equals (fieldName )) {
193+ deserializedProfilerConfiguration .setDefaultConfiguration (reader .getString ());
194+ } else if ("requestTriggerConfiguration" .equals (fieldName )) {
195+ deserializedProfilerConfiguration .setRequestTriggerConfiguration (
196+ reader .readArray (AlertingConfig .RequestTrigger ::fromJson ));
197+ } else {
198+ reader .skipChildren ();
199+ }
200+ }
201+ return deserializedProfilerConfiguration ;
202+ });
203+ }
128204}
0 commit comments