()
| 144 | } |
| 145 | |
| 146 | async function getClickhouseStats(): Promise<JSONObject> { |
| 147 | const result: JSONObject = { |
| 148 | connected: false, |
| 149 | dataSizeInBytes: null, |
| 150 | diskFreeInBytes: null, |
| 151 | diskTotalInBytes: null, |
| 152 | topTables: [], |
| 153 | }; |
| 154 | |
| 155 | try { |
| 156 | const client: ReturnType<typeof ClickhouseAppInstance.getDataSource> = |
| 157 | ClickhouseAppInstance.getDataSource(); |
| 158 | |
| 159 | if (!client) { |
| 160 | return result; |
| 161 | } |
| 162 | |
| 163 | // Total size of active parts on disk. |
| 164 | const sizeResult: ClickhouseJsonResult = (await ( |
| 165 | await client.query({ |
| 166 | query: |
| 167 | "SELECT sum(bytes_on_disk) AS bytes FROM system.parts WHERE active", |
| 168 | format: "JSON", |
| 169 | }) |
| 170 | ).json()) as ClickhouseJsonResult; |
| 171 | |
| 172 | result["connected"] = true; |
| 173 | result["dataSizeInBytes"] = toNumberOrNull(sizeResult.data?.[0]?.["bytes"]); |
| 174 | |
| 175 | // Underlying volume free/total capacity. |
| 176 | const diskResult: ClickhouseJsonResult = (await ( |
| 177 | await client.query({ |
| 178 | query: |
| 179 | "SELECT sum(free_space) AS free, sum(total_space) AS total FROM system.disks", |
| 180 | format: "JSON", |
| 181 | }) |
| 182 | ).json()) as ClickhouseJsonResult; |
| 183 | |
| 184 | result["diskFreeInBytes"] = toNumberOrNull(diskResult.data?.[0]?.["free"]); |
| 185 | result["diskTotalInBytes"] = toNumberOrNull( |
| 186 | diskResult.data?.[0]?.["total"], |
| 187 | ); |
| 188 | |
| 189 | // Largest tables (usually the telemetry tables) so operators can see what consumes space. |
| 190 | const tablesResult: ClickhouseJsonResult = (await ( |
| 191 | await client.query({ |
| 192 | query: |
| 193 | "SELECT table AS name, sum(bytes_on_disk) AS bytes FROM system.parts WHERE active GROUP BY table ORDER BY bytes DESC LIMIT 8", |
| 194 | format: "JSON", |
| 195 | }) |
| 196 | ).json()) as ClickhouseJsonResult; |
| 197 | |
| 198 | result["topTables"] = (tablesResult.data || []).map( |
| 199 | (row: JSONObject): JSONObject => { |
| 200 | return { |
| 201 | name: String(row["name"]), |
| 202 | sizeInBytes: toNumberOrNull(row["bytes"]), |
| 203 | }; |
no test coverage detected