IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 35192


Ignore:
Timestamp:
Feb 21, 2013, 1:18:35 PM (13 years ago)
Author:
watersc1
Message:

Updated SQL and database interactions for WSdiffs. This should allow it to run in a reasonable amount of time and without blocking the database.

Location:
trunk/ippTools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/share/Makefile.am

    r34933 r35192  
    155155        difftool_completed_runs.sql \
    156156        difftool_coalesce_run.sql \
     157        difftool_definewarpstack.sql \
    157158        difftool_definewarpstack_part1.sql \
    158159        difftool_definewarpstack_part2.sql \
  • trunk/ippTools/share/difftool_definewarpstack.sql

    r24572 r35192  
    1 -- Get list of warps that can be diffed, with any associated diff,
    2 -- and the best stack to use as a template
    3 -- Warps without an existing diff can be identified by a NULL diff_id
     1-- Get list of warp-stack pairs that can be diffed
     2-- and check results against existing diffs
    43SELECT
    5     warpsToDiff.warp_id,
    6     warpsToDiff.skycell_id,
    7     warpsToDiff.tess_id,
    8     warpsToDiff.filter,
    9     warpsToDiff.good_frac,
    10     warpsToDiff.diff_id,
    11     current_stack_id,
    12     best_stack_id,
    13     exp_id
    14 FROM (
    15     -- Get list of warps that can be diffed, with any associated diff
    16     SELECT
    17         warpSkyfile.warp_id,
    18         warpSkyfile.skycell_id,
    19         warpSkyfile.tess_id,
    20         warpSkyfile.good_frac,
    21         warpRun.label,
    22         filter,
    23         warpRun.label as warp_label,
    24         diffInputs.diff_id,
    25         diffInputs.stack2 AS current_stack_id,
    26         rawExp.exp_id
    27     FROM warpSkyfile
    28     JOIN warpRun USING(warp_id)
    29     JOIN fakeRun USING(fake_id)
    30     JOIN camRun USING(cam_id)
    31     JOIN chipRun USING(chip_id)
    32     JOIN rawExp USING(exp_id)
    33     -- Check if it has an associated diff
    34     LEFT JOIN diffInputSkyfile AS diffInputs
    35         ON diffInputs.warp1 = warpSkyfile.warp_id
    36         AND diffInputs.skycell_id = warpSkyfile.skycell_id
    37         AND diffInputs.stack2 IS NOT NULL
    38     WHERE
    39         warpSkyfile.fault = 0
    40         AND warpSkyfile.quality = 0
    41     -- warpsToDiff WHERE hook %s
    42     ) AS warpsToDiff
    43 -- Get best stack as a function of skycell_id, filter
    44 JOIN (
    45     SELECT
    46         MAX(stack_id) AS best_stack_id, -- most recent stack, by virtue of auto-increment
    47         skycell_id,
    48         filter
    49     FROM stackRun
    50     JOIN stackSumSkyfile USING(stack_id)
    51     WHERE stackRun.state = 'full'
    52         AND stackSumSkyfile.fault = 0
    53         AND stackSumSkyfile.quality = 0
    54     -- stacksForDiff WHERE hook %s
    55     GROUP BY
    56         skycell_id,
    57         filter
    58     ) AS stacksForDiff USING(skycell_id, filter)
     4   exp_id,
     5   warp_id,
     6   rawExp.filter,
     7   warpRun.label AS warpLabel,
     8   warpRun.data_group AS warpDataGroup,
     9   warpRun.tess_id,
     10   warpSkyfile.skycell_id,
     11   MAX(stack_id) AS stack_id,
     12   stackRun.label AS stackLabel,
     13   stackRun.data_group AS stackDataGroup,
     14   diff_id,
     15   diffRun.label AS diffLabel
     16FROM warpRun
     17     JOIN fakeRun USING(fake_id)
     18     JOIN camRun USING(cam_id)
     19     JOIN chipRun USING(chip_id)
     20     JOIN rawExp USING(exp_id)
     21     JOIN warpSkyfile USING(warp_id)
     22     JOIN stackRun ON
     23       (stackRun.skycell_id = warpSkyfile.skycell_id AND
     24        stackRun.filter     = rawExp.filter AND
     25        stackRun.tess_id    = warpRun.tess_id)
     26     JOIN stackSumSkyfile USING(stack_id)
     27     LEFT JOIN diffInputSkyfile ON
     28       (warp_id = diffInputSkyfile.warp1 AND
     29        stack_id = diffInputSkyfile.stack2 AND
     30        diffInputSkyfile.skycell_id = stackRun.skycell_id AND
     31        diffInputSkyfile.tess_id = stackRun.tess_id)
     32     LEFT JOIN diffRun USING(diff_id)
     33WHERE
     34     warpRun.state = 'full'
     35     AND stackRun.state = 'full'
     36     AND warpSkyfile.fault = 0
     37     AND warpSkyfile.quality = 0
     38     AND stackSumSkyfile.fault = 0
     39     AND stackSumSkyfile.quality = 0
     40     AND exp_id IS NOT NULL
     41-- %s
     42GROUP BY exp_id,warp_id,skycell_id
  • trunk/ippTools/src/difftool.c

    r34770 r35192  
    12261226}
    12271227
    1228 
     1228static bool definewarpstackMode(pxConfig *config)
     1229{
     1230  PS_ASSERT_PTR_NON_NULL(config, false);
     1231
     1232  psMetadata *where = psMetadataAlloc();
     1233
     1234  PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
     1235  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
     1236  PXOPT_COPY_STR(config->args, where, "-comment", "comment", "LIKE");
     1237  PXOPT_COPY_S64(config->args, where, "-warp_id", "warpRun.warp_id", "==");
     1238  PXOPT_COPY_STR(config->args, where, "-warp_label", "warpRun.label", "==");
     1239  PXOPT_COPY_STR(config->args, where, "-tess_id", "warpRun.tess_id", "==");
     1240  PXOPT_COPY_STR(config->args, where, "-data_group", "warpRun.data_group", "==");
     1241  PXOPT_COPY_STR(config->args, where, "-skycell_id", "warpSkyfile.skycell_id", "==");
     1242  PXOPT_COPY_F32(config->args, where,  "-good_frac", "warpSkyfile.good_frac", ">=");
     1243  PXOPT_COPY_STR(config->args, where, "-stack_label", "stackRun.label", "==");
     1244  PXOPT_COPY_STR(config->args, where, "-stack_data_group", "stackRun.data_group", "==");
     1245
     1246  PXOPT_LOOKUP_BOOL(bothways, config->args, "-bothways", false);
     1247 
     1248  PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false); // required option
     1249  PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false); // option
     1250  PXOPT_LOOKUP_STR(label, config->args, "-set_label", true, false); // option
     1251  PXOPT_LOOKUP_STR(data_group, config->args, "-set_data_group", false, false);
     1252  PXOPT_LOOKUP_STR(dist_group, config->args, "-set_dist_group", false, false);
     1253  PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
     1254  PXOPT_LOOKUP_TIME(registered, config->args, "-set_registered", false, false);
     1255 
     1256  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1257  PXOPT_LOOKUP_BOOL(newTemplates, config->args, "-new-templates", false);
     1258  PXOPT_LOOKUP_BOOL(reRun, config->args, "-rerun", false);
     1259  PXOPT_LOOKUP_BOOL(available, config->args, "-available", false);
     1260  PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     1261
     1262
     1263  // Get query file
     1264  psString query = pxDataGet("difftool_definewarpstack.sql");
     1265  if (!query) {
     1266    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1267    return false;
     1268  }
     1269
     1270  psString whereClause = psDBGenerateWhereConditionSQL(where,NULL);
     1271 
     1272  // Don't queue things that already have diffs.
     1273  if (! (reRun || newTemplates) ) {
     1274    psStringAppend(&whereClause, "\nAND diff_id IS NULL\n");
     1275  }
     1276  // Append diff qualifiers, if we have them
     1277  if (label) {
     1278    psStringAppend(&whereClause, "\nAND ((diffRun.label = '%s') OR (diffRun.label IS NULL))",label);
     1279  }
     1280  if (data_group) {
     1281    psStringAppend(&whereClause, "\nAND ((diffRun.data_group = '%s') OR (diffRun.data_group IS NULL))",data_group);
     1282  }
     1283  if (reduction) {
     1284    psStringAppend(&whereClause, "\nAND ((diffRun.reduction = '%s') OR (diffRun.reduction IS NULL))",reduction);
     1285  }
     1286
     1287  psString whereClauseString = psStringCopy("");
     1288  psStringAppend(&whereClauseString, " \n AND %s ", whereClause);
     1289  //  fprintf(stderr,query,whereClauseString);
     1290
     1291  // This is just a simple query, so we don't need to do a transaction
     1292  if (!p_psDBRunQueryF(config->dbh, query, whereClauseString)) {
     1293    psError(PS_ERR_UNKNOWN, false, "database error");
     1294    psFree(query);
     1295    psFree(whereClause);
     1296    return(false);
     1297  }
     1298  psFree(query);
     1299  psFree(whereClause);
     1300  psFree(whereClauseString);
     1301 
     1302  psArray *output = p_psDBFetchResult(config->dbh);
     1303  if (!output) {
     1304    psErrorCode err = psErrorCodeLast();
     1305    switch (err) {
     1306    case PS_ERR_DB_CLIENT:
     1307      psError(PXTOOLS_ERR_SYS, false, "database error");
     1308      break;
     1309    case PS_ERR_DB_SERVER:
     1310      psError(PXTOOLS_ERR_PROG, false, "database error");
     1311      break;
     1312    default:
     1313      psError(PXTOOLS_ERR_PROG, false, "unknown error");
     1314      break;
     1315    }
     1316    return false;
     1317  }
     1318  if (!psArrayLength(output)) {
     1319    psTrace("difftool", PS_LOG_INFO, "no rows found");
     1320    psFree(output);
     1321    return true;
     1322  }
     1323  if (pretend) {
     1324    // negative simple so the default is true
     1325    if (!ippdbPrintMetadatas(stdout, output, "diffRun", !simple)) {
     1326      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1327      psFree(output);
     1328      return false;
     1329    }
     1330    psFree(output);
     1331    return true;
     1332  }
     1333
     1334  psArray *list = psArrayAllocEmpty(16); // List of runs, to print
     1335  long numGood = 0;
     1336  psS64 last_exp_id = 0;
     1337  psS64 diff_id = 0;
     1338  for (long i = 0; i < output->n; i++) {
     1339    psMetadata *row = output->data[i];
     1340    bool mdok;
     1341
     1342    psS64 exp_id = psMetadataLookupS64(&mdok, row, "exp_id");
     1343    if (!mdok) {
     1344        psError(PXTOOLS_ERR_PROG, false, "exp_id not found");
     1345        if (!psDBRollback(config->dbh)) {
     1346          psError(PS_ERR_UNKNOWN, false, "database error");
     1347        }
     1348        return false;
     1349    }
     1350    if (exp_id != last_exp_id) {
     1351      if (diff_id != 0) { // We've added a run already, and are now switching to a new one.
     1352        // Set state to new
     1353        if (!setdiffRunState(config, diff_id, "new", false)) {
     1354          psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, diff_id);
     1355          if (!psDBRollback(config->dbh)) {
     1356            psError(PS_ERR_UNKNOWN, false, "database error");
     1357          }
     1358          return false;
     1359        }
     1360        // Commit results
     1361        if (!psDBCommit(config->dbh)) {
     1362          psError(PS_ERR_UNKNOWN, false, "database error");
     1363          psFree(list);
     1364          return false;
     1365        }
     1366      }
     1367     
     1368      // Begin transaction
     1369      if (!psDBTransaction(config->dbh)) {
     1370        psError(PS_ERR_UNKNOWN, false, "database error");
     1371        return false;
     1372      }
     1373      // Add a new diffRun row
     1374      psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1375      if (!mdok) {
     1376        psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1377        if (!psDBRollback(config->dbh)) {
     1378          psError(PS_ERR_UNKNOWN, false, "database error");
     1379        }
     1380        return false;
     1381      }
     1382      diffRunRow *run = diffRunRowAlloc(
     1383                                        0,        // ID
     1384                                        "reg",    // state
     1385                                        workdir,
     1386                                        label,
     1387                                        data_group ? data_group : label,
     1388                                        dist_group,
     1389                                        reduction,
     1390                                        NULL,     // dvodb
     1391                                        registered,
     1392                                        tess_id,
     1393                                        bothways, // bothways (default is false)
     1394                                        true,     // exposure
     1395                                        0,        // magicked
     1396                                        NULL,     // software_version
     1397                                        0,        // mask stat npix
     1398                                        NAN,      // static
     1399                                        NAN,      // dynamic
     1400                                        NAN,      // magic
     1401                                        NAN,      // advisory
     1402                                        IPP_DIFF_MODE_WARP_STACK,
     1403                                        note
     1404                                        );
     1405      if (!diffRunInsertObject(config->dbh, run)) {
     1406        psError(PS_ERR_UNKNOWN, false, "database error");
     1407        psFree(run);
     1408        if (!psDBRollback(config->dbh)) {
     1409          psError(PS_ERR_UNKNOWN, false, "database error");
     1410        }
     1411      }
     1412      diff_id = psDBLastInsertID(config->dbh);
     1413      run->diff_id = diff_id;
     1414
     1415      psArrayAdd(list, list->n, run);
     1416      numGood++;
     1417     
     1418      last_exp_id = exp_id;                                     
     1419    } // End Adding diffRun
     1420   
     1421    if (exp_id == last_exp_id) {
     1422      psString skycell_id = psMetadataLookupStr(&mdok, row, "skycell_id");
     1423      if (!mdok) {
     1424        psError(PXTOOLS_ERR_PROG, false, "skycell_id not found");
     1425        if (!psDBRollback(config->dbh)) {
     1426          psError(PS_ERR_UNKNOWN, false, "database error");
     1427        }
     1428        return false;
     1429      }
     1430      psString tess_id = psMetadataLookupStr(&mdok, row, "tess_id");
     1431      if (!mdok) {
     1432        psError(PXTOOLS_ERR_PROG, false, "tess_id not found");
     1433        if (!psDBRollback(config->dbh)) {
     1434          psError(PS_ERR_UNKNOWN, false, "database error");
     1435        }
     1436        return false;
     1437      }
     1438      psS64 warp_id = psMetadataLookupS64(&mdok, row, "warp_id");
     1439      if (!mdok) {
     1440        psError(PXTOOLS_ERR_PROG, false, "warp_id not found");
     1441        if (!psDBRollback(config->dbh)) {
     1442          psError(PS_ERR_UNKNOWN, false, "database error");
     1443        }
     1444        return false;
     1445      }
     1446      psS64 stack_id = psMetadataLookupS64(&mdok, row, "stack_id");
     1447      if (!mdok) {
     1448        psError(PXTOOLS_ERR_PROG, false, "stack_id not found");
     1449        if (!psDBRollback(config->dbh)) {
     1450          psError(PS_ERR_UNKNOWN, false, "database error");
     1451        }
     1452        return false;
     1453      }
     1454
     1455      // Add a new skyfile row
     1456      diffInputSkyfileRow *skyfile = diffInputSkyfileRowAlloc(
     1457                                                              diff_id,   // ID
     1458                                                              skycell_id,
     1459                                                              warp_id, // warp1_id
     1460                                                              PS_MAX_S64, // stack1 -> NULL
     1461                                                              PS_MAX_S64, // warp2_id -> NULL
     1462                                                              stack_id, // stack2
     1463                                                              tess_id,
     1464                                                              0 // diff_skyfile_id
     1465                                                              );
     1466      //      fprintf(stderr,"%"PRId64 " %"PRId64 " %"PRId64 " %s %s\n",diff_id,warp_id,stack_id,skycell_id, tess_id);
     1467      if (!diffInputSkyfileInsertObject(config->dbh, skyfile)) {
     1468        psError(PS_ERR_UNKNOWN, false, "database error");
     1469        psFree(list);
     1470        if (!psDBRollback(config->dbh)) {
     1471          psError(PS_ERR_UNKNOWN, false, "database error");
     1472        }
     1473        return false;
     1474      }
     1475    } // End Adding skyfile
     1476  } // End parsing result set
     1477 
     1478  // Finish the last run's update and close the connection
     1479  if (diff_id != 0) {
     1480    if (!setdiffRunState(config, diff_id, "new", false)) {
     1481      psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, diff_id);
     1482      if (!psDBRollback(config->dbh)) {
     1483        psError(PS_ERR_UNKNOWN, false, "database error");
     1484      }
     1485      return false;
     1486    }
     1487    // Commit results
     1488    if (!psDBCommit(config->dbh)) {
     1489      psError(PS_ERR_UNKNOWN, false, "database error");
     1490      psFree(list);
     1491      return false;
     1492    }
     1493  }
     1494 
     1495  if (numGood && !diffRunPrintObjects(stdout, list, !simple)) {
     1496    psError(PS_ERR_UNKNOWN, false, "failed to print object");
     1497    psFree(list);
     1498    return(false);
     1499  }
     1500  psFree(list);
     1501  // Free things
     1502  return true;
     1503
     1504
     1505#if (0)
    12291506static bool definewarpstackMode(pxConfig *config)
    12301507{
     
    16141891    return true;
    16151892}
     1893#endif
    16161894
    16171895static bool definewarpwarpMode(pxConfig *config)
Note: See TracChangeset for help on using the changeset viewer.