@@ -3,12 +3,12 @@ const common = require('./common.js'),
33 plugins = require ( '../../plugins/pluginManager.js' ) ,
44 manager = { } ;
55
6- let clickHouseRunner ;
6+ let chHealth = null ;
77try {
8- clickHouseRunner = require ( '../../plugins/clickhouse/api/queries/clickhouseCoreQueries .js' ) ;
8+ chHealth = require ( '../../plugins/clickhouse/api/health .js' ) ;
99}
10- catch ( e ) {
11- clickHouseRunner = null ;
10+ catch {
11+ //
1212}
1313
1414( function ( ) {
@@ -20,6 +20,16 @@ catch (e) {
2020 DELETED : "deleted"
2121 } ;
2222
23+ /**
24+ * Check if ClickHouse is enabled
25+ * @returns {boolean } true if ClickHouse is enabled, false otherwise
26+ */
27+ manager . isClickhouseEnabled = function ( ) {
28+ return ! ! ( common . queryRunner
29+ && typeof common . queryRunner . isAdapterAvailable === 'function'
30+ && common . queryRunner . isAdapterAvailable ( 'clickhouse' ) ) ;
31+ } ;
32+
2333 plugins . register ( "/master" , function ( ) {
2434 common . db . collection ( 'deletion_manager' ) . ensureIndex ( { "deletion_completion_ts" : 1 } , { expireAfterSeconds : 3 * 24 * 60 * 60 } , function ( ) { } ) ;
2535 } ) ;
@@ -50,23 +60,27 @@ catch (e) {
5060 plugins . register ( '/system/observability/collect' , async function ( ) {
5161 try {
5262 const summary = await getQueueSummary ( ) ;
53- const mutations = await getPendingMutationsFromCH ( ) ;
63+
64+ const metrics = {
65+ summary : {
66+ queued : summary . queued ,
67+ running : summary . running ,
68+ awaiting_validation : summary . awaiting_validation ,
69+ failed : summary . failed ,
70+ deleted : summary . deleted ,
71+ oldest_wait_sec : summary . oldest_wait_sec
72+ }
73+ } ;
74+
75+ if ( manager . isClickhouseEnabled ( ) ) {
76+ metrics . mutations = await getPendingMutationsFromCH ( ) ;
77+ }
5478
5579 return {
5680 provider : 'deletion' ,
5781 healthy : summary . failed === 0 ,
5882 issues : [ ] ,
59- metrics : {
60- summary : {
61- queued : summary . queued ,
62- running : summary . running ,
63- awaiting_validation : summary . awaiting_validation ,
64- failed : summary . failed ,
65- deleted : summary . deleted ,
66- oldest_wait_sec : summary . oldest_wait_sec
67- } ,
68- mutations
69- } ,
83+ metrics,
7084 date : new Date ( ) . toISOString ( )
7185 } ;
7286 }
@@ -134,41 +148,22 @@ catch (e) {
134148 * @returns {Promise<Object> } - Operational snapshot
135149 */
136150 async function getPendingMutationsFromCH ( ) {
137- if ( ! clickHouseRunner ) {
138- return { pending : 0 , details : [ ] , error : 'clickHouseRunner_not_available' } ;
139- }
140- const res = await clickHouseRunner . listPendingMutations ( { database : 'countly_drill' , table : 'drill_events' } ) ;
141- const rows = res && res . data ? res . data : [ ] ;
142- return {
143- pending : rows . length ,
144- details : rows . map ( r => ( {
145- command : r . command + '' ,
146- is_killed : r . is_killed === 1 || r . is_killed === '1' ,
147- latest_fail_reason : r . latest_fail_reason || null
148- } ) )
149- } ;
150- }
151-
152- manager . getDeletionClickhousePressureLimits = async function ( ) {
153151 try {
154- const doc = await common . db . collection ( 'plugins' ) . findOne (
155- { _id : 'plugins' } ,
156- { projection : { 'deletion_manager.ch_max_parts_per_partition' : 1 , 'deletion_manager.ch_max_total_mergetree_parts' : 1 } }
157- ) ;
158- const dmCfg = ( doc && doc . deletion_manager ) ? doc . deletion_manager : { } ;
152+ const rows = await chHealth . listPendingMutations ( { database : 'countly_drill' , table : 'drill_events' } ) ;
153+ const list = Array . isArray ( rows ) ? rows : [ ] ;
159154 return {
160- CH_MAX_PARTS_PER_PARTITION : Number ( dmCfg . ch_max_parts_per_partition ) || 1000 ,
161- CH_MAX_TOTAL_MERGETREE_PARTS : Number ( dmCfg . ch_max_total_mergetree_parts ) || 100000
155+ pending : list . length ,
156+ details : list . map ( r => ( {
157+ command : r . command + '' ,
158+ is_killed : r . is_killed === 1 || r . is_killed === '1' ,
159+ latest_fail_reason : r . latest_fail_reason || null
160+ } ) )
162161 } ;
163162 }
164163 catch ( e ) {
165- log . e ( 'Failed to load deletion manager thresholds from DB, using defaults' , e && e . message ? e . message : e ) ;
166- return {
167- CH_MAX_PARTS_PER_PARTITION : 1000 ,
168- CH_MAX_TOTAL_MERGETREE_PARTS : 100000
169- } ;
164+ return { pending : 0 , details : [ ] , error : 'clickhouse_health_unavailable' } ;
170165 }
171- } ;
166+ }
172167} ) ( manager ) ;
173168
174- module . exports = manager ;
169+ module . exports = manager ;
0 commit comments