A patch of size 44Kbytes... in short:
[rrdtool.git] / src / rrd_graph.h
1 #include "rrd_tool.h"
2 #include "rrd_rpncalc.h"
3
4 #define MAX_VNAME_LEN 29
5 #define DEF_NAM_FMT "%29[_A-Za-z0-9]"
6
7 #define ALTYGRID        0x01    /* use alternative y grid algorithm */
8 #define ALTAUTOSCALE    0x02    /* use alternative algorithm to find lower and upper bounds */
9 #define ALTAUTOSCALE_MAX 0x04   /* use alternative algorithm to find upper bounds */
10 #define NOLEGEND        0x08    /* use no legend */
11
12
13 enum tmt_en {TMT_SECOND=0,TMT_MINUTE,TMT_HOUR,TMT_DAY,
14              TMT_WEEK,TMT_MONTH,TMT_YEAR};
15
16 enum grc_en {GRC_CANVAS=0,GRC_BACK,GRC_SHADEA,GRC_SHADEB,
17              GRC_GRID,GRC_MGRID,GRC_FONT,GRC_FRAME,GRC_ARROW,__GRC_END__};
18
19
20 enum gf_en {GF_PRINT=0,GF_GPRINT,GF_COMMENT,GF_HRULE,GF_VRULE,GF_LINE1,
21             GF_LINE2,GF_LINE3,GF_AREA,GF_STACK,GF_TICK,
22             GF_DEF, GF_CDEF, GF_VDEF};
23
24 enum if_en {IF_GIF=0,IF_PNG=1};
25
26 enum vdef_op_en {
27                  VDEF_MAXIMUM   /* like the MAX in (G)PRINT */
28                 ,VDEF_MINIMUM   /* like the MIN in (G)PRINT */
29                 ,VDEF_AVERAGE   /* like the AVERAGE in (G)PRINT */
30                 ,VDEF_PERCENT   /* Nth percentile */
31                 ,VDEF_TOTAL     /* average multiplied by time */
32                 ,VDEF_FIRST     /* first non-unknown value and time */
33                 ,VDEF_LAST      /* last  non-unknown value and time */
34                 };
35 typedef struct vdef_t {
36     enum vdef_op_en     op;
37     double              param;  /* parameter for function, if applicable */
38     double              val;    /* resulting value */
39     time_t              when;   /* timestamp, if applicable */
40 } vdef_t;
41
42 typedef struct col_trip_t {
43     int red; /* red = -1 is no color */
44     int green;
45     int blue;
46     int i; /* color index assigned in gif image i=-1 is unasigned*/
47 } col_trip_t;
48
49
50 typedef struct xlab_t {
51     long         minsec;       /* minimum sec per pix */
52     enum tmt_en  gridtm;       /* grid interval in what ?*/
53     long         gridst;       /* how many whats per grid*/
54     enum tmt_en  mgridtm;      /* label interval in what ?*/
55     long         mgridst;      /* how many whats per label*/
56     enum tmt_en  labtm;        /* label interval in what ?*/
57     long         labst;        /* how many whats per label*/
58     long         precis;       /* label precision -> label placement*/
59     char         *stst;        /* strftime string*/
60 } xlab_t;
61
62 #if 0
63 xlab_t xlab[] = {
64     {0,        TMT_SECOND,30, TMT_MINUTE,5,  TMT_MINUTE,5,         0,"%H:%M"},
65     {2,        TMT_MINUTE,1,  TMT_MINUTE,5,  TMT_MINUTE,5,         0,"%H:%M"},
66     {5,        TMT_MINUTE,2,  TMT_MINUTE,10, TMT_MINUTE,10,        0,"%H:%M"},
67     {10,       TMT_MINUTE,5,  TMT_MINUTE,20, TMT_MINUTE,20,        0,"%H:%M"},
68     {30,       TMT_MINUTE,10, TMT_HOUR,1,    TMT_HOUR,1,           0,"%H:%M"},
69     {60,       TMT_MINUTE,30, TMT_HOUR,2,    TMT_HOUR,2,           0,"%H:%M"},
70     {180,      TMT_HOUR,1,    TMT_HOUR,6,    TMT_HOUR,6,           0,"%H:%M"},
71     /*{300,      TMT_HOUR,3,    TMT_HOUR,12,   TMT_HOUR,12,    12*3600,"%a %p"},  this looks silly*/
72     {600,      TMT_HOUR,6,    TMT_DAY,1,     TMT_DAY,1,      24*3600,"%a"},
73     {1800,     TMT_HOUR,12,   TMT_DAY,1,     TMT_DAY,2,      24*3600,"%a"},
74     {3600,     TMT_DAY,1,     TMT_WEEK,1,     TMT_WEEK,1,    7*24*3600,"Week %W"},
75     {3*3600,   TMT_WEEK,1,      TMT_MONTH,1,     TMT_WEEK,2,    7*24*3600,"Week %W"},
76     {6*3600,   TMT_MONTH,1,   TMT_MONTH,1,   TMT_MONTH,1, 30*24*3600,"%b"},
77     {48*3600,  TMT_MONTH,1,   TMT_MONTH,3,   TMT_MONTH,3, 30*24*3600,"%b"},
78     {10*24*3600, TMT_YEAR,1,  TMT_YEAR,1,    TMT_YEAR,1, 365*24*3600,"%y"},
79     {-1,TMT_MONTH,0,TMT_MONTH,0,TMT_MONTH,0,0,""}
80 };
81
82 /* sensible logarithmic y label intervals ...
83    the first element of each row defines the possible starting points on the
84    y axis ... the other specify the */
85
86 double yloglab[][12]= {{ 1e9, 1,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0 },
87                        {  1e3, 1,  0,  0,  0,   0,  0,  0,  0,  0,  0,  0 },
88                        {  1e1, 1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 },
89                        /* {  1e1, 1,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0 }, */
90                        {  1e1, 1,  2.5,  5,  7.5,  0,  0,  0,  0,  0,  0,  0 },
91                        {  1e1, 1,  2,  4,  6,  8,  0,  0,  0,  0,  0,  0 },
92                        {  1e1, 1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0 },
93                        {  0,   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0 }};
94
95 #endif
96
97 /* sensible y label intervals ...*/
98
99 typedef struct ylab_t {
100     double   grid;    /* grid spacing */
101     int      lfac[4]; /* associated label spacing*/
102 } ylab_t;
103
104 #if 0
105 ylab_t ylab[]= {
106     {0.1, {1,2, 5,10}},
107     {0.2, {1,5,10,20}},
108     {0.5, {1,2, 4,10}},
109     {1.0,   {1,2, 5,10}},
110     {2.0,   {1,5,10,20}},
111     {5.0,   {1,2, 4,10}},
112     {10.0,  {1,2, 5,10}},
113     {20.0,  {1,5,10,20}},
114     {50.0,  {1,2, 4,10}},
115     {100.0, {1,2, 5,10}},
116     {200.0, {1,5,10,20}},
117     {500.0, {1,2, 4,10}},
118     {0.0,   {0,0,0,0}}};
119
120
121
122 col_trip_t graph_col[] = { /* default colors */
123     {255,255,255,-1},   /* canvas */
124     {245,245,245,-1},   /* background */
125     {200,200,200,-1},   /* shade A    */
126     {150,150,150,-1},   /* shade B    */
127     {140,140,140,-1},      /* grid */
128     {130,30,30,-1},      /* major grid */
129     {0,0,0,-1},         /* font */      
130     {0,0,0,-1},         /* frame */
131     {255,0,0,-1}        /*arrow*/
132 };
133
134 #endif
135
136 /* this structure describes the elements which can make up a graph.
137    because they are quite diverse, not all elements will use all the
138    possible parts of the structure. */
139 #ifdef HAVE_SNPRINTF
140 #define FMT_LEG_LEN 200
141 #else
142 #define FMT_LEG_LEN 2000
143 #endif
144
145 typedef  struct graph_desc_t {
146     enum gf_en     gf;         /* graphing function */
147     char           vname[MAX_VNAME_LEN+1];  /* name of the variable */
148     long           vidx;       /* gdes reference */
149     char           rrd[255];   /* name of the rrd_file containing data */
150     char           ds_nam[DS_NAM_SIZE]; /* data source name */
151     long           ds;         /* data source number */
152     enum cf_en     cf;         /* consolidation function */
153     col_trip_t     col;        /* graph color */
154     char           format[FMT_LEG_LEN+5]; /* format for PRINT AND GPRINT */
155     char           legend[FMT_LEG_LEN+5]; /* legend*/
156     gdPoint        legloc;     /* location of legend */   
157     double         yrule;      /* value for y rule line and for VDEF */
158     time_t         xrule;      /* time for x rule line and for VDEF */
159     vdef_t         vf;         /* instruction for VDEF function */
160     rpnp_t         *rpnp;     /* instructions for CDEF function */
161
162     /* description of data fetched for the graph element */
163     time_t         start,end; /* timestaps for first and last data element */
164     unsigned long  step;      /* time between samples */
165     unsigned long  ds_cnt; /* how many data sources are there in the fetch */
166     long           data_first; /* first pointer to this data */
167     char           **ds_namv; /* name of datasources  in the fetch. */
168     rrd_value_t    *data; /* the raw data drawn from the rrd */
169     rrd_value_t    *p_data; /* processed data, xsize elments */
170
171 } graph_desc_t;
172
173 typedef struct image_desc_t {
174
175     /* configuration of graph */
176
177     char           graphfile[MAXPATH]; /* filename for graphic */
178     long           xsize,ysize;    /* graph area size in pixels */
179     col_trip_t     graph_col[__GRC_END__]; /* real colors for the graph */   
180     char           ylegend[200];   /* legend along the yaxis */
181     char           title[200];     /* title for graph */
182     int            draw_x_grid;      /* no x-grid at all */
183     int            draw_y_grid;      /* no x-grid at all */
184     xlab_t         xlab_user;      /* user defined labeling for xaxis */
185     char           xlab_form[200]; /* format for the label on the xaxis */
186
187     double         ygridstep;      /* user defined step for y grid */
188     int            ylabfact;       /* every how many y grid shall a label be written ? */
189
190     time_t         start,end;      /* what time does the graph cover */
191     unsigned long           step;           /* any preference for the default step ? */
192     rrd_value_t    minval,maxval;  /* extreme values in the data */
193     int            rigid;          /* do not expand range even with 
194                                       values outside */
195     char*          imginfo;         /* construct an <IMG ... tag and return 
196                                       as first retval */
197     int            lazy;           /* only update the gif if there is reasonable
198                                       probablility that the existing one is out of date */
199     int            logarithmic;    /* scale the yaxis logarithmic */
200     enum if_en     imgformat;         /* image format */
201     
202     /* status information */
203             
204     long           xorigin,yorigin;/* where is (0,0) of the graph */
205     long           xgif,ygif;      /* total size of the gif */
206     int            interlaced;     /* will the graph be interlaced? */
207     double         magfact;        /* numerical magnitude*/
208     long         base;            /* 1000 or 1024 depending on what we graph */
209     char           symbol;         /* magnitude symbol for y-axis */
210     int            unitsexponent;    /* 10*exponent for units on y-asis */
211     int            extra_flags;    /* flags for boolean options */
212     /* data elements */
213
214     long  prt_c;                  /* number of print elements */
215     long  gdes_c;                  /* number of graphics elements */
216     graph_desc_t   *gdes;          /* points to an array of graph elements */
217
218 } image_desc_t;
219
220 /* Prototypes */
221 int xtr(image_desc_t *,time_t);
222 int ytr(image_desc_t *, double);
223 enum gf_en gf_conv(char *);
224 enum if_en if_conv(char *);
225 enum tmt_en tmt_conv(char *);
226 enum grc_en grc_conv(char *);
227 int im_free(image_desc_t *);
228 void auto_scale( image_desc_t *,  double *, char **, double *);
229 void si_unit( image_desc_t *);
230 void expand_range(image_desc_t *);
231 void reduce_data( enum cf_en,  unsigned long,  time_t *, time_t *,  unsigned long *,  unsigned long *,  rrd_value_t **);
232 int data_fetch( image_desc_t *);
233 long find_var(image_desc_t *, char *);
234 long find_var_wrapper(void *arg1, char *key);
235 long lcd(long *);
236 int data_calc( image_desc_t *);
237 int data_proc( image_desc_t *);
238 time_t find_first_time( time_t,  enum tmt_en,  long);
239 time_t find_next_time( time_t,  enum tmt_en,  long);
240 void gator( gdImagePtr, int, int);
241 int print_calc(image_desc_t *, char ***);
242 int leg_place(image_desc_t *);
243 int horizontal_grid(gdImagePtr, image_desc_t *);
244 int horizontal_log_grid(gdImagePtr, image_desc_t *);
245 void vertical_grid( gdImagePtr, image_desc_t *);
246 void axis_paint( image_desc_t *, gdImagePtr);
247 void grid_paint( image_desc_t *, gdImagePtr);
248 gdImagePtr MkLineBrush(image_desc_t *,long, enum gf_en);
249 int lazy_check(image_desc_t *);
250 int graph_paint(image_desc_t *, char ***);
251 int gdes_alloc(image_desc_t *);
252 int scan_for_col(char *, int, char *);
253 int rrd_graph(int, char **, char ***, int *, int *);
254 void rrd_graph_init(image_desc_t *);
255 void rrd_graph_options(int, char **, image_desc_t *);
256 void rrd_graph_script(int, char **, image_desc_t *);
257 int rrd_graph_check_vname(image_desc_t *, char *, char *);
258 int rrd_graph_check_CF(image_desc_t *, char *, char *);
259 int bad_format(char *);
260 int vdef_parse(struct graph_desc_t *,char *);
261 int vdef_calc(image_desc_t *, int);
262 int vdef_percent_compar(const void *,const void *);