1 rick 1.1 /*
2 * smpl_02.c $DRMS/proj/cookbook/
3 *
4 * Prints a list of the series names known to DRMS, with record counts
5 * for each
6 * Illustrates the connection to the DRMS database and ways that SQL
7 * queries can be directly run and the results analyzed at the lowest
8 * level of the DRMS API
9 *
10 * Usage:
11 * smpl_02 [nmax= ...]
12 *
13 * Revision history is at end of file.
14 */
15
16 char *module_name = "CookbookRecipe:02";
17 char *version_id = "1.0";
18
19 #include <jsoc_main.h>
20 #include <regex.h>
21
22 rick 1.1 ModuleArgs_t module_args[] = {
23 {ARG_INT, "nmax", "100", "maximum number of series to be listed"},
24 {ARG_END}
25 };
26
27 int DoIt (void) {
28 CmdParams_t *params = &cmdparams;
29 DB_Text_Result_t *qres, *sqres;
30 int series, seriesct;
31 char query[DRMS_MAXQUERYLEN];
32
33 int nmax = params_get_int (params, "nmax");
34 /* Query the database to get all series names from the master list */
35 sprintf (query, "select seriesname from %s()", DRMS_MASTER_SERIES_TABLE);
36 if ((qres = drms_query_txt (drms_env->session, query)) == NULL) {
37 fprintf (stderr, "Cant find DRMS\n");
38 return 1;
39 }
40 seriesct = qres->num_rows;
41 printf ("%d series found", seriesct);
42 if (seriesct > nmax) {
43 rick 1.1 seriesct = nmax;
44 printf (" (only the first %d will be listed)", seriesct);
45 }
46 printf ("\n");
47
48 for (series = 0; series < seriesct; series++) {
49 char *seriesname = qres->field[series][0];
50 printf ("%s\t", seriesname);
51 sprintf (query, "select count (recnum) from %s", seriesname);
52 /* Query the database to get the record count from the series table */
53 /* (every data series must have a "recnum" field) */
54 if (sqres = drms_query_txt (drms_env->session, query)) {
55 printf ("%s", sqres->field[0][0]);
56 db_free_text_result (sqres);
57 } else printf ("?");
58 printf ("\n");
59 }
60
61 db_free_text_result (qres);
62 return 0;
63 }
64 rick 1.1
65 /*
66 * Revision History
67 *
68 * 09.04.20 file created by R Bogart
69 */
|