IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 12, 2013, 3:29:45 PM (13 years ago)
Author:
bills
Message:

sript, ippTools, and database changes to support queuing staticsky runs
on the basis of lapGroups collections of lapRuns that have finished for
each of a list of filters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/laptool.c

    r34081 r35308  
    3434static bool inactiveexpMode(pxConfig *config);
    3535
     36// Groups
     37static bool definegroupMode(pxConfig *config);
     38static bool pendinggroupMode(pxConfig *config);
     39static bool filtersforgroupMode(pxConfig *config);
     40static bool updategroupMode(pxConfig *config);
     41static bool revertgroupMode(pxConfig *config);
     42static bool listgroupMode(pxConfig *config);
     43
    3644# define MODECASE(caseName, func) \
    3745  case caseName: \
     
    6775   
    6876    MODECASE(LAPTOOL_MODE_INACTIVEEXP,   inactiveexpMode);
     77
     78    MODECASE(LAPTOOL_MODE_DEFINEGROUP,   definegroupMode);
     79    MODECASE(LAPTOOL_MODE_PENDINGGROUP,  pendinggroupMode);
     80    MODECASE(LAPTOOL_MODE_FILTERSFORGROUP, filtersforgroupMode);
     81    MODECASE(LAPTOOL_MODE_UPDATEGROUP,   updategroupMode);
     82    MODECASE(LAPTOOL_MODE_REVERTGROUP,   revertgroupMode);
     83    MODECASE(LAPTOOL_MODE_LISTGROUP,     listgroupMode);
     84
    6985  default:
    7086    psAbort("invalid option (this should not happen)");
     
    900916  return(true);
    901917}
    902 
     918// ---------------------------
     919// Group level (a collection of completed lapRuns for a given lapSequence projection cell and collection of filters
     920
     921static bool definegroupMode(pxConfig *config)
     922{
     923  PS_ASSERT_PTR_NON_NULL(config, false);
     924
     925  PXOPT_LOOKUP_S64(seq_id,          config->args, "-seq_id",    true, false);
     926  PXOPT_LOOKUP_STR(label,           config->args, "-set_label", true, false);
     927
     928  PXOPT_LOOKUP_BOOL(pretend,        config->args, "-pretend",    false);
     929  PXOPT_LOOKUP_BOOL(simple,         config->args, "-simple",    false);
     930  PXOPT_LOOKUP_U64(limit,           config->args, "-limit",     false, false);
     931
     932  // the following insures that the -filter argument has been set up properly (adapted from pxAddLabelSearchArgs)
     933  psMetadataItem *item = psMetadataLookup(config->args, "-filter");
     934  psAssert (item, "-filter argument not found in config->args");
     935  psAssert (item->type == PS_DATA_METADATA_MULTI, "%s should be a multi container", "filter");
     936  psAssert (item->data.list->n, "%s should at least have a place-holder", "filter");
     937  psMetadataItem *entry = (psMetadataItem *)item->data.list->head->data;
     938  psAssert (entry, "%s should at least have a place-holder", "filter");
     939  // end of checking
     940
     941  // Now if the ony entry is the place-holder then the user supplied no -filter arguments
     942  // which are required
     943  if (!entry->data.str) {
     944    psError(PXTOOLS_ERR_ARGUMENTS, true, "at least one -filter is required");
     945    return false;
     946  }
     947
     948  int nFilters = item->data.list->n;
     949
     950  psString query = psStringCopy("SELECT seq_id, tess_id, projection_cell,\n");
     951
     952  // We construct a query joining completed lap runs with the same lapSequence.seq_id,
     953  // tess_id, and projection_cell
     954
     955  // select the lap_ids for the various filters. These are only used for debugging.
     956  psStringAppend(&query, "lap_id_%d", 0);
     957  for (int i = 1; i < nFilters; i++) {
     958    psStringAppend(&query, ", lap_id_%d", i);
     959  }
     960
     961
     962  // sub query for each supplied filter
     963  char * lapRunForFilter = "(\nSELECT seq_id, tess_id, projection_cell, lap_id as 'lap_id_%d'\n"
     964    "FROM lapRun\n"
     965    "WHERE filter LIKE '%s'\n"
     966    "   AND lapRun.seq_id = %"PRId64"\n"
     967    "   AND (lapRun.state = 'done' or lapRun.state = 'full')\n"
     968    " ) as lap_%d\n";
     969
     970  // loop over supplied filters and flesh out the query using the format above
     971  psListIterator *iter = psListIteratorAlloc (item->data.list, PS_LIST_HEAD, true);
     972  psMetadataItem *filterItem = NULL;
     973  int i = -1;
     974  while ((filterItem = psListGetAndIncrement(iter))) {
     975    ++i;
     976    if (i == 0) {
     977      psStringAppend(&query, "\nFROM\n");
     978    } else {
     979      psStringAppend(&query, "\nJOIN\n");
     980    }
     981
     982    psString filter = filterItem->data.str;
     983    psStringAppend(&query, lapRunForFilter, i, filter, seq_id, i);
     984
     985    if (i != 0) {
     986      psStringAppend(&query, "USING (seq_id, tess_id, projection_cell)\n");
     987    }
     988  }
     989  // now join to lapGroup
     990  psStringAppend(&query, "\nLEFT JOIN lapGroup USING(seq_id, tess_id, projection_cell)\n");
     991
     992  // we only want projection cells which do not already of an entry
     993  psStringAppend(&query, "\nWHERE lapGroup.projection_cell IS NULL\n");
     994  psStringAppend(&query, "\nORDER by projection_cell\n");
     995
     996  if (limit) {
     997    psString limitString = psDBGenerateLimitSQL(limit);
     998    psStringAppend(&query, "\n %s", limitString);
     999    psFree(limitString);
     1000  }
     1001
     1002  if (!p_psDBRunQuery(config->dbh, query)) {
     1003      psError(PS_ERR_UNKNOWN, false, "database error");
     1004      psFree(query);
     1005      return false;
     1006  }
     1007  psFree(query);
     1008
     1009  psArray *output = p_psDBFetchResult(config->dbh);
     1010  if (!output) {
     1011      psError(PS_ERR_UNKNOWN, false, "database error");
     1012      return false;
     1013  }
     1014
     1015  if (!psArrayLength(output)) {
     1016      psTrace("laptool", PS_LOG_INFO, "no rows found");
     1017      psFree(output);
     1018      return true;
     1019  }
     1020
     1021  if (pretend) {
     1022    if (!ippdbPrintMetadatas(stdout, output, "new_lapGroups", !simple)) {
     1023        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1024        psFree(output);
     1025        return false;
     1026    }
     1027    psFree(output);
     1028    return true;
     1029  }
     1030
     1031  if (!psDBTransaction(config->dbh)) {
     1032    psError(PS_ERR_UNKNOWN, false, "database error");
     1033    return false;
     1034  }
     1035
     1036  psTime *now = psTimeGetNow(PS_TIME_TAI);
     1037
     1038  psString last_projection_cell = NULL;
     1039  for (long i = 0; i < psArrayLength(output); i++) {
     1040    psMetadata *row = output->data[i];
     1041
     1042    psString tess_id = psMetadataLookupStr(NULL, row, "tess_id");
     1043    psString projection_cell = psMetadataLookupStr(NULL, row, "projection_cell");
     1044    if (last_projection_cell && !strcmp(last_projection_cell, projection_cell)) {
     1045        // duplicate lap runs for a filter will generate multiple rows
     1046        // Since we care about projection_cells not lapRuns per se this is not problem.
     1047        // Skip any duplicates.
     1048        continue;
     1049    }
     1050    last_projection_cell = projection_cell;
     1051
     1052    lapGroupRow *group = lapGroupRowAlloc(
     1053                                  seq_id,
     1054                                  tess_id,
     1055                                  projection_cell,
     1056                                  "new",  // state
     1057                                  label,
     1058                                  now,    // registered
     1059                                  0       // fault
     1060                                  );
     1061    if (!group) {
     1062      psError(PS_ERR_UNKNOWN, false, "failed to alloc lapGroup object");
     1063      psFree(output);
     1064      psFree(now);
     1065      return(false);
     1066    }
     1067
     1068
     1069    if (!lapGroupInsertObject(config->dbh, group)) {
     1070      if (!psDBRollback(config->dbh)) {
     1071        psError(PS_ERR_UNKNOWN, false, "database error");
     1072      }
     1073      psError(PS_ERR_UNKNOWN, false, "database error");
     1074      psFree(output);
     1075      return(true);
     1076    }
     1077  }
     1078
     1079  // point of no return
     1080  if (!psDBCommit(config->dbh)) {
     1081    psError(PS_ERR_UNKNOWN, false, "database error");
     1082    return false;
     1083  }
     1084
     1085  psFree(now);
     1086  psFree(output);
     1087
     1088  return(true); 
     1089}
     1090
     1091static bool pendinggroupMode(pxConfig *config)
     1092{
     1093  PS_ASSERT_PTR_NON_NULL(config, false);
     1094
     1095  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1096  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
     1097 
     1098  psMetadata *where = psMetadataAlloc();
     1099  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
     1100  pxAddLabelSearchArgs(config, where, "-label", "lapRun.label", "==");
     1101 
     1102  psString query = pxDataGet("laptool_pendinggroup.sql");
     1103  if (!query) {
     1104    psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     1105    return false;
     1106  }
     1107
     1108  if (psListLength(where->list)) {
     1109    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1110    psStringAppend(&query, " AND %s", whereClause);
     1111    psFree(whereClause);
     1112  }
     1113  if (limit) {
     1114    psString limitString = psDBGenerateLimitSQL(limit);
     1115    psStringAppend(&query, "\n %s", limitString);
     1116    psFree(limitString);
     1117  }
     1118 
     1119  if (!p_psDBRunQuery(config->dbh, query)) {
     1120    psError(PS_ERR_UNKNOWN, false, "database error");
     1121    psFree(query);
     1122    return false;
     1123  }
     1124  psFree(query);
     1125
     1126  psArray *output = p_psDBFetchResult(config->dbh);
     1127  if (!output) {
     1128    psErrorCode err = psErrorCodeLast();
     1129    switch (err) {
     1130    case PS_ERR_DB_CLIENT:
     1131      psError(PXTOOLS_ERR_SYS, false, "database error");
     1132    case PS_ERR_DB_SERVER:
     1133      psError(PXTOOLS_ERR_PROG, false, "database error");
     1134    default:
     1135      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1136    }
     1137
     1138    return false;
     1139  }
     1140  if (!psArrayLength(output)) {
     1141    psTrace("laptool", PS_LOG_INFO, "no rows found");
     1142    psFree(output);
     1143    return true;
     1144  }
     1145
     1146  if (psArrayLength(output)) {
     1147    if (!ippdbPrintMetadatas(stdout, output, "lapGroup", !simple)) {
     1148      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1149      psFree(output);
     1150      return false;
     1151    }
     1152  }
     1153
     1154  psFree(output);
     1155
     1156  return true;
     1157}
     1158static bool filtersforgroupMode(pxConfig *config)
     1159{
     1160  PS_ASSERT_PTR_NON_NULL(config, false);
     1161
     1162  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1163 
     1164  psMetadata *where = psMetadataAlloc();
     1165  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
     1166  PXOPT_COPY_STR(config->args, where, "-tess_id", "tess_id", "==");
     1167  PXOPT_COPY_STR(config->args, where, "-projection_cell", "projection_cell", "==");
     1168 
     1169  psString query = pxDataGet("laptool_filtersforgroup.sql");
     1170  if (!query) {
     1171    psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     1172    return false;
     1173  }
     1174
     1175  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1176  psStringAppend(&query, " WHERE %s", whereClause);
     1177  psFree(whereClause);
     1178
     1179  if (!p_psDBRunQuery(config->dbh, query)) {
     1180    psError(PS_ERR_UNKNOWN, false, "database error");
     1181    psFree(query);
     1182    return false;
     1183  }
     1184  psFree(query);
     1185
     1186  psArray *output = p_psDBFetchResult(config->dbh);
     1187  if (!output) {
     1188    psErrorCode err = psErrorCodeLast();
     1189    switch (err) {
     1190    case PS_ERR_DB_CLIENT:
     1191      psError(PXTOOLS_ERR_SYS, false, "database error");
     1192    case PS_ERR_DB_SERVER:
     1193      psError(PXTOOLS_ERR_PROG, false, "database error");
     1194    default:
     1195      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1196    }
     1197
     1198    return false;
     1199  }
     1200  if (!psArrayLength(output)) {
     1201    psTrace("laptool", PS_LOG_INFO, "no rows found");
     1202    psFree(output);
     1203    return true;
     1204  }
     1205
     1206  if (psArrayLength(output)) {
     1207    if (!ippdbPrintMetadatas(stdout, output, "filters", !simple)) {
     1208      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1209      psFree(output);
     1210      return false;
     1211    }
     1212  }
     1213
     1214  psFree(output);
     1215
     1216  return true;
     1217}
     1218
     1219static bool updategroupMode(pxConfig *config)
     1220{
     1221  PS_ASSERT_PTR_NON_NULL(config, false);
     1222
     1223  // check that all of the keys that define a lapGroup are supplied
     1224  PXOPT_LOOKUP_S64(seq_id, config->args, "-seq_id", true, false);
     1225  PXOPT_LOOKUP_STR(tess_id, config->args, "-tess_id", true, false);
     1226  PXOPT_LOOKUP_STR(projection_cell, config->args, "-projection_cell", true, false);
     1227
     1228  psMetadata *where = psMetadataAlloc();
     1229  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
     1230  PXOPT_COPY_STR(config->args, where, "-tess_id", "tess_id", "==");
     1231  PXOPT_COPY_STR(config->args, where, "-projection_cell", "projection_cell", "==");
     1232
     1233  psMetadata *values = psMetadataAlloc();
     1234  PXOPT_COPY_STR(config->args, values, "-set_state", "state", "==");
     1235  PXOPT_COPY_STR(config->args, values, "-set_label", "label", "==");
     1236  PXOPT_COPY_S16(config->args, values, "-set_fault", "fault", "==");
     1237
     1238  if (!psListLength(values->list)) {
     1239    psFree(values);
     1240    psFree(where);
     1241    psError(PXTOOLS_ERR_ARGUMENTS, true, "must set at least one column");
     1242    return false;
     1243  }
     1244
     1245  long rows = psDBUpdateRows(config->dbh, "lapGroup", where, values);
     1246  if (rows < 1) {
     1247    psFree(values);
     1248    psError(PXTOOLS_ERR_SYS, true, "failed to update lapGroup for %" PRId64 " %s %s", seq_id, tess_id, projection_cell);
     1249    return false;
     1250  }
     1251  psFree(values);
     1252
     1253  return(true);
     1254}
     1255
     1256static bool revertgroupMode(pxConfig *config)
     1257{
     1258  PS_ASSERT_PTR_NON_NULL(config, false);
     1259
     1260  psMetadata *where = psMetadataAlloc();
     1261  pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     1262  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
     1263  PXOPT_COPY_STR(config->args, where, "-tess_id", "tess_id", "==");
     1264  PXOPT_COPY_STR(config->args, where, "-projection_cell", "projection_cell", "==");
     1265
     1266  if (!psListLength(where->list)) {
     1267    psFree(where);
     1268    psError(PXTOOLS_ERR_CONFIG, false, "search arguments are required");
     1269    return false;
     1270  }
     1271
     1272  psString query = pxDataGet("laptool_revertgroup.sql");
     1273  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1274  psStringAppend(&query, " AND %s", whereClause);
     1275  psFree(whereClause);
     1276  psFree(where);
     1277  if (!p_psDBRunQuery(config->dbh, query)) {
     1278    psError(PS_ERR_UNKNOWN, false, "database error");
     1279    psFree(query);
     1280    return false;
     1281  }
     1282  psFree(query);
     1283
     1284  psU64 affected = psDBAffectedRows(config->dbh);
     1285  psLogMsg("laptool", PS_LOG_INFO, "Updated %" PRIu64 " lapGroups", affected);
     1286
     1287  return true;
     1288}
     1289
     1290static bool listgroupMode(pxConfig *config)
     1291{
     1292  PS_ASSERT_PTR_NON_NULL(config, false);
     1293
     1294  psError(PXTOOLS_ERR_SYS, true, "not yet implemented");
     1295  return false;
     1296
     1297  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1298  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",  false, false);
     1299 
     1300  psMetadata *where = psMetadataAlloc();
     1301  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
     1302  PXOPT_COPY_STR(config->args, where, "-seq_name",   "name",   "LIKE");
     1303
     1304  psString query = pxDataGet("laptool_listsequence.sql");
     1305  if (!query) {
     1306    psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     1307    return false;
     1308  }
     1309  if (psListLength(where->list)) {
     1310    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1311    psStringAppend(&query, " WHERE %s", whereClause);
     1312    psFree(whereClause);
     1313  }
     1314
     1315  if (limit) {
     1316    psString limitString = psDBGenerateLimitSQL(limit);
     1317    psStringAppend(&query, " %s", limitString);
     1318    psFree(limitString);
     1319  }
     1320  if (!p_psDBRunQuery(config->dbh, query)) {
     1321    psError(PS_ERR_UNKNOWN, false, "database error");
     1322    psFree(query);
     1323    return false;
     1324  }
     1325  psFree(query);
     1326
     1327  psArray *output = p_psDBFetchResult(config->dbh);
     1328  if (!output) {
     1329    psErrorCode err = psErrorCodeLast();
     1330    switch (err) {
     1331    case PS_ERR_DB_CLIENT:
     1332      psError(PXTOOLS_ERR_SYS, false, "database error");
     1333    case PS_ERR_DB_SERVER:
     1334      psError(PXTOOLS_ERR_PROG, false, "database error");
     1335    default:
     1336      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1337    }
     1338
     1339    return false;
     1340  }
     1341  if (!psArrayLength(output)) {
     1342    psTrace("laptool", PS_LOG_INFO, "no rows found");
     1343    psFree(output);
     1344    return true;
     1345  }
     1346
     1347  if (psArrayLength(output)) {
     1348    if (!ippdbPrintMetadatas(stdout, output, "lapSequence", !simple)) {
     1349      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1350      psFree(output);
     1351      return false;
     1352    }
     1353  }
     1354
     1355  psFree(output);
     1356
     1357  return true;
     1358}
Note: See TracChangeset for help on using the changeset viewer.