Add /api/stats/database/summary documentation

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-01-03 20:17:34 +01:00
parent 9c19abb86d
commit 5019cc7ac7
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
3 changed files with 80 additions and 30 deletions

View File

@ -54,6 +54,9 @@ paths:
/stats/summary:
$ref: 'stats.yaml#/components/paths/summary'
/stats/database/summary:
$ref: 'stats.yaml#/components/paths/database_summary'
/stats/upstreams:
$ref: 'stats.yaml#/components/paths/upstreams'

View File

@ -23,6 +23,31 @@ components:
schema:
$ref: 'common.yaml#/components/errors/unauthorized'
database_summary:
get:
summary: Get database content details
tags:
- Metrics
operationId: "get_metrics_database_summary"
description: |
Request various database content details
parameters:
- $ref: 'common.yaml#/components/parameters/database/from'
- $ref: 'common.yaml#/components/parameters/database/until'
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: 'stats.yaml#/components/schemas/database_summary'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: 'common.yaml#/components/errors/unauthorized'
upstreams:
get:
summary: Get metrics about Pi-hole's upstream destinations
@ -462,68 +487,87 @@ components:
properties:
A:
type: integer
description: Number of queries of type A
description: Type A queries
example: 18268
AAAA:
type: integer
description: Number of queries of type AAAA
description: Type AAAA queries
example: 2332
ANY:
type: integer
description: Number of queries of type ANY
description: Type ANY queries
example: 0
SRV:
type: integer
description: Number of queries of type SRV
description: Type SRV queries
example: 6
SOA:
type: integer
description: Number of queries of type SOA
description: Type SOA queries
example: 44
PTR:
type: integer
description: Number of queries of type PTR
description: Type PTR queries
example: 389
TXT:
type: integer
description: Number of queries of type TXT
description: Type TXT queries
example: 0
NAPTR:
type: integer
description: Number of queries of type NAPTR
description: Type NAPTR queries
example: 1
MX:
type: integer
description: Number of queries of type MX
description: Type MX queries
example: 109
DS:
type: integer
description: Number of queries of type DS
description: Type DS queries
example: 596
RRSIG:
type: integer
description: Number of queries of type RRSIG
description: Type RRSIG queries
example: 25
DNSKEY:
type: integer
description: Number of queries of type DNSKEY
description: Type DNSKEY queries
example: 240
NS:
type: integer
description: Number of queries of type NS
description: Type NS queries
example: 18
SVCB:
type: integer
description: Number of queries of type SVCB
description: Type SVCB queries
example: 0
HTTPS:
type: integer
description: Number of queries of type HTTPS
description: Type HTTPS queries
example: 11
OTHER:
type: integer
description: Number of queries of type OTHER
description: Type OTHER queries
example: 0
database_summary:
type: object
properties:
sum_queries:
type: integer
description: Total number of queries
example: 29160
sum_blocked:
type: integer
description: Total number of blocked queries
example: 6379
percent_blocked:
type: number
description: Percentage of blocked queries
example: 21.9
total_clients:
type: integer
description: Total number of clients
example: 10
parameters:
recent_blocked:

View File

@ -19,6 +19,10 @@
// db
#include "../database/common.h"
// SQL Query type filters for the database
#define FILTER_STATUS_NOT_BLOCKED "status IN (0,2,3,12,13,14,17)"
#define FILTER_STATUS_BLOCKED "status NOT IN (0,2,3,12,13,14,17)"
int api_history_database(struct ftl_conn *api)
{
// Verify requesting client is allowed to see this ressource
@ -231,7 +235,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
querystr = "SELECT COUNT(*),d.domain AS cnt FROM query_storage q "
"JOIN domain_by_id d ON d.id = q.domain "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status NOT IN (0,2,3,12,13,14,17) "
"AND " FILTER_STATUS_BLOCKED " "
"GROUP by q.domain";
}
else
@ -240,7 +244,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
querystr = "SELECT COUNT(*),d.domain AS cnt FROM query_storage q "
"JOIN domain_by_id d ON d.id = q.domain "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status IN (0,2,3,12,13,14,17) "
"AND " FILTER_STATUS_NOT_BLOCKED " "
"GROUP by q.domain";
}
@ -251,7 +255,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
// Count total number of blocked queries for domains
count_blocked_str = "SELECT COUNT(DISTINCT domain) FROM query_storage "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status NOT IN (0,2,3,12,13,14,17)";
"AND " FILTER_STATUS_BLOCKED;
}
else
{
@ -261,7 +265,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
querystr = "SELECT COUNT(*),c.ip,c.name AS cnt FROM query_storage q "
"JOIN client_by_id c ON c.id = q.client"
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status NOT IN (0,2,3,12,13,14,17) "
"AND " FILTER_STATUS_BLOCKED " "
"GROUP by q.client";
}
else
@ -270,7 +274,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
querystr = "SELECT COUNT(*),c.ip,c.name AS cnt FROM query_storage q "
"JOIN client_by_id c ON c.id = q.client "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status IN (0,2,3,12,13,14,17) "
"AND " FILTER_STATUS_NOT_BLOCKED " "
"GROUP by q.client";
}
@ -281,7 +285,7 @@ int api_stats_database_top_items(struct ftl_conn *api)
// Count number of blocked queries for clients
count_blocked_str = "SELECT COUNT(DISTINCT client) FROM query_storage "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status NOT IN (0,2,3,12,13,14,17)";
"AND " FILTER_STATUS_BLOCKED;
}
@ -402,20 +406,20 @@ int api_stats_database_summary(struct ftl_conn *api)
const char *querystr;
querystr = "SELECT COUNT(*) FROM queries "
"WHERE timestamp >= :from AND timestamp <= :until";
int sum_queries = db_query_int_from_until(db, querystr, from, until);
const int total_queries = db_query_int_from_until(db, querystr, from, until);
querystr = "SELECT COUNT(*) FROM queries "
"WHERE timestamp >= :from AND timestamp <= :until "
"AND status != 0 AND status != 2 AND status != 3";
int blocked_queries = db_query_int_from_until(db, querystr, from, until);
"AND " FILTER_STATUS_BLOCKED;
const int blocked_queries = db_query_int_from_until(db, querystr, from, until);
querystr = "SELECT COUNT(DISTINCT client) FROM queries "
"WHERE timestamp >= :from AND timestamp <= :until";
int total_clients = db_query_int_from_until(db, querystr, from, until);
const int total_clients = db_query_int_from_until(db, querystr, from, until);
float percent_blocked = 1e2f*blocked_queries/sum_queries;
const float percent_blocked = 1e2f*blocked_queries/total_queries;
if(sum_queries < 0 || blocked_queries < 0 || total_clients < 0)
if(total_queries < 0 || blocked_queries < 0 || total_clients < 0)
{
// Close (= unlock) database connection
@ -429,8 +433,7 @@ int api_stats_database_summary(struct ftl_conn *api)
// Loop over and accumulate results
cJSON *json = JSON_NEW_OBJECT();
JSON_REF_STR_IN_OBJECT(json, "gravity_size", "?");
JSON_ADD_NUMBER_TO_OBJECT(json, "sum_queries", sum_queries);
JSON_ADD_NUMBER_TO_OBJECT(json, "total_queries", total_queries);
JSON_ADD_NUMBER_TO_OBJECT(json, "blocked_queries", blocked_queries);
JSON_ADD_NUMBER_TO_OBJECT(json, "percent_blocked", percent_blocked);
JSON_ADD_NUMBER_TO_OBJECT(json, "total_clients", total_clients);