<commit_before>const db = require('./db');

const checks = {
    db: function(callback) {
        db.getTuple('SELECT 1 AS [ok]', null, function(err, row) {
            if (err) {
                return callback(err);
            }
            if (row.ok.value !== 1) {
                return callback(new Error('DB query error'));
            }
            return callback(null);
        });
    }
};

module.exports = function healthcheck(callback) {
    let pending = 0;
    const results = {
        healthy: true,
        checks: {}
    };
    Object.keys(checks).forEach((checkName) => {
        pending += 1;
        checks[checkName]((err, result) => {
            if (err) {
                results.healthy = false;
                results.checks[checkName] = err.message;
            } else {
                results.checks[checkName] = 'ok';
            }
            pending -= 1;
            finalize();
        });
    });

    function finalize() {
        if (pending) {
            return;
        }

        results.uptime = process.uptime();

        try {
            results.build = require('../build-info.json');
        } catch (ex) {
            // no build info to show
        }

        callback(null, results);
    }
};<commit_msg>Also show version number in healthcheck response<commit_after>const db = require('./db');

const checks = {
    db: function(callback) {
        db.getTuple('SELECT 1 AS [ok]', null, function(err, row) {
            if (err) {
                return callback(err);
            }
            if (row.ok.value !== 1) {
                return callback(new Error('DB query error'));
            }
            return callback(null);
        });
    }
};

module.exports = function healthcheck(callback) {
    let pending = 0;
    const results = {
        healthy: true,
        checks: {}
    };
    Object.keys(checks).forEach((checkName) => {
        pending += 1;
        checks[checkName]((err, result) => {
            if (err) {
                results.healthy = false;
                results.checks[checkName] = err.message;
            } else {
                results.checks[checkName] = 'ok';
            }
            pending -= 1;
            finalize();
        });
    });

    function finalize() {
        if (pending) {
            return;
        }

        results.uptime = process.uptime();

        try {
            results.build = require('../build-info.json');
        } catch (ex) {
            // no build info to show
        }

        try {
            results.version = require('../package.json').version;
        } catch (ex) {
            // no version info to show
        }

        callback(null, results);
    }
};
{'fraction_non_alphanumeric': 0.1359940872135994, 'fraction_numerical': 0.003695491500369549, 'mean_word_length': 1.35801393728223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5357029', 'n_tokens_mistral': 762, 'n_tokens_neox': 723, 'n_words': 166}