示例#1
0
文件: json.c 项目: IMISME/goaccess
/**
 * Generate JSON on partial fields for the following modules:
 * OS, BROWSERS, REFERRERS, REFERRING_SITES, KEYPHRASES, STATUS_CODES
 */
static void
print_json_generic (FILE * fp, const GHolder * h, int process)
{
  char *data;
  const char *id = NULL;
  float percent;
  int i, hits;

  if (h->module == BROWSERS)
    id = BROWS_ID;
  else if (h->module == OS)
    id = OPERA_ID;
  else if (h->module == REFERRERS)
    id = REFER_ID;
  else if (h->module == REFERRING_SITES)
    id = SITES_ID;
  else if (h->module == KEYPHRASES)
    id = KEYPH_ID;
  else if (h->module == STATUS_CODES)
    id = CODES_ID;
#ifdef HAVE_LIBGEOIP
  else if (h->module == GEO_LOCATION)
    id = GEOLO_ID;
#endif

  fprintf (fp, "\t\"%s\": [\n", id);

  for (i = 0; i < h->idx; i++) {
    hits = h->items[i].hits;
    data = h->items[i].data;
    percent = get_percentage (process, hits);
    percent = percent < 0 ? 0 : percent;

    fprintf (fp, "\t\t{\n");
    fprintf (fp, "\t\t\t\"hits\": \"%d\",\n", hits);
    fprintf (fp, "\t\t\t\"percent\": \"%4.2f%%\",\n", percent);
    fprintf (fp, "\t\t\t\"data\": \"");
    escape_json_output (fp, data);
    fprintf (fp, "\"");

    if (h->module == OS || h->module == BROWSERS || h->module == STATUS_CODES
#ifdef HAVE_LIBGEOIP
        || h->module == GEO_LOCATION
#endif
      )
      print_json_sub_items (fp, h->items[i].sub_list, process);

    fprintf (fp, "\n\t\t}");

    if (i != h->idx - 1)
      fprintf (fp, ",\n");
    else
      fprintf (fp, "\n");
  }
  fprintf (fp, "\n\t]");
}
示例#2
0
文件: json.c 项目: fecori/goaccess
static void
print_json_data (FILE * fp, GHolder * h, int processed)
{
  GMetrics *nmetrics;
  char *sep = char_repeat (2, '\t');
  int i;

  fprintf (fp, "\t\"%s\": [\n", module_to_id (h->module));
  for (i = 0; i < h->idx; i++) {
    set_data_metrics (h->items[i].metrics, &nmetrics, processed);

    fprintf (fp, "%s{\n", sep);
    print_json_block (fp, nmetrics, sep);
    if (h->sub_items_size)
      print_json_sub_items (fp, h, i, processed);
    fprintf (fp, (i != h->idx - 1) ? "\n%s},\n" : "\n%s}\n", sep);

    free (nmetrics);
  }
  fprintf (fp, "\t]");

  free (sep);
}