-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.py
More file actions
346 lines (279 loc) · 13.1 KB
/
client.py
File metadata and controls
346 lines (279 loc) · 13.1 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
#!/usr/bin/env python
# coding: utf-8
""" Sight Machine SDK Client """
from __future__ import unicode_literals, absolute_import
import pandas as pd
try:
# for newer pandas versions >1.X
from pandas import json_normalize
except ImportError:
from pandas.io.json import json_normalize
from smsdk.utils import get_url
from smsdk.Auth.auth import Authenticator
from smsdk.tool_register import smsdkentities
from smsdk.client_v0 import ClientV0
import logging
log = logging.getLogger(__name__)
def time_string_to_epoch(time_string):
try:
dt = pd.to_datetime(time_string)
time_epoch = (dt - pd.to_datetime('1970-01-01')).total_seconds() * 1000 # SM timestamps in ms
except ValueError as e:
log.error(f'Unable to parse time string {time_string}: {e}')
return 0
except Exception as e:
log.error(f'Bad date specified: {time_string}')
return 0
return (time_epoch)
def dict_to_df(data, normalize=True):
if normalize:
# special case to handle the 'stats' block
if data and 'stats' in data[0]:
if isinstance(data[0]['stats'], dict):
# part stats are dict
df = json_normalize(data)
else:
# machine type stats are list
cols = [*data[0]]
cols.remove('stats')
df = json_normalize(data, 'stats', cols, record_prefix='stats.', errors='ignore')
else:
try:
df = json_normalize(data)
except:
# From cases like _distinct which don't have a "normal" return format
return pd.DataFrame({'values': data})
else:
df = pd.DataFrame(data)
if len(df) > 0:
if '_id' in df.columns:
df.set_index('_id', inplace=True)
if 'id' in df.columns:
df.set_index('id', inplace=True)
return df
# We don't have a downtime schema, so hard code one
downmap = {'machine__source': 'Machine',
'starttime': 'Start Time',
'endtime': 'End Time',
'total': 'Duration',
'shift': 'Shift',
'metadata__reason': 'Downtime Reason',
'metadata__category': 'Downtime Category',
'metadata__downtime_type': 'Downtime Type'}
downmapinv = {'Machine': 'machine__source',
'Start Time': 'starttime',
'End Time': 'endtime',
'Duration': 'total',
'Shift': 'shift',
'Downtime Reason': 'metadata__reason',
'Downtime Category': 'metadata__category',
'Downtime Type': 'metadata__downtime_type'}
class Client(ClientV0):
"""Connection point to the Sight Machine platform to retrieve data"""
session = None
tenant = None
config = None
def __init__(self, tenant, site_domain="sightmachine.io", protocol = "https"):
"""
Initialize the client.
:param tenant: The tenant within Sight Machine to access.
:type tenant: :class:`string`
:param site_domain:
The site domain to connect to. Necessary to change if deploying in
a non-standard environment.
:type site_domain: :class:`string`
"""
self.tenant = tenant
# Handle internal configuration
self.config = {}
self.config["protocol"] = protocol
self.config["site.domain"] = site_domain
# Setup Authenticator
self.auth = Authenticator(self)
self.session = self.auth.session
def get_data_v1(self, ename, util_name, normalize=True, *args, **kwargs):
"""
Main data fetching function for all the entities. Note this is the general data fetch function. You probably want to use the model-specific functions such as get_cycles().
:param ename: Name of the entities
:param util_name: Name of the utility function
:param normalize: Flatten nested data structures
:return: pandas dataframe
"""
base_url = get_url(
self.config["protocol"], self.tenant, self.config["site.domain"]
)
df = pd.DataFrame()
# load the entity class and initialize it
cls = smsdkentities.get(ename)(self.session, base_url)
# The current API is inconsistent where most paramters use the MongoEngine-like __ notation for ., but _only requires .
# So let the user enter '__', but convert those to '.' for API compatibility
# if '_only' in kwargs:
# new_cols = []
# for colname in kwargs.pop('_only'):
# new_cols.append(colname.replace('__', '.'))
# kwargs['_only'] = new_cols
# Fix format for __in commands
#for key, val in kwargs.items():
# if '__in' in key:
# kwargs[key] = str(val)
# check if requested util_name belong the list of
# registerd utilites
if util_name in getattr(cls, "get_utilities")(*args, **kwargs):
# call the utility function
# all the dict params are passed as kwargs
# dict params strictly follow {'key':'value'} format
# sub_kwargs = kwargs
if util_name in ['get_cycles', 'get_downtime', 'get_parts', 'get_factories', 'get_machines', 'get_machine_types']:
sub_kwargs = [kwargs]
else:
sub_kwargs = self.fix_only(kwargs)
if len(sub_kwargs) == 1:
if util_name in ['get_factories', 'get_machines', 'get_machine_types']:
# data = dict_to_df(getattr(cls, util_name)(*args, **sub_kwargs[0]), normalize)
return getattr(cls, util_name)(normalize, *args, **sub_kwargs[0])
else:
data = dict_to_df(getattr(cls, util_name)(*args, **sub_kwargs[0]), normalize)
else:
data = dict_to_df(getattr(cls, util_name)(*args, **sub_kwargs[0]), normalize)
for sub in sub_kwargs[1:]:
sub_data = dict_to_df(getattr(cls, util_name)(*args, **sub), normalize)
data = data.join(sub_data, rsuffix='__joined')
joined_cols = [col for col in data.columns if '__joined' in col]
data.drop(joined_cols, axis=1)
# To keep consistent, rename columns back from '.' to '__'
data.columns = [name.replace('.', '__') for name in data.columns]
else:
# raise error if requested for unregistered utility
raise ValueError("Error - {}".format("Not a registered utility"))
if 'endtime' in data.columns:
data['endtime'] = pd.to_datetime(data['endtime'])
if 'starttime' in data.columns:
data['starttime'] = pd.to_datetime(data['starttime'])
return data
@ClientV0.validate_input
@ClientV0.cycle_decorator
def get_cycles(self, normalize=True, clean_strings_in=True, clean_strings_out=True, *args, **kwargs):
df = self.get_data_v1('cycle_v1', 'get_cycles', normalize, *args, **kwargs)
return df
@ClientV0.validate_input
@ClientV0.downtime_decorator
def get_downtimes(self, normalize=True, clean_strings_in=True, clean_strings_out=True, *args, **kwargs):
df = self.get_data_v1('downtime_v1', 'get_downtime', normalize, *args, **kwargs)
return df
@ClientV0.validate_input
@ClientV0.part_decorator
def get_parts(self, normalize=True, clean_strings_in=True, clean_strings_out=True, datatab_api=True, *args,
**kwargs):
df = self.get_data_v1('part_v1', 'get_parts', normalize, *args, **kwargs)
return df
@ClientV0.get_machine_schema_decorator
def get_machine_schema(self, machine_source, types=[], return_mtype=False, **kwargs):
stats = kwargs.get('stats', [])
fields = []
for stat in stats:
if not stat.get('display', {}).get('ui_hidden', False):
if len(types) == 0 or stat['analytics']['columns'][0]['type'] in types:
try:
fields.append({'name': stat['analytics']['columns'][0]['name'],
'display': stat['display']['title_prefix'],
'type': stat['analytics']['columns'][0]['type']})
except:
log.warning(
f"Unknow stat schema identified :: machine_type {machine_source} - "
f"title_prefix :: {stat.get('display', {}).get('title_prefix', '')}")
return fields
def _get_factories(self, normalize=True, *args, **kwargs):
"""
Get list of factories and associated metadata. Note this includes extensive internal metadata.
:param normalize: Flatten nested data structures
:type normalize: bool
:return: pandas dataframe
"""
return self.get_data_v1('factory_v1', 'get_factories', normalize, *args, **kwargs)
def _get_machines(self, normalize=True, *args, **kwargs) -> pd.DataFrame:
"""
Get list of machines and associated metadata. Note this includes extensive internal metadata. If you only want to get a list of machine names
then see also get_machine_names().
:param normalize: Flatten nested data structures
:type normalize: bool
:return: pandas dataframe
"""
return self.get_data_v1('machine_v1', 'get_machines', normalize, *args, **kwargs)
def _get_machine_types(self, normalize=True, *args, **kwargs):
"""
Get list of machine types and associated metadata. Note this includes extensive internal metadata. If you only want to get a list of machine type names
then see also get_machine_type_names().
:param normalize: Flatten nested data structures
:type normalize: bool
:return: pandas dataframe
"""
return self.get_data_v1('machine_type_v1', 'get_machine_types', normalize, *args, **kwargs)
def get_factories(self, normalize=True, *args, **kwargs):
generator = self._get_factories(normalize=normalize, *args, **kwargs)
data = []
for page in generator:
try:
data.append(page)
except Exception as e:
print(e)
data = pd.concat(data)
return data
def get_machines(self, normalize=True, *args, **kwargs):
generator = self._get_machines(normalize=normalize, *args, **kwargs)
data = []
for page in generator:
try:
data.append(page)
except Exception as e:
print(e)
data = pd.concat(data)
return data
def get_machine_types(self, normalize=True, *args, **kwargs):
generator = self._get_machine_types(normalize=normalize, *args, **kwargs)
data = []
for page in generator:
try:
data.append(page)
except Exception as e:
print(e)
data = pd.concat(data)
return data
def get_machine_names(self, source_type=None, clean_strings_out=True):
"""
Get a list of machine names. This is a simplified version of get_machines().
:param source_type: filter the list to only the specified source_type
:type source_type: str
:param clean_strings_out: If true, return the list using the UI-based display names. If false, the list contains the Sight Machine internal machine names.
:return: list
"""
query_params = {'_only': ['source', 'source_clean', 'source_type'],
'_order_by': 'source_clean'}
if source_type:
# Double check the type
mt = self.get_machine_types(source_type=source_type)
# If it was found, then no action to take, otherwise try looking up from clean string
mt = self.get_machine_types(source_type_clean=source_type) if not len(mt) else []
if len(mt):
source_type = mt['source_type'].iloc[0]
else:
log.error('Machine Type not found')
return []
query_params['source_type'] = source_type
machines = self.get_data_v1('machine_v1', 'get_machines', normalize=True, **query_params)
if clean_strings_out:
return machines['source_clean'].to_list()
else:
return machines['source'].to_list()
def get_machine_type_names(self, clean_strings_out=True):
"""
Get a list of machine type names. This is a simplified version of get_machine_types().
:param clean_strings_out: If true, return the list using the UI-based display names. If false, the list contains the Sight Machine internal machine types.
:return: list
"""
query_params = {'_only': ['source_type', 'source_type_clean'],
'_order_by': 'source_type_clean'}
machine_types = self.get_data_v1('machine_type_v1', 'get_machine_types', normalize=True, **query_params)
if clean_strings_out:
return machine_types['source_type_clean'].to_list()
else:
return machine_types['source_type'].to_list()