HOW TO: save / export a MyKPI data into a CSV file

×

Warning message

You can't delete this newsletter because it has not been sent to all its subscribers.

if you would like to save your MyKPI data into a CSV file you can use the following IOT Application.

First Part

The first flow creates a web page with a form inside

the WEB page on Chrome is accessible from https://iot-app.snap4city.org/nodered/YYYYY/simplex09, where YYYYY is the IOT App ID of your APP.

set the GET HTTP node as

 please note that the IOT App ID is reported at the end of the dialog.

The form is specified as decribed in the following into the "Basic HTML" node, for msg.payload, and mustache templates: whre YYYYYY has to substituted with your IOT App ID.

<html>
 <head></head>
 <body>
      <h1>Please enter the Key to get the CSV file of pax09!</h1>

 <div id="idhello"><span id="idnameout"></span></div>
 <form id="idform" action="/nodered/YYYYYYY/simplex09" method="post">
 <div>
 <label for="StartDateTime">Start Date and Time (gg/mm/aaaa hh:mm) in Chrome assisted input:</label>
 <span>
 <input type="datetime-local" id="startdaytime" name="StartDateTime">
 </span>
 <P>
  <span>
 <label for="var1">Key to get:</label>
 <input type="password" name="var1" id="var1" />
 </span>
 </p>
 <span>
 <input type="submit" value="Submit" id="idsubmit" />
 </span>
 </div>
 </form>
The end date is the current date.
 </body>
 </html>

The Second part

set the first HTTP node in POST

     set the switch for the KEY your would like to impose

in node "remove Req and Res" please insert the following code to set Object and set dates:

flow.set("reqObject", msg.req);
flow.set("resObject", msg.res);
var now = new Date().toLocaleString("IT");

var str = msg.payload.StartDateTime
var res = str.replace(" ", "T");
var str2 = now
var res2 = str2.replace(" ", "T");

msg = {
    payload : {
//    : msg.payload.var1.toString()
      "startdate" : res,
      "endate" : res2
    }
}
return msg;

In the MyKPI identify you MyKPI and nothing else.

For the rest of the flow please import the following nodes via clipboard thus generatin file XXXXXXX.csv. The nodes are used to set the date and time, for generating the CSV file from the set of messages, for the addition of an header.

[{"id":"e1cb3a08.df0a58","type":"csv","z":"59c1daf8.515c04","name":"JSON ARRAY to CSV","sep":",","hdrin":false,"hdrout":true,"multi":"one","ret":"\\r\\n","temp":"id,kpiId,dataTime,insertTime,value","x":460,"y":340,"wires":[["320177b8.ffb408"]]},{"id":"db484555.611408","type":"http response","z":"59c1daf8.515c04","name":"","statusCode":"","headers":{},"x":990,"y":320,"wires":[]},{"id":"ffe0696f.cf54a8","type":"function","z":"59c1daf8.515c04","name":"Format Date","func":"for (var i = 0; i< msg.payload.length; i++){\n    msg.payload[i].dataTime = new Date(msg.payload[i].dataTime).toISOString();\n    msg.payload[i].insertTime = new Date(msg.payload[i].insertTime).toISOString();\n}\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":340,"wires":[["e1cb3a08.df0a58"]]},{"id":"320177b8.ffb408","type":"function","z":"59c1daf8.515c04","name":"Insert Req e Res and headers","func":"msg.req = flow.get(\"reqObject\");\nmsg.res = flow.get(\"resObject\");\nmsg.headers = {\n    \"Content-disposition\":\"attachment; filename=XXXXXXX.csv\",\n    \"Content-type\": \"text/csv\"\n}\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":340,"wires":[["db484555.611408"]]},{"id":"feeb5c66.155d3","type":"template","z":"59c1daf8.515c04","name":"wrong code","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html> \n <head>\n </head>\n <body>\n <h1>Wrong Code</h1>\n <p>Retry</p>\n </body>\n</html> ","output":"str","x":770,"y":300,"wires":[["db484555.611408"]]}]

The the execution when the Key is correct a CSV is provided as download from the above mentioned Web Page and providing the file XXXXXXX.csv

Please note that this is possible only for MYKPI which are public, are your on for which you have been delegated in access by the owner.