Objectives
In this Exercise you will learn how to register custom function with Maximo Monitor.
Install the custom function locally
Install the custom function in your local environment. Update the variables in {} to match your environment.
pip3 install git+https://{XXXXXX}@github.com/{user_id}{path_to_repository}@starter_package --upgrade
Replace {xxxxxx}
with your personal access token.
- Test your custom function locally
- Create a script called test_my_custom_function.py in the scripts folder. In the script, import Python libraries and packages:
import datetime as dt import json import pandas as pd import numpy as np from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func from iotfunctions.base import BaseTransformer from iotfunctions.metadata import EntityType from iotfunctions.db import Database from iotfunctions import ui
-
Connect to Maximo Monitor. In the test_my_custom_function.py script, add:
with open('credentials_as.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials)
- Import and instantiate the function. In the test_my_custom_function.py script, add the following code. Update the variables in {} to match your environment.
from custom{yourinitials}.multiplybyfactor{yourinitials} import MultiplyByFactor{YourInitials} fn = MultiplyByFactor{YourInitials}( input_items = ['speed', 'travel_time'], factor = '2', output_items = ['adjusted_speed', 'adjusted_travel_time'] ) df = fn.execute_local_test(db=db, db_schema=db_schema, generate_days=1,to_csv=True) print(df)
- Run the script from the command line. The date frame results are saved to a .csv file. Look for df_test_entity_for_multiplybyfactor{your_initials}.csv in the scripts directory. Enter:
python3 test_my_custom_function.py
Register your custom function
- Create a script called register_my_custom_function.py in the scripts folder. In the script, import Python libraries and packages:
-
import datetime as dt import json import pandas as pd import numpy as np from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func from iotfunctions.base import BaseTransformer from iotfunctions.metadata import EntityType from iotfunctions.db import Database from iotfunctions import ui
- Connect to Maximo Monitor. In the register_my_custom_function.py script, add:
with open('credentials_as.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials)
- In the register_my_custom_function.py script, to register the function, add the following code. Update the variables in {} to match your environment.
from custom{yourinitials}.multiplybyfactor{yourinitials} import MultiplyByFactor{YourInitials} db.register_functions([MultiplyByFactor{YourInitials}])
- Run the script from the command line. For example:
python3 register_my_custom_function.py
Congratulations you have successfully registerd a custom function with maximo monitor