-
Notifications
You must be signed in to change notification settings - Fork 2
Add V1 endpoints for factory, machine, machine_type #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 18 commits
ae370cd
259faf1
45288ca
e1e36f9
fa4c575
e685cf4
daf5261
65a7b8f
99cb7b8
9fec588
05556db
4d5eb96
3f7cfa1
b3e6f03
14b5313
e3a0631
13fe6bc
c954b27
220bdbe
c3add50
d3ac6ef
2b1d17a
407e9d8
ae793a7
4d12842
69e0706
101f987
b0440c2
3070b87
fd75a78
b0ae92f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |
| except ImportError: | ||
| from pandas.io.json import json_normalize | ||
|
|
||
| from smsdk.utils import get_url | ||
| from smsdk.utils import get_url, escape_mongo_field_name | ||
| from smsdk.Auth.auth import Authenticator, X_SM_DB_SCHEMA | ||
| from smsdk.tool_register import smsdkentities | ||
| from smsdk.client_v0 import ClientV0 | ||
|
|
@@ -46,7 +46,7 @@ def dict_to_df(data, normalize=True): | |
| # machine type stats are list | ||
| cols = [*data[0]] | ||
| cols.remove('stats') | ||
| df = json_normalize(data, 'stats', cols, record_prefix='stats.') | ||
| df = json_normalize(data, 'stats', cols, record_prefix='stats.', errors='ignore') | ||
| else: | ||
| try: | ||
| df = json_normalize(data) | ||
|
|
@@ -62,9 +62,12 @@ def dict_to_df(data, normalize=True): | |
|
|
||
| if 'id' in df.columns: | ||
| df.set_index('id', inplace=True) | ||
|
|
||
| return df | ||
|
|
||
| def generator_to_df(generator) -> pd.DataFrame: | ||
| data = pd.concat([page for page in generator]) | ||
| return data | ||
|
|
||
| # We don't have a downtime schema, so hard code one | ||
| downmap = {'machine__source': 'Machine', | ||
|
|
@@ -157,13 +160,17 @@ def get_data_v1(self, ename, util_name, normalize=True, *args, **kwargs): | |
| # dict params strictly follow {'key':'value'} format | ||
|
|
||
| # sub_kwargs = kwargs | ||
| if util_name in ['get_cycles', 'get_downtime', 'get_parts']: | ||
| if util_name in ['get_cycles', 'get_downtime', 'get_parts', 'get_factories', 'get_machines', 'get_machine_types']: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unit Tests are a separate PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A useful combination of tools to see how well the code created has test coverage is:
ex. The report given goes off the code vs the diff, but still useful. |
||
| sub_kwargs = [kwargs] | ||
| else: | ||
| sub_kwargs = self.fix_only(kwargs) | ||
|
|
||
| if len(sub_kwargs) == 1: | ||
| data = dict_to_df(getattr(cls, util_name)(*args, **sub_kwargs[0]), normalize) | ||
| 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]) | ||
|
mks-sight marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI, It it the way the current SDK is designed, but I dislike the need to need to inspect the method names to determine function signatures rather than leveraging the language and OO methods. |
||
| 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:]: | ||
|
|
@@ -173,7 +180,7 @@ def get_data_v1(self, ename, util_name, normalize=True, *args, **kwargs): | |
| data.drop(joined_cols, axis=1) | ||
|
|
||
| # To keep consistent, rename columns back from '.' to '__' | ||
| data.columns = [name.replace('.', '__') for name in data.columns] | ||
| data.columns = [escape_mongo_field_name(name)for name in data.columns] | ||
|
|
||
| else: | ||
| # raise error if requested for unregistered utility | ||
|
|
@@ -290,6 +297,109 @@ def get_fields_of_machine_type(self, machine_type, types=[], show_hidden=False, | |
|
|
||
| return fields | ||
|
|
||
|
|
||
| def _get_factories(self, *args, normalize=True, **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, *args, normalize=True, **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, *args, normalize=True, **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, *args, normalize=True, **kwargs): | ||
| generator = self._get_factories(normalize=normalize, *args, **kwargs) | ||
| data = generator_to_df(generator) | ||
| return data | ||
|
|
||
| def get_machines(self, *args, normalize=True, **kwargs): | ||
| generator = self._get_machines(normalize=normalize, *args, **kwargs) | ||
| data = generator_to_df(generator) | ||
| return data | ||
|
|
||
| def get_machine_types(self, *args, normalize=True, **kwargs): | ||
| generator = self._get_machine_types(normalize=normalize, *args, **kwargs) | ||
| data = generator_to_df(generator) | ||
| 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 = { | ||
| 'select': ['source', 'source_clean', 'source_type'], | ||
| 'order_by': [{'name':'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 [] | ||
|
mks-sight marked this conversation as resolved.
Outdated
|
||
| 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) | ||
| machines = generator_to_df(machines) | ||
|
|
||
| if clean_strings_out: | ||
| return machines['source_clean'].to_list() | ||
| else: | ||
| return machines['source'].to_list() | ||
|
mks-sight marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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 = { | ||
| 'select': ['source_type', 'source_type_clean'], | ||
| 'order_by': [{'name':'source_type_clean'}] | ||
| } | ||
| machine_types = self.get_data_v1('machine_type_v1', 'get_machine_types', normalize=True, **query_params) | ||
| machine_types = generator_to_df(machine_types) | ||
|
|
||
| if clean_strings_out: | ||
| return machine_types['source_type_clean'].to_list() | ||
| else: | ||
| return machine_types['source_type'].to_list() | ||
| def get_cookbooks(self, **kwargs): | ||
| """ | ||
| Gets all of the cookbooks accessable to the logged in user. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this module kept around for? What v0 interfaces remain? |
Uh oh!
There was an error while loading. Please reload this page.