How to call a global script from dashboard?
-
Could anyone tell me how to call a global script from a dashboard?
The layout would be pretty simple:
- point selector
- date range picker
- email recipients
- button
The script takes values from the point selector, date range picker and email recipients. This I know how to do but I just don't know how to call the global script as the on-click action of the button.
Thanks!
-
Hi cbyrne,
You'd want to POST to /rest/v2/script/run with an invocation of the global script function that you want to execute.
-
@phildunlap thanks Phil, could you give me an example of how exactly to call the $http.post request? Sorry, I'm not fluent with angular at all. What I can find online does the whole
var app = angular.module...
boilerplate but I know I'm already in that context in the dashboard designer so I'm note sure how to define the function as I usually would in a scripting context. -
@phildunlap After some work I got a little further but I've hit a stumbling block with the POST data spec
{ "context": [ { "contextUpdate": true, "variableName": "string", "xid": "string" } ], "logLevel": "TRACE", "permissions": [ "string" ], "resultDataType": "string", "script": "string", "wrapInFunction": true }
The global function I want to call is something like
send_http(point, start_time, end_time, email_addresses)
and I have the four parameters. The point I presume I can send as the context, but I don't know how to send the other parameters. -
I was thinking you would just put them directly into the script, in the call to the function, but you're right that you'd have to pass the point as an XID or something, or place it in the context. Even then you'd have a point in the sense of Mango scripting, which has different functions available than a point in dashboard code.
-
Finally got a working solution. Posted the basic custom module below for anyone else who comes across this thread.
define(['angular', 'require'], function(angular, require) { 'use strict'; var userModule = angular.module('userModule', ['maUiApp']); userModule.component('userComponent', { bindings: {}, controller: ['$scope', '$http', function($scope, $http){ $scope.msg = null; $scope.sendPost = function(point_xid, date_from, date_to, email_recipients){ var from = JSON.stringify(date_from); var to = JSON.stringify(date_to); var emails = JSON.stringify(email_recipients); var script = "send_HTTP_debug(point_xid, " + from + ", " + to + ", " + emails + ")" var address= "/rest/v2/script/run"; var content = {'Content-Type': 'application/json;charset=UTF-8'}; var data = { "context": [ { "contextUpdate": true, "variableName": "point_xid", "xid": point_xid }, ], "logLevel": "DEBUG", "permissions": [ "admin" ], "script": script, "wrapInFunction": true }; function success(response){$scope.msg=response;}; function error(response){$scope.msg=response;}; $http.post(address, JSON.stringify(data),content).then(success, error); }; }], template: '\ <div flex layout="column">\ <md-input-container md-no-float="">\ <label>Point</label>\ <ma-point-list ng-model="point" query="query_tmpVal" start="start_tmpVal" limit="limit_tmpVal" sort="sort_tmpVal"></ma-point-list>\ </md-input-container>\ <div flex layout="row">\ <md-input-container style="flex-grow:1">\ <label>From</label>\ <ma-date-picker ng-model="date_from" mode="date" format="DD/MM/YYYY"></ma-date-picker>\ </md-input-container>\ <div style="width="10vw"> </div>\ <md-input-container style="flex-grow:1">\ <label>To</label>\ <ma-date-picker ng-model="date_to" mode="date" format="DD/MM/YYYY"></ma-date-picker>\ </md-input-container>\ </div>\ <md-input-container>\ <ma-email-recipients ng-model="email_recipients"></ma-email-recipients>\ </md-input-container>\ <md-button class="md-primary md-raised" ng-click="sendPost(point.xid, date_from, date_to, email_recipients)">Request Report</md-button>\ <p>{{msg}}</p>\ </div>\ ' }); return userModule; }); // define
-
Thanks for sharing what you came up with!
I will offer calling the component "userComponent" could lead to confusion in the future, but maybe that's just for the sake of the example.
Maybe also for posterity i'll offer the script permission being used is "admin" instead of "superadmin", in case someone tries leveraging this and doesn't have an "admin" permission group defined.
Thanks!
-
@phildunlap Thanks Phil! The naming was just for show, it's named differently in our system for the exact reason you gave. Thanks for the note about permissions, fixed now.