As I was working on an app that displays live data, I was called to display the time when the QVF was created. In Qlik Sense we have the function ReloadTime(), but how do we call that in our script from the API and how do we display that based on our region since, our server may very well be in another one?
In an earlier post, I showed how do connect to our app with qsocks and the Engine API Engine API and qSocks - Connecting and getting a list of available apps
After we establish our connection and create the session, we will create our HyperQube of a simple ListObject.
var obj = {
"qInfo": {
"qId": "LB02",
"qType": "ListObject"
},
"qListObjectDef": {
"qStateName": "$",
"qDef": {
"qFieldDefs": [
"Round"
],
"qFieldLabels": [
"Round"
],
"qSortCriterias": [{
"qSortByExpression": -1,
"qExpression": {
"qv": ""
}
}]
},
"qInitialDataFetch": [{
"qTop": 0,
"qLeft": 0,
"qHeight": 100,
"qWidth": 2
}]
}
This only returns one field, in this case the "Round". In order to get the time we will need to create an expression and get it as a second field. So we add right after "qInitialDataFetch"
,
"qExpressions": [{
"qExpr": "=ReloadTime()"
}]
So now we can create our session and grab all of the fields. For now, we will just focus on the time so here we will take it from the array returned and store it in a global variable reloadTime.
qsocks.Connect(app.config).then(function(global) {
global.openDoc(id).then(function(app) {
app.createSessionObject(obj).then(function(list) {
list.getLayout().then(function(layout) {
reloadTime = layout.qListObject.qDataPages[0].qMatrix[0][1].qText;
});
});
});
});
Now we need to display this on our page but based on the timezone of the user.
First we need to create the returned date as a javascript date so we can work with it,
var lastReloadTime = new Date(reloadTime);
Get the time server/user time difference in hours.
var timeDiff = lastReloadTime.getTimezoneOffset() / 60;
Now set the time to the users time.
lastReloadTime.setHours(lastReloadTime.getHours() - timeDiff);
Finally Display it.
$('#lastReloadTime').html('LAST UPDATE: ' + lastReloadTime.toLocaleTimeString());