======================================== Using Python with LiveView REST API ======================================== This sample demonstrates the LiveView REST API using simple Python scripts. The sample Python code is designed to be run from a console terminal, while the Hello LiveView sample is assumed to be running on the default port. The samples support both Python 2 and 3. These Python samples illustrate different features of the LiveView REST API: lvlisttables.py Lists all tables, or optionally all information about a given table, in the LDM lvsnapquery.py Performs snapshot queries lvlivequery.py Performs live queries lvpublish.py Takes input from the console and publishes it to a table lvlistalerts.py Lists all alerts, or optionally all information about a given alert lvmanagealerts.py Create, modify, or delete alerts Each sample has help available by suppling the option "--help" The Python samples all use the Python package "requests". The live query sample additionally uses "sseclient-py". One way to get these packages is: pip install --user Load this sample into Studio as described in the topic, "Loading LiveView Samples into Studio Workspace". From StreamBase Studio on Windows, go to the Help menu > Help Contents > TIBCO Live Datamart Documentation > Start Here > Getting Started with LiveView Server. On Linux, load the following path into your web browser: /opt/tibco/sb-cep/n.m/doc/lvindex.html Then open the Start Here document from the home page link. Also load the Hello LiveView sample into Studio. This sample provides a simple instance of LiveView where the primary data table, ItemsSales, is automatically filled with a feed simulation. To use the Python samples, you must first run the Hello LiveView sample. Follow these steps: 1. Load and start the Hello LiveView sample as documented. Wait for a message in the Console view that begins "All tables have been loaded..." 2. In Studio, select the sample_lv-Python project and right click. Select "StreamBase -> Open StreamBase Command Prompt Here". In that console, type: $ python lvlisttables.py Found 11 tables: LVTables - Information about Tables including description, capabilities, and table space ItemsSales - Live view of all items sold over time, quantity in stock and the last sold price LVSessions - Session information including remote host, recent activity LVSessionQueries - Session Query information including predicate and performance information LVNodeInfo - Local node information LVTableColumns - Table Column information including field names, types and descriptions LVAlertRulesStatus - Recent alert rules status table ItemsInventory - Live view of total inventory dollar exposure at current pricing, aggregated by category LiveViewStatistics - LiveView table statistics LVSessionPublishers - Session Publisher information including table and performance information LVAlerts - Default LiveView Alerts table that Alert Rule Publish Actions populate Next try: $ python lvsnapquery.py -t ItemsSales begin_snapshot: category, quantityRemaining, Item, lastSoldPrice, transactionTime, transactionID Add: [u'electronics', 420, u'Earphones', 14.0, 1502820473231L, 8734] Add: [u'electronics', 66, u'Cellphone', 90.0, 1502820474231L, 8735] . . . Add: [u'clothes', 16, u'Leather Jacket', 30.0, 1502820719298L, 8980] Add: [u'clothes', 15, u'Dress Shoe', 31.0, 1502820720299L, 8981] end_snapshot Found 248 rows You can also supply a LiveQL query: $ python lvsnapquery.py -t ItemsSales -q "select count() as Total,category from ItemsSales group by category" begin_snapshot: category, Total Add: [u'electronics', 56] Add: [u'book', 53] Add: [u'automotive', 49] Add: [u'clothes', 80] Add: [u'toy', 60] end_snapshot Found 5 rows Try with lvlivequery.py $ python lvlivequery.py -t ItemsSales begin_snapshot: category, quantityRemaining, Item, lastSoldPrice, transactionTime, transactionID add: [u'electronics', 0, u'Music Player', 45.0, 1502821373475L, 9634] add: [u'automotive', 131, u'Fuzzy Dice', 20.0, 1502821374475L, 9635] . . . add: [u'clothes', 12, u'Prom Dress', 51.0, 1502821654567L, 9915] add: [u'book', 100, u'Oliver Twist', 9.0, 1502821655568L, 9916] {u'type': u'end_snapshot'} add: [u'electronics', 180, u'Charger', 12.0, 1502821656568L, 9917] add: [u'electronics', 400, u'Earphones', 16.0, 1502821657568L, 9918] . . . You can list the alerts in the server: $ python lvlistalerts.py Found 3 alerts: name: "Trim ItemsSales table" enabled: True status: OK id: dc64bfff-649b-57d7-f0c3-0f71a83c46ed name: "Out of Item" enabled: True status: OK id: 317ad012-6b8a-5a24-f0c3-0f71a83c46ee name: "Low inventory on Item" enabled: True status: OK id: 13fc1fe7-de71-53a3-f0c3-0f71a83c46ef You can list an alert as a json string: $ python lvlistalerts.py -i 13fc1fe7-de71-53a3-f0c3-0f71a83c46ef {"status":"OK","description":null,"message":"Low inventory on item $Item, category $category","name":"Low inventory on Item","severity":3,"quiescence":null,"enabled":true,"owner":"sbuser","created":1392056557715,"lastUpdate":1392056506402,"id":"13fc1fe7-de71-53a3-f0c3-0f71a83c46ef","valid":true,"actions":[{"type":"publish_alert","type":"publish_alert","description":null,"enabled":true,"fieldSubs":{},"message":null,"key":null,"recipient":"*"}],"trigger":{"query":"select * from ItemsSales where quantityRemaining < 30 limit 10000","table":"ItemsSales"}} You can then copy/edit/paste that string to create a new alert. For example starting with the above, create an alert for when "quantityRemaining < 10" and have the message "Very low inventory on item $Item": $ python lvmanagealerts.py -c '{"status":"OK","description":null,"message":"Very low inventory on item $Item, category $category","name":"Very low inventory on Item","severity":3,"enabled":true,"owner":"sbuser","valid":true,"actions":[{"type":"publish_alert","type":"publish_alert","description":null,"enabled":true,"fieldSubs":{},"message":null,"key":null,"recipient":"*"}],"trigger":{"query":"select * from ItemsSales where quantityRemaining < 10 limit 10000","table":"ItemsSales"}}' Now list the alerts and see the newly added one: [campbell@qa7 lv-sample-rest-python]$ python lvlistalerts.py Found 4 alerts: name: "Trim ItemsSales table" enabled: True status: OK id: dc64bfff-649b-57d7-f0c3-0f71a83c46ed name: "Out of Item" enabled: True status: OK id: 317ad012-6b8a-5a24-f0c3-0f71a83c46ee name: "Low inventory on Item" enabled: True status: OK id: 13fc1fe7-de71-53a3-f0c3-0f71a83c46ef name: "Very low inventory on Item" enabled: True status: OK id: 24546a5e-fa49-5fbb-f0c3-0f71a83c46e4 Version History: --------------- v1.2 - Fix serveral scripts v1.1 - add LDM REST 1.1 examples v1.0 - initial release of LDM REST API examples