-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotpg.c
374 lines (311 loc) · 11.9 KB
/
plotpg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <plotpg.h>
PG_MODULE_MAGIC;
/*
* The plot function receives an SQL statement and generates a plot.
*
* It does this roughly in a five step process. Outline below:
*
* 1. execute the sql statement
*
* 2. store contents in tsv temporary file
*
* 3. write a gnuplot script based on options and parameters to a temp file.
*
* 4. invoke gnuplot to plot the temporary script created in step 2.
*
* 5. Read the output file from step 3, and return it.
*
*/
PG_FUNCTION_INFO_V1(plot);
Datum plot(PG_FUNCTION_ARGS) {
FILE *f;
SPITupleTable *coltuptable;
StringInfoData data_filename;
StringInfoData output_filename;
StringInfoData gnuplot_script_filename;
StringInfoData gnuplot_script_buf;
StringInfoData gnuplot_command;
StringInfoData plot_statement;
StringInfoData result_set_line;
StringInfoData resultbuf;
int ret;
int processed;
int i, j;
char pid_str[40];
char line[80];
char *sql_in = text_to_cstring(PG_GETARG_TEXT_PP(0));
char *gnuplot_cmds = text_to_cstring(PG_GETARG_TEXT_PP(1));
int natts = 0;
int plot_type = GNUPLOT_UNKNOWN;
/*
* Prep variables for temporary files
*/
sprintf(pid_str, "%ld", (long) getpid());
initStringInfo(&data_filename);
appendStringInfo(&data_filename, "/tmp/plotpg_%s.data", pid_str);
initStringInfo(&output_filename);
appendStringInfo(&output_filename, "/tmp/plotpg_%s.output", pid_str);
initStringInfo(&gnuplot_script_filename);
appendStringInfo(&gnuplot_script_filename, "/tmp/plotpg_%s.script", pid_str);
initStringInfo(&gnuplot_command);
appendStringInfo(&gnuplot_command, "gnuplot %s", gnuplot_script_filename.data);
initStringInfo(&result_set_line);
initStringInfo(&resultbuf);
initStringInfo(&gnuplot_script_buf);
initStringInfo(&plot_statement);
/*
*
* Execute SQL to build data file
*
*/
if ((ret = SPI_connect()) < 0) {
PG_RETURN_TEXT_P(cstring_to_text("Unable to connect to SPI"));
}
/* Retrieve the desired rows */
ret = SPI_execute(sql_in, false, 0);
processed = SPI_processed;
coltuptable = SPI_tuptable;
/* Check if everything looks ok */
if (ret != SPI_OK_SELECT) {
SPI_finish();
PG_RETURN_TEXT_P(cstring_to_text("Unable to execute statement."));
}
if (processed <= 0) {
SPI_finish();
PG_RETURN_TEXT_P(cstring_to_text("No results to plot."));
}
if (coltuptable == NULL) {
SPI_finish();
PG_RETURN_TEXT_P(cstring_to_text("Empty query result."));
}
natts = coltuptable->tupdesc->natts;
if (natts < 2) {
SPI_finish();
PG_RETURN_TEXT_P(cstring_to_text("Not sure how to plot a result set with a single column. See gnuplot() function for manual control of plotting facility."));
}
/* Set the chart type, a defined enumlist */
plot_type = infer_chart_type(processed,
SPI_gettype(coltuptable->tupdesc, 1),
SPI_gettype(coltuptable->tupdesc, 2));
/* Set the default X, Y axis labels */
appendStringInfo(&gnuplot_script_buf, "set xlabel '%s';", SPI_fname(coltuptable->tupdesc, 1));
appendStringInfo(&gnuplot_script_buf, "set ylabel '%s';", SPI_fname(coltuptable->tupdesc, 2));
/*
* Build the plot statement. Handle first field separately, then iterate
* over the fields to add additional commands.
*/
switch (plot_type) {
case GNUPLOT_TIMESERIES :
appendStringInfo(&plot_statement, "plot '%s' using 1", data_filename.data);
for(j = 2; j <= natts; j++) {
if (is_ordinal(SPI_gettype(coltuptable->tupdesc, j)))
appendStringInfo(&plot_statement, ":%d", j + 1);
}
break;
case GNUPLOT_HORIZ_BAR :
appendStringInfo(&plot_statement, "plot '%s' using 1", data_filename.data);
appendStringInfo(&plot_statement, " title '%s'", SPI_fname(coltuptable->tupdesc, 1));
for(j = 2; j <= natts; j++) {
if (is_ordinal(SPI_gettype(coltuptable->tupdesc, j)))
appendStringInfo(&plot_statement, ", '' using %d title '%s'", j, SPI_fname(coltuptable->tupdesc, j));
}
break;
case GNUPLOT_HISTOGRAM :
appendStringInfo(&plot_statement, "plot '%s' using 2:xticlabels(1)", data_filename.data);
appendStringInfoString(&gnuplot_script_buf, "set style data histogram;");
appendStringInfoString(&gnuplot_script_buf, "set style fill pattern;");
break;
case GNUPLOT_SCATTER :
appendStringInfo(&plot_statement, "plot '%s' using 1", data_filename.data);
for(j = 2; j <= natts; j++) {
if (is_ordinal(SPI_gettype(coltuptable->tupdesc, j)))
appendStringInfo(&plot_statement, ":%d", j);
}
appendStringInfoString(&gnuplot_script_buf, "set style data points;");
break;
}
/* No key if 1 dependent variable*/
if (natts < 3) {
appendStringInfoString(&gnuplot_script_buf, "set key off;");
}
/* Iterate over result set writing to file */
f = fopen(data_filename.data, "w");
if (f == NULL) {
fclose(f);
SPI_finish();
PG_RETURN_TEXT_P(cstring_to_text("Unable to open tmp file to write data."));
}
/* Poor man's COPY */
for(i = 0; i < processed; i++) {
for(j = 1; j <= natts; j++) {
// append the value to the line for writing to the file.
if (SPI_getvalue(coltuptable->vals[i], coltuptable->tupdesc, j) != NULL) {
appendStringInfoString(&result_set_line, SPI_getvalue(coltuptable->vals[i], coltuptable->tupdesc, j));
}
appendStringInfo(&result_set_line, "\t"); //delimiter
}
// write the line to the file and reset the buffer
appendStringInfo(&result_set_line, "\n");
fprintf(f, "%s", result_set_line.data);
resetStringInfo(&result_set_line);
}
fclose(f);
SPI_finish();
/*
*
* Write Gnuplot script.
*
* Add manually set variables from GUCs, gnuplot commands passed as an
* argument, and the explicit plot command. Note, some gnuplot commands
* were likely already added when examining the data while writing it
* to a gnuplot compatible tsv file in the execution section above.
*
*/
/* Add gnuplot commands based on the plot_type */
switch (plot_type) {
case GNUPLOT_HORIZ_BAR :
appendStringInfoString(&gnuplot_script_buf, "set style data histogram;");
appendStringInfoString(&gnuplot_script_buf, "set style histogram rowstacked;");
appendStringInfoString(&gnuplot_script_buf, "set xlabel '';");
appendStringInfoString(&gnuplot_script_buf, "set ylabel '';");
appendStringInfoString(&gnuplot_script_buf, "unset border;");
appendStringInfoString(&gnuplot_script_buf, "unset xtics;");
appendStringInfoString(&gnuplot_script_buf, "set xtics format ' ';");
appendStringInfoString(&gnuplot_script_buf, "unset ytics;");
appendStringInfoString(&gnuplot_script_buf, "set ytics format ' ';");
appendStringInfoString(&gnuplot_script_buf, "set style fill solid border -1;");
appendStringInfoString(&gnuplot_script_buf, "set xrange [-1:2];");
break;
case GNUPLOT_TIMESERIES :
/*
* Timeseries: If the first attribute is a time formatted type, set the
* date format for the x axis to be marked as a time data, as well as
* the timefmt so gnuplot can read the data and convert to internal
* gnuplot-psuedo-epoch-time.
*/
appendStringInfoString(&gnuplot_script_buf, "set xdata time;");
appendStringInfoString(&gnuplot_script_buf, "set timefmt '%Y-%m-%d %H:%M:%S';");
appendStringInfoString(&gnuplot_script_buf, "set format x '%H:%M:%S';");
appendStringInfoString(&gnuplot_script_buf, "set xtics rotate by -45 offset -1;");
appendStringInfoString(&gnuplot_script_buf, "set xtics nomirror;");
appendStringInfoString(&gnuplot_script_buf, "set ytics nomirror;");
appendStringInfoString(&gnuplot_script_buf, "set style data lines;");
break;
case GNUPLOT_HISTOGRAM :
appendStringInfoString(&gnuplot_script_buf, "set style data histogram;");
appendStringInfoString(&gnuplot_script_buf, "set style fill pattern;");
break;
case GNUPLOT_SCATTER :
appendStringInfoString(&gnuplot_script_buf, "set style data points;");
break;
default :
PG_RETURN_TEXT_P(cstring_to_text("Unsure how to plot data set."));
}
/* Apply GUC variables to respective gnuplot commands */
if (strcmp(gnuplot_terminal, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set terminal %s;", gnuplot_terminal);
if (strcmp(gnuplot_size, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set size %s;", gnuplot_size);
if (strcmp(gnuplot_title, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set title %s;", gnuplot_title);
if (strcmp(gnuplot_xlabel, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set xlabel %s;", gnuplot_xlabel);
if (strcmp(gnuplot_ylabel, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set ylabel %s;", gnuplot_ylabel);
if (strcmp(gnuplot_xtics, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set xtics %s;", gnuplot_xtics);
if (strcmp(gnuplot_ytics, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set ytics %s;", gnuplot_ytics);
if (strcmp(gnuplot_key, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set key %s;", gnuplot_key);
if (strcmp(gnuplot_border, "") != 0)
appendStringInfo(&gnuplot_script_buf, "set border %s;", gnuplot_border);
appendStringInfo(&gnuplot_script_buf, "set output '%s';", output_filename.data);
appendStringInfoString(&gnuplot_script_buf, gnuplot_cmds);
appendStringInfoString(&gnuplot_script_buf, plot_statement.data);
f = fopen(gnuplot_script_filename.data, "w");
if (f == NULL) {
PG_RETURN_TEXT_P(cstring_to_text("Unable to write temporary gnuplot script file in /tmp"));
}
fprintf(f, "%s", gnuplot_script_buf.data);
fclose(f);
/*
* Execute Gnuplot script.
*
* Call the gnuplot binary to run the gnuplot script generated above.
*
*/
ret = system(gnuplot_command.data);
if (ret < 0) {
PG_RETURN_TEXT_P(cstring_to_text("There was an error running the gnuplot command"));
}
/*
* Read and return the output from running the generated gnuplot script
*/
f = fopen(output_filename.data, "rb");
i = 0;
while(fgets(line, 80, f) != NULL) {
// skip first line which has a bogus character in dumb mode
if ((strstr(gnuplot_terminal, "dumb") != gnuplot_terminal) || i > 0) {
appendStringInfoString(&resultbuf, line);
}
i++;
}
fclose(f);
/* Cleanup */
if (plotpg_persist < 1) {
remove(gnuplot_script_filename.data);
remove(data_filename.data);
remove(output_filename.data);
}
PG_RETURN_TEXT_P(cstring_to_text(resultbuf.data));
}
/*
* Make a determination on how to plot the result set based on some rigid
* heuristics. The rules are based on the number of rows in the result set
* and the data type of the first and second columns.
*
* 1. Number of records in result set
*
* single row -> Horizontal Stacked Bar
*
* 2. Values of first column / Second column
*
* timestamp / ordinal -> Timeseries
* timestamptz / ordinal -> Timeseries
*
* text / ordinal -> Histogram
*
* numeric / ordinal -> Scatter
* int / ordinal -> Scatter
* ... ordinals / ordinal -> Scatter
*
*
* If doesn't match, then give up or just try whatever. Remember, users
* are always free to override heuristic based commands by supplying their
* own commands to plot, or even using the more manual gnuplot() function.
*/
int infer_chart_type(int n_records, char* first_col_field_type, char* second_col_field_type) {
if (n_records == 1) {
return GNUPLOT_HORIZ_BAR;
} else if (strcmp(first_col_field_type, "timestamp") == 0 ||
strcmp(first_col_field_type, "timestamptz") == 0) {
return GNUPLOT_TIMESERIES;
} else if (strcmp(first_col_field_type, "text") == 0 && (is_ordinal(second_col_field_type))) {
return GNUPLOT_HISTOGRAM;
} else if (is_ordinal(first_col_field_type) && is_ordinal(second_col_field_type)) {
return GNUPLOT_SCATTER;
}
return GNUPLOT_UNKNOWN;
}
bool is_ordinal(char* field_type) {
if (strcmp(field_type, "numeric") == 0 ||
strcmp(field_type, "real") == 0 ||
strcmp(field_type, "float") == 0 ||
strcmp(field_type, "bigint") == 0 ||
strstr(field_type, "int") == field_type ||
strcmp(field_type, "decimal") == 0) {
return true;
}
return false;
}