ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/JSOC/proj/cookbook/smpl_03.c
(Generate patch)

Comparing proj/cookbook/smpl_03.c (file contents):
Revision 1.1 by rick, Mon Apr 20 22:27:07 2009 UTC vs.
Revision 1.2 by rick, Mon Jul 27 23:42:09 2009 UTC

# Line 1 | Line 1
1   /*
2   *  smpl_03.c                                           $DRMS/proj/cookbook/
3   *
4 < *  Prints the number of unique records in the selected data series, and
5 < *    the defined number of data segments per record for the series
6 < *  Illustrates features of the DRMS_Record struct, and concept of uniqueness
7 < *    for DRMS records
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_03 ds= ...
11 > *    smpl_03 [nmax= ...]
12   *
13   *  Revision history is at end of file.
14   */
# Line 19 | Line 20 | char *version_id = "1.0";
20   #include <regex.h>
21  
22   ModuleArgs_t module_args[] = {
23 <  {ARG_STRING,  "ds", "", "name of data series"},
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;
29 <  DRMS_Record_t *record;
30 <  regmatch_t pmatch[10];
29 >  DB_Text_Result_t *qres, *sqres;
30    int series, seriesct;
32  int status = 0;
31    char query[DRMS_MAXQUERYLEN];
32  
33 <  char *ds = params_get_str (params, "ds");
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 >    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 <  record = drms_template_record (drms_env, ds, &status);
62 <  if (record && !status) {
39 <    if (record->seriesinfo->pidx_num) {
40 <      char qry[1024];
41 <      sprintf (query, "select %s from %s group by %s",
42 <          (record->seriesinfo->pidx_keywords[0])->info->name, ds,
43 <          (record->seriesinfo->pidx_keywords[0])->info->name);
44 <      for (int i=1; i<record->seriesinfo->pidx_num; i++) {
45 <        sprintf (qry, ", %s",
46 <            (record->seriesinfo->pidx_keywords[i])->info->name);
47 <        strcat (query, qry);
48 <      }
49 <      if ((qres = drms_query_txt (drms_env->session, query))) {
50 <        printf ("%s contains %d unique records", ds, qres->num_rows);
51 <        db_free_text_result (qres);
52 <      }
53 <    } else printf ("%s: no records found", ds);
54 <    printf (", with %d data segment(s) per record\n", record->segments.num_total);
55 <    drms_free_record (record);
56 <  } else printf ("%s: no such series\n", ds);
57 <
58 <  return status;
61 >  db_free_text_result (qres);
62 >  return 0;
63   }
64  
65   /*

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines