forked from menault/TD2_DEV51_Qualite_Algo
Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
8253ce6fe8 |
Binary file not shown.
Before Width: | Height: | Size: 222 KiB |
BIN
Qualité Algorithmique - Profiling & Complexité cyclomatique.pptx
Normal file
BIN
Qualité Algorithmique - Profiling & Complexité cyclomatique.pptx
Normal file
Binary file not shown.
@@ -1,48 +0,0 @@
|
||||
# Complexité Cyclomatique - Projet ping-report
|
||||
|
||||
## ping-report.c
|
||||
- `main()` : **4**
|
||||
|
||||
## daemon.c
|
||||
- `create_daemon()` : **4**
|
||||
- `ping_request()` : **2**
|
||||
- `send_check()` : **3**
|
||||
- `check_keep_working()` : **4**
|
||||
- `daemon_work()` : **3**
|
||||
|
||||
## db-sqlite.c
|
||||
- `db_connect()` : **1**
|
||||
- `db_disconnect()` : **1**
|
||||
- `insert_hourly_report()` : **1**
|
||||
|
||||
## utils.c
|
||||
- `write_pid_file()` : **2**
|
||||
- `remove_file()` : **1**
|
||||
|
||||
## stats.c
|
||||
- `get_ping_from_temp_log()` : **14** → **3** (refactorisé)
|
||||
- `write_ping_log()` : **4**
|
||||
- `set_stats_ping()` : **12** → **3** (refactorisé)
|
||||
|
||||
|
||||
## Améliorations apportées
|
||||
|
||||
### Fonctions refactorisées (complexité > 6)
|
||||
|
||||
**1. `get_ping_from_temp_log()` : 14 → 3**
|
||||
- Divisée en 5 fonctions d'aide
|
||||
- Chaque fonction fait une seule chose
|
||||
- Plus facile à tester et déboguer
|
||||
|
||||
**2. `set_stats_ping()` : 12 → 3**
|
||||
- Divisée en 3 fonctions d'aide
|
||||
- Structure PingStats pour clarifier
|
||||
- Logique de calcul séparée
|
||||
|
||||
### Avantages (comme vu en cours)
|
||||
- Code plus lisible
|
||||
- Maintenance plus simple (si un developpeur veut changer une donnée, il peut le faire à un seul endroit pour que tout puisse changer. Cela évite aussi de copier coller des bouts de codes )
|
||||
- Tests unitaires possibles
|
||||
- Réutilisabilité des fonctions d'aide
|
||||
|
||||
### Le calcul de la complexité de chaque fonction modifiée a été mise en commentaire directement.
|
BIN
set_stats.png
BIN
set_stats.png
Binary file not shown.
Before Width: | Height: | Size: 252 KiB |
@@ -1,197 +0,0 @@
|
||||
/* 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user