"use strict";
/**
 * 数据湖presto-client模块
 * @date 2022-03
 * @author 何华东
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryRows = exports.prestoClientInstance = void 0;
var mslib_1 = require("mslib");
var Config_1 = require("./Config");
var DateUtils_1 = require("./DateUtils");
var JSONbig = require('json-bigint');
var presto = require('presto-client');
/**
 * 默认配置
 */
var defaults = {
    host: '112.124.209.229',
    port: '8097',
    user: 'hdfs'
};
/**
 * presto-client配置
 */
var config = Object.assign(defaults, Config_1.getDataLakePrestoClientConfig(), { jsonParser: JSONbig });
/**
 * presto-client实例
 */
exports.prestoClientInstance = new presto.Client(config);
/**
 * 查询数据行
 * @param sql sql语句
 * @param timeout 超时时间(毫秒),默认10分钟
 * @returns
 */
function queryRows(sql, timeout) {
    return new Promise(function (resolve, reject) {
        var response = {
            query_id: null,
            columns: null,
            data: [],
        };
        //超时取消查询任务
        var timer = setTimeout(function () {
            if (!response.query_id)
                return;
            exports.prestoClientInstance.kill(response.query_id, function (error) {
                if (error) {
                    mslib_1.logger.error("\u53D6\u6D88\u67E5\u8BE2\u4EFB\u52A1\u5931\u8D25\uFF0Cquery_id\uFF1A" + response.query_id, error);
                }
            });
        }, timeout || 1000 * 60 * 10);
        var startTime = new Date();
        exports.prestoClientInstance.execute({
            query: sql,
            state: function (error, query_id, stats) {
                response.query_id = query_id;
            },
            columns: function (error, data) {
                response.columns = data;
            },
            data: function (error, data, columns, stats) {
                response.data = response.data.concat(data);
            },
            success: function (error, stats, info) {
                var _a;
                clearTimeout(timer);
                var endTime = new Date();
                mslib_1.logger.info("\u6570\u636E\u6E56\u67E5\u8BE2\u5DF2\u5B8C\u6210\uFF0Cquery_id:" + response.query_id + ",\u7ED3\u675F\u67E5\u8BE2\u65F6\u95F4:" + DateUtils_1.default.format(endTime) + ",\u8017\u65F6:" + DateUtils_1.default.secondsDiff(startTime, endTime) + "s,\u603B\u884C\u6570:" + ((_a = response.data) === null || _a === void 0 ? void 0 : _a.length) + ",sql:" + sql);
                var rows = [];
                for (var _i = 0, _b = response.data; _i < _b.length; _i++) {
                    var data = _b[_i];
                    var obj = {};
                    for (var j = 0; j < response.columns.length; j++) {
                        obj[response.columns[j]['name']] = data[j];
                    }
                    rows.push(obj);
                }
                resolve(rows);
            },
            error: function (error) {
                clearTimeout(timer);
                mslib_1.logger.error("\u6570\u636E\u6E56\u67E5\u8BE2\u5931\u8D25\uFF0Cquery_id:" + response.query_id + "\uFF0Csql:" + sql, error.message);
                reject(error);
            }
        });
    });
}
exports.queryRows = queryRows;
//# sourceMappingURL=DataLakePrestoClient.js.map