IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 8, 2009, 5:19:09 PM (17 years ago)
Author:
bills
Message:

disttool -definebyquery

File:
1 edited

Legend:

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

    r23742 r23765  
    132132static bool definebyqueryMode(pxConfig *config)
    133133{
    134     psError(PS_ERR_PROGRAMMING, true, "-definebyquery is not implemented yet");
    135     return false;
     134    PS_ASSERT_PTR_NON_NULL(config, false);
     135
     136    // required
     137    PXOPT_LOOKUP_STR(stage, config->args,     "-stage", true, false);
     138    PXOPT_LOOKUP_STR(workdir, config->args,   "-workdir", true, false);
     139    PXOPT_LOOKUP_S64(magic_ds_id, config->args, "-magic_ds_id",  false, false);
     140
     141    // optional
     142    PXOPT_LOOKUP_BOOL(no_magic, config->args, "-no_magic", false);
     143    PXOPT_LOOKUP_STR(set_label, config->args, "-set_label", false, false);
     144    PXOPT_LOOKUP_S64(limit, config->args, "-limit", false, false);
     145   
     146    PXOPT_LOOKUP_BOOL(dry_run, config->args, "-dry_run", false);
     147
     148    // select arguments
     149    psMetadata *where = psMetadataAlloc();
     150
     151    PXOPT_COPY_S64(config->args, where, "-target_id", "distTarget.target_id",    "==");
     152    PXOPT_COPY_S64(config->args, where, "-exp_id",    "rawExp.exp_id",           "==");
     153    PXOPT_COPY_S64(config->args, where, "-chip_id",   "chipRun.chip_id",         "==");
     154    PXOPT_COPY_STR(config->args, where, "-obs_mode",  "rawExp.obs_mode",         "==");
     155
     156    PXOPT_LOOKUP_STR(label, config->args, "-label", false, false);
     157
     158    psString query = NULL;
     159    psString runType = NULL;
     160    psString runJoinStr = NULL;
     161    if (!strcmp(stage, "raw")) {
     162        runType = "rawExp";
     163        runJoinStr = "rawExp.exp_id";
     164        query = pxDataGet("disttool_definebyquery_raw.sql");
     165        if (!query) {
     166            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     167            psFree(where);
     168            return false;
     169        }
     170    } else if (!strcmp(stage, "chip")) {
     171        runType = "chipRun";
     172        runJoinStr = "chipRun.chip_id";
     173        query = pxDataGet("disttool_definebyquery_chip.sql");
     174        if (!query) {
     175            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     176            psFree(where);
     177            return false;
     178        }
     179
     180        if (label) {
     181            psStringAppend(&query, " AND (chipRun.label = '%s')", label);
     182        }
     183    } else if (!strcmp(stage, "camera")) {
     184        runType = "camRun";
     185        query = pxDataGet("disttool_definebyquery_camera.sql");
     186        if (!query) {
     187            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     188            psFree(where);
     189            return false;
     190        }
     191
     192        if (label) {
     193            psStringAppend(&query, " AND (camRun.label = '%s')", label);
     194        }
     195    } else if (!strcmp(stage, "fake")) {
     196        runType = "fakeRun";
     197        query = pxDataGet("disttool_definebyquery_fake.sql");
     198        if (!query) {
     199            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     200            psFree(where);
     201            return false;
     202        }
     203
     204        if (label) {
     205            psStringAppend(&query, " AND (fakeRun.label = '%s')", label);
     206        }
     207        // fake stage doesn't require magic
     208        no_magic = true;
     209    } else if (!strcmp(stage, "warp")) {
     210        runType = "warpRun";
     211        runJoinStr = "warpRun.warp_id";
     212        query = pxDataGet("disttool_definebyquery_warp.sql");
     213        if (!query) {
     214            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     215            psFree(where);
     216            return false;
     217        }
     218
     219        if (label) {
     220            psStringAppend(&query, " AND (warpRun.label = '%s')", label);
     221        }
     222
     223    } else if (!strcmp(stage, "diff")) {
     224        runType = "diffRun";
     225        runJoinStr = "diffRun.diff_id";
     226        query = pxDataGet("disttool_definebyquery_diff.sql");
     227        if (!query) {
     228            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     229            psFree(where);
     230            return false;
     231        }
     232
     233        if (label) {
     234            psStringAppend(&query, " AND (diffRun.label = '%s')", label);
     235        }
     236
     237    } else if (!strcmp(stage, "stack")) {
     238        runType = "stackRun";
     239        query = pxDataGet("disttool_definebyquery_stack.sql");
     240        if (!query) {
     241            psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     242            psFree(where);
     243            return false;
     244        }
     245
     246        if (label) {
     247            psStringAppend(&query, " AND (stackRun.label = '%s')", label);
     248        }
     249        // stack stage doesn't require magic (perhaps let the script do this?
     250        no_magic = true;
     251    } else {
     252        psError(PS_ERR_UNKNOWN, true, "unknown value for stage: %s", stage);
     253        psFree(where);
     254        return false;
     255    }
     256
     257    if (psListLength(where->list)) {
     258        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     259        psStringAppend(&query, " AND %s", whereClause);
     260        psFree(whereClause);
     261    }
     262    psFree(where);
     263
     264    psString joinHook = NULL;
     265
     266    if (!no_magic) {
     267        psStringAppend(&query, " AND (distTarget.clean OR %s.magicked)", runType);
     268
     269        // is selecting by magic_ds_id really interesting?
     270        if (magic_ds_id) {
     271            if (strcmp(stage, "camera")) {
     272                // stage other than camera
     273                if (!runJoinStr) {
     274                    psError(PS_ERR_PROGRAMMING, true, "cannot select by magic_ds_id for stage: %s", stage);
     275                    psFree(query);
     276                    return false;
     277                }
     278                psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = distTarget.stage"
     279                                              " AND magicDSRun.stage_id = %s", runJoinStr);
     280            } else {
     281                // camera masks are magicked when the chipRun is magicked
     282                // XXX: This is confusing. Is it dangerous?
     283                // Maybe I should add a magicked bit to camRun. Note this isn't
     284                psStringAppend(&joinHook, "\nJOIN magicDSRun ON magicDSRun.stage = 'chip'"
     285                                              " AND magicDSRun.stage_id = chipRun.chip_id");
     286            }
     287            psStringAppend(&query, " AND (magicDSRun.state = 'full' AND magicDSRun.re_place AND (magic_ds_id = %" PRId64 "))", magic_ds_id);
     288        }
     289    }
     290
     291    if (limit) {
     292        psString limitString = psDBGenerateLimitSQL(limit);
     293        psStringAppend(&query, " %s", limitString);
     294        psFree(limitString);
     295    }
     296
     297    if (!p_psDBRunQueryF(config->dbh, query, joinHook ? joinHook : "")) {
     298        psError(PS_ERR_UNKNOWN, false, "database error");
     299        psFree(query);
     300        return false;
     301    }
     302    psFree(query);
     303    psFree(joinHook);
     304
     305    psArray *output = p_psDBFetchResult(config->dbh);
     306    if (!output) {
     307        psError(PS_ERR_UNKNOWN, false, "database error");
     308        return false;
     309    }
     310    if (!psArrayLength(output)) {
     311        psTrace("warptool", PS_LOG_INFO, "no rows found");
     312        psFree(output);
     313        return true;
     314    }
     315
     316    // XXX Remove this debugging output at some point
     317    if (!ippdbPrintMetadatas(stdout, output, "newdistRuns", true)) {
     318        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     319        psFree(output);
     320        return false;
     321    }
     322    if (dry_run) {
     323        psFree(output);
     324        return true;
     325    }
     326
     327    for (long i=0; i < psArrayLength(output); i++) {
     328        psMetadata *md = output->data[i];
     329        psString stage = psMetadataLookupStr(NULL, md, "stage");
     330        psString run_tag = psMetadataLookupStr(NULL, md, "run_tag");
     331        psS64 stage_id = psMetadataLookupS64(NULL, md, "stage_id");
     332        psS64 target_id = psMetadataLookupS64(NULL, md, "target_id");
     333        bool clean = psMetadataLookupBool(NULL, md, "clean");
     334
     335        psString outroot = NULL;
     336        psStringAppend(&outroot, "%s/%s/%s", workdir, run_tag, stage);
     337
     338        if (!distRunInsert(config->dbh,
     339                0,      // dist_id
     340                target_id,
     341                stage,
     342                stage_id,
     343                set_label,
     344                outroot,
     345                clean,
     346                no_magic,
     347                "new",
     348                NULL,   // time_stamp
     349                0       // fault
     350        )) {
     351            psError(PS_ERR_UNKNOWN, false, "database error");
     352            psFree(outroot);
     353            psFree(output);
     354            return false;
     355        }
     356        psFree(outroot);
     357    }
     358
     359    psFree(output);
     360
     361    return true;
    136362}
    137363
Note: See TracChangeset for help on using the changeset viewer.