def main(argv): sys.path.append( os.path.realpath(os.path.join(os.path.dirname(__file__), '..'))) credPath = os.path.join(os.path.dirname(__file__), f"credentials_as_{os.environ['USERNAME']}.json") print(f"Loading credentials from {credPath}") with io.open(credPath, encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials) from goodvibrations.predictStatus import PredictCondition print(f"Registering function") db.unregister_functions(["PredictCondition"]) try: db.register_functions([PredictCondition]) except Exception as exc: print(exc) fn = PredictCondition(condition='predStatus') df = fn.execute_local_test(db=db, db_schema=db_schema, generate_days=1, to_csv=True) print(df)
#!/user/bin/env python3 import json import logging from iotfunctions.db import Database from iotfunctions.enginelog import EngineLogging EngineLogging.configure_console_logging(logging.DEBUG) with open('../dev_resources/credentials_as_dev.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials) db.unregister_functions(['MultiplyByFactorSS'])
import datetime as dt import json import pandas as pd import numpy as np import logging from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func from iotfunctions.db import Database from iotfunctions.enginelog import EngineLogging EngineLogging.configure_console_logging(logging.DEBUG) ''' You can test functions locally before registering them on the server to understand how they work. Supply credentials by pasting them from the usage section into the UI. Place your credentials in a separate file that you don't check into the repo. ''' with open('credentials.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials) db.unregister_functions(['NewFunction'])
PACKAGE_URL = "https://github.com/sedgewickmm18/mmfunctions" credentials = { "tenantId": "AnalyticsServiceDev", "db2": { "username": "******", "password": "******", "databaseName": "BLUDB", "port": 50000, "httpsUrl": "https://dashdb-enterprise-yp-dal13-74.services.dal.bluemix.net:50000", "host": "dashdb-enterprise-yp-dal13-74.services.dal.bluemix.net" } } #with open('credentials_as_dev.json', encoding='utf-8') as F: # credentials = json.loads(F.read()) ''' The db_schema is the db2_schema name for your client database. If you are using the default schema of your user, there is no need to provide a schema. ''' db_schema = None ''' Use the credentials to build an AS Database connection. ''' db = Database(credentials=credentials) db.unregister_functions(["AggregateItemStats"])
import json from iotfunctions.db import Database with open('credentials_as.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials) # from rawkintrevo_tutorial2.divbyfactor import RawkintrevosDivByFactor from rawkintrevo_tutorial2.LegitRequest import ExternalModel db.unregister_functions(["ExternalModel"]) db.register_functions([ExternalModel]) import rawkintrevo_tutorial2 db.register_module(rawkintrevo_tutorial2) import rawkintrevo_tutorial2.LegitRequest db.register_module(rawkintrevo_tutorial2.LegitRequest)
# Explore > Usage > Watson IOT Platform Analytics > Copy to clipboard # Paste contents in credentials_as.json file # Save in scripts ''' ''' 1. Create a database object to access Watson IOT Platform Analytics DB. ''' schema = 'bluadmin' # set if you are not using the default with open('./scripts/credentials_as.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db = Database(credentials=credentials) ''' 2. Register custom function You must unregister_functions if you change the method signature or required inputs ''' db.unregister_functions(['SampleDimensionPreload_SS']) db.register_functions([SampleDimensionPreload_random]) ''' 3. To do anything with IoT Platform Analytics, you will need one or more entity type. This example assumes that the entity to which we are adding dimensions already exists We add the custom function to this entity type to test it locally ''' entity_name = 'issue_455_blank_random' entity_type = db.get_entity_type(name=entity_name) # get dimension table name - to add dimension values to try: dim_table_name = ( entity_type.get_attributes_dict()['_dimension_table_name']).lower() except: dim_table_name = entity_name + '_dimension'
import datetime as dt import json import pandas as pd import numpy as np import logging from sqlalchemy import Column, Integer, String, Float, DateTime, Boolean, func from iotfunctions.db import Database from iotfunctions.enginelog import EngineLogging EngineLogging.configure_console_logging(logging.DEBUG) ''' You can test functions locally before registering them on the server to understand how they work. Supply credentials by pasting them from the usage section into the UI. Place your credentials in a separate file that you don't check into the repo. ''' with open('credentials.json', encoding='utf-8') as F: credentials = json.loads(F.read()) db_schema = None db = Database(credentials=credentials) db.unregister_functions(['HelloWorld'])
#EngineLogging.configure_console_logging(logging.DEBUG) ''' # Replace with a credentials dictionary or provide a credentials # Explore > Usage > Watson IOT Platform Analytics > Copy to clipboard # Past contents in a json file. ''' with open('credentials.json', encoding='utf-8') as F: credentials = json.loads(F.read()) ''' Developing Test Pipelines ------------------------- When creating a set of functions you can test how they these functions will work together by creating a test pipeline. ''' # t = BaseTransformer() ''' Create a database object to access Watson IOT Platform Analytics DB. ''' db = Database(credentials = credentials) db_schema = None # set if you are not using the default function_name = "InvokeWMLModel" db.unregister_functions(function_name) db.register_functions([InvokeWMLModel]) print("Function registered") exit()
Column('ORGANIZATION', String(50)), Column('FUNCTION', String(50)), **{'schema': db_schema}) entity_dimension = entity.get_attributes_dict()['_dimension_table_name'] ''' When creating an EntityType object you will need to specify the name of the entity, the database object that will contain entity data After creating an EntityType you will need to register it so that it visible in the Add Data to Entity Function UI. To also register the functions and constants associated with the entity type, specify 'publish_kpis' = True. ''' entity.register(raise_error=False) # When creating a custom preload function you can register it by uncommenting the following lines # You must unregister_functions if you change the method signature or required inputs. db.unregister_functions(['Issue455HTTPPreload']) db.register_functions([Issue455HTTPPreload]) ''' To test the execution of kpi calculations defined for the entity type locally use this function. A local test will not update the server job log or write kpi data to the AS data lake. Instead kpi data is written to the local filesystem in csv form. ''' entity.exec_local_pipeline(**{'_production_mode': False}) ''' view entity data ''' print("Read Table of new entity") df = db.read_table(table_name=entity_name, schema=db_schema)