forked from splunk/splunk-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataModelObject.java
More file actions
394 lines (358 loc) · 14.8 KB
/
DataModelObject.java
File metadata and controls
394 lines (358 loc) · 14.8 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
* Copyright 2014 Splunk, Inc.
*
* 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 com.splunk;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import java.util.*;
import java.util.Map.Entry;
/**
* DataModelObject represents one of the structured views in a data model.
*/
public class DataModelObject {
private DataModel model;
private String name;
private String[] lineage;
private String displayName;
private String parentName;
private Map<String, DataModelField> autoextractedFields;
private Collection<DataModelConstraint> constraints;
private Map<String, DataModelCalculation> calculations;
protected DataModelObject(DataModel model) {
this.model = model;
}
/**
* Checks whether there is a field with the given name in this
* data model object.
*
* @param fieldName name of the field to check for.
* @return true if there is such a field; false otherwise.
*/
public boolean containsField(String fieldName) {
if (autoextractedFields.containsKey(fieldName)) {
return true;
}
for (DataModelCalculation c : calculations.values()) {
if (c.containsGeneratedField(fieldName)) {
return true;
}
}
return false;
}
/**
* Local acceleration is tsidx acceleration of a data model object that is handled
* manually by a user. You create a job which generates an index, and then use that
* index in your pivots on the data model object.
*
* The namespace created by the job is 'sid={sid}' where {sid} is the job's sid. You
* would use it in another job by starting your search query with
*
* | tstats ... from sid={sid} | ...
*
* The tsidx index created by this job is deleted when the job is garbage collected
* by Splunk.
*
* It is the user's responsibility to manage this job, including cancelling it.
*
* @return a Job writing a tsidx index.
*/
public Job createLocalAccelerationJob() {
return createLocalAccelerationJob(null);
}
/**
* Local acceleration is tsidx acceleration of a data model object that is handled
* manually by a user. You create a job which generates an index, and then use that
* index in your pivots on the data model object.
*
* The namespace created by the job is 'sid={sid}' where {sid} is the job's sid. You
* would use it in another job by starting your search query with
*
* | tstats ... from sid={sid} | ...
*
* The tsidx index created by this job is deleted when the job is garbage collected
* by Splunk.
*
* It is the user's responsibility to manage this job, including cancelling it.
*
* @param earliestTime A time modifier (e.g., "-2w") setting the earliest time to index.
* @return a Job writing a tsidx index.
*/
public Job createLocalAccelerationJob(String earliestTime) {
String query = "| datamodel " + this.model.getName() + " " +
this.getName() + " search | tscollect";
JobArgs args = new JobArgs();
if (earliestTime != null) {
args.setEarliestTime(earliestTime);
}
return this.model.getService().search(query, args);
}
/**
* Return the calculations done by this data model object to produce fields.
*
* Each calculation has a unique ID assigned to it by splunkd, which is the key
* in the returned map. For most purposes you will probably only want the values.
*
* @return a map of calculation IDs to DataModelCalculation objects.
*/
public Map<String, DataModelCalculation> getCalculations() {
return Collections.unmodifiableMap(this.calculations);
}
/**
* Fetch a calculation by its unique ID.
*
* @param calculationId a splunkd assigned unique ID for this calculation.
* @return a DataModelCalculation object.
*/
public DataModelCalculation getCalculation(String calculationId) {
return this.calculations.get(calculationId);
}
/**
* @return a collection of the constraints limiting events that will appear in this data model object.
*/
public Collection<DataModelConstraint> getConstraints() {
return Collections.unmodifiableCollection(this.constraints);
}
/**
* Fetch the data model on which this object is defined.
*
* @return A DataModel instance containing this object.
*/
public DataModel getDataModel() {
return this.model;
}
/**
* @return the human readable name of this data model object.
*/
public String getDisplayName() {
return this.displayName;
}
/**
* Fetch a single field of a given name from this data model object.
*
* @param fieldName Name of the field to fetch.
* @return A DataModelField object, or null if there is no field of the given name.
*/
public DataModelField getField(String fieldName) {
if (autoextractedFields.containsKey(fieldName)) {
return autoextractedFields.get(fieldName);
}
for (DataModelCalculation c : this.calculations.values()) {
if (c.containsGeneratedField(fieldName)) {
return c.getGeneratedField(fieldName);
}
}
return null;
}
/**
* Get a collection of objects specifying all the fields that were automatically extracted
* from events (as opposed to generated by calculations in a data model).
*
* @return a collection of DataModelField objects.
*/
public Collection<DataModelField> getAutoExtractedFields() {
return Collections.unmodifiableCollection(autoextractedFields.values());
}
/**
* Return all the fields, whether input or created by calculations.
* @return a collection of DataModelField objects.
*/
public Collection<DataModelField> getFields() {
Collection<DataModelField> fields = new ArrayList<DataModelField>();
fields.addAll(this.autoextractedFields.values());
for (DataModelCalculation c : this.calculations.values()) {
fields.addAll(c.getGeneratedFields());
}
return fields;
}
public String getQuery() {
return "| datamodel " + this.getDataModel().getName() + " " + this.getName() + " search";
}
/**
* @return Splunk's identifier for this data model object.
*/
public String getName() { return this.name; }
/**
* Data model objects can inherit from other data model objects
* in the same data model (or from a couple of global base objects
* such as BaseEvent and BaseTransaction). The lineage is a list of
* data model object names tracing this inheritance, starting with the
* most remote ancestor and ending with this object.
*
* @return An array of names, starting with this object's name, followed by
* the names up the hierarchy.
*/
public String[] getLineage() { return this.lineage; }
/**
* Returns the name of the parent of this object.
*
* @return a String giving the name.
*/
public String getParentName() {
return this.parentName;
}
/**
* @return the data model object this one inherits from if it is a user defined data model object
* in the same data model; otherwise returns null (for example if the data model object inherits from BaseEvent
* or BaseTransaction).
*/
public DataModelObject getParent() {
return this.getDataModel().getObject(this.parentName);
}
/**
* Create a PivotSpecification on this data model object.
*
* @return a PivotSpecification instance.
*/
public PivotSpecification createPivotSpecification() {
return new PivotSpecification(this);
}
/**
* Start a job that fetches all the events of this data model object.
*
* @return a Job object.
*/
public Job runQuery() {
return runQuery("", null);
}
/**
* Start a job that fetches all the events of this data model object.
*
* @param args arguments specifying behavior of the job.
* @return a Job object.
*/
public Job runQuery(JobArgs args) {
return runQuery("", args);
}
/**
* Start a job that applies querySuffix to all the events in this data model object.
*
* @param querySuffix a search query, starting with a '|' that will be appended to the command to fetch
* the contents of this data model object (e.g., "| head 3").
* @return a Job object.
*/
public Job runQuery(String querySuffix) {
return runQuery(querySuffix, null);
}
/**
* Start a job that applies querySuffix to all the events in this data model object.
*
* @param querySuffix a search query, starting with a '|' that will be appended to the command to fetch
* the contents of this data model object (e.g., "| head 3").
* @param args arguments to control the job.
* @return a Job object.
*/
public Job runQuery(String querySuffix, JobArgs args) {
return getDataModel().getService().search(getQuery() + querySuffix, args);
}
/**
* Produce a data model object from a JSON dictionary specifying it plus a data model that contains it.
* @param dataModel a DataModel instance that contains this data model object.
* @param object a JsonElement (as produced by Gson) specifying this data model object (usually one of
* the entries in the array of objects in the JSON description of the data model).
* @return a DataModelObject instance.
*/
static DataModelObject parse(DataModel dataModel, JsonElement object) {
String name = null;
String displayName = null;
String comment = null;
String[] lineage = new String[0];
String parentName = null;
Map<String, DataModelField> fields = new HashMap<String, DataModelField>();
Collection<String> children = new ArrayList<String>();
Collection<DataModelConstraint> constraints = new ArrayList<DataModelConstraint>();
Map<String, DataModelCalculation> calculations = new HashMap<String, DataModelCalculation>();
// Fields specific to objects inheriting directly from BaseSearch.
String baseSearch = null;
// Fields specific to objects inheriting directly from BaseTransaction
String transactionMaxPause = null;
String transactionMaxTimeSpan = null;
Collection<String> groupByFields = new ArrayList<String>();
Collection<String> objectsToGroup = new ArrayList<String>();
for (Entry<String, JsonElement> entry : object.getAsJsonObject().entrySet()) {
if (entry.getKey().equals("objectName")) {
name = entry.getValue().getAsString();
} else if (entry.getKey().equals("displayName")) {
displayName = entry.getValue().getAsString();
} else if (entry.getKey().equals("lineage")) {
lineage = entry.getValue().getAsString().split("\\.");
} else if (entry.getKey().equals("parentName")) {
parentName = entry.getValue().getAsString();
} else if (entry.getKey().equals("fields")) {
JsonArray fieldsJson = entry.getValue().getAsJsonArray();
fields.clear();
for (JsonElement fieldJson : fieldsJson) {
DataModelField field = DataModelField.parse(fieldJson);
fields.put(field.getName(), field);
}
} else if (entry.getKey().equals("constraints")) {
JsonArray constraintsJson = entry.getValue().getAsJsonArray();
for (JsonElement constraintJson : constraintsJson) {
DataModelConstraint constraint = DataModelConstraint.parse(constraintJson);
constraints.add(constraint);
}
} else if (entry.getKey().equals("calculations")) {
calculations.clear();
for (JsonElement cjson : entry.getValue().getAsJsonArray()) {
DataModelCalculation c = DataModelCalculation.parse(cjson);
String cid = c.getCalculationID();
calculations.put(cid, c);
}
} else if (entry.getKey().equals("baseSearch")) {
baseSearch = entry.getValue().getAsString();
} else if (entry.getKey().equals("transactionMaxPause")) {
transactionMaxPause = entry.getValue().getAsString();
} else if (entry.getKey().equals("transactionMaxTimeSpan")) {
transactionMaxTimeSpan = entry.getValue().getAsString();
} else if (entry.getKey().equals("groupByFields")) {
for (JsonElement e : entry.getValue().getAsJsonArray()) {
groupByFields.add(e.getAsString());
}
} else if (entry.getKey().equals("objectsToGroup")) {
for (JsonElement e : entry.getValue().getAsJsonArray()) {
objectsToGroup.add(e.getAsString());
}
}
}
DataModelObject dmo;
// Create the right subclass of DataModelObject.
if (baseSearch != null) {
dmo = new DataModelSearch(dataModel);
} else if (transactionMaxPause != null) {
dmo = new DataModelTransaction(dataModel);
} else {
dmo = new DataModelObject(dataModel);
}
// Set the fields common to all data model objects
dmo.name = name;
dmo.displayName = displayName;
dmo.lineage = lineage;
dmo.parentName = parentName;
dmo.autoextractedFields = fields;
dmo.constraints = constraints;
dmo.calculations = calculations;
// Set the fields of particular types
if (baseSearch != null) {
((DataModelSearch)dmo).baseSearch = baseSearch;
} else if (transactionMaxPause != null) {
((DataModelTransaction)dmo).groupByFields = groupByFields;
((DataModelTransaction)dmo).objectsToGroup = objectsToGroup;
((DataModelTransaction)dmo).maxPause = transactionMaxPause;
((DataModelTransaction)dmo).maxSpan = transactionMaxTimeSpan;
} else {
// Has no additional fields
}
return dmo;
}
}