LCOV - code coverage report
Current view: top level - src - rptsample.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 18 18 100.0 %
Date: 2024-12-05 21:00:54 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /**
       2             :  *    @file  rptsample.c
       3             :  *   @brief  Basic report without connection to test output format
       4             :  *
       5             :  *  @author  François Cerbelle (Fanfan), francois@cerbelle.net
       6             :  *
       7             :  *  @internal
       8             :  *       Created:  28/10/2024
       9             :  *      Revision:  none
      10             :  * Last modified:  2024-11-14 18:14
      11             :  *      Compiler:  gcc
      12             :  *  Organization:  Cerbelle.net
      13             :  *     Copyright:  Copyright (c) 2024, François Cerbelle
      14             :  *
      15             :  *  This source code is released for free distribution under the terms of the
      16             :  *  GNU General Public License as published by the Free Software Foundation.
      17             :  */
      18             : 
      19             : #ifdef HAVE_CONFIG_H
      20             : #include "config.h"
      21             : #endif
      22             : 
      23             : #include "rptsample.h"
      24             : #include "clustercon.h"
      25             : #include "json.h"
      26             : #include "csv.h"
      27             : #include <string.h>
      28             : 
      29             : /* To refactor in a report common file */
      30         132 : static void csv_addjsonfield(FILE* reportfile, const cJSON* json, char* fieldname) {
      31         132 :     char* text = json2text(cJSON_GetObjectItemCaseSensitive(json, fieldname));
      32         132 :     csv_addfield(reportfile,text);
      33         132 :     free(text);
      34         132 : }
      35             : 
      36          12 : void report_sample_header(FILE* reportfile) {
      37          12 :     fprintf(reportfile,
      38             :             "field1,field2,field3\r\n"
      39             :            );
      40          12 : }
      41             : 
      42          22 : void report_sample(FILE* reportfile, const cluster_t* cluster) {
      43             :     cJSON* samples_json;
      44             :     (void)cluster;
      45             : 
      46          22 :     samples_json = cJSON_Parse("["
      47             :                                "{\"field1\":\"value1\",\"field2\":\"value 2\",\"field3\":\" value3\"},"
      48             :                                "{\"field1\":\"value4 \",\"field2\":\"value,5\",\"field3\":\"value\\\"6\\\"\"}"
      49             :                                "]");
      50             :     const cJSON* sample_json;
      51          66 :     cJSON_ArrayForEach(sample_json, samples_json) {
      52          44 :         csv_addfield(reportfile,cluster->host);
      53          44 :         csv_addjsonfield(reportfile, sample_json, "field1");
      54          44 :         csv_addjsonfield(reportfile, sample_json, "field2");
      55          44 :         csv_addjsonfield(reportfile, sample_json, "field3");
      56             : 
      57          44 :         csv_addline(reportfile);
      58             :     }
      59          22 :     cJSON_Delete(samples_json);
      60          22 : }
      61             : /* vim: set tw=80: */

Generated by: LCOV version 1.16