|
| 1 | +from nisystemlink.clients.testmonitor import TestMonitorClient |
| 2 | +from nisystemlink.clients.testmonitor.models import ( |
| 3 | + CreateResultRequest, |
| 4 | + CreateStepRequest, |
| 5 | + NamedValue, |
| 6 | + QueryStepsRequest, |
| 7 | + QueryStepValuesRequest, |
| 8 | + StepField, |
| 9 | + StepIdResultIdPair, |
| 10 | + UpdateStepRequest, |
| 11 | +) |
| 12 | +from nisystemlink.clients.testmonitor.models._status import Status |
| 13 | +from nisystemlink.clients.testmonitor.models._step_data import Measurement, StepData |
| 14 | + |
| 15 | + |
| 16 | +def create_test_result(): |
| 17 | + """Create example result on your server.""" |
| 18 | + new_results = [ |
| 19 | + CreateResultRequest( |
| 20 | + part_number="Example 123 AA", |
| 21 | + program_name="Example Name", |
| 22 | + host_name="Example Host", |
| 23 | + status=Status.PASSED(), |
| 24 | + keywords=["original keyword"], |
| 25 | + properties={"original property key": "yes"}, |
| 26 | + ) |
| 27 | + ] |
| 28 | + create_response = client.create_results(new_results) |
| 29 | + return create_response |
| 30 | + |
| 31 | + |
| 32 | +# Server configuration is not required when used with Systemlink Client or run throught Jupyter on SLE |
| 33 | +server_configuration = None |
| 34 | + |
| 35 | +# # Example of setting up the server configuration to point to your instance of SystemLink Enterprise |
| 36 | +# server_configuration = HttpConfiguration( |
| 37 | +# server_uri="https://yourserver.yourcompany.com", |
| 38 | +# api_key="YourAPIKeyGeneratedFromSystemLink", |
| 39 | +# ) |
| 40 | + |
| 41 | +client = TestMonitorClient(configuration=server_configuration) |
| 42 | + |
| 43 | +# create a result to attach the steps to |
| 44 | +create_response = create_test_result() |
| 45 | + |
| 46 | +# Create the step requests |
| 47 | +result_id = create_response.results[0].result_id |
| 48 | +step_requests = [ |
| 49 | + CreateStepRequest( |
| 50 | + step_id="step1", |
| 51 | + name="step1", |
| 52 | + result_id=result_id, |
| 53 | + inputs=[ |
| 54 | + NamedValue(name="Temperature", value="35"), |
| 55 | + NamedValue(name="Voltage", value="5"), |
| 56 | + ], |
| 57 | + ), |
| 58 | + CreateStepRequest( |
| 59 | + step_id="step2", |
| 60 | + name="step2", |
| 61 | + result_id=result_id, |
| 62 | + ), |
| 63 | +] |
| 64 | + |
| 65 | +# Create the steps |
| 66 | +create_response = client.create_steps(steps=step_requests) |
| 67 | +created_steps = create_response.steps |
| 68 | +print(create_response) |
| 69 | + |
| 70 | +# You can query steps based on any field using DynamicLinq syntax. |
| 71 | +# These are just some representative examples. |
| 72 | +# Query based on result id |
| 73 | +query_response = client.query_steps( |
| 74 | + QueryStepsRequest(filter=f'resultId == "{result_id}"') |
| 75 | +) |
| 76 | +queried_steps = query_response.steps |
| 77 | + |
| 78 | +# query step name using query step values |
| 79 | +query_values_response = client.query_step_values( |
| 80 | + QueryStepValuesRequest( |
| 81 | + filter=f'resultId == "{result_id}"', |
| 82 | + field=StepField.NAME, |
| 83 | + ) |
| 84 | +) |
| 85 | + |
| 86 | +# update the data of the step |
| 87 | +# extra properties of the measurements will be converted to string if not already a string |
| 88 | +update_response = client.update_steps( |
| 89 | + steps=[ |
| 90 | + UpdateStepRequest( |
| 91 | + step_id=step.step_id, |
| 92 | + result_id=step.result_id, |
| 93 | + data=StepData( |
| 94 | + text="My output string", |
| 95 | + parameters=[ |
| 96 | + Measurement( |
| 97 | + name="Temperature", |
| 98 | + status="Passed", |
| 99 | + measurement="35", |
| 100 | + lowLimit="30", |
| 101 | + highLimit="40", |
| 102 | + units="C", |
| 103 | + comparisonType="Numeric", |
| 104 | + spec_id="spec1", |
| 105 | + spec_info={ |
| 106 | + "specKey": 10 |
| 107 | + }, # will be converted to string as '{"specKey": 10}' |
| 108 | + ) |
| 109 | + ], |
| 110 | + ), |
| 111 | + ) |
| 112 | + for step in created_steps |
| 113 | + ] |
| 114 | +) |
| 115 | + |
| 116 | +# delete all steps at once |
| 117 | +delete_response = client.delete_steps( |
| 118 | + steps=[ |
| 119 | + StepIdResultIdPair(step_id=step.step_id, result_id=step.result_id) |
| 120 | + for step in queried_steps |
| 121 | + ] |
| 122 | +) |
| 123 | + |
| 124 | +create_response = client.create_steps(steps=step_requests) |
| 125 | +created_steps = create_response.steps |
| 126 | + |
| 127 | +# delete steps one by one |
| 128 | +for step in created_steps: |
| 129 | + if step.step_id and step.result_id: |
| 130 | + client.delete_step(result_id=step.result_id, step_id=step.step_id) |
0 commit comments