00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include "jsoc_main.h"
00069 #include "json.h"
00070
00071
00072 char *module_name = "drms_parsekeys";
00073
00074 typedef enum
00075 {
00076 PKSTAT_SUCCESS = 0,
00077 PKSTAT_FILEIO,
00078 PKSTAT_NOMEM,
00079 PKSTAT_CANTMODSERIES,
00080 PKSTAT_BADKEYDESC,
00081 PKSTAT_NOTIMPL
00082 } PKSTAT_t;
00083
00084 #define SERIES "series"
00085 #define SPEC "spec"
00086 #define DOIT "d"
00087
00088 ModuleArgs_t module_args[] =
00089 {
00090 {ARG_STRING, SERIES, NULL, "The series containing the keywords to modify."},
00091 {ARG_STRING, SPEC, NULL, "The keyword specification(s). This can be a JSD keyword description, or a path to a file that contains JSD keyword descriptions, one per line."},
00092 {ARG_FLAG, DOIT, NULL, "Commit the changes to the DRMS database."},
00093 {ARG_END}
00094 };
00095
00096
00097 static void sprintDefVal(DRMS_Keyword_t *key, char *defvalout, size_t size)
00098 {
00099 char defval[DRMS_DEFVAL_MAXLEN];
00100
00101 if (defvalout)
00102 {
00103 if (key->info->type == DRMS_TYPE_TIME)
00104 {
00105 drms_sprintfval(defval, key->info->type, &key->value, 1);
00106 }
00107 else
00108 {
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 drms_sprintfval(defval, key->info->type, &key->value, 0);
00121
00122
00123
00124 if (key->info->type == DRMS_TYPE_STRING)
00125 {
00126 size_t slen = strlen(defval);
00127 int ich;
00128
00129 if (slen > 1)
00130 {
00131 if ((defval[0] == '\'' && defval[slen - 1] == '\'') ||
00132 (defval[0] == '\"' && defval[slen - 1] == '\"'))
00133 {
00134 ich = 0;
00135 while(ich < slen - 2)
00136 {
00137 defval[ich] = defval[ich + 1];
00138 ich++;
00139 }
00140
00141 defval[ich] = '\0';
00142 }
00143 }
00144 }
00145 }
00146
00147 snprintf(defvalout, size, "%s", defval);
00148 }
00149 }
00150
00151 void escapePercent(const char *unescaped, char *escaped, size_t size)
00152 {
00153 const char *pCh = NULL;
00154 char *pEscaped = NULL;
00155
00156 for (pCh = unescaped, pEscaped = escaped; pCh && *pCh && pEscaped - escaped < size; pCh++)
00157 {
00158 if (*pCh == '%')
00159 {
00160 *pEscaped++ = '%';
00161 *pEscaped++ = '%';
00162 }
00163 else
00164 {
00165 *pEscaped++ = *pCh;
00166 }
00167 }
00168
00169 *pEscaped = '\0';
00170 }
00171
00172 int DoIt(void)
00173 {
00174 PKSTAT_t rv = PKSTAT_SUCCESS;
00175 int drmsstat = DRMS_SUCCESS;
00176
00177 const char *series = NULL;
00178 const char *spec = NULL;
00179 char serieslc[DRMS_MAXSERIESNAMELEN];
00180 struct stat stBuf;
00181 FILE *fptr = NULL;
00182 char *specin = NULL;
00183 json_t *root = NULL;
00184
00185 series = cmdparams_get_str(&cmdparams, SERIES, NULL);
00186 spec = cmdparams_get_str(&cmdparams, SPEC, NULL);
00187
00188 root = json_new_object();
00189
00190 if (!stat(spec, &stBuf) && S_ISREG(stBuf.st_mode))
00191 {
00192
00193 char rbuf[512];
00194 size_t nbytes;
00195 size_t ntot = 0;
00196 size_t szspec;
00197
00198 fptr = fopen(spec, "r");
00199 if (fptr)
00200 {
00201 szspec = 1024;
00202 specin = calloc(1, szspec);
00203
00204 if (specin)
00205 {
00206 while ((nbytes = fread(rbuf, sizeof(char), sizeof(rbuf) - 1, fptr)) > 0)
00207 {
00208 rbuf[nbytes] = '\0';
00209 ntot += nbytes;
00210 specin = base_strcatalloc(specin, rbuf, &szspec);
00211 }
00212
00213 if (ntot != stBuf.st_size)
00214 {
00215 fprintf(stderr, "Error reading file %s.\n", spec);
00216 rv = PKSTAT_FILEIO;
00217 }
00218 }
00219 else
00220 {
00221 rv = PKSTAT_NOMEM;
00222 }
00223
00224 fclose(fptr);
00225 }
00226 else
00227 {
00228 fprintf(stderr, "Unable to open file %s for reading.\n", spec);
00229 rv = PKSTAT_FILEIO;
00230 }
00231 }
00232 else
00233 {
00234
00235 specin = strdup(spec);
00236 }
00237
00238 if (rv == PKSTAT_SUCCESS)
00239 {
00240 HContainer_t *keys = NULL;
00241 DRMS_Record_t *seriesTempl = NULL;
00242 int nkeys;
00243
00244 seriesTempl = drms_template_record(drms_env, series, &drmsstat);
00245
00246 if (!seriesTempl || drmsstat != DRMS_SUCCESS)
00247 {
00248 rv = PKSTAT_CANTMODSERIES;
00249 fprintf(stderr, "Series '%s'does not exist.\n", series);
00250 }
00251
00252 if (rv == PKSTAT_SUCCESS)
00253 {
00254 keys = drms_parse_keyworddesc(drms_env, specin, &drmsstat);
00255 if (!keys || drmsstat != DRMS_SUCCESS)
00256 {
00257 rv = PKSTAT_BADKEYDESC;
00258 fprintf(stderr, "Unable to parse keyword description. Please fix syntactic errors are re-try.\n");
00259 }
00260 }
00261
00262 if (rv == PKSTAT_SUCCESS)
00263 {
00264
00265 HIterator_t *hit = NULL;
00266 DRMS_Keyword_t *key = NULL;
00267 DRMS_Keyword_t *origKey = NULL;
00268 char defval[DRMS_DEFVAL_MAXLEN];
00269 json_t *seriesObj = NULL;
00270 json_t *stableArr = NULL;
00271 json_t *ktableArr = NULL;
00272 json_t *ktableKeyObj = NULL;
00273 json_t *jObj = NULL;
00274 char *namespace = NULL;
00275 char *table = NULL;
00276 char drmskeyTable[DRMS_MAXSERIESNAMELEN];
00277 char keylc[DRMS_MAXKEYNAMELEN];
00278 char numBuf[64];
00279 char formatBuf[128];
00280 int kwflags;
00281
00282 hit = hiter_create(keys);
00283
00284 if (hit)
00285 {
00286 nkeys = 0;
00287
00288 snprintf(serieslc, sizeof(serieslc), "%s", series);
00289 strtolower(serieslc);
00290
00291 get_namespace(serieslc, &namespace, &table);
00292 snprintf(drmskeyTable, sizeof(drmskeyTable), "%s.%s",namespace, DRMS_MASTER_KEYWORD_TABLE);
00293 free(namespace);
00294 namespace = NULL;
00295 free(table);
00296 table = NULL;
00297
00298
00299
00300 seriesObj = json_new_object();
00301 json_insert_pair_into_object(root, series, seriesObj);
00302
00303 stableArr = json_new_array();
00304 json_insert_pair_into_object(seriesObj, serieslc, stableArr);
00305
00306 ktableArr = json_new_array();
00307 json_insert_pair_into_object(seriesObj, drmskeyTable, ktableArr);
00308
00309
00310
00311 while((key = (DRMS_Keyword_t *)hiter_getnext(hit)) != NULL)
00312 {
00313
00314 if (!(origKey = drms_keyword_lookup(seriesTempl, key->info->name, 0)))
00315 {
00316 fprintf(stderr, "Unable to parse description for keyword '%s'; it does not exist. Skipping.\n", key->info->name);
00317 continue;
00318 }
00319
00320 nkeys++;
00321
00322 snprintf(keylc, sizeof(keylc), "%s", key->info->name);
00323 strtolower(keylc);
00324
00325
00326
00327
00328 jObj = json_new_object();
00329 json_insert_pair_into_object(jObj, keylc, json_new_string(db_type_string(drms2dbtype(key->info->type))));
00330 json_insert_child(stableArr, jObj);
00331
00332
00333 ktableKeyObj = json_new_object();
00334 jObj = json_new_object();
00335 json_insert_pair_into_object(jObj, key->info->name, ktableKeyObj);
00336 json_insert_child(ktableArr, jObj);
00337
00338
00339 json_insert_pair_into_object(ktableKeyObj, "seriesname", json_new_string(series));
00340
00341
00342 json_insert_pair_into_object(ktableKeyObj, "keywordname", json_new_string(key->info->name));
00343
00344
00345 json_insert_pair_into_object(ktableKeyObj, "linkname", json_new_string(key->info->linkname));
00346
00347
00348 json_insert_pair_into_object(ktableKeyObj, "targetkeyw", json_new_string(key->info->target_key));
00349
00350
00351 json_insert_pair_into_object(ktableKeyObj, "type", json_new_string(drms_type2str(key->info->type)));
00352
00353 sprintDefVal(key, defval, sizeof(defval));
00354
00355 json_insert_pair_into_object(ktableKeyObj, "defaultval", json_new_string(defval));
00356
00357
00358
00359 escapePercent(key->info->format, formatBuf, sizeof(formatBuf));
00360 json_insert_pair_into_object(ktableKeyObj, "format", json_new_string(formatBuf));
00361
00362
00363 json_insert_pair_into_object(ktableKeyObj, "unit", json_new_string(key->info->unit));
00364
00365
00366 snprintf(numBuf, sizeof(numBuf), "%d", key->info->islink);
00367 json_insert_pair_into_object(ktableKeyObj, "islink", json_new_string(numBuf));
00368
00369
00370 snprintf(numBuf, sizeof(numBuf), "%d", (int)key->info->recscope);
00371 json_insert_pair_into_object(ktableKeyObj, "isconstant", json_new_string(numBuf));
00372
00373
00374
00375
00376
00377 kwflags = (origKey->info->kwflags & 0xFFFF0000) | (key->info->kwflags & 0x0000FFFF);
00378 snprintf(numBuf, sizeof(numBuf), "%d", kwflags);
00379 json_insert_pair_into_object(ktableKeyObj, "persegment", json_new_string(numBuf));
00380
00381
00382 json_insert_pair_into_object(ktableKeyObj, "description", json_new_string(key->info->description));
00383 }
00384
00385 hiter_destroy(&hit);
00386 }
00387 else
00388 {
00389 rv = PKSTAT_NOMEM;
00390 }
00391 }
00392 }
00393
00394 if (rv == PKSTAT_SUCCESS)
00395 {
00396 char *jsonStr = NULL;
00397
00398 json_tree_to_string(root, &jsonStr);
00399 printf(jsonStr);
00400 free(jsonStr);
00401 printf("\n");
00402 }
00403
00404 if (root)
00405 {
00406 json_free_value(&root);
00407 }
00408
00409 if (specin)
00410 {
00411 free(specin);
00412 }
00413
00414 return rv;
00415 }