-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
549 lines (452 loc) · 22.6 KB
/
program.py
File metadata and controls
549 lines (452 loc) · 22.6 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
"""This sample script demonstrates how to invoke the Data View REST API"""
import json
import copy
import datetime
import random
import traceback
from adh_sample_library_preview import (DataView, Field, FieldSource, ADHClient, Query,
SdsStream, SdsType, SdsTypeCode,
SdsTypeProperty, SummaryDirection, SdsSummaryType)
# Sample Data Information
SAMPLE_TYPE_ID_1 = 'Time_SampleType1'
SAMPLE_TYPE_ID_2 = 'Time_SampleType2'
SAMPLE_STREAM_ID_1 = 'dvTank2'
SAMPLE_STREAM_NAME_1 = 'Tank2'
SAMPLE_STREAM_ID_2 = 'dvTank100'
SAMPLE_STREAM_NAME_2 = 'Tank100'
SAMPLE_FIELD_TO_CONSOLIDATE_TO = 'temperature'
SAMPLE_FIELD_TO_CONSOLIDATE = 'ambient_temp'
SAMPLE_FIELD_TO_ADD_UOM_COLUMN_1 = 'pressure'
SAMPLE_FIELD_TO_ADD_UOM_COLUMN_2 = 'temperature'
SUMMARY_FIELD_ID = 'pressure'
SUMMARY_TYPE_1 = 'Mean'
SUMMARY_TYPE_2 = 'Total'
# Data View Information
SAMPLE_DATAVIEW_ID = 'DataView_Sample'
SAMPLE_DATAVIEW_NAME = 'DataView_Sample_Name'
SAMPLE_DATAVIEW_DESCRIPTION = 'A Sample Description that describes that this '\
'Data View is just used for our sample.'
SAMPLE_QUERY_ID = 'stream'
SAMPLE_QUERY_STRING = 'dvTank*'
SAMPLE_INTERVAL = '00:20:00'
def get_appsettings():
"""Open and parse the appsettings.json file"""
# Try to open the configuration file
try:
with open(
'appsettings.json',
'r',
) as f:
appsettings = json.load(f)
except Exception as error:
print(f'Error: {str(error)}')
print(f'Could not open/read appsettings.json')
exit()
return appsettings
def suppress_error(sds_call):
"""Suppresses an error thrown by SDS"""
try:
sds_call()
except Exception as error:
print(('Encountered Error: {error}'.format(error=error)))
def find_field(fieldset_fields, field_source):
"""Find a field by field source"""
for field in fieldset_fields:
if field.Source == field_source:
return field
return None
def find_fieldset(fieldsets, fieldset_query_id):
"""Find a fieldset by query id"""
for fieldset in fieldsets:
if fieldset.QueryId == fieldset_query_id:
return fieldset
return None
def find_field_key(fieldset_fields, field_source, key):
"""Find a field by source and key"""
for field in fieldset_fields:
if field.Source == field_source and any(key in s for s in field.Keys):
return field
return None
def main(test=False):
"""This function is the main body of the Data View sample script"""
exception = None
appsettings = get_appsettings()
print('--------------------------------------------------------------------')
print(' ###### # # ###### # # ')
print(' # # ## ##### ## # # # ###### # # # # # # ')
print(' # # # # # # # # # # # # # # # # # ')
print(' # # # # # # # # # # ##### # # ###### # ')
print(' # # ###### # ###### # # # # # ## # # # ')
print(' # # # # # # # # # # # ## ## # # ')
print(' ###### # # # # # ## # ###### # # # # ')
print('--------------------------------------------------------------------')
# Step 1
print()
print('Step 1: Authenticate against ADH')
adh_client = ADHClient(appsettings.get('ApiVersion'),
appsettings.get('TenantId'),
appsettings.get('Resource'),
appsettings.get('ClientId'),
appsettings.get('ClientSecret'))
namespace_id = appsettings.get('NamespaceId')
print(namespace_id)
print(adh_client.uri)
try:
# Step 2
print()
print('Step 2: Create types, streams, and data')
times = create_data(namespace_id, adh_client)
sample_start_time = times[0]
sample_end_time = times[1]
# Step 3
print()
print('Step 3: Create a data view')
dataview = DataView(SAMPLE_DATAVIEW_ID,
SAMPLE_DATAVIEW_NAME, SAMPLE_DATAVIEW_DESCRIPTION)
adh_client.DataViews.postDataView(namespace_id, dataview)
# Step 4
print()
print('Step 4: Retrieve the data view')
dataview = adh_client.DataViews.getDataView(
namespace_id, SAMPLE_DATAVIEW_ID)
print(dataview.toJson())
# Step 5
print()
print('Step 5: Add a query for data items')
query = Query(SAMPLE_QUERY_ID, value=SAMPLE_QUERY_STRING)
dataview.Queries = [query]
# No Data View returned, success is 204
adh_client.DataViews.putDataView(namespace_id, dataview)
# Step 6
print()
print('Step 6: View items found by the query')
print('List data items found by the query:')
data_items = adh_client.DataViews.getResolvedDataItems(
namespace_id, SAMPLE_DATAVIEW_ID, SAMPLE_QUERY_ID)
print(data_items.toJson())
print('List ineligible data items found by the query:')
data_items = adh_client.DataViews.getResolvedIneligibleDataItems(
namespace_id, SAMPLE_DATAVIEW_ID, SAMPLE_QUERY_ID)
print(data_items.toJson())
# Step 7
print()
print('Step 7: View fields available to include in the data view')
available_fields = adh_client.DataViews.getResolvedAvailableFieldSets(
namespace_id, SAMPLE_DATAVIEW_ID)
print(available_fields.toJson())
# Step 8
print()
print('Step 8: Include some of the available fields')
dataview.DataFieldSets = available_fields.Items
adh_client.DataViews.putDataView(namespace_id, dataview)
print('List available field sets:')
available_fields = adh_client.DataViews.getResolvedAvailableFieldSets(
namespace_id, SAMPLE_DATAVIEW_ID)
print(available_fields.toJson())
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
print(len(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
print(len(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 9
print()
print('Step 9: Group the data view')
grouping = Field(source=FieldSource.Id,
label='{DistinguisherValue} {FirstKey}')
dataview.GroupingFields = [grouping]
# No DataView returned, success is 204
adh_client.DataViews.putDataView(namespace_id, dataview)
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 10
print()
print('Step 10: Identify data items')
identify = dataview.GroupingFields.pop()
dataview_dataitem_fieldset = find_fieldset(
dataview.DataFieldSets, SAMPLE_QUERY_ID)
dataview_dataitem_fieldset.IdentifyingField = identify
# No Data View returned, success is 204
adh_client.DataViews.putDataView(namespace_id, dataview)
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 11
print()
print('Step 11: Consolidate data fields')
field1 = find_field_key(dataview_dataitem_fieldset.DataFields,
FieldSource.PropertyId, SAMPLE_FIELD_TO_CONSOLIDATE_TO)
field2 = find_field_key(dataview_dataitem_fieldset.DataFields,
FieldSource.PropertyId, SAMPLE_FIELD_TO_CONSOLIDATE)
print(field1.toJson())
print(field2.toJson())
field1.Keys.append(SAMPLE_FIELD_TO_CONSOLIDATE)
dataview_dataitem_fieldset.DataFields.remove(field2)
# No Data View returned, success is 204
adh_client.DataViews.putDataView(namespace_id, dataview)
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 12
print()
print('Step 12: Add Units of Measure Column')
field1 = find_field_key(dataview_dataitem_fieldset.DataFields,
FieldSource.PropertyId, SAMPLE_FIELD_TO_ADD_UOM_COLUMN_1)
field2 = find_field_key(dataview_dataitem_fieldset.DataFields,
FieldSource.PropertyId, SAMPLE_FIELD_TO_ADD_UOM_COLUMN_2)
field1.IncludeUom = True
field2.IncludeUom = True
adh_client.DataViews.putDataView(namespace_id, dataview)
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 13
print()
print('Step 13: Add Summaries Columns')
# find the field for which we want to add a couple summary columns
ref_field = find_field_key(dataview_dataitem_fieldset.DataFields,
FieldSource.PropertyId, SAMPLE_FIELD_TO_ADD_UOM_COLUMN_1)
# deep copy the field twice so we can change properties
field1 = copy.deepcopy(ref_field)
field2 = copy.deepcopy(ref_field)
# set the summary properties and add the fields to the data fields array
field1.SummaryDirection = SummaryDirection.Forward
field1.SummaryType = SdsSummaryType[SUMMARY_TYPE_1]
field2.SummaryDirection = SummaryDirection.Forward
field2.SummaryType = SdsSummaryType[SUMMARY_TYPE_2]
dataview_dataitem_fieldset.DataFields.append(field1)
dataview_dataitem_fieldset.DataFields.append(field2)
adh_client.DataViews.putDataView(namespace_id, dataview)
print('Retrieving interpolated data from the data view:')
dataview_data = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view:')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 14
print()
print('Step 14: Iterate through pages of results')
print('Retrieving interpolated data from the data view with a page size of 2 rows to force paging:')
dataview_data, next_page, first_page = adh_client.DataViews.getDataInterpolated(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, interval=SAMPLE_INTERVAL, count=2)
# iterate through each subsequent page of results until there are no more pages
while next_page != None:
data_page, next_page, first_page = adh_client.DataViews.getDataInterpolated(url=next_page)
dataview_data.extend(data_page)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view interpolated data'
print('Retrieving stored data from the data view with a page size of 2 rows to force paging:')
dataview_data, next_page, first_page = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=sample_start_time,
end_index=sample_end_time, count=2)
# iterate through each subsequent page of results until there are no more pages
while next_page != None:
data_page, next_page, first_page = adh_client.DataViews.getDataInterpolated(url=next_page)
dataview_data.extend(data_page)
print(str(dataview_data))
assert len(dataview_data) > 0, 'Error getting data view stored data'
# Step 15
print()
print('Step 15: Demonstrate verbosity header usage')
print('Writing null values to the streams')
# Keep the times in the future, guaranteeing no overlaps with existing data
null_data_start_time = datetime.datetime.now() + datetime.timedelta(hours=1)
null_data_end_time = null_data_start_time + datetime.timedelta(hours=1)
# The first value is only a pressure, keeping temperature as null. Vice versa for the second
values = [{"time": null_data_start_time.isoformat(timespec='seconds'), "pressure": 100}, # temperature is null
{"time": null_data_end_time.isoformat(timespec='seconds'), "temperature": 50}] # pressure is null
adh_client.Streams.insertValues(namespace_id, SAMPLE_STREAM_ID_1, json.dumps(values))
adh_client.Streams.insertValues(namespace_id, SAMPLE_STREAM_ID_2, json.dumps(values))
print()
print('Data View results will not include null values written to nullable properties if the accept-verbosity header is set to non-verbose.')
print('The values just written include nulls for one of the properties; note the presence or absense of these values in the following outputs:')
print()
print('Retrieving these values in the data view with the base client setting of accept-verbosity set to False will use accept-verbosity: non-verbose')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=null_data_start_time,
end_index=null_data_end_time)
print()
print(str(dataview_data))
print()
print('The client\'s accept-verbosity setting can be overridden at the query level with the verbose parameter, returning verbose data view data')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=null_data_start_time,
end_index=null_data_end_time, verbose=True)
print()
print(str(dataview_data))
print()
print('Alternatively, enabling the base client\'s accept-verbosity setting will also result in verbose data view output, but is a client-wide setting')
adh_client.acceptverbosity = True
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=null_data_start_time,
end_index=null_data_end_time)
print()
print(str(dataview_data))
print()
print('A verbose client can also be overridden to show non-verbose data view output using the verbose parameter.')
dataview_data = adh_client.DataViews.getDataStored(
namespace_id, SAMPLE_DATAVIEW_ID, start_index=null_data_start_time,
end_index=null_data_end_time, verbose=False)
print()
print(str(dataview_data))
print()
print()
print('Setting the accept-verbosity back to the original value')
adh_client.acceptverbosity = False
except Exception as error:
print((f'Encountered Error: {error}'))
print()
traceback.print_exc()
print()
exception = error
finally:
#######################################################################
# Data View deletion
#######################################################################
# Step 16
print()
print('Step 16: Delete sample objects from OCS')
print('Deleting data view...')
suppress_error(lambda: adh_client.DataViews.deleteDataView(
namespace_id, SAMPLE_DATAVIEW_ID))
# check, including assert is added to make sure we deleted it
dataview = None
try:
dataview = adh_client.DataViews.getDataView(
namespace_id, SAMPLE_DATAVIEW_ID)
except Exception as error:
# Exception is expected here since Data View has been deleted
dataview = None
finally:
assert dataview is None, 'Delete failed'
print('Verification OK: Data View deleted')
print('Deleting sample streams...')
suppress_error(lambda: adh_client.Streams.deleteStream(
namespace_id, SAMPLE_STREAM_ID_1))
suppress_error(lambda: adh_client.Streams.deleteStream(
namespace_id, SAMPLE_STREAM_ID_2))
print('Deleting sample types...')
suppress_error(lambda: adh_client.Types.deleteType(
namespace_id, SAMPLE_TYPE_ID_1))
suppress_error(lambda: adh_client.Types.deleteType(
namespace_id, SAMPLE_TYPE_ID_2))
if test and exception is not None:
raise exception
print('Complete!')
def create_data(namespace_id, adh_client: ADHClient):
"""Creates sample data for the script to use"""
double_type = SdsType('doubleType', SdsTypeCode.NullableDouble)
datetime_type = SdsType('dateTimeType', SdsTypeCode.DateTime)
pressure_property = SdsTypeProperty('pressure', False, double_type, uom='bar')
temperature_property = SdsTypeProperty(SAMPLE_FIELD_TO_CONSOLIDATE_TO, False,
double_type, uom='degree Celsius')
ambient_temperature_property = SdsTypeProperty(SAMPLE_FIELD_TO_CONSOLIDATE, False,
double_type, uom='degree Celsius')
time_property = SdsTypeProperty('time', True, datetime_type)
sds_type_1 = SdsType(
SAMPLE_TYPE_ID_1, SdsTypeCode.Object, [
pressure_property, temperature_property, time_property],
description='This is a sample Sds type for storing Pressure type '
'events for Data Views')
sds_type_2 = SdsType(
SAMPLE_TYPE_ID_2, SdsTypeCode.Object, [
pressure_property, ambient_temperature_property, time_property],
description='This is a new sample Sds type for storing Pressure type '
'events for Data Views')
print('Creating SDS Types...')
adh_client.Types.getOrCreateType(namespace_id, sds_type_1)
adh_client.Types.getOrCreateType(namespace_id, sds_type_2)
stream1 = SdsStream(
SAMPLE_STREAM_ID_1,
SAMPLE_TYPE_ID_1,
SAMPLE_STREAM_NAME_1,
'A Stream to store the sample Pressure events')
stream2 = SdsStream(
SAMPLE_STREAM_ID_2,
SAMPLE_TYPE_ID_2,
SAMPLE_STREAM_NAME_2,
'A Stream to store the sample Pressure events')
print('Creating SDS Streams...')
adh_client.Streams.createOrUpdateStream(namespace_id, stream1)
adh_client.Streams.createOrUpdateStream(namespace_id, stream2)
sample_start_time = datetime.datetime.now() - datetime.timedelta(hours=1)
sample_end_time = datetime.datetime.now()
values1 = []
values2 = []
def value_with_time(timestamp, value, field_name, value2):
"""Formats a JSON data object"""
return f'{{"time": "{timestamp}", "pressure": {str(value)}, "{field_name}": {str(value2)}}}'
print('Generating values...')
for i in range(1, 30, 1):
timestamp = (sample_start_time + datetime.timedelta(minutes=i * 2)
).isoformat(timespec='seconds')
val1 = value_with_time(timestamp, random.uniform(
0, 100), SAMPLE_FIELD_TO_CONSOLIDATE_TO, random.uniform(50, 70))
val2 = value_with_time(timestamp, random.uniform(
0, 100), SAMPLE_FIELD_TO_CONSOLIDATE, random.uniform(50, 70))
values1.append(val1)
values2.append(val2)
print('Sending values...')
adh_client.Streams.insertValues(
namespace_id,
SAMPLE_STREAM_ID_1,
str(values1).replace("'", ""))
adh_client.Streams.insertValues(
namespace_id,
SAMPLE_STREAM_ID_2,
str(values2).replace("'", ""))
return (sample_start_time, sample_end_time)
if __name__ == '__main__':
main()