share/collection.js: Keep visibility when redrawing graphs.
[collection4.git] / src / action_graph.c
index 0bb5856..b3b815f 100644 (file)
@@ -1,3 +1,28 @@
+/**
+ * collection4 - action_graph.c
+ * Copyright (C) 2010  Florian octo Forster
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Florian octo Forster <ff at octo.it>
+ **/
+
+#include "config.h"
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
 #include "common.h"
 #include "action_graph.h"
+#include "graph.h"
+#include "graph_instance.h"
 #include "graph_list.h"
-#include "utils_params.h"
+#include "utils_cgi.h"
 #include "utils_array.h"
 
 #include <fcgiapp.h>
@@ -21,7 +48,7 @@
 
 struct graph_data_s
 {
-  str_array_t *args;
+  rrd_args_t *args;
   rrd_info_t *info;
   time_t mtime;
   time_t expires;
@@ -31,80 +58,6 @@ struct graph_data_s
 };
 typedef struct graph_data_s graph_data_t;
 
-static int get_time_args (graph_data_t *data) /* {{{ */
-{
-  const char *begin_str;
-  const char *end_str;
-  long now;
-  long begin;
-  long end;
-  char *endptr;
-  long tmp;
-
-  begin_str = param ("begin");
-  end_str = param ("end");
-
-  now = (long) time (NULL);
-  data->now = now;
-  data->begin = now - 86400;
-  data->end = now;
-
-  if (begin_str != NULL)
-  {
-    endptr = NULL;
-    errno = 0;
-    tmp = strtol (begin_str, &endptr, /* base = */ 0);
-    if ((endptr == begin_str) || (errno != 0))
-      return (-1);
-    if (tmp <= 0)
-      begin = now + tmp;
-    else
-      begin = tmp;
-  }
-  else /* if (begin_str == NULL) */
-  {
-    begin = now - 86400;
-  }
-
-  if (end_str != NULL)
-  {
-    endptr = NULL;
-    errno = 0;
-    tmp = strtol (end_str, &endptr, /* base = */ 0);
-    if ((endptr == end_str) || (errno != 0))
-      return (-1);
-    end = tmp;
-    if (tmp <= 0)
-      end = now + tmp;
-    else
-      end = tmp;
-  }
-  else /* if (end_str == NULL) */
-  {
-    end = now;
-  }
-
-  if (begin == end)
-    return (-1);
-
-  if (begin > end)
-  {
-    tmp = begin;
-    begin = end;
-    end = tmp;
-  }
-
-  data->begin = begin;
-  data->end = end;
-
-  array_append (data->args, "-s");
-  array_append_format (data->args, "%li", begin);
-  array_append (data->args, "-e");
-  array_append_format (data->args, "%li", end);
-
-  return (0);
-} /* }}} int get_time_args */
-
 static void emulate_graph (int argc, char **argv) /* {{{ */
 {
   int i;
@@ -183,6 +136,7 @@ static int output_graph (graph_data_t *data) /* {{{ */
   if (status == 0)
     printf ("Expires: %s\n", time_buffer);
 
+  printf ("X-Generator: "PACKAGE_STRING"\n");
   printf ("\n");
 
   fwrite (img->value.u_blo.ptr, img->value.u_blo.size,
@@ -204,6 +158,9 @@ int action_graph (void) /* {{{ */
   graph_instance_t *inst;
   int status;
 
+  int argc;
+  char **argv;
+
   cfg = gl_graph_get_selected ();
   if (cfg == NULL)
     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
@@ -212,31 +169,46 @@ int action_graph (void) /* {{{ */
   if (inst == NULL)
     OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
 
-  data.args = array_create ();
+  data.args = ra_create ();
   if (data.args == NULL)
     return (ENOMEM);
 
-  array_append (data.args, "graph");
-  array_append (data.args, "-");
-  array_append (data.args, "--imgformat");
-  array_append (data.args, "PNG");
+  array_append (data.args->options, "graph");
+  array_append (data.args->options, "-");
+  array_append (data.args->options, "--imgformat");
+  array_append (data.args->options, "PNG");
 
-  get_time_args (&data);
+  status = get_time_args (&data.begin, &data.end, &data.now);
+  if (status == 0)
+  {
+    array_append (data.args->options, "-s");
+    array_append_format (data.args->options, "%li", data.begin);
+    array_append (data.args->options, "-e");
+    array_append_format (data.args->options, "%li", data.end);
+  }
 
   status = inst_get_rrdargs (cfg, inst, data.args);
   if (status != 0)
   {
-    array_destroy (data.args);
+    ra_destroy (data.args);
     OUTPUT_ERROR ("inst_get_rrdargs failed with status %i.\n", status);
   }
 
+  argc = ra_argc (data.args);
+  argv = ra_argv (data.args);
+  if ((argc < 0) || (argv == NULL))
+  {
+    ra_destroy (data.args);
+    return (-1);
+  }
+
   rrd_clear_error ();
-  data.info = rrd_graph_v (array_argc (data.args), array_argv (data.args));
+  data.info = rrd_graph_v (argc, argv);
   if ((data.info == NULL) || rrd_test_error ())
   {
     printf ("Content-Type: text/plain\n\n");
     printf ("rrd_graph_v failed: %s\n", rrd_get_error ());
-    emulate_graph (array_argc (data.args), array_argv (data.args));
+    emulate_graph (argc, argv);
   }
   else
   {
@@ -262,7 +234,8 @@ int action_graph (void) /* {{{ */
   if (data.info != NULL)
     rrd_info_free (data.info);
 
-  array_destroy (data.args);
+  ra_argv_free (argv);
+  ra_destroy (data.args);
   data.args = NULL;
 
   return (0);