graph_list: Implement better iterator functions.
[collection4.git] / action_list_graphs.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <errno.h>
5
6 #include <fcgiapp.h>
7 #include <fcgi_stdio.h>
8
9 #include "action_list_graphs.h"
10 #include "graph_list.h"
11 #include "utils_params.h"
12
13 static int print_graph_json (const graph_list_t *gl, void *user_data) /* {{{ */
14 {
15   _Bool *first;
16
17   if ((gl == NULL) || (user_data == NULL))
18     return (EINVAL);
19
20   first = (_Bool *) user_data;
21   if (!*first)
22     printf (",\n");
23   *first = 0;
24
25   printf (" {");
26
27   printf ("\"host\":\"%s\"", gl->host);
28       
29   printf (",\"plugin\":\"%s\"", gl->plugin);
30   if (gl->plugin_instance != NULL)
31     printf (",\"plugin_instance\":\"%s\"", gl->plugin_instance);
32   else
33     printf (",\"plugin_instance\":null");
34
35   printf (",\"type\":\"%s\"", gl->type);
36   if (gl->type_instance != NULL)
37     printf (",\"type_instance\":\"%s\"", gl->type_instance);
38   else
39     printf (",\"type_instance\":null");
40
41   printf ("}");
42
43   return (0);
44 } /* }}} int print_graph_json */
45
46 static int print_graph_inst_html (__attribute__((unused)) graph_config_t *cfg, /* {{{ */
47     graph_instance_t *inst,
48     __attribute__((unused)) void *user_data)
49 {
50   char buffer[1024];
51
52   memset (buffer, 0, sizeof (buffer));
53   gl_instance_get_ident (inst, buffer, sizeof (buffer));
54
55   printf ("<li>%s</li>\n", buffer);
56
57   return (0);
58 } /* }}} int print_graph_inst_html */
59
60 static int print_graph_html (graph_config_t *cfg, /* {{{ */
61     __attribute__((unused)) void *user_data)
62 {
63   printf ("<li>%p\n<ul>\n", (void *) cfg);
64   gl_graph_instance_get_all (cfg, print_graph_inst_html, /* user_data = */ NULL);
65   printf ("</ul>\n");
66
67   return (0);
68 } /* }}} int print_graph_html */
69
70 static int list_graphs_json (void) /* {{{ */
71 {
72   _Bool first = 1;
73
74   printf ("Content-Type: application/json\n\n");
75
76   printf ("[\n");
77   gl_foreach (print_graph_json, /* user_data = */ &first);
78   printf ("\n]");
79
80   return (0);
81 } /* }}} int list_graphs_json */
82
83 static int list_graphs_html (void) /* {{{ */
84 {
85   printf ("Content-Type: text/html\n\n");
86
87   printf ("<ul>\n");
88   gl_graph_get_all (print_graph_html, /* user_data = */ NULL);
89   printf ("</ul>\n");
90
91   return (0);
92 } /* }}} int list_graphs_html */
93
94 int action_list_graphs (void) /* {{{ */
95 {
96   const char *format;
97
98   gl_update ();
99
100   format = param ("format");
101   if (format == NULL)
102     format = "html";
103
104   if (strcmp ("json", format) == 0)
105     return (list_graphs_json ());
106   else
107     return (list_graphs_html ());
108 } /* }}} int action_list_graphs */
109
110 /* vim: set sw=2 sts=2 et fdm=marker : */