Skip to content

Commit

Permalink
Merge pull request #2275 from pi-hole/fix/maxHistory
Browse files Browse the repository at this point in the history
Fix webserver.api.maxHistory usage in API
  • Loading branch information
DL6ER authored Feb 24, 2025
2 parents 0c91c17 + 4e5ed22 commit acec7a8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/FTL.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
#define MAXITER 1000

// How many hours do we want to store in FTL's memory? [hours]
#define MAXLOGAGE 24
#define MAXLOGAGE 24u

// Interval for overTime data [seconds]
// Default: 600 (10 minutes)
#define OVERTIME_INTERVAL 600
#define OVERTIME_INTERVAL 600u

// How many overTime slots do we need?
// This is the maximum log age divided by the overtime interval
Expand Down
10 changes: 7 additions & 3 deletions src/api/history.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
#include "overTime.h"
// config struct
#include "config/config.h"
// get_max_overtime_slot()
#include "gc.h"

int api_history(struct ftl_conn *api)
{
lock_shm();

// Loop over all overTime slots and add them to the array
cJSON *history = JSON_NEW_ARRAY();
for(unsigned int slot = 0; slot < OVERTIME_SLOTS; slot++)
const unsigned int max_slot = get_max_overtime_slot();
// Loop over all overTime slots and add them to the array
for(unsigned int slot = 0; slot <= max_slot; slot++)
{
cJSON *item = JSON_NEW_OBJECT();
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", overTime[slot].timestamp);
Expand Down Expand Up @@ -148,7 +151,8 @@ int api_history_clients(struct ftl_conn *api)
int others_total = 0;

cJSON *history = JSON_NEW_ARRAY();
for(unsigned int slot = 0; slot < OVERTIME_SLOTS; slot++)
const unsigned int max_slot = get_max_overtime_slot();
for(unsigned int slot = 0; slot <= max_slot; slot++)
{
cJSON *item = JSON_NEW_OBJECT();
JSON_ADD_NUMBER_TO_OBJECT(item, "timestamp", overTime[slot].timestamp);
Expand Down
2 changes: 1 addition & 1 deletion src/database/aliasclients.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static void recompute_aliasclient(const int aliasclientID)
// Add counts of this client to the alias-client
aliasclient->count += client->count;
aliasclient->blockedcount += client->blockedcount;
for(int idx = 0; idx < OVERTIME_SLOTS; idx++)
for(unsigned int idx = 0; idx < OVERTIME_SLOTS; idx++)
aliasclient->overTime[idx] += client->overTime[idx];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/datastructure.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ void change_clientcount(clientsData *client, int total, int blocked, int overTim
{
client->count += total;
client->blockedcount += blocked;
if(overTimeIdx > -1 && overTimeIdx < OVERTIME_SLOTS)
if(overTimeIdx > -1 && (unsigned int)overTimeIdx < OVERTIME_SLOTS)
{
overTime[overTimeIdx].total += overTimeMod;
client->overTime[overTimeIdx] += overTimeMod;
Expand All @@ -432,7 +432,7 @@ void change_clientcount(clientsData *client, int total, int blocked, int overTim
clientsData *aliasclient = getClient(client->aliasclient_id, true);
aliasclient->count += total;
aliasclient->blockedcount += blocked;
if(overTimeIdx > -1 && overTimeIdx < OVERTIME_SLOTS)
if(overTimeIdx > -1 && (unsigned int)overTimeIdx < OVERTIME_SLOTS)
aliasclient->overTime[overTimeIdx] += overTimeMod;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,22 @@ static void recycle(void)
}
}

/**
* @brief Computes the maximum overtime slot to send.
*
* This function calculates the maximum overtime slot based on the
* configuration settings for the webserver API's maximum history value.
* The calculation involves converting the maximum history value from
* hours to seconds, dividing by the overtime interval, and adjusting
* the result to get the correct slot number.
*
* @return The maximum overtime slot as an unsigned integer.
*/
unsigned int __attribute__((pure)) get_max_overtime_slot(void)
{
return min(config.webserver.api.maxHistory.v.ui, MAXLOGAGE * 3600) / OVERTIME_INTERVAL;
}

// Subtract rate-limitation count from individual client counters
// As long as client->rate_limit is still larger than the allowed
// maximum count, the rate-limitation will just continue
Expand Down
1 change: 1 addition & 0 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

void *GC_thread(void *val);
void runGC(const time_t now, time_t *lastGCrun, const bool flush);
unsigned int get_max_overtime_slot(void) __attribute__((pure));
int get_rate_limit_turnaround(const unsigned int rate_limit_count);
unsigned int set_gc_interval(void);

Expand Down
8 changes: 4 additions & 4 deletions src/overTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ void initOverTime(void)
localtime_r(&newest, &tm_n);
strftime(first, 20, "%Y-%m-%d %H:%M:%S", &tm_o);
strftime(last, 20, "%Y-%m-%d %H:%M:%S", &tm_n);
log_debug(DEBUG_OVERTIME, "initOverTime(): Initializing %i slots from %s (%lu) to %s (%lu)",
log_debug(DEBUG_OVERTIME, "initOverTime(): Initializing %u slots from %s (%lu) to %s (%lu)",
OVERTIME_SLOTS, first, (unsigned long)oldest, last, (unsigned long)newest);
}

// Iterate over overTime
for(int i = 0; i < OVERTIME_SLOTS; i++)
for(unsigned int i = 0; i < OVERTIME_SLOTS; i++)
{
time_t this_slot_ts = oldest + OVERTIME_INTERVAL * i;
// Initialize overTime slot
Expand All @@ -117,14 +117,14 @@ unsigned int _getOverTimeID(time_t timestamp, const char *file, const int line)
// Return first timestamp in case negative timestamp was determined
return 0;
}
else if(id == OVERTIME_SLOTS)
else if((unsigned int)id == OVERTIME_SLOTS)
{
// Possible race-collision (moving of the timeslots is just about to
// happen), silently add to the last bin because this is the correct
// thing to do
return OVERTIME_SLOTS-1;
}
else if(id > OVERTIME_SLOTS)
else if((unsigned int)id > OVERTIME_SLOTS)
{
// This is definitely wrong. We warn about this (but only once)
if(!warned_about_hwclock)
Expand Down

0 comments on commit acec7a8

Please sign in to comment.