<commit_before>/*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
regexp:true, undef:true, strict:true, trailing:true, white:true */
/*global X: true, XM:true */

(function () {
  "use strict";

  /**
    When a client asks us to run a report, run it, save it in a temporary table,
    and redirect to pentaho with a key that will allow pentaho to access the report.
   */


  var queryForData = function (session, query, callback) {
    var userId = session.passport.user.username,
      userQueryPayload = '{"requestType":"retrieveRecord","recordType":"XM.UserAccountRelation","id":"%@"}'.f(userId),
      userQuery = "select xt.retrieve_record('%@')".f(userQueryPayload);

    // first make sure that the user has permissions to export to CSV
    // (can't trust the client)
    X.database.query(session.passport.user.organization, userQuery, function (err, res) {
      var retrievedRecord;

      if (err || !res || res.rowCount < 1) {
        callback("Error verifying user permissions", null);
        return;
      }

      retrievedRecord = JSON.parse(res.rows[0].retrieve_record);
      if (retrievedRecord.disableExport) {
        // nice try, asshole.
        callback("Stop trying to hack into our database", null);
        return;
      }

      X.database.query(session.passport.user.organization, query, callback);
    });
  };
  exports.queryForData = queryForData;

  exports.report = function (req, res) {
    var requestDetails = req.query.details,
      requestDetailsQuery,
      query;

    requestDetails = JSON.parse(requestDetails);
    requestDetails.username = req.session.passport.user.username;
    requestDetailsQuery = requestDetails.query;
    requestDetails = JSON.stringify(requestDetails);
    query = "select xt.fetch('%@')".f(requestDetails);

    queryForData(req.session, query, function (err, result) {
      if (err || !result) {
        res.send({isError: true, message: err});
        return;
      }
      // thanks http://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript
      var randomKey = Math.random().toString(36).substr(2, 15),
        tempDataModel = new XM.BiCache(null, {isNew: true}),

        attrs = {
          key: randomKey,
          query: JSON.stringify(requestDetailsQuery),
          data: result.rows[0].fetch,
          created: new Date()
        },
        success = function () {
          var biUrl = X.options.datasource.biUrl || "",
            modelName = requestDetailsQuery.recordType.suffix().replace("Item", ""),
            fileName = modelName + ".prpt",
            redirectUrl = biUrl + "&name=" + fileName + "&dataKey=" + randomKey;

          res.redirect(redirectUrl);
        },
        error = function (model, err, options) {
          res.send({isError: true, message: err});
        };

      tempDataModel.save(attrs, {success: success, error: error});
    });
  };

}());
<commit_msg>issue #19379: better logging in report route<commit_after>/*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
regexp:true, undef:true, strict:true, trailing:true, white:true */
/*global X: true, XM:true */

(function () {
  "use strict";

  /**
    When a client asks us to run a report, run it, save it in a temporary table,
    and redirect to pentaho with a key that will allow pentaho to access the report.
   */


  var queryForData = function (session, query, callback) {
    var userId = session.passport.user.username,
      userQueryPayload = '{"requestType":"retrieveRecord","recordType":"XM.UserAccountRelation","id":"%@"}'.f(userId),
      userQuery = "select xt.retrieve_record('%@')".f(userQueryPayload);

    // first make sure that the user has permissions to export to CSV
    // (can't trust the client)
    X.database.query(session.passport.user.organization, userQuery, function (err, res) {
      var retrievedRecord;

      if (err || !res || res.rowCount < 1) {
        callback("Error verifying user permissions", null);
        return;
      }

      retrievedRecord = JSON.parse(res.rows[0].retrieve_record);
      if (retrievedRecord.disableExport) {
        // nice try, asshole.
        callback("Stop trying to hack into our database", null);
        return;
      }

      X.database.query(session.passport.user.organization, query, callback);
    });
  };
  exports.queryForData = queryForData;

  exports.report = function (req, res) {
    var requestDetails = req.query.details,
      requestDetailsQuery,
      query;

    requestDetails = JSON.parse(requestDetails);
    requestDetails.username = req.session.passport.user.username;
    requestDetailsQuery = requestDetails.query;
    requestDetails = JSON.stringify(requestDetails);
    query = "select xt.fetch('%@')".f(requestDetails);

    queryForData(req.session, query, function (err, result) {
      if (err || !result) {
        res.send({
          isError: true,
          error: err,
          message: err.params && err.params.error && err.params.error.message
        });
        return;
      }
      // thanks http://stackoverflow.com/questions/10726909/random-alpha-numeric-string-in-javascript
      var randomKey = Math.random().toString(36).substr(2, 15),
        tempDataModel = new XM.BiCache(null, {isNew: true}),

        attrs = {
          key: randomKey,
          query: JSON.stringify(requestDetailsQuery),
          data: result.rows[0].fetch,
          created: new Date()
        },
        success = function () {
          var biUrl = X.options.datasource.biUrl || "",
            modelName = requestDetailsQuery.recordType.suffix().replace("Item", ""),
            fileName = modelName + ".prpt",
            redirectUrl = biUrl + "&name=" + fileName + "&dataKey=" + randomKey;

          res.redirect(redirectUrl);
        },
        error = function (model, err, options) {
          res.send({
            isError: true,
            error: err,
            message: err.params && err.params.error && err.params.error.message
          });
        };

      tempDataModel.save(attrs, {success: success, error: error});
    });
  };

}());

{'fraction_non_alphanumeric': 0.13557676751334735, 'fraction_numerical': 0.006309658631289436, 'mean_word_length': 2.7672151127361366, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1599478', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1783, 'n_words': 430}