-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.py
159 lines (131 loc) · 7.11 KB
/
program.py
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
"""This sample script demonstrates how to invoke the Sequential Data Store REST API for read only clients"""
import json
import datetime
from adh_sample_library_preview import (ADHClient)
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 print_data(data:list):
"""Print values of the data list to the console"""
print(f'Total events found: {str(len(data))}')
for val in data:
print(val)
print()
def main(test=False):
"""This function is the main body of the SDS sample script"""
exception = None
try:
print('Step 1. Authenticate against OCS')
appsettings = get_appsettings()
# Step 1 Try to open the configuration file
namespace_id = appsettings.get('NamespaceId')
community_id = appsettings.get('CommunityId')
stream_id = appsettings.get('StreamId')
sds_client = ADHClient(
appsettings.get('ApiVersion'),
appsettings.get('TenantId'),
appsettings.get('Resource'),
appsettings.get('ClientId'),
appsettings.get('ClientSecret'))
print(r'-------------------------------------------------------')
print(r' _____ _____ _ ____ _____ _____ _____ ')
print(r'| __ \_ _| | / __ \ / ____|/ ____| __ \ ')
print(r'| |__) || | | |_ ___ | | | | | | (___ | |__) | _ ')
print(r'| ___/ | | | __/ _ \| | | | | \___ \| ___/ | | |')
print(r'| | _| |_| || (_) | |__| | |____ ____) | | | |_| |')
print(r'|_| |_____|\__\___/ \____/ \_____|_____/|_| \__, |')
print(r' __/ |')
print(r' |___/ ')
print(r'-------------------------------------------------------')
print(f'Sds endpoint at {sds_client.uri}')
print()
# Create start and end indices for the past day
currentTime = datetime.datetime.utcnow()
endIndex = currentTime
startIndex = endIndex - datetime.timedelta(days=1)
# Get PI to ADH Stream
print('Step 2. Retrieve stream')
if community_id:
stream = next(iter(sds_client.Communities.getCommunityStreams(community_id, f'id:{stream_id}')), None)
if stream:
community_tenant_id = stream.TenantId
community_namespace_id = stream.NamespaceId
else:
print(f'Stream with Id {stream_id} not found!')
raise Exception(f'Stream with Id {stream_id} not found!')
else:
stream = sds_client.Streams.getStream(namespace_id, stream_id)
print(f'Stream found: {stream.Id}')
print()
print('Step 3. Retrieve Window events')
if community_id:
values = sds_client.SharedStreams.getWindowValues(community_tenant_id, community_namespace_id, community_id, stream.Id, start=startIndex, end=endIndex)
else:
values = sds_client.Streams.getWindowValues(namespace_id, stream.Id, start=startIndex, end=endIndex)
print_data(values)
print('Step 4. Retrieve Window events in table form')
if community_id:
values = sds_client.SharedStreams.getWindowValuesForm(
community_tenant_id, community_namespace_id, community_id, stream.Id, start=startIndex, end=endIndex, form='tableh')
else:
values = sds_client.Streams.getWindowValuesForm(namespace_id, stream.Id, None, start=startIndex, end=endIndex, form='tableh')
print_data(values['Rows'])
print('Step 5. Retrieve Paged Window events')
if community_id:
pagedValues = sds_client.SharedStreams.getWindowValuesPaged(community_tenant_id, community_namespace_id, community_id, value_class=None, stream_id=stream.Id, start=startIndex, end=endIndex, count=2)
else:
pagedValues = sds_client.Streams.getWindowValuesPaged(namespace_id, stream.Id, value_class=None, start=startIndex, end=endIndex, count=2)
# Print first page
print_data(pagedValues.Results)
# Print any further pages
while not pagedValues.end():
if community_id:
pagedValues = sds_client.SharedStreams.getWindowValuesPaged(community_tenant_id, community_namespace_id, community_id, stream.Id, value_class=None, start=startIndex, end=endIndex, count=2, continuation_token=pagedValues.ContinuationToken)
else:
pagedValues = sds_client.Streams.getWindowValuesPaged(namespace_id, stream.Id, value_class=None, start=startIndex, end=endIndex, count=2, continuation_token=pagedValues.ContinuationToken)
print_data(pagedValues.Results)
print('Step 6. Retrieve Range events')
if community_id:
values = sds_client.SharedStreams.getRangeValues(
community_tenant_id, community_namespace_id, community_id, stream.Id, start=startIndex, value_class=None, skip=0, count=10,
reversed=False, boundary_type=0)
else:
values = sds_client.Streams.getRangeValues(
namespace_id, stream.Id, start=startIndex, value_class=None, skip=0, count=10,
reversed=False, boundary_type=0)
print_data(values)
print('Step 7. Retrieve Interpolated events')
print('Sds can interpolate or extrapolate data at an index location where data does not explicitly exist:')
if community_id:
retrieved_interpolated = sds_client.SharedStreams.getRangeValuesInterpolated(
community_tenant_id, community_namespace_id, community_id, stream.Id, start=startIndex, end=endIndex, count=10)
else:
retrieved_interpolated = sds_client.Streams.getRangeValuesInterpolated(
namespace_id, stream.Id, None, start=startIndex, end=endIndex, count=10)
print_data(retrieved_interpolated)
print('Step 8. Retrieve Filtered events')
print(f'To show the filter functionality, we will use the less than operator to show values less than 0. (This value can be updated in filter statement below to better fit the data set)')
if community_id:
filtered_events = sds_client.SharedStreams.getWindowValues(
community_tenant_id, community_namespace_id, community_id, stream.Id, start=startIndex, end=endIndex, value_class=None, filter=f'Value lt 0')
else:
filtered_events = sds_client.Streams.getWindowValues(
namespace_id, stream.Id, start=startIndex, end=endIndex, value_class=None, filter=f'Value lt 0')
print_data(filtered_events)
finally:
if test and exception is not None:
raise exception
print('Complete!')
if __name__ == '__main__':
main()