From 2500c548830ccc4dd5a668cf0295dd74086ad8c6 Mon Sep 17 00:00:00 2001 From: TheSinay Date: Mon, 14 Oct 2024 20:33:40 +0200 Subject: [PATCH] fin de la factorisation --- .vscode/c_cpp_properties.json | 18 +++ .vscode/launch.json | 24 +++ .vscode/settings.json | 59 +++++++ ping-report/src/stats copy.c | 132 --------------- ping-report/src/stats factorise.c | 258 ++++++++++++++++++++++++++++++ 5 files changed, 359 insertions(+), 132 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json delete mode 100644 ping-report/src/stats copy.c create mode 100644 ping-report/src/stats factorise.c diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cea4d3f --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "windows-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..d8be62f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/yanis/TD2_DEV51_Qualite_Algo", + "program": "c:/Users/yanis/TD2_DEV51_Qualite_Algo/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/ping-report/src/stats copy.c b/ping-report/src/stats copy.c deleted file mode 100644 index 43d7873..0000000 --- a/ping-report/src/stats copy.c +++ /dev/null @@ -1,132 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../include/utils.h" -#include "../include/stats.h" -#include "../include/db-sqlite.h" - -/* - -- get_ping_from_temp_log -- - Desc : - Function which get the ping from a temp log containing the last ping did by the program - In-param : - None - Out-param : - None - Return value : - Ping value as a string or NULL if an error occured -*/ -/*@null@*/char* get_ping_from_temp_log(){ - - /* Variables */ - FILE* fd = NULL; - char* read_line = NULL; - size_t n = 0; - size_t nmatch = 2; - regex_t *p_reg; - regmatch_t* pmatch; - char* ping = NULL; - int start; - 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 */ - } - - /* Open ping log file */ - fd = fopen("/var/log/ping-report/last-ping.log","r"); - if(fd == NULL){ - free(p_reg); - return ping; /* NULL */ - } - - /* 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 */ - } - - /* match info memory allocation */ - pmatch = malloc(sizeof(*pmatch) * nmatch); - if(pmatch == NULL){ - (void) fclose(fd); - regfree(p_reg); - free(p_reg); - return ping; /* NULL */ - } - - - - /* Read file */ - - whilegetline(getline(&read_line,&n,fd)) - - - /* free allocated memory */ - regfree(p_reg); - free(p_reg); - free(pmatch); - if(read_line != NULL){ - free(read_line); - }c - - (void) fclose(fd); - - /* ping may be null, then it must mean that the ping request was lost */ - return ping; -} - -void whilegetline( int gegetline) { - while ( gegetline =! -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_egetlineo; - size_ping = (size_t) (end - start); - - /* ping string memory allocation */ - ping = malloc(sizeof(char) * (size_ping+2)); - if(ping == NULL){ - free(read_line); - read_line = NULL; - n = 0; - 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; - break; - } - - free(read_line); - read_line = NULL; - n = 0; - } - -} \ No newline at end of file diff --git a/ping-report/src/stats factorise.c b/ping-report/src/stats factorise.c new file mode 100644 index 0000000..2e66b24 --- /dev/null +++ b/ping-report/src/stats factorise.c @@ -0,0 +1,258 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../include/utils.h" +#include "../include/stats.h" +#include "../include/db-sqlite.h" + +/* + -- get_ping_from_temp_log -- + Desc : + Function which get the ping from a temp log containing the last ping did by the program + In-param : + None + Out-param : + None + Return value : + Ping value as a string or NULL if an error occured +*/ +/*@null@*/ char *get_ping_from_temp_log() +{ + + /* Variables */ + FILE *fd = NULL; + char *read_line = NULL; + size_t n = 0; + size_t nmatch = 2; + regex_t *p_reg; + regmatch_t *pmatch; + char *ping = NULL; + int start; + 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 */ + } + + /* Open ping log file */ + fd = fopen("/var/log/ping-report/last-ping.log", "r"); + if (fd == NULL) + { + free(p_reg); + return ping; /* NULL */ + } + + /* 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 */ + } + + /* match info memory allocation */ + pmatch = malloc(sizeof(*pmatch) * nmatch); + if (pmatch == NULL) + { + (void)fclose(fd); + regfree(p_reg); + free(p_reg); + return ping; /* NULL */ + } + + /* Read file */ + + whilegetline(getline(&read_line, &n, fd)) + + /* free allocated memory */ + regfree(p_reg); + free(p_reg); + free(pmatch); + if (read_line != NULL) + { + free(read_line); + } + c + + (void) fclose(fd); + + /* ping may be null, then it must mean that the ping request was lost */ + return ping; +} + +void whilegetline(int gegetline) +{ + while (gegetline = !-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_egetlineo; + size_ping = (size_t)(end - start); + + /* ping string memory allocation */ + ping = malloc(sizeof(char) * (size_ping + 2)); + if (ping == NULL) + { + free(read_line); + read_line = NULL; + n = 0; + 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; + break; + } + + free(read_line); + read_line = NULL; + n = 0; + } +} + +/* + -- set_stats_ping -- + Desc : + Function which calculate statistics about ping values, from log file. + In-param : + None + Out-param : + None + Return value : + None +*/ +void set_stats_ping() +{ + + /* Variables */ + FILE *fd; + /* Open log file */ + fd = fopen("/var/log/ping-report/all-ping.log", "r"); + + if (fd != NULL) + { + whileSet_stats_ping(); + } + else + { + perror("stats : "); + } +} + +void whileSet_stats_ping() +{ + double ping = 0.0; + double sum = 0.0; + double max = 0.0; + double min = 100.0; + double mean = 0.0; + int nb_high = 0; + int nb_loss = 0; + int nb_ping = 0; + 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++; + evaluatePing(ping,&max,&min,&nb_high,&sum) + + } + } + free(read_line); + n = 0; + } + meanCaclulatoion(max,min,nb_high,nb_loss,nb_ping); + +} + +void evaluatePing(double ping , double *max ,double *min ,int *nb_high, double *sum) +{ + + /* 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; +} + +void meanCaclulatoion( double max, double min, int nb_high, int nb_loss, int nb_ping,char* read_line) +{ + /* Mean calculation */ + mean = sum / (double)nb_ping; + (void)fclose(fd); + + insert_hourly_report(mean, max, min, nb_high, nb_loss, nb_ping); + + if (read_line != NULL) + { + free(read_line); + } +} \ No newline at end of file