Skip to content

Commit bacdba5

Browse files
Merge pull request #26 from sightmachine/ENG-585_KPI_support
[Eng-585] kpi support
2 parents 4b66dee + 68d16c0 commit bacdba5

14 files changed

Lines changed: 594 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Asset Selection
2+
Asset Selection is used in a few different places in order to tell the API which asset or assets you wish to perform an action on. Typically it will either select a machine_type or types or specific machines within a machine_type in order to select machine_types it should look like the following:
3+
```
4+
asset_selection: {
5+
machine_type: ["Lasercut", ...]
6+
7+
}
8+
```
9+
10+
In order to select machines within a type it should look like the follwoing:
11+
```
12+
asset_selection: {
13+
machine_type: ["Lasercut"],
14+
machine_source: ["JB_AB_Lasercut_1", ...]
15+
16+
}
17+
```
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# Data Vizualation Query
2+
Data Viz Queries are used whenever the SDK calls our Data Vizualation APIs. The functions you call via SDK do some work on the query for you so this Query will look slightly different from the one our API uses directly. We will break down each field in some more detail futher along in this doc but as an example a full Data Viz Query looks like the following:
3+
```
4+
{
5+
"asset_selection": {
6+
"machine_source": [
7+
"JB_AB_Lasercut_1"
8+
],
9+
"machine_type": [
10+
"Lasercut"
11+
]
12+
},
13+
"d_vars": [
14+
{
15+
"name": "quality",
16+
"aggregate": [
17+
"avg"
18+
]
19+
}
20+
],
21+
"i_vars": [
22+
{
23+
"name": "endtime",
24+
"time_resolution": "day",
25+
"query_tz": "America/Los_Angeles",
26+
"output_tz": "America/Los_Angeles",
27+
"bin_strategy": "user_defined2",
28+
"bin_count": 50
29+
}
30+
],
31+
"time_selection": {
32+
"time_type": "relative",
33+
"relative_start": 7,
34+
"relative_unit": "year",
35+
"ctime_tz": "America/Los_Angeles"
36+
},
37+
"where": [],
38+
"db_mode": "sql"
39+
}
40+
```
41+
42+
## Asset_selection
43+
Used to select the asset(s) you want to recieve data from see the [asset_selection doc](/docs/commonly_used_data_types/asset_selection.md) for more information.
44+
45+
## d_vars
46+
The Dependent variables. These will change depending on the entity you are trying to access. But will always be a list in the following form:
47+
```
48+
{
49+
"name": "quality",
50+
"aggregate": [
51+
"avg"
52+
]
53+
}
54+
```
55+
### name
56+
This is the name of dependent variable you wish to view. This typically the name of a value we store on a machine or the name of a KPI.
57+
58+
### aggregate
59+
This is how you wish to aggregate the data of the named value in the time_resolution you have selected. The options for this are:
60+
* avg
61+
* sum
62+
* min
63+
* max
64+
65+
## i_vars
66+
The indepent variables. These should typically be time based values that are stored on the machine_type you are using. They will always be a list in the following form:
67+
```
68+
{
69+
"name": "endtime",
70+
"time_resolution": "day",
71+
"query_tz": "America/Los_Angeles",
72+
"output_tz": "America/Los_Angeles",
73+
"bin_strategy": "user_defined2",
74+
"bin_count": 50
75+
}
76+
```
77+
### name
78+
This is the name of idependent varaible you are using.
79+
80+
### time_resolution
81+
This is optional and is how detailed of a time breakdown you want in the variable the options for time_resolution are as follows:
82+
* year
83+
* month
84+
* week
85+
* day
86+
* hour
87+
* minute
88+
* second
89+
90+
### query_tz
91+
This is optional and tells the system what time zone the query is in.
92+
93+
### output_tz
94+
This is optional and tells the system what time zone to return the data in.
95+
96+
### bin_strategy
97+
This is optional and tells the sytem how you wish to bin the data. You have the following options:
98+
* user_defined2
99+
* none
100+
* categorical
101+
102+
### bin_count
103+
This is optoinal and tels the system how many bins you wish to put the data into.
104+
105+
## time_selection
106+
This is the time frame you want to grab data from there are two different ways to make this time selection, Relative and Absolute
107+
108+
### Relative Time Selection
109+
Relative Time Selections goes back from now a certain amount of time based on what you tell it. The format for this time selection is the following:
110+
```
111+
{
112+
"time_type": "relative",
113+
"relative_start": 7,
114+
"relative_unit": "year",
115+
"ctime_tz": "America/Los_Angeles"
116+
}
117+
```
118+
#### Time Type
119+
For a relative time selection this needs to be set to "relative".
120+
121+
#### Relative Start
122+
The amount of units from now you wish to go back to start your time selection.
123+
124+
#### Relative Unit
125+
The unit of time you wish to go back from now. Your options for this are as follows:
126+
* year
127+
* month
128+
* week
129+
* day
130+
* hour
131+
* minute
132+
* second
133+
134+
#### ctime_tz
135+
The time zone for your time selection
136+
137+
### Absolute Time Selection
138+
Absolute Time Selections have a start and end time and will gather data from between the two. The format for this time selection is as follows:
139+
```
140+
{
141+
"time_type": "absolute",
142+
"start_time": "2023-02-23T08:00:00.000Z",
143+
"end_time": "2023-03-01T21:35:35.499Z",
144+
"time_zone": "America/Los_Angeles"
145+
}
146+
```
147+
#### Time Type
148+
For absolute time selecitons this must be set to absolute.
149+
150+
#### Start Time
151+
The time you wish to start the time selection at.
152+
153+
#### End Time
154+
The time you wish to end the time selection at.
155+
156+
#### Time Zone
157+
The time zone for the time selection.
158+
159+
## Where
160+
This is optional. It will narrow down the data return based on criteria given. The list will be anded together. This is a list in the following format:
161+
```
162+
{
163+
"name": "type__part_type",
164+
"op": "eq",
165+
"value": "EngineBlock"
166+
}
167+
```
168+
169+
### Name
170+
The name of the field you are using in this criteria.
171+
172+
### Op
173+
The type of operation you are doing.
174+
175+
### Value
176+
The value to compare with the operation.
177+
178+
## db_mode
179+
This is optional. It will default to 'sql' and usually should be but we have a 'mongo' mode as well. You will likely ever need to set this.

docs/entities/kpis.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# KPIs
2+
3+
KPIs are user defined calculated fields in the Sight Machine software.
4+
5+
## Functions
6+
The SDK has three functions related to KPIs. The first returns a list of all availible KPis. The second of which allows a user to see which KPIs are availible for a particular asset. The third makes use of our Data Visualization api which allows a user to see these KPIs over a timeframe.
7+
8+
### Get KPIs
9+
This is the first KPI function allowing you to see all KPIs availible to you. In order to call this function you must first have a logged in client see the [quick start guide](/README.md) for more information on logging in. Once you have a logged in client you can call the function as follows:
10+
11+
```
12+
cli.get_kpis()
13+
```
14+
This will return a full list of KPIs which will look something like this example:
15+
```
16+
[{'name': 'performance', 'display_name': 'Performance', 'formula': '( IdealCycle / Recorded_time ) * 100 if ( Recorded_time > 0 ) else None', 'data_type': '', 'dependencies': [{'aggregate': 'sum', 'name': 'Recorded_time'}, {'aggregate': 'sum', 'name': 'IdealCycle'}]}, ...]
17+
```
18+
19+
In order to make use of the data viz function you'll need the name of the KPI you wish to get.
20+
21+
### Get KPIs For Asset
22+
This is the second KPI function allowing you to see which KPIs are availible for a particular asset. Once you have a logged in client you can call the function as follows:
23+
24+
```
25+
cli.get_kpis_for_asset(**asset_selection)
26+
```
27+
28+
For more info on [asset_selection](/docs/commonly_used_data_types/asset_selection.md) click on the previous link. After a moment the SDK should return a list that looks something like this:
29+
30+
```
31+
[{'name': 'quality', 'display_name': 'Quality', 'unit': '', 'type': 'continuous', 'data_type': 'float', 'stream_types': [], 'raw_data_field': ''},...]
32+
```
33+
34+
In order to make use of the data viz function you'll need the name of the KPI you wish to get.
35+
36+
### Get KPI Data Viz
37+
Once you have the name of the KPI you wish to access and a logged in client you can make a call to the data viz api with the following SDK function call:
38+
```
39+
cli.get_kpi_data_viz(machine_source, kpis, i_vars, time_selection, **optional_data_viz_query)
40+
```
41+
42+
After some time the SDK should return a list that looks something like this:
43+
```
44+
[{'i_vals': {'endtime': {'i_pos': 0, 'bin_no': 0, 'bin_min': '2022-10-20T00:00:00-07:00', 'bin_max': '2022-10-20T00:00:00-07:00', 'bin_avg': '2022-10-20T00:00:00-07:00'}}, 'd_vals': {'quality': {'avg': 95.18072289156626}}, '_count': 418, 'kpi_dependencies': {'quality': {'Output': 395.0, 'ScrapQuantity': 20.0}}},...]
45+
```
46+
47+
There's two ways to call this function you can use a data_viz_query,For more information on [data_viz_queries](/docs/commonly_used_data_types/data_viz_query.md) click on the previous link, or have the function fill out the query for you by passing in a few variable we will now go over one at a time.
48+
49+
#### machine_sources
50+
This is a list of strings and is the name of machine(s) you wish to run a query on.
51+
52+
#### kpis
53+
This is a list of the names of all the kpis you wish to run this query on.
54+
55+
#### i_vars
56+
This is a list, this is the same as the i_vars object in the data_viz_query and is the axis you are querying the kpis against it will look like the following:
57+
```
58+
[
59+
{
60+
"name": "endtime",
61+
"time_resolution": "day",
62+
"query_tz": "America/Los_Angeles",
63+
"output_tz": "America/Los_Angeles"
64+
}
65+
]
66+
```
67+
68+
#### time_selection
69+
This is an object, this is the same as the [time_selection](/docs/commonly_used_data_types/data_viz_query.md#time_selection) object in the data_viz_query and more info can be found at that link. The most common form is the relative time selection and looks like this:
70+
```
71+
{
72+
"time_type": "relative",
73+
"relative_start": 7,
74+
"relative_unit": "day",
75+
"ctime_tz": "America/Los_Angeles"
76+
}
77+
```

docs/entities/machine.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Machines
2+
Machines are just that, machines in factories. The machine object is how the Sight Machine software replesents and is the key to accessing data that we collect from them
3+
4+
## Functions
5+
6+
### get_type_from_machine
7+
The get_type_from_machine function allows you to get the type of any machine from it's name and is called this way:
8+
```
9+
cli.get_type_from_machine(machine_name)
10+
```
11+
12+
And will return something like the following:
13+
```
14+
'Lasercut'
15+
```

smsdk/client.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,53 @@ def get_machine_schema(self, machine_source, types=[], return_mtype=False, **kwa
228228
f"Unknow stat schema identified :: machine_type {machine_source} - "
229229
f"title_prefix :: {stat.get('display', {}).get('title_prefix', '')}")
230230
return fields
231+
232+
def get_kpis(self, **kwargs):
233+
kpis = smsdkentities.get('kpi')
234+
base_url = get_url(
235+
self.config["protocol"], self.tenant, self.config["site.domain"]
236+
)
237+
return kpis(self.session, base_url).get_kpis(**kwargs)
238+
239+
def get_kpis_for_asset(self, **kwargs):
240+
kpis = smsdkentities.get('kpi')
241+
base_url = get_url(
242+
self.config["protocol"], self.tenant, self.config["site.domain"]
243+
)
244+
return kpis(self.session, base_url).get_kpis_for_asset(**kwargs)
245+
246+
def get_kpi_data_viz(self, machine_sources=None, kpis=None, i_vars=None, time_selection=None, **kwargs):
247+
kpi_entity = smsdkentities.get('kpi')
248+
if machine_sources:
249+
machine_types = []
250+
for machine_source in machine_sources:
251+
machine_types.append(self.get_type_from_machine(machine_source, **kwargs))
252+
kwargs["asset_selection"]= {
253+
"machine_source": machine_sources,
254+
"machine_type": list(set(machine_types))
255+
}
256+
257+
if kpis:
258+
d_vars = []
259+
for kpi in kpis:
260+
d_vars.append({"name": kpi, "aggregate": ["avg"]})
261+
kwargs['d_vars'] = d_vars
262+
263+
if i_vars:
264+
kwargs['i_vars'] = i_vars
265+
266+
if time_selection:
267+
kwargs["time_selection"] = time_selection
268+
269+
base_url = get_url(
270+
self.config["protocol"], self.tenant, self.config["site.domain"]
271+
)
272+
return kpi_entity(self.session, base_url).get_kpi_data_viz(**kwargs)
273+
274+
def get_type_from_machine(self, machine_source=None, **kwargs):
275+
machine = smsdkentities.get('machine')
276+
base_url = get_url(
277+
self.config["protocol"], self.tenant, self.config["site.domain"]
278+
)
279+
return machine(self.session, base_url).get_type_from_machine_name(machine_source, **kwargs)
280+

smsdk/config/api_endpoints.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
},
2929
"DataViz": {
3030
"estimate_cycle": "/v1/selector/datavis/estimate/cycle",
31-
"estimate_part": "/v1/selector/datavis/estimate/part"
31+
"estimate_part": "/v1/selector/datavis/estimate/part",
32+
"task": "/v1/datavis/task/async"
33+
},
34+
"KPI": {
35+
"availible_kpis_for_asset": "/v1/selector/datavis/kpi/y_axis"
36+
},
37+
"Assets": {
38+
"url":"/v1/selector/assets"
3239
}
3340
}

0 commit comments

Comments
 (0)