share/collection.js: Keep visibility when redrawing graphs.
[collection4.git] / src / action_graph.c
index a0f257e..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>
 #include <fcgi_stdio.h>
 
+struct graph_data_s
+{
+  rrd_args_t *args;
+  rrd_info_t *info;
+  time_t mtime;
+  time_t expires;
+  long now;
+  long begin;
+  long end;
+};
+typedef struct graph_data_s graph_data_t;
+
 static void emulate_graph (int argc, char **argv) /* {{{ */
 {
   int i;
@@ -51,11 +90,14 @@ static int ag_info_print (rrd_info_t *info) /* {{{ */
   return (0);
 } /* }}} int ag_info_print */
 
-static int output_graph (rrd_info_t *info) /* {{{ */
+static int output_graph (graph_data_t *data) /* {{{ */
 {
   rrd_info_t *img;
+  char time_buffer[256];
+  time_t expires;
+  int status;
 
-  for (img = info; img != NULL; img = img->next)
+  for (img = data->info; img != NULL; img = img->next)
     if ((strcmp ("image", img->key) == 0)
         && (img->type == RD_I_BLO))
       break;
@@ -64,9 +106,39 @@ static int output_graph (rrd_info_t *info) /* {{{ */
     return (ENOENT);
 
   printf ("Content-Type: image/png\n"
-      "Content-Length: %lu\n"
-      "\n",
+      "Content-Length: %lu\n",
       img->value.u_blo.size);
+  if (data->mtime > 0)
+  {
+    int status;
+    
+    status = time_to_rfc1123 (data->mtime, time_buffer, sizeof (time_buffer));
+    if (status == 0)
+      printf ("Last-Modified: %s\n", time_buffer);
+  }
+
+  /* Print Expires header. */
+  if (data->end >= data->now)
+  {
+    /* The end of the timespan can be seen. */
+    long secs_per_pixel;
+
+    /* FIXME: Handle graphs with width != 400. */
+    secs_per_pixel = (data->end - data->begin) / 400;
+
+    expires = (time_t) (data->now + secs_per_pixel);
+  }
+  else /* if (data->end < data->now) */
+  {
+    expires = (time_t) (data->now + 86400);
+  }
+  status = time_to_rfc1123 (expires, time_buffer, sizeof (time_buffer));
+  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,
       /* nmemb = */ 1, stdout);
 
@@ -81,12 +153,14 @@ static int output_graph (rrd_info_t *info) /* {{{ */
 
 int action_graph (void) /* {{{ */
 {
-  str_array_t *args;
+  graph_data_t data;
   graph_config_t *cfg;
   graph_instance_t *inst;
-  rrd_info_t *info;
   int status;
 
+  int argc;
+  char **argv;
+
   cfg = gl_graph_get_selected ();
   if (cfg == NULL)
     OUTPUT_ERROR ("gl_graph_get_selected () failed.\n");
@@ -95,35 +169,54 @@ int action_graph (void) /* {{{ */
   if (inst == NULL)
     OUTPUT_ERROR ("inst_get_selected (%p) failed.\n", (void *) cfg);
 
-  args = array_create ();
-  if (args == NULL)
+  data.args = ra_create ();
+  if (data.args == NULL)
     return (ENOMEM);
 
-  array_append (args, "graph");
-  array_append (args, "-");
-  array_append (args, "--imgformat");
-  array_append (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");
 
-  status = inst_get_rrdargs (cfg, inst, args);
+  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 (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 ();
-  info = rrd_graph_v (array_argc (args), array_argv (args));
-  if ((info == NULL) || rrd_test_error ())
+  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 (args), array_argv (args));
+    emulate_graph (argc, argv);
   }
   else
   {
     int status;
 
-    status = output_graph (info);
+    data.mtime = inst_get_mtime (inst);
+
+    status = output_graph (&data);
     if (status != 0)
     {
       rrd_info_t *ptr;
@@ -131,18 +224,19 @@ int action_graph (void) /* {{{ */
       printf ("Content-Type: text/plain\n\n");
       printf ("output_graph failed. Maybe the \"image\" info was not found?\n\n");
 
-      for (ptr = info; ptr != NULL; ptr = ptr->next)
+      for (ptr = data.info; ptr != NULL; ptr = ptr->next)
       {
         ag_info_print (ptr);
       }
     }
   }
 
-  if (info != NULL)
-    rrd_info_free (info);
+  if (data.info != NULL)
+    rrd_info_free (data.info);
 
-  array_destroy (args);
-  args = NULL;
+  ra_argv_free (argv);
+  ra_destroy (data.args);
+  data.args = NULL;
 
   return (0);
 } /* }}} int action_graph */