logswan

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

output.c (3801B)


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