926fde2946f1573fd06a3d1d78e947a78402d399
[collection4.git] / graph_def.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <errno.h>
4
5 #include "graph_def.h"
6 #include "common.h"
7
8 /*
9  * Data structures
10  */
11 struct graph_def_s
12 {
13   graph_ident_t *select;
14
15   char *ds_name;
16   uint32_t color;
17
18   graph_def_t *next;
19 };
20
21 /*
22  * Private functions
23  */
24
25 /*
26  * Public functions
27  */
28 graph_def_t *def_create (graph_config_t *cfg, graph_ident_t *ident, /* {{{ */
29     const char *ds_name)
30 {
31   graph_ident_t *selector;
32   graph_def_t *ret;
33
34   if ((cfg == NULL) || (ident == NULL) || (ds_name == NULL))
35     return (NULL);
36
37   selector = gl_graph_get_selector (cfg);
38   if (selector == NULL)
39     return (NULL);
40
41   ret = malloc (sizeof (*ret));
42   if (ret == NULL)
43   {
44     ident_destroy (selector);
45     return (NULL);
46   }
47   memset (ret, 0, sizeof (*ret));
48
49   ret->ds_name = strdup (ds_name);
50   if (ret->ds_name == NULL)
51   {
52     ident_destroy (selector);
53     free (ret);
54     return (NULL);
55   }
56
57   ret->color = get_random_color ();
58   ret->next = NULL;
59
60   ret->select = ident_copy_with_selector (selector, ident,
61       IDENT_FLAG_REPLACE_ALL);
62   if (ret->select == NULL)
63   {
64     ident_destroy (selector);
65     free (ret->ds_name);
66     free (ret);
67     return (NULL);
68   }
69
70   ident_destroy (selector);
71   return (ret);
72 }; /* }}} graph_def_t *def_create */
73
74 void def_destroy (graph_def_t *def) /* {{{ */
75 {
76   graph_def_t *next;
77
78   if (def == NULL)
79     return;
80
81   next = def->next;
82
83   ident_destroy (def->select);
84
85   free (def->ds_name);
86
87   free (def);
88
89   def_destroy (next);
90 } /* }}} void def_destroy */
91
92 int def_append (graph_def_t *head, graph_def_t *def) /* {{{ */
93 {
94   graph_def_t *ptr;
95
96   if ((head == NULL) || (def == NULL))
97     return (EINVAL);
98
99   ptr = head;
100   while (ptr->next != NULL)
101     ptr = ptr->next;
102
103   ptr->next = def;
104
105   return (0);
106 } /* }}} int def_append */
107
108 graph_def_t *def_search (graph_def_t *head, graph_ident_t *ident, /* {{{ */
109     const char *ds_name)
110 {
111   graph_def_t *ptr;
112
113   if ((head == NULL) || (ident == NULL) || (ds_name == NULL))
114     return (NULL);
115
116   for (ptr = head; ptr != NULL; ptr = ptr->next)
117   {
118     if (!ident_matches (ptr->select, ident))
119       continue;
120
121     if (strcmp (ptr->ds_name, ds_name) == 0)
122       return (ptr);
123   }
124
125   return (NULL);
126 } /* }}} graph_def_t *def_search */
127
128 _Bool def_matches (graph_def_t *def, graph_ident_t *ident) /* {{{ */
129 {
130   return (ident_matches (def->select, ident));
131 } /* }}} _Bool def_matches */
132
133 int def_foreach (graph_def_t *def, def_callback_t callback, /* {{{ */
134     void *user_data)
135 {
136   graph_def_t *ptr;
137
138   if ((def == NULL) || (callback == NULL))
139     return (EINVAL);
140
141   for (ptr = def; ptr != NULL; ptr = ptr->next)
142   {
143     int status;
144
145     status = (*callback) (ptr, user_data);
146     if (status != 0)
147       return (status);
148   }
149
150   return (0);
151 } /* }}} int def_foreach */
152
153 int def_get_rrdargs (graph_def_t *def, graph_ident_t *ident, /* {{{ */
154     str_array_t *args)
155 {
156   char *file;
157   int index;
158
159   if ((def == NULL) || (ident == NULL) || (args == NULL))
160     return (EINVAL);
161
162   file = ident_to_file (ident);
163   if (file == NULL)
164   {
165     DEBUG ("gl_ident_get_rrdargs: ident_to_file returned NULL.\n");
166     return (-1);
167   }
168
169   DEBUG ("gl_ident_get_rrdargs: file = %s;\n", file);
170
171   index = array_argc (args);
172
173   /* CDEFs */
174   array_append_format (args, "DEF:def_%04i_min=%s:%s:MIN",
175       index, file, def->ds_name);
176   array_append_format (args, "DEF:def_%04i_avg=%s:%s:AVERAGE",
177       index, file, def->ds_name);
178   array_append_format (args, "DEF:def_%04i_max=%s:%s:MAX",
179       index, file, def->ds_name);
180   /* VDEFs */
181   array_append_format (args, "VDEF:vdef_%04i_min=def_%04i_min,MINIMUM",
182       index, index);
183   array_append_format (args, "VDEF:vdef_%04i_avg=def_%04i_avg,AVERAGE",
184       index, index);
185   array_append_format (args, "VDEF:vdef_%04i_max=def_%04i_max,MAXIMUM",
186       index, index);
187   array_append_format (args, "VDEF:vdef_%04i_lst=def_%04i_avg,LAST",
188       index, index);
189
190   /* Graph part */
191   array_append_format (args, "LINE1:def_%04i_avg#%06"PRIx32":%s",
192       index, def->color, def->ds_name);
193   array_append_format (args, "GPRINT:vdef_%04i_min:%%lg min,", index);
194   array_append_format (args, "GPRINT:vdef_%04i_avg:%%lg avg,", index);
195   array_append_format (args, "GPRINT:vdef_%04i_max:%%lg max,", index);
196   array_append_format (args, "GPRINT:vdef_%04i_lst:%%lg last\\l", index);
197
198   free (file);
199
200   return (0);
201 } /* }}} int def_get_rrdargs */
202
203 /* vim: set sw=2 sts=2 et fdm=marker : */