Ajout des fonctions factorisées et complexitée réduite

This commit is contained in:
Silvio
2025-09-18 20:39:13 +00:00
parent 21ce5c8d88
commit 2729bab439

197
stats_ameliorer.c Normal file
View File

@@ -0,0 +1,197 @@
/* Version améliorée de stats.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <regex.h>
#include "../include/utils.h"
#include "../include/stats.h"
#include "../include/db-sqlite.h"
// Structure pour les statistiques
typedef struct {
double mean, max, min;
int nb_high, nb_loss, nb_ping;
} PingStats;
// Ouverture du fichier log (complexité : 1)
static FILE* open_ping_log_file() {
return fopen("/var/log/ping-report/last-ping.log", "r");
}
// Compilation de la regex (complexité : 3)
static regex_t* compile_ping_regex() {
regex_t* p_reg = malloc(sizeof(*p_reg));
if (p_reg == NULL) return NULL;
if (regcomp(p_reg, "time=(.*) ms", REG_EXTENDED) != 0) {
free(p_reg);
return NULL;
}
return p_reg;
}
// Extraction de la valeur ping (complexité : 2)
static char* extract_ping_value(char* line, regmatch_t* pmatch) {
int start = pmatch[1].rm_so;
int end = pmatch[1].rm_eo;
size_t size_ping = end - start;
char* ping = malloc(size_ping + 2);
if (ping == NULL) return NULL;
strncpy(ping, &line[start], size_ping);
ping[size_ping] = '\n';
ping[size_ping + 1] = '\0';
return ping;
}
// Extraction du ping depuis le fichier (complexité : 3)
static char* extract_ping_from_file(FILE* fd, regex_t* regex) {
char* read_line = NULL;
size_t n = 0;
regmatch_t pmatch[2];
while (getline(&read_line, &n, fd) != -1) {
if (read_line == NULL) break;
if (regexec(regex, read_line, 2, pmatch, 0) == 0) {
char* ping = extract_ping_value(read_line, pmatch);
free(read_line);
return ping;
}
free(read_line);
read_line = NULL;
n = 0;
}
if (read_line != NULL) free(read_line);
return NULL;
}
// Nettoyage des ressources regex (complexité : 2)
static void cleanup_regex_resources(regex_t* regex) {
if (regex != NULL) {
regfree(regex);
free(regex);
}
}
// Complexité : 3 (au lieu de 14)
char* get_ping_from_temp_log() {
FILE* fd = open_ping_log_file();
if (fd == NULL) return NULL;
regex_t* regex = compile_ping_regex();
if (regex == NULL) {
fclose(fd);
return NULL;
}
char* ping = extract_ping_from_file(fd, regex);
cleanup_regex_resources(regex);
fclose(fd);
return ping;
}
// Mise à jour des statistiques (complexité : 4)
static void update_ping_statistics(double ping, PingStats* stats, double* sum) {
stats->nb_ping++;
if (ping > stats->max) stats->max = ping;
if (ping < stats->min) stats->min = ping;
if (ping > 100.0) stats->nb_high++;
*sum += ping;
}
// Traitement d'une ligne de ping (complexité : 3)
static void process_ping_line(char* line, PingStats* stats, double* sum) {
if (strcmp(line, "LOSS") == 0) {
stats->nb_loss++;
} else {
double ping = strtod(line, NULL);
if (ping >= 0.1) {
update_ping_statistics(ping, stats, sum);
}
}
}
// Calcul des statistiques (complexité : 2)
static PingStats calculate_ping_statistics(FILE* fd) {
PingStats stats = {0.0, 0.0, 100.0, 0, 0, 0};
double sum = 0.0;
char* read_line = NULL;
size_t n = 0;
while (getline(&read_line, &n, fd) != -1) {
if (read_line == NULL) break;
process_ping_line(read_line, &stats, &sum);
free(read_line);
read_line = NULL;
n = 0;
}
if (read_line != NULL) free(read_line);
if (stats.nb_ping > 0) {
stats.mean = sum / stats.nb_ping;
}
return stats;
}
// Complexité : 3 (au lieu de 12)
void set_stats_ping() {
FILE* fd = fopen("/var/log/ping-report/all-ping.log", "r");
if (fd == NULL) {
perror("stats : ");
return;
}
PingStats stats = calculate_ping_statistics(fd);
fclose(fd);
if (stats.nb_ping > 0) {
insert_hourly_report(stats.mean, stats.max, stats.min,
stats.nb_high, stats.nb_loss, stats.nb_ping);
}
}
// Complexité : 4 (déjà acceptable)
void write_ping_log(char* new_ping) {
FILE* fd = fopen("/var/log/ping-report/all-ping.log", "a+");
if (fd != NULL) {
if (new_ping == NULL) {
new_ping = malloc(5 * sizeof(char));
if (new_ping == NULL) {
fclose(fd);
return;
}
snprintf(new_ping, 5, "LOSS");
}
fwrite(new_ping, sizeof(char), strlen(new_ping), fd);
fclose(fd);
} else {
perror("write ping : ");
}
free(new_ping);
}