(file) Return to smpl_03.c CVS log (file) (dir) Up to [Development] / JSOC / proj / cookbook

Diff for /JSOC/proj/cookbook/smpl_03.c between version 1.1 and 1.2

version 1.1, 2009/04/20 22:27:07 version 1.2, 2009/07/27 23:42:09
Line 1 
Line 1 
 /* /*
  *  smpl_03.c                                           $DRMS/proj/cookbook/  *  smpl_03.c                                           $DRMS/proj/cookbook/
  *  *
  *  Prints the number of unique records in the selected data series, and   *  Prints a list of the series names known to DRMS, with record counts
  *    the defined number of data segments per record for the series   *    for each
  *  Illustrates features of the DRMS_Record struct, and concept of uniqueness   *  Illustrates the connection to the DRMS database and ways that SQL
  *    for DRMS records   *    queries can be directly run and the results analyzed at the lowest
    *    level of the DRMS API
  *  *
  *  Usage:  *  Usage:
  *    smpl_03 ds= ...   *    smpl_03 [nmax= ...]
  *  *
  *  Revision history is at end of file.  *  Revision history is at end of file.
  */  */
Line 19  char *version_id = "1.0";
Line 20  char *version_id = "1.0";
 #include <regex.h> #include <regex.h>
  
 ModuleArgs_t module_args[] = { ModuleArgs_t module_args[] = {
   {ARG_STRING,  "ds", "", "name of data series"},    {ARG_INT,     "nmax", "100", "maximum number of series to be listed"},
   {ARG_END}   {ARG_END}
 }; };
  
 int DoIt (void) { int DoIt (void) {
   CmdParams_t *params = &cmdparams;   CmdParams_t *params = &cmdparams;
   DB_Text_Result_t *qres;    DB_Text_Result_t *qres, *sqres;
   DRMS_Record_t *record;  
   regmatch_t pmatch[10];  
   int series, seriesct;   int series, seriesct;
   int status = 0;  
   char query[DRMS_MAXQUERYLEN];   char query[DRMS_MAXQUERYLEN];
  
   char *ds = params_get_str (params, "ds");    int nmax = params_get_int (params, "nmax");
           /*  Query the database to get all series names from the master list  */
   record = drms_template_record (drms_env, ds, &status);    sprintf (query, "select seriesname from %s()", DRMS_MASTER_SERIES_TABLE);
   if (record && !status) {    if ((qres = drms_query_txt (drms_env->session, query)) == NULL) {
     if (record->seriesinfo->pidx_num) {      fprintf (stderr, "Cant find DRMS\n");
       char qry[1024];      return 1;
       sprintf (query, "select %s from %s group by %s",  
           (record->seriesinfo->pidx_keywords[0])->info->name, ds,  
           (record->seriesinfo->pidx_keywords[0])->info->name);  
       for (int i=1; i<record->seriesinfo->pidx_num; i++) {  
         sprintf (qry, ", %s",  
             (record->seriesinfo->pidx_keywords[i])->info->name);  
         strcat (query, qry);  
       }       }
       if ((qres = drms_query_txt (drms_env->session, query))) {    seriesct = qres->num_rows;
         printf ("%s contains %d unique records", ds, qres->num_rows);    printf ("%d series found", seriesct);
         db_free_text_result (qres);    if (seriesct > nmax) {
       seriesct = nmax;
       printf (" (only the first %d will be listed)", seriesct);
       }       }
     } else printf ("%s: no records found", ds);    printf ("\n");
     printf (", with %d data segment(s) per record\n", record->segments.num_total);  
     drms_free_record (record);  
   } else printf ("%s: no such series\n", ds);  
  
   return status;    for (series = 0; series < seriesct; series++) {
       char *seriesname = qres->field[series][0];
       printf ("%s\t", seriesname);
       sprintf (query, "select count (recnum) from %s", seriesname);
          /*  Query the database to get the record count from the series table  */
                             /*  (every data series must have a "recnum" field) */
       if (sqres = drms_query_txt (drms_env->session, query)) {
         printf ("%s", sqres->field[0][0]);
         db_free_text_result (sqres);
       } else printf ("?");
       printf ("\n");
     }
   
     db_free_text_result (qres);
     return 0;
 } }
  
 /* /*


Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

Karen Tian
Powered by
ViewCVS 0.9.4