Compare commits

6 Commits
main ... main

Author SHA1 Message Date
b1512ef6e2 Add: Two pdf file explains the two functions 2025-09-19 19:54:42 +02:00
37ddcf20bc Fix: Add - 2025-09-18 13:25:33 +02:00
acee97b42d Add: Add file 2025-09-18 13:21:11 +02:00
3ee38258b6 Add: Add file 2025-09-18 13:19:43 +02:00
db5f6bd795 Add: Function regrex 2025-09-17 09:36:48 +02:00
40e269449c Add: New File 2025-09-15 17:41:11 +02:00
4 changed files with 233 additions and 34 deletions

74
Compte-rendu.md Normal file
View File

@@ -0,0 +1,74 @@
## Calculer la complexité
## damemon.c
- Fonction create_daemon: complixité cyclomatique de 4
- Fonction ping_request: complixité cyclomatique de 1
- Fonction send_check: complixité cyclomatique de 3
- Fonction check_keep_working: complixité cyclomatique de 4
- Fonction daemon_work: complixité cyclomatique de 3
## db-sqlite.c
- Fonction db_connect : complixité cyclonique de 1
- db_disconnect : 1
- insert_hourly_report: 1
## ping-report.c
- main : complixité cyclonique de 4
## stats.c
- get_ping_from_temp_log : complixité cyclonique de 15
- write_ping_log: complixité cyclonique de 4
- set_stats_ping : complixité cyclonique de 13
## utils.c
- write_pid_file : complixité cyclonique de 2
- remove_file : complixité cyclonique de 1
# Shéma des deux fonctions
## Get_ping_from_temp_log
[Get_ping_from_temp_log (PDF)](pdf/Diagramme_get_ping_from_temp_log.pdf)
## Set_stats_ping
[Set_stats_ping (PDF)](pdf/Diagramme_set_stats_ping.pdf)
## Objectif
- Découper les fonctions en unités simples et réutilisables.
- Réduire limbrication des conditions pour rendre le code plus clair.
- Sécuriser les calculs (éviter la division par zéro).
- Faciliter la maintenance et la lecture.
---
## 🔹 Découpage de `get_ping_from_temp_log`
### 1. `extract_ping_from_line`
- Rôle : soccuper uniquement de lextraction via regex.
- Prend une ligne en entrée.
- Retourne la valeur du ping si trouvée, sinon erreur.
### 2. `read_ping_from_file`
- Rôle : lecture du fichier et appel à `extract_ping_from_line`.
- Parcourt toutes les lignes.
- Retourne le premier ping trouvé ou `NULL`.
### 3. `get_ping_from_temp_log`
- Alias avec le chemin en dur :
```c
return read_ping_from_file("/var/log/ping-report/last-ping.log");
## 🔹 Inversion des conditions: `set_stats_ping`
- Plutot que d'imbriquer des if ... else , on fait if(...){ continue; } quand on veut ignorer une ligne
- Moins de niveaux de blocs if dans un if
- Sécurisation : si nb_ping == 0, la moyenne reste 0.0 => evite une division par zéro

Binary file not shown.

Binary file not shown.

View File

@@ -21,10 +21,79 @@
None
Return value :
Ping value as a string or NULL if an error occured
*/
/*@null@*/char* get_ping_from_temp_log(){
/* Variables */
*/ /*Fonction pour compiler et exécuter le regex */
/*@null@*/ int extract_ping_from_line(const char* line, char** ping){
regex_t reg;
regmatch_t pmatch[2];
int start, end, size_ping;
if(regcomp(p_reg,"time=(.*) ms",REG_EXTENDED) != 0){
return -1;
}
if(regrexec(&reg, line, 2, pmatch, 0) != 0){
regfree(&reg);
return -1;
}
start = (int)pmatch[1].rm_so;
end = (int)pmatch[1].rm_eo;
size_ping = end - start;
*ping = malloc(sizeof(char) * (size_ping + 1));
if( *ping == NULL){
regfree(&reg);
return -1;
}
strncpy(*ping, &line[start], size_ping);
(*ping)[size_ping] = '\0';
regfree(&reg);
return 0;
}
/*@null@*/char* read_ping_from_file(const char* filepath){
FILE* fd = fopen(filepath, "r");
if(fd == NULL) {
return NULL;
}
char* line = NULL;
size_t n = 0;
char* ping = NULL;
while(getline(&line, &n, fd) != -1) {
if(line != NULL) {
if(extract_ping_from_line(line, &ping) == 0) {
// On a trouvé un ping → on arrête
free(line);
line = NULL;
break;
}
}
free(line);
line = NULL;
n = 0;
}
fclose(fd);
return ping;
}
/*@null@*/ char* get_ping_from_temp_log() {
return read_ping_from_file("/var/log/ping-report/last-ping.log");
}
/*
char* get_ping_from_temp_log(){
FILE* fd = NULL;
char* read_line = NULL;
size_t n = 0;
@@ -36,54 +105,54 @@
int end;
size_t size_ping;
/* regex struct memory allocation */
p_reg = (regex_t *) malloc(sizeof(*p_reg));
if(p_reg == NULL){
return ping; /* NULL */
return ping;
}
/* Open ping log file */
fd = fopen("/var/log/ping-report/last-ping.log","r");
if(fd == NULL){
free(p_reg);
return ping; /* NULL */
return ping;
}
/* Construct regex to get ping from log file */
if(regcomp(p_reg,"time=(.*) ms",REG_EXTENDED) != 0){
if(p_reg != NULL){
free(p_reg);
}
(void) fclose(fd);
return ping; /* NULL */
return ping;
}
/* match info memory allocation */
pmatch = malloc(sizeof(*pmatch) * nmatch);
if(pmatch == NULL){
(void) fclose(fd);
regfree(p_reg);
free(p_reg);
return ping; /* NULL */
return ping;
}
/* Read file */
while(getline(&read_line,&n,fd) != -1){
if(read_line == NULL){
break;
}
/* Exec regex to find ping */
if(regexec(p_reg,read_line,nmatch,pmatch,0) == 0){
/* Extract ping position from read line */
start = (int) pmatch[1].rm_so;
end = (int) pmatch[1].rm_eo;
size_ping = (size_t) (end - start);
/* ping string memory allocation */
ping = malloc(sizeof(char) * (size_ping+2));
if(ping == NULL){
free(read_line);
@@ -92,12 +161,10 @@
break;
}
/* Create ping string */
(void) strncpy(ping, &read_line[start], size_ping);
ping[size_ping]='\n';
ping[size_ping+1]='\0';
/* Free memory */
free(read_line);
read_line = NULL;
n = 0;
@@ -109,7 +176,6 @@
n = 0;
}
/* free allocated memory */
regfree(p_reg);
free(p_reg);
free(pmatch);
@@ -119,9 +185,11 @@
(void) fclose(fd);
/* ping may be null, then it must mean that the ping request was lost */
return ping;
}
*/
/*
-- write_ping_log --
@@ -171,15 +239,72 @@ void write_ping_log(char* new_ping){
Return value :
None
*/
void set_stats_ping(void) {
FILE* fd = fopen("/var/log/ping-report/all-ping.log", "r");
if (fd == NULL) {
perror("stats : ");
return;
}
double ping, sum = 0.0, max = 0.0, min = 100.0, mean = 0.0;
int nb_high = 0, nb_loss = 0, nb_ping = 0;
char* line = NULL;
size_t n = 0;
while (getline(&line, &n, fd) != -1) {
if (line == NULL) {
continue;
}
/* Cas "LOSS" */
if (strcmp(line, "LOSS") == 0) {
nb_loss++;
free(line);
line = NULL;
n = 0;
continue;
}
/* Conversion en double */
ping = strtod(line, NULL);
free(line);
line = NULL;
n = 0;
/* On ignore les valeurs trop petites */
if (ping < 0.1) {
continue;
}
nb_ping++;
if (ping > max) max = ping;
if (ping < min) min = ping;
if (ping > 100.0) nb_high++;
sum += ping;
}
if (nb_ping > 0) {
mean = sum / (double) nb_ping;
}
fclose(fd);
insert_hourly_report(mean, max, min, nb_high, nb_loss, nb_ping);
if (line != NULL) {
free(line);
}
}
/*
void set_stats_ping(){
/* Variables */
FILE* fd;
/* Open log file */
fd = fopen("/var/log/ping-report/all-ping.log","r");
if(fd != NULL){
/* Stats variables */
double ping = 0.0;
double sum = 0.0;
double max = 0.0;
@@ -191,39 +316,39 @@ void set_stats_ping(){
char* read_line = NULL;
size_t n = 0;
/* Read file */
while(getline(&read_line,&n,fd) != -1){
/* Check getline error */
if(read_line == NULL){
break;
}
/* Check if the ping is flagged as LOSS */
if(strcmp(read_line,"LOSS") == 0){
nb_loss++;
}else{
/* Evaluate the ping as a double */
ping = strtod(read_line,NULL);
/* Test null ping */
if(ping < 0.1){
/* Ignore null ping */
}else{
/* Number of ping readed (for mean calculation) */
nb_ping++;
/* Max ping */
if(ping > max){
max = ping;
}
/* Min ping */
if(ping < min){
min = ping;
}
/* Number of ping above 100 ms */
if(ping > 100.0){
nb_high++;
}
/* Sum (for mean calculation) */
sum += ping;
}
}
@@ -231,7 +356,7 @@ void set_stats_ping(){
n = 0;
}
/* Mean calculation */
mean = sum / (double) nb_ping;
(void) fclose(fd);