diff --git a/lib/db/clickhouse.js b/lib/db/clickhouse.js index 88163cd3..008b70e4 100644 --- a/lib/db/clickhouse.js +++ b/lib/db/clickhouse.js @@ -25,7 +25,7 @@ const axios = require('axios') const { samplesTableName, samplesReadTableName } = UTILS const path = require('path') const { Transform } = require('stream') -const { CORS, bun } = require('../../common') +const { CORS, bun, readonly } = require('../../common') const clickhouseOptions = require('./clickhouse_options').databaseOptions const { getClickhouseUrl } = require('./clickhouse_options') @@ -180,6 +180,10 @@ const initialize = async function (dbName) { logger.info('Initializing DB... ' + dbName) const tmp = { ...clickhouseOptions, queryOptions: { database: '' } } ch = new ClickHouse(tmp) + if (readonly) { + state = 'READY' + return + } if (!isOmitTablesCreation()) { const maintain = require('./maintain/index') await maintain.upgrade({ name: dbName, storage_policy: storagePolicy }) @@ -1333,9 +1337,12 @@ const samplesReadTable = { } }, settingsVersions: async function () { - const versions = await axios.post(`${getClickhouseUrl()}/?database=${UTILS.DATABASE_NAME()}`, + const versions = await rawRequest( `SELECT argMax(name, inserted_at) as _name, argMax(value, inserted_at) as _value - FROM settings${dist} WHERE type == 'update' GROUP BY fingerprint HAVING _name != '' FORMAT JSON`) + FROM settings${dist} WHERE type == 'update' GROUP BY fingerprint HAVING _name != '' FORMAT JSON`, + null, + UTILS.DATABASE_NAME() + ) for (const version of versions.data.data) { this.versions[version._name] = parseInt(version._value) * 1000 } diff --git a/qryn_bun.mjs b/qryn_bun.mjs index 6153f796..098ba091 100644 --- a/qryn_bun.mjs +++ b/qryn_bun.mjs @@ -71,8 +71,8 @@ const http_user = process.env.QRYN_LOGIN || process.env.CLOKI_LOGIN || undefined const http_password = process.env.QRYN_PASSWORD || process.env.CLOKI_PASSWORD || undefined export default async() => { + await init(process.env.CLICKHOUSE_DB || 'cloki') if (!readonly) { - await init(process.env.CLICKHOUSE_DB || 'cloki') await startAlerting() } await DATABASE.checkDB() diff --git a/qryn_node.js b/qryn_node.js index 57057fa2..3f80063f 100755 --- a/qryn_node.js +++ b/qryn_node.js @@ -71,8 +71,8 @@ let fastify = require('fastify')({ }); (async () => { try { + await init(process.env.CLICKHOUSE_DB || 'cloki') if (!this.readonly) { - await init(process.env.CLICKHOUSE_DB || 'cloki') await startAlerting() } await DATABASE.checkDB()