static void print_csv_sub_items (FILE * fp, GSubList * sub_list, int process, const char *id, int *idx) { char *data; float percent; GSubItem *iter; int hits, i = 0; for (iter = sub_list->head; iter; iter = iter->next) { hits = iter->hits; data = (char *) iter->data; percent = get_percentage (process, hits); percent = percent < 0 ? 0 : percent; fprintf (fp, "\"%d\",", i); /* idx */ fprintf (fp, "\"%d\",", (*idx)); /* parent idx */ fprintf (fp, "\"%s\",", id); fprintf (fp, "\"%d\",", hits); fprintf (fp, "\"%4.2f%%\",", percent); fprintf (fp, "\""); escape_cvs_output (fp, data); fprintf (fp, "\","); fprintf (fp, "\r\n"); /* parent idx */ i++; } }
/** * Generate CSV on complete fields for the following modules: * REQUESTS, REQUESTS_STATIC, NOT_FOUND, HOSTS */ static void print_csv_complete (FILE * fp, GHolder * holder, int process) { char *data, *method = NULL, *protocol = NULL; const char *id = NULL; float percent; GHolder *h; int i, j, hits; unsigned long long bw, usecs; for (i = 0; i < 4; i++) { switch (i) { case 0: h = holder + REQUESTS; id = REQUE_ID; break; case 1: h = holder + REQUESTS_STATIC; id = STATI_ID; break; case 2: h = holder + NOT_FOUND; id = FOUND_ID; break; case 3: h = holder + HOSTS; id = HOSTS_ID; break; } for (j = 0; j < h->idx; j++) { hits = h->items[j].hits; data = h->items[j].data; percent = get_percentage (process, hits); percent = percent < 0 ? 0 : percent; bw = h->items[j].bw; usecs = h->items[j].usecs; method = h->items[j].method; protocol = h->items[j].protocol; fprintf (fp, "\"%d\",", j); /* idx */ fprintf (fp, ","); /* parent idx */ fprintf (fp, "\"%s\",", id); fprintf (fp, "\"%d\",", hits); fprintf (fp, "\"%4.2f%%\",", percent); fprintf (fp, "\""); escape_cvs_output (fp, data); fprintf (fp, "\","); fprintf (fp, "\"%lld\"", bw); if (conf.serve_usecs) fprintf (fp, ",\"%lld\"", usecs); if (conf.append_protocol && protocol) fprintf (fp, ",\"%s\"", protocol); if (conf.append_method && method) fprintf (fp, ",\"%s\"", method); fprintf (fp, "\r\n"); } } }
/** * Generate CSV on partial fields for the following modules: * OS, BROWSERS, REFERRERS, REFERRING_SITES, KEYPHRASES, STATUS_CODES */ static void print_csv_generic (FILE * fp, const GHolder * h, int process) { char *data; const char *id = NULL; float percent; int i, idx, 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 for (i = 0, idx = 0; i < h->idx; i++, idx++) { hits = h->items[i].hits; data = h->items[i].data; percent = get_percentage (process, hits); percent = percent < 0 ? 0 : percent; fprintf (fp, "\"%d\",", idx); /* idx */ fprintf (fp, ","); /* parent idx */ fprintf (fp, "\"%s\",", id); fprintf (fp, "\"%d\",", hits); fprintf (fp, "\"%4.2f%%\",", percent); fprintf (fp, "\""); escape_cvs_output (fp, data); fprintf (fp, "\","); fprintf (fp, "\r\n"); /* parent idx */ if (h->module == OS || h->module == BROWSERS || h->module == STATUS_CODES #ifdef HAVE_LIBGEOIP || h->module == GEO_LOCATION #endif ) print_csv_sub_items (fp, h->items[i].sub_list, process, id, &idx); } }
/* Output a sublist (double linked-list) items for a particular parent node. * * On error, it exits early. * On success, outputs item value. */ static void print_csv_sub_items (FILE * fp, GHolder * h, int idx, int valid) { GSubList *sub_list = h->items[idx].sub_list; GSubItem *iter; float percent; int i = 0; if (sub_list == NULL) return; for (iter = sub_list->head; iter; iter = iter->next, i++) { percent = get_percentage (valid, iter->metrics->hits); percent = percent < 0 ? 0 : percent; fprintf (fp, "\"%d\",", i); /* idx */ fprintf (fp, "\"%d\",", idx); /* parent idx */ fprintf (fp, "\"%s\",", module_to_id (h->module)); fprintf (fp, "\"%d\",", iter->metrics->hits); fprintf (fp, "\"%d\",", iter->metrics->visitors); fprintf (fp, "\"%4.2f%%\",", percent); if (conf.bandwidth) fprintf (fp, "\"%lld\",", (long long) iter->metrics->bw.nbw); if (conf.serve_usecs) { fprintf (fp, "\"%lld\",", (long long) iter->metrics->avgts.nts); fprintf (fp, "\"%lld\",", (long long) iter->metrics->cumts.nts); fprintf (fp, "\"%lld\",", (long long) iter->metrics->maxts.nts); } if (conf.append_method && iter->metrics->method) fprintf (fp, "\"%s\"", iter->metrics->method); fprintf (fp, ","); if (conf.append_protocol && iter->metrics->protocol) fprintf (fp, "\"%s\"", iter->metrics->protocol); fprintf (fp, ","); fprintf (fp, "\""); escape_cvs_output (fp, iter->metrics->data); fprintf (fp, "\","); fprintf (fp, "\r\n"); /* parent idx */ } }
/* Output first-level items. */ static void print_csv_data (FILE * fp, GHolder * h, int valid) { GMetrics *nmetrics; int i; for (i = 0; i < h->idx; i++) { set_data_metrics (h->items[i].metrics, &nmetrics, valid); fprintf (fp, "\"%d\",", i); /* idx */ fprintf (fp, ","); /* no parent */ fprintf (fp, "\"%s\",", module_to_id (h->module)); fprintf (fp, "\"%d\",", nmetrics->hits); fprintf (fp, "\"%d\",", nmetrics->visitors); fprintf (fp, "\"%4.2f%%\",", nmetrics->percent); if (conf.bandwidth) fprintf (fp, "\"%lld\",", (long long) nmetrics->bw.nbw); if (conf.serve_usecs) { fprintf (fp, "\"%lld\",", (long long) nmetrics->avgts.nts); fprintf (fp, "\"%lld\",", (long long) nmetrics->cumts.nts); fprintf (fp, "\"%lld\",", (long long) nmetrics->maxts.nts); } if (conf.append_method && nmetrics->method) fprintf (fp, "\"%s\"", nmetrics->method); fprintf (fp, ","); if (conf.append_protocol && nmetrics->protocol) fprintf (fp, "\"%s\"", nmetrics->protocol); fprintf (fp, ","); fprintf (fp, "\""); escape_cvs_output (fp, nmetrics->data); fprintf (fp, "\"\r\n"); if (h->sub_items_size) print_csv_sub_items (fp, h, i, valid); free (nmetrics); } }
/* Output metrics. * * On success, outputs item value. */ static void print_csv_metric_block (FILE * fp, GMetrics * nmetrics) { /* basic metrics */ fprintf (fp, "\"%d\",", nmetrics->hits); fprintf (fp, "\"%4.2f%%\",", nmetrics->hits_perc); fprintf (fp, "\"%d\",", nmetrics->visitors); fprintf (fp, "\"%4.2f%%\",", nmetrics->visitors_perc); /* bandwidth */ if (conf.bandwidth) { fprintf (fp, "\"%lld\",", (long long) nmetrics->bw.nbw); fprintf (fp, "\"%4.2f%%\",", nmetrics->bw_perc); } /* time served metrics */ if (conf.serve_usecs) { fprintf (fp, "\"%lld\",", (long long) nmetrics->avgts.nts); fprintf (fp, "\"%lld\",", (long long) nmetrics->cumts.nts); fprintf (fp, "\"%lld\",", (long long) nmetrics->maxts.nts); } /* request method */ if (conf.append_method && nmetrics->method) fprintf (fp, "\"%s\"", nmetrics->method); fprintf (fp, ","); /* request protocol */ if (conf.append_protocol && nmetrics->protocol) fprintf (fp, "\"%s\"", nmetrics->protocol); fprintf (fp, ","); /* data field */ fprintf (fp, "\""); escape_cvs_output (fp, nmetrics->data); fprintf (fp, "\"\r\n"); }