MCPcopy
hub / github.com/directus/directus / testDatabase

Method testDatabase

api/src/services/server.ts:264–323  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

262 return this.accountability?.admin === true ? data : { status: data.status };
263
264 async function testDatabase(): Promise<Record<string, ServerHealthCheck[]>> {
265 if (enabledServices.includes('database') === false) {
266 return {};
267 }
268
269 const database = getDatabase();
270 const client = env['DB_CLIENT'];
271
272 const checks: Record<string, ServerHealthCheck[]> = {};
273
274 // Response time
275 // ----------------------------------------------------------------------------------------
276 checks[`${client}:responseTime`] = [
277 {
278 status: 'ok',
279 componentType: 'datastore',
280 observedUnit: 'ms',
281 observedValue: 0,
282 threshold: env['DB_HEALTHCHECK_THRESHOLD'] ? +env['DB_HEALTHCHECK_THRESHOLD'] : 150,
283 },
284 ];
285
286 const startTime = performance.now();
287
288 if (await hasDatabaseConnection()) {
289 checks[`${client}:responseTime`]![0]!.status = 'ok';
290 } else {
291 checks[`${client}:responseTime`]![0]!.status = 'error';
292 checks[`${client}:responseTime`]![0]!.output = `Can't connect to the database.`;
293 }
294
295 const endTime = performance.now();
296 checks[`${client}:responseTime`]![0]!.observedValue = +(endTime - startTime).toFixed(3);
297
298 if (
299 Number(checks[`${client}:responseTime`]![0]!.observedValue!) >
300 checks[`${client}:responseTime`]![0]!.threshold! &&
301 checks[`${client}:responseTime`]![0]!.status !== 'error'
302 ) {
303 checks[`${client}:responseTime`]![0]!.status = 'warn';
304 }
305
306 checks[`${client}:connectionsAvailable`] = [
307 {
308 status: 'ok',
309 componentType: 'datastore',
310 observedValue: database.client.pool.numFree(),
311 },
312 ];
313
314 checks[`${client}:connectionsUsed`] = [
315 {
316 status: 'ok',
317 componentType: 'datastore',
318 observedValue: database.client.pool.numUsed(),
319 },
320 ];
321

Callers

nothing calls this directly

Calls 2

getDatabaseFunction · 0.85
hasDatabaseConnectionFunction · 0.85

Tested by

no test coverage detected