00001
00035 #include "drms.h"
00036 #include "jsoc_main.h"
00037
00038
00039 ModuleArgs_t module_args[] = {
00040 {ARG_END}
00041 };
00042
00043 char *module_name = "describe_series";
00044 #ifdef NEED_JSD_STUFF
00045 void drms_keyword_print_jsd(DRMS_Keyword_t *key) {
00046 printf("Keyword:%s",key->info->name);
00047 if (key->info->islink) {
00048 printf(", link, %s, %s, %s\n", key->info->linkname,
00049 key->info->target_key,
00050 key->info->description);
00051 } else {
00052 printf(", %s", drms_type2str(key->info->type));
00053 int stat;
00054 const char *rscope = drms_keyword_getrecscopestr(key, &stat);
00055 fprintf(stdout, ", %s", stat == DRMS_SUCCESS ? rscope : NULL);
00056 if (key->info->per_segment)
00057 printf(", segment");
00058 else
00059 printf(", record");
00060 printf(", ");
00061 if (key->info->type == DRMS_TYPE_STRING) {
00062 char qf[DRMS_MAXFORMATLEN+2];
00063 sprintf(qf, "\"%s\"", key->info->format);
00064 printf(qf, key->value.string_val);
00065 }
00066 else
00067 drms_keyword_printval(key);
00068 if (key->info->unit[0] != ' ') {
00069 printf(", %s, %s, \"%s\"", key->info->format,
00070 key->info->unit,
00071 key->info->description);
00072 } else {
00073 printf(", %s, none, \"%s\"", key->info->format,
00074 key->info->description);
00075 }
00076 }
00077 printf("\n");
00078 }
00079
00080 void drms_segment_print_jsd(DRMS_Segment_t *seg) {
00081 int i;
00082 printf("Data: %s, ", seg->info->name);
00083 if (seg->info->islink) {
00084 printf("link, %s, %s", seg->info->linkname, seg->info->target_seg);
00085 if (seg->info->naxis) {
00086 printf(", %d", seg->info->naxis);
00087 printf(", %d", seg->axis[0]);
00088 for (i=1; i<seg->info->naxis; i++) {
00089 printf(", %d", seg->axis[i]);
00090 }
00091 }
00092 } else {
00093 switch(seg->info->scope)
00094 {
00095 case DRMS_CONSTANT:
00096 printf("constant");
00097 break;
00098 case DRMS_VARIABLE:
00099 printf("variable");
00100 break;
00101 case DRMS_VARDIM:
00102 printf("vardim");
00103 break;
00104 default:
00105 printf("Illegal value: %d", (int)seg->info->scope);
00106 }
00107 printf(", %s, %d", drms_type2str(seg->info->type), seg->info->naxis);
00108 if (seg->info->naxis) {
00109 printf(", %d", seg->axis[0]);
00110 for (i=1; i<seg->info->naxis; i++) {
00111 printf(", %d", seg->axis[i]);
00112 }
00113 }
00114 printf(", %s, ", seg->info->unit);
00115 char *protstr = drms_prot2str(seg->info->protocol);
00116 if (protstr)
00117 {
00118 printf(protstr);
00119 }
00120 else
00121 {
00122 printf("Illegal protocol: %d", (int)seg->info->protocol);
00123 }
00124 }
00125 printf(", \"%s\"\n", seg->info->description);
00126 }
00127
00128 void drms_link_print_jsd(DRMS_Link_t *link) {
00129 printf("Link: %s, %s, ", link->info->name, link->info->target_series);
00130 if (link->info->type == STATIC_LINK)
00131 printf("static");
00132 else
00133 printf("dynamic");
00134 printf(", \"%s\"\n", link->info->description);
00135 }
00136
00137
00138 void print_jsd(DRMS_Record_t *rec) {
00139 const int fwidth=17;
00140 int i;
00141 HIterator_t hit;
00142 DRMS_Link_t *link;
00143 DRMS_Keyword_t *key;
00144 DRMS_Segment_t *seg;
00145
00146 printf("#=====General Series Information=====\n");
00147 printf("%-*s\t%s\n",fwidth,"Seriesname:",rec->seriesinfo->seriesname);
00148 printf("%-*s\t\"%s\"\n",fwidth,"Author:",rec->seriesinfo->author);
00149 printf("%-*s\t%s\n",fwidth,"Owner:",rec->seriesinfo->owner);
00150 printf("%-*s\t%d\n",fwidth,"Unitsize:",rec->seriesinfo->unitsize);
00151 printf("%-*s\t%d\n",fwidth,"Archive:",rec->seriesinfo->archive);
00152 printf("%-*s\t%d\n",fwidth,"Retention:",rec->seriesinfo->retention);
00153 printf("%-*s\t%d\n",fwidth,"Tapegroup:",rec->seriesinfo->tapegroup);
00154
00155 int npkeys = 0;
00156 char **extpkeys =
00157 drms_series_createpkeyarray(rec->env, rec->seriesinfo->seriesname, &npkeys, NULL);
00158 if (extpkeys && npkeys > 0)
00159 {
00160 printf("%-*s\t%s",fwidth,"Index:",extpkeys[0]);
00161 for (i=1; i<npkeys; i++)
00162 printf(", %s", extpkeys[i]);
00163 printf("\n");
00164 }
00165
00166 if (extpkeys)
00167 {
00168 drms_series_destroypkeyarray(&extpkeys, npkeys);
00169 }
00170
00171 printf("%-*s\t%s\n",fwidth,"Description:",rec->seriesinfo->description);
00172 printf("\n#=====Links=====\n");
00173 hiter_new_sort(&hit, &rec->links, drms_linke_ranksort);
00174 while( (link = (DRMS_Link_t *)hiter_getnext(&hit)) )
00175 drms_link_print_jsd(link);
00176
00177 printf("\n#=====Keywords=====\n");
00178 hiter_new_sort(&hit, &rec->keywords, drms_keyword_ranksort);
00179 while( (key = (DRMS_Keyword_t *)hiter_getnext(&hit)) )
00180 drms_keyword_print_jsd(key);
00181
00182 printf("\n#=====Segments=====\n");
00183 hiter_new_sort(&hit, &rec->segments, drms_segment_ranksort);
00184 while( (seg = (DRMS_Segment_t *)hiter_getnext(&hit)) )
00185 drms_segment_print_jsd(seg);
00186 }
00187 #endif
00188
00189 int DoIt(void) {
00190 int status;
00191 int jsd;
00192 DRMS_Record_t *template;
00193 char *series;
00194 char query[1024];
00195 DB_Text_Result_t *qres;
00196 DB_Binary_Result_t *bres;
00197
00198
00199 if (cmdparams_numargs (&cmdparams) < 2) goto usage;
00200
00201
00202 series = cmdparams_getarg(&cmdparams, 1);
00203
00204
00205 template = drms_template_record(drms_env, series, &status);
00206 if (template==NULL)
00207 {
00208 printf("Series '%s' does not exist. drms_template_record returned "
00209 "status=%d\n",series,status);
00210 return 1;
00211 }
00212
00213 jsd = cmdparams_exists(&cmdparams,"j");
00214 if (jsd) {
00215 drms_jsd_print(drms_env, series);
00216 goto done;
00217 }
00218
00219 drms_print_record(template);
00220
00221 printf("======================================================================\n");
00222 strtolower(series);
00223
00224
00225
00226
00227
00228
00229 sprintf(query, "select max(recnum) from %s",series);
00230 if ((qres = drms_query_txt(drms_env->session, query)) &&
00231 qres->num_rows>0 &&
00232 qres->field[0][0][0])
00233 {
00234 printf("Last record number = %s\n", qres->field[0][0]);
00235 }
00236 db_free_text_result(qres);
00237
00238 long long rec_cnt;
00239
00240 sprintf(query, "select count(recnum) from %s",series);
00241 if ((qres = drms_query_txt(drms_env->session, query)) && qres->num_rows>0)
00242 {
00243 printf("Number of records = %s\n", qres->field[0][0]);
00244 rec_cnt = atoll(qres->field[0][0]);
00245 }
00246 db_free_text_result(qres);
00247
00248 if (template->seriesinfo->pidx_num) {
00249 char *p = query;
00250 p += sprintf(p, "select %s from %s group by %s", (template->seriesinfo->pidx_keywords[0])->info->name, series, (template->seriesinfo->pidx_keywords[0])->info->name);
00251 for (int i=1; i<template->seriesinfo->pidx_num; i++) {
00252 p += sprintf(p, ", %s", (template->seriesinfo->pidx_keywords[i])->info->name);
00253 }
00254 if ((qres = drms_query_txt(drms_env->session, query)) && qres->num_rows>0)
00255 {
00256 printf("Number of records with distinct primary index = %u\n",qres->num_rows);
00257 }
00258 db_free_text_result(qres);
00259 }
00260
00261 sprintf(query, "select pg_size_pretty(pg_total_relation_size('%s'))", series);
00262 if ((qres = drms_query_txt(drms_env->session, query)) && qres->num_rows>0) {
00263 printf("Total disk space used: %s\n", qres->field[0][0]);
00264 }
00265 db_free_text_result(qres);
00266
00267 if (rec_cnt) {
00268 sprintf(query, "select pg_total_relation_size('%s')", series);
00269 if ((qres = drms_query_txt(drms_env->session, query)) && qres->num_rows>0) {
00270 double tsize = atoll(qres->field[0][0]);
00271 printf("Average bytes per record on disk: %.2f\n", tsize/rec_cnt);
00272 }
00273 db_free_text_result(qres);
00274 }
00275
00276 printf("Bytes per record in memory (est.) = %lld\n",
00277 drms_record_memsize(template));
00278
00279 for (int i=0; i<template->seriesinfo->pidx_num; i++) {
00280 char *name = (template->seriesinfo->pidx_keywords[i])->info->name;
00281
00282 DRMS_Type_t type = (template->seriesinfo->pidx_keywords[i])->info->type;
00283
00284 sprintf(query, "select min(%s),max(%s) from %s", name, name, series);
00285 if ((bres = drms_query_bin(drms_env->session, query)) &&
00286 bres->num_rows>0) {
00287 DRMS_Type_Value_t min, max;
00288 if (type == DRMS_TYPE_STRING) {
00289 min.string_val = NULL;
00290 max.string_val = NULL;
00291 }
00292 if (!db_binary_field_is_null(bres, 0, 0) &&
00293 !db_binary_field_is_null(bres, 0, 1)) {
00294 drms_copy_db2drms(type, &min,
00295 db_binary_column_type(bres, 0),
00296 db_binary_field_get(bres, 0, 0));
00297 drms_copy_db2drms(type, &max,
00298 db_binary_column_type(bres, 1),
00299 db_binary_field_get(bres, 0, 1));
00300 printf("%s: (", name);
00301 drms_printfval(type, &min);
00302 printf(", ");
00303 drms_printfval(type, &max);
00304 printf(")\n");
00305 if (type == DRMS_TYPE_STRING) {
00306 free(min.string_val);
00307 free(max.string_val);
00308 }
00309 }
00310 }
00311 db_free_binary_result(bres);
00312 }
00313
00314 done:
00315 return 0;
00316
00317 usage:
00318 printf("Usage: %s seriesname\n", cmdparams_getarg (&cmdparams, 0));
00319 return 1;
00320 }
00321