Debugging scripts

How to debug your scripts locally

Please check the previous page Endpoints for further detail on how to obtain data from our API.

Prerequisites

  1. All prerequisites from Endpoints fulfilled

  2. An IDE setup for your script language e.g:

    • VS-Code with the plugins for the script language

      • R and RTools

      • Python

    • RStudio

    • Pycharm

  3. Open the script in the SF platform

Generated variable names

In the SF platform, the execution of datasets for scripts and the assignment to variables is done in the background. These variable names are sanitized and deduplicated, which means they are depending on all the names of the datasets, the column names, and the order they are assigned to the script. Therefore it's no simple task to "guess" the variable name that is created in the background.

R Python

Suggested approach

In order to be able to easily switch between local development and the SF platform, you should start the script with intermediate variables and assign the variables of the dataset.

Assigning variables in SF

RPython

Retrieving data from SF

Here is a minimal example of how to retrieve data from SF and assign the columns to variables.

RPython library(httr) library(jsonlite) rest_url <- "https://<your senseforce backend platform url>/api/dataset/execute/<id>" header_auth <- c("Authorization" = "Bearer <your API access token>") header_type <- c("Content-Type" = "application/json") headers <- add_headers(header_auth, header_type) req <- POST(rest_url, body = "[]", headers) stop_for_status(req) res_df <- data.frame(fromJSON(content(req, "text", "application/json"))) script_variable_1 <- res_df$timestamp script_variable_2 <- res_df$someColumn

Interchangeable code

With this setup, you can now use these variables the same way locally and in SF. Therefore you can copy, the interchangeable part, of your local script to SF and vice versa.

For flawless copying, it is also advisable to make use of the results also in the local script.

The interchangeable part of the code is everything after the setup of the variables and before, possible debug code of the local implementation (e.g. print(variable)).

Senseforce platform

Local code

 

More complex examples

The following shows a more complex example. Further details can be found in the Endpoints section.

  • Multiple datasets: Some scripts use multiple datasets and for this, it is advisable to abstract the loading of datasets.

  • Filters: When it is necessary to simulate the filters from dashboards or automations, the API gives the possibility to add filters to a dataset.