Expression Script Examples: Difference between revisions
No edit summary |
|||
Line 115: | Line 115: | ||
<pre> | <pre> | ||
// Copies all | // Copies all datatables in a project to another project including datatable contents. | ||
// Usage instructions: | // Usage instructions: | ||
// 1. Create expression script in the project from where you want to copy the | // 1. Create expression script in the project from where you want to copy the datatables. | ||
// 2. Create a new project named as "<name of the project to be moved> - Snowflake". New datatables will be created here. E.g., when moving project named "SAP_OrderToCash", the target project should be named as "SAP_OrderToCash - Snowflake". | // 2. Create a new project named as "<name of the project to be moved> - Snowflake". New datatables will be created here. E.g., when moving project named "SAP_OrderToCash", the target project should be named as "SAP_OrderToCash - Snowflake". | ||
// 3. Run the script. | // 3. Run the script. |
Revision as of 23:01, 21 June 2022
This page contains script examples written in the QPR ProcessAnalyzer expression language. See how expression scripts can be created in the Workspace. For documentation for the syntax, functions and entities can be found from the main page in the KPI Expression Language section.
Call web service
Contact to a web service, fetch some data, and store it to a datatable.
let datatableName = "Web Service Data"; let webServiceData = ParseJson(ReadWebService( #{"Address": "https://processanalyzer.onqpr.com/qprpa/api/serverinfo"} )); let targetDatatable = Project.Datatables.Where(name==datatableName); if (Count(targetDatatable) == 0) { targetDatatable = Project.CreateDatatable(datatableName) .AddColumn("Setting name", "String") .AddColumn("Setting value", "String") .AddColumn("Data read", "DateTime"); } else { targetDatatable = targetDatatable[0]; } let currentTime = Now; let dataAsDf = ToDataFrame( webServiceData.keys.{ let key = _; [key, webServiceData[key], currentTime]; }, ["Setting name", "Setting value", "Data read"] ); targetDatatable.Import(dataAsDf, #{"Append":true}); WriteLog(`${CountTop(dataAsDf.Rows)} rows written to datatable`);
Store data to datatable
Get all models in the system and store them to a datatable.
let newDatatable = Project .CreateDatatable("Models list " + ToString(Now, "dd.MM.yyyy HH:mm:ss")) .AddColumn("Model name", "String") .AddColumn("Project name", "String") .AddColumn("Created time", "DateTime") .AddColumn("Cases", "Integer"); let startTime = Now; let modelsData = ToDataFrame( Models.([Name, Project.Name, CreatedDate, NCases]), ["Model name", "Project name", "Created time", "Cases"] ); WriteLog(`Listing models took ${(Now - startTime).TotalSeconds.Round(2)} seconds.`); newDatatable.Import(modelsData); WriteLog(`Datatable ${newDatatable.Id} created.`);
Convert datatable column data
This script can be used to convert a single column into numerical data type. To use the script, you need to setup the following in the beginning of the script:
- Project name where the datatable is located.
- Datatable name
- Name of the column to be converted
Note that the conversion fails, if there is data that cannot be converted into numerical format. The conversion assumes that period (.) is used as the decimal point.
let projectName = "New Project"; let datatableName = "qpr processanalyzer events"; let columnName = "Event order in case"; let project = (Projects.Where(Name==projectName))[0]; let datatable = (project.Datatables.Where(Name==datatableName))[0]; DatatableById(datatable.Id).DataFrame .SetColumns([ columnName: () => { let data = Column(columnName); if (data == null) { null; } else { ToFloat(data); } } ]) .Persist(datatable.Name, ["ProjectId": project.Id, "Append": false]);
Instead of converting to numeric (with the ToFloat function), data can be converted into string using the ToString function.
Show DataFrame as HTML table
This script defines a function to show dataframe as a HTML table, and uses the function for a literal dataframe.
function dataframeToHtmlTable(df) { return `<table> <tr> ${StringJoin("\r\n\t\t", + df.columns.`<th>${_}</th>`)} </tr> ${StringJoin("", df.Rows.( "\r\n\t<tr>" + StringJoin("", _.`\r\n\t\t<td>${ToString(_)}</td>`) + "\r\n\t</tr>" ))} </table>` } let data = ToDataFrame( [ ["one", "two", "three"], ["four", "five", "six"], ["seven", "eight", "nine"] ], ["Column 1", "Column 2", "Column 3"] ); return dataframeToHtmlTable(data);
Copy local datatables to Snowflake
// Copies all datatables in a project to another project including datatable contents. // Usage instructions: // 1. Create expression script in the project from where you want to copy the datatables. // 2. Create a new project named as "<name of the project to be moved> - Snowflake". New datatables will be created here. E.g., when moving project named "SAP_OrderToCash", the target project should be named as "SAP_OrderToCash - Snowflake". // 3. Run the script. // NOTE: Columns of type "Any" will be created as "String"-columns in Snowflake, thus it is recommended that actual data types are set for the tables prior to the move. let sourceProject = Project; let sourceProjectName = Project.Name; let targetProjectName = `${sourceProjectName} - Snowflake`; let targetProject = First(Projects.Where(Name == targetProjectName)); if (IsNull(targetProject)) { WriteLog(`Unable to find target project named "${targetProjectName}". Aborting operation.`); return; } let dts = sourceProject.DataTables; WriteLog(`Copying all ${CountTop(dts)} data tables found in project "${sourceProject.Name}" (id: ${sourceProject.Id}) to Snowflake in project "${targetProject.Name}" (id: ${targetProject.Id})`); dts.{ let sourceDt = _; WriteLog(`Starting to copy data table "${Name}" (id: ${Id}) having ${NRows} rows and ${NColumns} columns.`); let targetDt = targetProject.CreateDataTable(sourceDt.Name, #{"Connection": CreateSnowflakeConnection(#{"ProjectId": targetProject.Id})}); targetDt.Import(sourceDt.SqlDataFrame); WriteLog(`Finished copying data table "${Name}" (id: ${Id}) to table "${targetDt.Name}" (id: ${targetDt.Id})`); } WriteLog(`Finished copying all the data tables found in project "${sourceProject.Name}" (id: ${sourceProject.Id}) to Snowflake in project "${targetProject.Name}" (id: ${targetProject.Id})`);