logswan

Fast Web log analyzer using probabilistic data structures
Log | Files | Refs | README | LICENSE

output.c (3742B)


      1 /*
      2  * Logswan 2.1.6
      3  * Copyright (c) 2015-2020, Frederic Cambus
      4  * https://www.logswan.org
      5  *
      6  * Created:      2015-05-31
      7  * Last Updated: 2019-01-19
      8  *
      9  * Logswan is released under the BSD 2-Clause license.
     10  * See LICENSE file for details.
     11  */
     12 
     13 #include <stddef.h>
     14 #include <stdint.h>
     15 
     16 #include <jansson.h>
     17 
     18 #include "config.h"
     19 #include "continents.h"
     20 #include "countries.h"
     21 #include "output.h"
     22 
     23 char
     24 *output(struct results *results)
     25 {
     26 	json_t *output = json_object();
     27 	json_t *hits = json_object();
     28 	json_t *visits = json_object();
     29 	json_t *continents = json_array();
     30 	json_t *countries = json_array();
     31 	json_t *hours = json_array();
     32 	json_t *status = json_array();
     33 	json_t *methods = json_array();
     34 	json_t *protocols = json_array();
     35 
     36 	for (size_t loop = 0; loop < CONTINENTS; loop++) {
     37 		if (results->continents[loop]) {
     38 			json_array_append_new(
     39 			    continents,
     40 			    json_pack("{s:s, s:s, s:i}",
     41 			    "data", continentsId[loop],
     42 			    "name", continentsNames[loop],
     43 			    "hits", results->continents[loop]));
     44 		}
     45 	}
     46 
     47 	for (size_t loop = 0; loop < COUNTRIES; loop++) {
     48 		if (results->countries[loop]) {
     49 			json_array_append_new(
     50 			    countries,
     51 			    json_pack("{s:s, s:s, s:i}",
     52 			    "data", countriesId[loop],
     53 			    "name", countriesNames[loop],
     54 			    "hits", results->countries[loop]));
     55 		}
     56 	}
     57 
     58 	for (size_t loop = 0; loop < 24; loop++) {
     59 		if (results->hours[loop]) {
     60 			json_array_append_new(
     61 			    hours,
     62 			    json_pack("{s:i, s:i}",
     63 			    "data", loop,
     64 			    "hits", results->hours[loop]));
     65 		}
     66 	}
     67 
     68 	for (size_t loop = 0; loop < STATUS_CODE_MAX; loop++) {
     69 		if (results->status[loop]) {
     70 			json_array_append_new(
     71 			    status,
     72 			    json_pack("{s:i, s:i}",
     73 			    "data", loop,
     74 			    "hits", results->status[loop]));
     75 		}
     76 	}
     77 
     78 	for (size_t loop = 0; loop < METHODS; loop++) {
     79 		if (results->methods[loop]) {
     80 			json_array_append_new(
     81 			    methods,
     82 			    json_pack("{s:s, s:i}",
     83 			    "data", methodsNames[loop],
     84 			    "hits", results->methods[loop]));
     85 		}
     86 	}
     87 
     88 	for (size_t loop = 0; loop < PROTOCOLS; loop++) {
     89 		if (results->protocols[loop]) {
     90 			json_array_append_new(
     91 			    protocols,
     92 			    json_pack("{s:s, s:i}",
     93 			    "data", protocolsNames[loop],
     94 			    "hits", results->protocols[loop]));
     95 		}
     96 	}
     97 
     98 	json_object_set_new(hits, "ipv4", json_integer(results->hitsIPv4));
     99 	json_object_set_new(hits, "ipv6", json_integer(results->hitsIPv6));
    100 	json_object_set_new(hits, "total", json_integer(results->hits));
    101 
    102 	json_object_set_new(visits, "ipv4", json_integer(results->visitsIPv4));
    103 	json_object_set_new(visits, "ipv6", json_integer(results->visitsIPv6));
    104 	json_object_set_new(visits, "total", json_integer(results->visits));
    105 
    106 	json_object_set_new(output, "date", json_string(results->timeStamp));
    107 	json_object_set_new(output, "generator", json_string(VERSION));
    108 	json_object_set_new(output, "file_name", json_string(results->fileName));
    109 	json_object_set_new(output, "file_size", json_integer(results->fileSize));
    110 	json_object_set_new(output, "processed_lines", json_integer(results->processedLines));
    111 	json_object_set_new(output, "invalid_lines", json_integer(results->invalidLines));
    112 	json_object_set_new(output, "bandwidth", json_integer(results->bandwidth));
    113 	json_object_set_new(output, "runtime", json_real(results->runtime));
    114 	json_object_set_new(output, "hits", hits);
    115 	json_object_set_new(output, "visits", visits);
    116 	json_object_set_new(output, "continents", continents);
    117 	json_object_set_new(output, "countries", countries);
    118 	json_object_set_new(output, "hours", hours);
    119 	json_object_set_new(output, "methods", methods);
    120 	json_object_set_new(output, "protocols", protocols);
    121 	json_object_set_new(output, "status", status);
    122 
    123 	return json_dumps(output, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
    124 }