Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
d_pranskus
Partner - Creator III
Partner - Creator III

Creating a measure with enigma.js

Hi

I am trying to create a measure using enigma.js. Following is my code

const enigma = require('../node_modules/enigma.js/enigma');

const config = require('../config/config');

const fs = require('fs');

const schema = require(`enigma.js/schemas/${config.enigma.schema}.json`);

const WebSocket = require('ws');

const enigmaConfig = documentId => {

    let suffix = '';

    if (documentId) suffix = `/app/${documentId}`;

    return {

        schema: schema,

        url: `wss://${config.engine.hostname}:${config.engine.portNumber}${suffix}`,

        createSocket: url => new WebSocket(url, {

            ca: [fs.readFileSync(config.certificates.root)],

            key: fs.readFileSync(config.certificates.client_key),

            cert: fs.readFileSync(config.certificates.client),

            headers: {

                'Content-Type': 'application/json',

                'X-Qlik-User': config.engine.repoAccount

            },

        })

    };

};

let state = {};

module.exports = {

    test: (documentId) => {

        state = {};

        return new Promise((resolve, reject) => {

            state['session'] = enigma['create'](enigmaConfig(null));

            state['session'].open()

                .then(global => {

                    return global['openDoc'](documentId, '', '', '', true);

                })

                .then(document => {

                    const data = {

                        qInfo: {

                            qType: 'measure',

                            qId: 'ABC123'

                        },

                        qMeasure: {

                            qLabel: 'Sample Measure',

                            qDef: '=SUM(50)',

                            qGrouping: "N",

                            qExpressions: [],

                            qActiveExpression: 0

                        },

                        qMetaDef: {

                            title: 'Sample Measure',

                            description: "",

                            tags: []

                        }

                    };

                    return document['createMeasure'](data)

                        .then(measure => {

                            return measure['getProperties']();

                        });

                })

                .then(properties => {

                    state['session'].close();

                    return resolve(properties);

                })

                .catch(error => {

                    if (state['session']) state['session'].close();

                    reject(error);

                });

        })

    }

};

When I run this code I get the response that the measure has been created with the measure properties:

{

    "qInfo": {

        "qId": "ABC123",

        "qType": "measure"

    },

    "qMeasure": {

        "qLabel": "Sample Measure",

        "qDef": "=SUM(50)",

        "qGrouping": "N",

        "qExpressions": [],

        "qActiveExpression": 0

    },

    "qMetaDef": {

        "title": "Sample Measure",

        "description": "",

        "tags": []

    }

}

But when I open Qliksense App through hub, I can't see the measure in the master Items.

What might be a problem?

Thanks

Darius

1 Solution

Accepted Solutions
d_pranskus
Partner - Creator III
Partner - Creator III
Author

Hi Again

It looks like this is related to the user sessions. I was using INTERNAL sa_repository user for enigma.js calls and local user for hub. That's why the metrics created by sa_reository user via enigma calls were not visible for local user. If I use the same local user to login using hub and the same to create the measure with Enigma JS, then the measure appears in the hub.

Could somebody please comment on this. What I need to do so that measure created with one user is visible for another?

Thank you

View solution in original post

1 Reply
d_pranskus
Partner - Creator III
Partner - Creator III
Author

Hi Again

It looks like this is related to the user sessions. I was using INTERNAL sa_repository user for enigma.js calls and local user for hub. That's why the metrics created by sa_reository user via enigma calls were not visible for local user. If I use the same local user to login using hub and the same to create the measure with Enigma JS, then the measure appears in the hub.

Could somebody please comment on this. What I need to do so that measure created with one user is visible for another?

Thank you