Add /history/database/clients documentation

Signed-off-by: DL6ER <dl6er@dl6er.de>
This commit is contained in:
DL6ER 2023-01-02 21:37:17 +01:00
parent 225187447f
commit ffacb1ad9e
No known key found for this signature in database
GPG Key ID: 00135ACBD90B28DD
6 changed files with 43 additions and 21 deletions

View File

@ -28,6 +28,8 @@ components:
tags:
- Metrics
operationId: "get_activity_metrics_database"
description: |
Request long-term data needed to generate the activity graph
parameters:
- $ref: 'common.yaml#/components/parameters/database/from'
- $ref: 'common.yaml#/components/parameters/database/until'
@ -72,6 +74,8 @@ components:
tags:
- Metrics
operationId: "get_client_metrics_database"
description: |
Request long-term data needed to generate the client activity graph
parameters:
- $ref: 'common.yaml#/components/parameters/database/from'
- $ref: 'common.yaml#/components/parameters/database/until'

View File

@ -69,6 +69,9 @@ paths:
/history/database:
$ref: 'history.yaml#/components/paths/database_history'
/history/database/clients:
$ref: 'history.yaml#/components/paths/database_clients'
/queries:
$ref: 'queries.yaml#/components/paths/queries'

View File

@ -188,11 +188,11 @@ components:
description: Interface this device is connected to
example: enp2s0
firstSeen:
type: interger
type: integer
description: Unix timestamp when this device was first seen by your Pi-hole
example: 1664623620
lastQuery:
type: interger
type: integer
description: Unix timestamp when your Pi-hole received the last query from this device
example: 1664688620
numQueries:
@ -217,9 +217,11 @@ components:
description: Associated hostname (can be null)
example: ubuntu-server
lastSeen:
type: integer
description: Unix timestamp when your Pi-hole has seen this address the last time
example: 1664688620
nameUpdated:
type: integer
description: Unix timestamp when device updated its hostname the last time
example: 1664688620

View File

@ -258,7 +258,7 @@ components:
description: Array of suggested domains
items:
type: string
example: []
example: ["pi-hole.net","amazon.com"]
client:
type: array
description: Array of suggested clients

View File

@ -29,6 +29,8 @@ components:
tags:
- Metrics
operationId: "get_metrics_upstreams"
description: |
Request upstream metrics
responses:
'200':
description: OK
@ -45,10 +47,12 @@ components:
database_upstreams:
get:
summary: Get metrics about Pi-hole's upstream destinations (database)
summary: Get metrics about Pi-hole's upstream destinations (long-term database)
tags:
- Metrics
operationId: "get_metrics_upstreams_database"
description: |
Request upstream metrics (long-term database)
parameters:
- $ref: 'common.yaml#/components/parameters/database/from'
- $ref: 'common.yaml#/components/parameters/database/until'
@ -220,11 +224,11 @@ components:
type: object
properties:
response:
type: string
type: number
description: Average response time of this upstream destination in seconds (0 if not applicable)
example: 0.0254856
variance:
type: string
type: number
description: Standard deviation of the average response time (0 if not applicable)
example: 0.02058
forwarded_queries:

View File

@ -34,11 +34,11 @@ int api_history_database(struct ftl_conn *api)
}
// Check if we received the required information
if(until < 1.0)
if(from < 1.0 || until < 1.0)
{
return send_json_error(api, 400,
"bad_request",
"You need to specify \"until\" in the request.",
"You need to specify both \"from\" and \"until\" in the request.",
NULL);
}
@ -138,7 +138,7 @@ int api_history_database(struct ftl_conn *api)
}
item = JSON_NEW_OBJECT();
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", timeslot);
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", previous_timeslot);
}
const int status = sqlite3_column_int(stmt, 1);
@ -456,7 +456,8 @@ int api_history_database_clients(struct ftl_conn *api)
"Failed to open long-term database",
NULL);
const char *querystr = "SELECT DISTINCT client FROM queries "
const char *querystr = "SELECT DISTINCT(client),ip,name FROM query_storage "
"JOIN client_by_id ON client_by_id.id = client "
"WHERE timestamp >= :from AND timestamp <= :until "
"ORDER BY client DESC";
@ -508,10 +509,9 @@ int api_history_database_clients(struct ftl_conn *api)
unsigned int num_clients = 0;
while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
const char* client = (char*)sqlite3_column_text(stmt, 0);
cJSON *item = JSON_NEW_OBJECT();
JSON_COPY_STR_TO_OBJECT(item, "ip", client);
JSON_REF_STR_IN_OBJECT(item, "name", "");
JSON_COPY_STR_TO_OBJECT(item, "ip", sqlite3_column_text(stmt, 1));
JSON_COPY_STR_TO_OBJECT(item, "name", sqlite3_column_text(stmt, 2));
JSON_ADD_ITEM_TO_ARRAY(clients, item);
num_clients++;
}
@ -579,17 +579,19 @@ int api_history_database_clients(struct ftl_conn *api)
NULL);
}
cJSON *over_time = JSON_NEW_ARRAY();
cJSON *item = NULL;
cJSON *data = NULL;
int previous_timestamp = 0;
unsigned int previous_timeslot = 0u;
cJSON *over_time = JSON_NEW_ARRAY();
while((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
const int timestamp = sqlite3_column_int(stmt, 0);
// Begin new array item for each new timestamp
if(timestamp != previous_timestamp)
// Get timestamp and derive timeslot from it
const unsigned int timestamp = sqlite3_column_int(stmt, 0);
const unsigned int timeslot = timestamp - timestamp % interval;
// Begin new array item for each new timeslot
if(timeslot != previous_timeslot)
{
previous_timestamp = timestamp;
previous_timeslot = timeslot;
if(item != NULL && data != NULL)
{
JSON_ADD_ITEM_TO_OBJECT(item, "data", data);
@ -606,7 +608,7 @@ int api_history_database_clients(struct ftl_conn *api)
{
JSON_ADD_NUMBER_TO_ARRAY(data, 0);
}
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", timestamp);
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", previous_timeslot);
}
const char *client = (char*)sqlite3_column_text(stmt, 1);
@ -634,12 +636,19 @@ int api_history_database_clients(struct ftl_conn *api)
JSON_REPLACE_NUMBER_IN_ARRAY(data, idx, count);
}
// Append final timeslot at the end if applicable
if(item != NULL && data != NULL)
{
JSON_ADD_ITEM_TO_OBJECT(item, "data", data);
JSON_ADD_ITEM_TO_ARRAY(over_time, item);
}
// Finalize statement and close (= unlock) database connection
sqlite3_finalize(stmt);
dbclose(&db);
cJSON *json = JSON_NEW_OBJECT();
JSON_ADD_ITEM_TO_OBJECT(json, "over_time", over_time);
JSON_ADD_ITEM_TO_OBJECT(json, "history", over_time);
JSON_ADD_ITEM_TO_OBJECT(json, "clients", clients);
JSON_SEND_OBJECT(json);
}