IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 10, 2010, 6:28:51 PM (16 years ago)
Author:
watersc1
Message:

Skycell Summary and stack Association stuff should be finished. I'll merge and do final tests on monday.

Location:
branches/czw_branch/20100519
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20100519

  • branches/czw_branch/20100519/ippTools/src

    • Property svn:ignore
      •  

        old new  
        3838stamp-h1
        3939warptool
        40 
         40staticskytool
  • branches/czw_branch/20100519/ippTools/src/addtool.c

    r27773 r28304  
    4040static bool maskedMode(pxConfig *config);
    4141static bool unblockMode(pxConfig *config);
     42static bool addminidvodbrunMode(pxConfig *config);
     43static bool updateminidvodbrunMode(pxConfig *config);
     44static bool listminidvodbrunMode(pxConfig *config);
     45static bool flipminidvodbrunMode(pxConfig *config);
     46static bool checkminidvodbrunaddrunMode(pxConfig *config);
     47static bool addminidvodbprocessedMode(pxConfig *config);
     48static bool listminidvodbprocessedMode(pxConfig *config);
     49static bool revertminidvodbprocessedMode(pxConfig *config);
     50static bool updateminidvodbprocessedMode(pxConfig *config);
     51
     52
    4253
    4354# define MODECASE(caseName, func) \
     
    6980        MODECASE(ADDTOOL_MODE_MASKED,               maskedMode);
    7081        MODECASE(ADDTOOL_MODE_UNBLOCK,              unblockMode);
     82        MODECASE(ADDTOOL_MODE_ADDMINIDVODBRUN,      addminidvodbrunMode);
     83        MODECASE(ADDTOOL_MODE_UPDATEMINIDVODBRUN,   updateminidvodbrunMode);
     84        MODECASE(ADDTOOL_MODE_LISTMINIDVODBRUN,     listminidvodbrunMode);
     85        MODECASE(ADDTOOL_MODE_FLIPMINIDVODBRUN,     flipminidvodbrunMode);
     86        MODECASE(ADDTOOL_MODE_CHECKMINIDVODBRUNADDRUN, checkminidvodbrunaddrunMode);
     87        MODECASE(ADDTOOL_MODE_ADDMINIDVODBPROCESSED,addminidvodbprocessedMode);
     88        MODECASE(ADDTOOL_MODE_LISTMINIDVODBPROCESSED,listminidvodbprocessedMode);
     89        MODECASE(ADDTOOL_MODE_REVERTMINIDVODBPROCESSED,revertminidvodbprocessedMode);
     90        MODECASE(ADDTOOL_MODE_UPDATEMINIDVODBPROCESSED,updateminidvodbprocessedMode);
     91
    7192        default:
    7293            psAbort("invalid option (this should not happen)");
     
    102123    PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
    103124
     125
    104126    if (!psListLength(where->list)) {
    105127        psFree(where);
     
    114136    PXOPT_LOOKUP_STR(reduction,   config->args, "-set_reduction", false, false);
    115137    PXOPT_LOOKUP_STR(note,        config->args, "-set_note", false, false);
     138    PXOPT_LOOKUP_STR(minidvodb_name,  config->args, "-set_minidvodb_name", false, false);
     139    PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-set_minidvodb_group", false, false);
    116140    PXOPT_LOOKUP_BOOL(image_only, config->args, "-image_only", false);
     141    PXOPT_LOOKUP_BOOL(minidvodb,  config->args, "-set_minidvodb", false);
    117142    PXOPT_LOOKUP_BOOL(destreaked, config->args, "-destreaked", false);
    118143    PXOPT_LOOKUP_BOOL(pretend,    config->args, "-pretend", false);
    119144    PXOPT_LOOKUP_BOOL(simple,     config->args, "-simple", false);
     145
     146
    120147
    121148    // find the cam_id of all the exposures that we want to queue up.
     
    245272                               dvodb       ? dvodb     : row->dvodb,
    246273                               note        ? note      : NULL,
    247                                image_only
     274                               image_only,
     275                               minidvodb,
     276                               minidvodb_group,
     277                               minidvodb_name
    248278        )) {
    249279            if (!psDBRollback(config->dbh)) {
    250                 psError(PS_ERR_UNKNOWN, false, "database error");
     280                psError(PS_ERR_UNKNOWN, false, "database error sfg");
    251281            }
    252282            psError(PS_ERR_UNKNOWN, false,
     
    291321
    292322    // pxUpdateRun gets parameters from config->args and runs the update query
    293     bool result = pxUpdateRun(config, where, &query, "addRun", "add_id", 
     323    bool result = pxUpdateRun(config, where, &query, "addRun", "add_id",
    294324        "addProcessedExp", false);
    295325
     
    374404    PXOPT_LOOKUP_STR(path_base,     config->args, "-path_base", false, false);
    375405    PXOPT_LOOKUP_F32(dtime_addstar, config->args, "-dtime_addstar", false, false);
     406    PXOPT_LOOKUP_STR(dvodb_path, config->args, "-dvodb_path", false, false);
     407    PXOPT_LOOKUP_STR(minidvodb_name, config->args, "-minidvodb_name", false, false);
    376408    PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
    377409
     
    399431        return false;
    400432    }
     433
    401434    psFree(query);
    402435
     
    424457        dtime_addstar,
    425458        path_base,
     459        dvodb_path,
    426460        fault
    427461        );
     
    437471        return false;
    438472    }
     473
     474    //if there is a minidvodb_name, set it in addRun (it's not known until it is processed)
     475    if (minidvodb_name) {
     476      psString setName = NULL;
     477      psStringAppend (&setName, "UPDATE addRun set minidvodb_name = '%s' where add_id = %" PRId64, minidvodb_name, row->add_id);
     478      if (!p_psDBRunQuery(config->dbh, setName)) {
     479        if (!psDBRollback(config->dbh)) {
     480          psError(PS_ERR_UNKNOWN, false, "database error");
     481        }
     482        psError(PS_ERR_UNKNOWN, false, "database error");
     483
     484        return false;
     485      }
     486    }
     487
     488
     489
     490
    439491
    440492    // since there is only one exp per 'new' set addRun.state = 'full'
     
    522574
    523575    if (!p_psDBRunQuery(config->dbh, query)) {
    524         psError(PS_ERR_UNKNOWN, false, "database error");
     576      psError(PS_ERR_UNKNOWN, false, "database error");
    525577        psFree(query);
    526578        return false;
     
    576628
    577629    {
    578         psString query = pxDataGet("addtool_reset_faulted_runs.sql");
     630        psString query = pxDataGet("addtool_revertprocessedexp.sql");
    579631        if (!query) {
     632            // rollback
     633            if (!psDBRollback(config->dbh)) {
     634                psError(PS_ERR_UNKNOWN, false, "database error");
     635            }
    580636            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    581637            psFree(where);
     
    602658        psFree(query);
    603659    }
    604 
    605     {
    606         psString query = pxDataGet("addtool_revertprocessedexp.sql");
    607         if (!query) {
    608             // rollback
    609             if (!psDBRollback(config->dbh)) {
    610                 psError(PS_ERR_UNKNOWN, false, "database error");
    611             }
    612             psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    613             psFree(where);
    614             return false;
    615         }
    616 
    617         // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
    618         if (where && psListLength(where->list)) {
    619             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    620             psStringAppend(&query, " AND %s", whereClause);
    621             psFree(whereClause);
    622         }
    623 
    624         if (!p_psDBRunQuery(config->dbh, query)) {
    625             // rollback
    626             if (!psDBRollback(config->dbh)) {
    627                 psError(PS_ERR_UNKNOWN, false, "database error");
    628             }
    629             psError(PS_ERR_UNKNOWN, false, "database error");
    630             psFree(query);
    631             psFree(where);
    632             return false;
    633         }
    634         psFree(query);
    635     }
    636660    psFree(where);
    637661
     
    733757    return true;
    734758}
     759
     760static bool addminidvodbrunMode(pxConfig *config)
     761{
     762  PS_ASSERT_PTR_NON_NULL(config, NULL);
     763
     764  // required
     765
     766  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-set_minidvodb_group", true, false);
     767  PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-set_mergedvodb_path", true, false);
     768
     769  //optional
     770  PXOPT_LOOKUP_STR(minidvodb_name, config->args, "-set_minidvodb_name", false, false);
     771  PXOPT_LOOKUP_STR(minidvodb_path, config->args, "-set_minidvodb_path", false, false);
     772  PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
     773
     774  if (!psDBTransaction(config->dbh)) {
     775    psError(PS_ERR_UNKNOWN, false, "database error");
     776    return false;
     777  }
     778
     779  psString minidvodbpath = "NULL";
     780
     781  // I don't know how to get around the complaints of minidvodb_path can't be null. this 'fixes' it, but someone smarter can fix it properly.
     782  if (minidvodb_path) {
     783    minidvodbpath = minidvodb_path;
     784  }
     785
     786  if (!minidvodbRunInsert(config->dbh,
     787            0, // job_id
     788            minidvodb_name,
     789            minidvodb_group,
     790            minidvodbpath,
     791            mergedvodb_path,
     792            "new",
     793            0
     794            )) {
     795    psError(PS_ERR_UNKNOWN, false, "database error");
     796    return false;
     797  }
     798
     799  psU64 affected = psDBAffectedRows(config->dbh);
     800  if (affected != 1) {
     801    psError(PS_ERR_UNKNOWN, false,
     802            "should have affected one row but %" PRIu64 " rows were modified",
     803            affected);
     804    return false;
     805  }
     806
     807  psS64 minidvodb_id = psDBLastInsertID(config->dbh);
     808  printf("%" PRId64 "\n", minidvodb_id);
     809
     810
     811  if (!minidvodb_name) {
     812    psStringAppend(&minidvodb_name, "%s.%" PRIu64,minidvodb_group,minidvodb_id);
     813  }
     814
     815  if (!minidvodb_path) {
     816    psStringAppend(&minidvodb_path,"%s/%s",mergedvodb_path,minidvodb_name);
     817  }
     818
     819  psString query = NULL;
     820
     821  psStringAppend(&query, "UPDATE minidvodbRun SET minidvodb_path = '%s', minidvodb_name = '%s' where minidvodb_id = %" PRIu64";", minidvodb_path, minidvodb_name, minidvodb_id);
     822
     823  if (!p_psDBRunQuery(config->dbh, query)) {
     824    psError(PS_ERR_UNKNOWN, false, "database error");
     825    psFree(query);
     826    return false;
     827  }
     828
     829  if (!psDBCommit(config->dbh)) {
     830    psError(PS_ERR_UNKNOWN, false, "database error");
     831    return false;
     832  }
     833
     834  return true;
     835}
     836
     837static bool updateminidvodbrunMode(pxConfig *config) {
     838  PS_ASSERT_PTR_NON_NULL(config, false);
     839
     840    psMetadata *where = psMetadataAlloc();
     841    PXOPT_COPY_S64(config->args, where, "-minidvodb_id",     "minidvodb_id", "==");
     842    PXOPT_COPY_STR(config->args, where, "-minidvodb_name",     "minidvodb_name", "==");
     843    PXOPT_COPY_STR(config->args, where, "-state",     "state", "==");
     844    PXOPT_COPY_STR(config->args, where, "-minidvodb_path", "minidvodb_path", "==");
     845    PXOPT_COPY_STR(config->args, where, "-mergedvodb_path", "mergedvodb_path", "==");
     846    PXOPT_COPY_STR(config->args, where, "-minidvodb_group",     "minidvodb_group", "==");
     847
     848    PXOPT_LOOKUP_STR(minidvodb_name,  config->args, "-set_minidvodb_name", false, false);
     849    PXOPT_LOOKUP_STR(minidvodb_path,  config->args, "-set_minidvodb_path", false, false);
     850    PXOPT_LOOKUP_STR(state,  config->args, "-set_state", false, false);
     851    PXOPT_LOOKUP_STR(mergedvodb_path,  config->args, "-set_mergedvodb_path", false, false);
     852    PXOPT_LOOKUP_STR(minidvodb_group,  config->args, "-set_minidvodb_group", false, false);
     853
     854
     855    if (!psListLength(where->list)) {
     856      psFree(where);
     857      psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     858      return false;
     859    }
     860
     861    psString query = psStringCopy("UPDATE minidvodbRun SET ");
     862    int cnt = 0;
     863    psString comma = ",";
     864    if (minidvodb_name) {
     865      psStringAppend(&query, " minidvodb_name = '%s'", minidvodb_name);
     866      cnt++;
     867    }
     868
     869    if (minidvodb_path) {
     870      if (cnt) {
     871          psStringAppend(&query, "%s", comma);
     872      }
     873
     874      psStringAppend(&query, " minidvodb_path = '%s'", minidvodb_path);
     875      cnt++;
     876    }
     877
     878    if (state) {
     879      if (cnt) {
     880        psStringAppend(&query, "%s", comma);
     881      }
     882      psStringAppend(&query, " state = '%s'", state);
     883      cnt++;
     884    }
     885
     886    if (mergedvodb_path) {
     887      if (cnt) {
     888        psStringAppend(&query, "%s", comma);
     889      }
     890      psStringAppend(&query, " mergedvodb_path = '%s'", mergedvodb_path);
     891      cnt++;
     892    }
     893
     894    if (minidvodb_group) {
     895      if (cnt) {
     896        psStringAppend(&query, "%s", comma);
     897      }
     898      psStringAppend(&query, " minidvodb_group = '%s'", minidvodb_group);
     899      cnt++;
     900    }
     901
     902    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     903    psStringAppend(&query, " WHERE %s", whereClause);
     904
     905    if (!p_psDBRunQuery(config->dbh, query)) {
     906      psError(PS_ERR_UNKNOWN, false, "database error");
     907      psFree(query);
     908      return false;
     909    }
     910
     911    psFree(query);
     912    psFree(where);
     913
     914    return true;
     915
     916}
     917
     918static bool flipminidvodbrunMode(pxConfig *config) {
     919  PS_ASSERT_PTR_NON_NULL(config, false);
     920
     921  psMetadata *where = psMetadataAlloc();
     922  PXOPT_LOOKUP_STR(minidvodb_group,  config->args, "-minidvodb_group",true, false);
     923  PXOPT_COPY_STR(config->args, where, "-minidvodb_group",     "minidvodb_group", "==");
     924
     925//this flips the new - > active
     926// and the active - > waiting in one action
     927
     928
     929// the first query looks to find things that are new and where all the fields are filled (ie, ready to be flipped to active)
     930
     931// start a transaction eraly so it will contain any row level locks
     932  if (!psDBTransaction(config->dbh)) {
     933    psError(PS_ERR_UNKNOWN, false, "database error");
     934    return false;
     935  }
     936
     937  psString firstquery = psStringCopy("SELECT * from  minidvodbRun where state = 'new' and minidvodb_name is NOT NULL and minidvodb_group IS NOT NULL and minidvodb_path IS NOT NULL and mergedvodb_path IS NOT NULL");
     938
     939  psString firstwhereClause = psDBGenerateWhereConditionSQL(where, NULL);
     940  psStringAppend(&firstquery, " AND %s", firstwhereClause);
     941
     942
     943
     944  if (!p_psDBRunQuery(config->dbh, firstquery)) {
     945    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     946    if (!psDBRollback(config->dbh)) {
     947      psError(PS_ERR_UNKNOWN, false, "database error");
     948    }
     949    return false;
     950  }
     951  psFree(firstquery);
     952
     953 //we don't care what the stuff is that is found, just that there is stuff. This is a check to see that there is something in the 'new' state, before flipping (so that if there is nothing in new, it won't flip the active to waiting.  the flipminidvo is just to make it easy to flip from new -> active.
     954
     955  psArray *output = p_psDBFetchResult(config->dbh);
     956  if (!output) {
     957    psError(PS_ERR_UNKNOWN, false, "database error");
     958    return false;
     959  }
     960  if (!psArrayLength(output)) {
     961    psTrace("addtool", PS_LOG_INFO, "no rows found");
     962    psFree(output);
     963    return true;
     964  }
     965
     966
     967  //ok, there's something, so flip active -> waiting
     968
     969  psString query = psStringCopy("UPDATE minidvodbRun SET state = 'waiting' WHERE state = 'active' ");
     970
     971  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     972  psStringAppend(&query, " AND %s", whereClause);
     973
     974
     975  if (!p_psDBRunQuery(config->dbh, query)) {
     976    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     977    if (!psDBRollback(config->dbh)) {
     978      psError(PS_ERR_UNKNOWN, false, "database error");
     979    }
     980    return false;
     981  }
     982
     983
     984  //now flip new -> active
     985  psString query2 = psStringCopy("UPDATE minidvodbRun SET state = 'active' WHERE state = 'new' AND minidvodb_name is NOT NULL and minidvodb_group IS NOT NULL and minidvodb_path IS NOT NULL and mergedvodb_path IS NOT NULL ");
     986  psStringAppend(&query2, " AND minidvodb_group = '%s' limit 1;", minidvodb_group);
     987  if (!p_psDBRunQuery(config->dbh, query2)) {
     988    psError(PS_ERR_UNKNOWN, false, "database error");
     989    if (!psDBRollback(config->dbh)) {
     990      psError(PS_ERR_UNKNOWN, false, "database error");
     991    }
     992    psFree(query);
     993    return false;
     994  }
     995
     996
     997  if (!psDBCommit(config->dbh)) {
     998    psError(PS_ERR_UNKNOWN, false, "database error");
     999    if (!psDBRollback(config->dbh)) {
     1000      psError(PS_ERR_UNKNOWN, false, "database error");
     1001    }
     1002    psFree(query);
     1003  return false;
     1004  }
     1005 psFree(query2);
     1006 psFree(where);
     1007
     1008 return true;
     1009
     1010}
     1011
     1012
     1013static bool checkminidvodbrunaddrunMode(pxConfig *config) {
     1014  PS_ASSERT_PTR_NON_NULL(config, false);
     1015  psMetadata *where = psMetadataAlloc();
     1016
     1017  //this checks to see if a minidvod_group/name is has completed addRun processing
     1018  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbRun.minidvodb_group", "==");
     1019  PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbRun.minidvodb_name", "==");
     1020  PXOPT_COPY_STR(config->args, where, "-state", "minidvodbRun.state", "==");
     1021
     1022
     1023  PXOPT_LOOKUP_U64(limit,      config->args, "-limit", false, false);
     1024  PXOPT_LOOKUP_BOOL(all_addrun_states, config->args, "-all_addrun_states", false);
     1025  //this doesn't care what state the addRun is in (useful for counting addRuns)
     1026  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1027
     1028  if (!psListLength(where->list)) {
     1029        psFree(where);
     1030        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1031        return false;
     1032  }
     1033
     1034  psString query = pxDataGet("addtool_checkminidvodbaddrun.sql");
     1035
     1036  if (!query) {
     1037        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1038        return false;
     1039  }
     1040
     1041
     1042  if (psListLength(where->list)) {
     1043    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1044        psStringAppend(&query, " WHERE %s", whereClause);
     1045        psFree(whereClause);
     1046  }
     1047
     1048  if (!all_addrun_states) {
     1049    psStringAppend(&query, " AND (cnt2 = cnt) ");
     1050  }
     1051
     1052  if (limit) {
     1053    psString limitString = psDBGenerateLimitSQL(limit);
     1054    psStringAppend(&query, " %s", limitString);
     1055    psFree(limitString);
     1056  }
     1057
     1058
     1059  if (!p_psDBRunQuery(config->dbh, query)) {
     1060    psError(PS_ERR_UNKNOWN, false, "database error");
     1061    psFree(query);
     1062    return false;
     1063  }
     1064  psFree(query);
     1065  psArray *output = p_psDBFetchResult(config->dbh);
     1066  if (!output) {
     1067    psError(PS_ERR_UNKNOWN, false, "database error");
     1068    return false;
     1069  }
     1070  if (!psArrayLength(output)) {
     1071    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1072    psFree(output);
     1073    return true;
     1074  }
     1075
     1076  if (!ippdbPrintMetadatas(stdout, output, "minidvodbRun", !simple)) {
     1077    psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1078    psFree(output);
     1079    return false;
     1080  }
     1081
     1082  psFree(output);
     1083
     1084  return true;
     1085}
     1086
     1087
     1088
     1089static bool listminidvodbrunMode(pxConfig *config) {
     1090    PS_ASSERT_PTR_NON_NULL(config, false);
     1091    psMetadata *where = psMetadataAlloc();
     1092
     1093    PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbRun.minidvodb_name", "LIKE");
     1094    PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbRun.minidvodb_group", "==");
     1095    PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbRun.minidvodb_id", "==");
     1096
     1097    PXOPT_COPY_STR(config->args, where, "-state", "minidvodbRun.state", "==");
     1098    PXOPT_LOOKUP_U64(limit,      config->args, "-limit", false, false);
     1099    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1100
     1101
     1102    if (!psListLength(where->list)) {
     1103      psFree(where);
     1104      psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1105      return false;
     1106    }
     1107
     1108    psString query = pxDataGet("addtool_find_minidvodbrun.sql");
     1109
     1110    if (!query) {
     1111      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1112      return false;
     1113    }
     1114
     1115    if (psListLength(where->list)) {
     1116      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1117      psStringAppend(&query, " WHERE %s", whereClause);
     1118      psFree(whereClause);
     1119    }
     1120
     1121    if (limit) {
     1122      psString limitString = psDBGenerateLimitSQL(limit);
     1123      psStringAppend(&query, " %s", limitString);
     1124      psFree(limitString);
     1125    }
     1126
     1127    if (!p_psDBRunQuery(config->dbh, query)) {
     1128      psError(PS_ERR_UNKNOWN, false, "database error");
     1129      psFree(query);
     1130      return false;
     1131    }
     1132
     1133    psFree(query);
     1134    psArray *output = p_psDBFetchResult(config->dbh);
     1135    if (!output) {
     1136      psError(PS_ERR_UNKNOWN, false, "database error");
     1137      return false;
     1138    }
     1139    if (!psArrayLength(output)) {
     1140      psTrace("addtool", PS_LOG_INFO, "no rows found");
     1141      psFree(output);
     1142      return true;
     1143    }
     1144
     1145    if (!ippdbPrintMetadatas(stdout, output, "minidvodbRun",  !simple)) {
     1146      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1147      psFree(output);
     1148      return false;
     1149    }
     1150
     1151    psFree(output);
     1152
     1153return true;
     1154}
     1155
     1156static bool addminidvodbprocessedMode(pxConfig *config) {
     1157
     1158  PS_ASSERT_PTR_NON_NULL(config, false);
     1159
     1160    // required
     1161  PXOPT_LOOKUP_U64(minidvodb_id, config->args, "-minidvodb_id", true, false);
     1162  PXOPT_LOOKUP_STR(mergedvodb_path, config->args, "-mergedvodb_path", true, false);
     1163  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-minidvodb_group", true, false);
     1164
     1165    // optional
     1166  PXOPT_LOOKUP_U64(merge_order,     config->args, "-merge_order", false, false);
     1167  PXOPT_LOOKUP_F32(dtime_relphot, config->args, "-dtime_relphot", false, false);
     1168  PXOPT_LOOKUP_F32(dtime_resort, config->args, "-dtime_resort", false, false);
     1169  PXOPT_LOOKUP_F32(dtime_merge, config->args, "-dtime_merge", false, false);
     1170  PXOPT_LOOKUP_TIME(epoch, config->args, "-epoch", false, false);
     1171  PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
     1172    //generate restrictions
     1173  psMetadata *where = psMetadataAlloc();
     1174  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",   "minidvodbRun.minidvodb_id",   "==");
     1175
     1176
     1177  if (!psDBTransaction(config->dbh)) {
     1178    psError(PS_ERR_UNKNOWN, false, "database error");
     1179    return false;
     1180  }
     1181
     1182
     1183  psString query = pxDataGet("addtool_find_pendingmergeprocess.sql");
     1184  if (!query) {
     1185    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1186    return false;
     1187  }
     1188  // use psDBGenerateWhereSQL because the SQL yields an intermediate table
     1189  if (psListLength(where->list)) {
     1190    psString whereClaus = psDBGenerateWhereConditionSQL(where, NULL);
     1191    psStringAppend(&query, " AND %s", whereClaus);
     1192    psFree(whereClaus);
     1193  }
     1194  psFree(where);
     1195
     1196  if (!p_psDBRunQuery(config->dbh, query)) {
     1197    psError(PS_ERR_UNKNOWN, false, "database error");
     1198    psFree(query);
     1199    return false;
     1200  }
     1201  psFree(query);
     1202
     1203  psArray *output = p_psDBFetchResult(config->dbh);
     1204  if (!output) {
     1205    psError(PS_ERR_UNKNOWN, false, "database error");
     1206    return false;
     1207  }
     1208  if (!psArrayLength(output)) {
     1209    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1210    psFree(output);
     1211    return true;
     1212  }
     1213
     1214  minidvodbRunRow *pendingRow = minidvodbRunObjectFromMetadata(output->data[0]);
     1215  psFree(output);
     1216  minidvodbProcessedRow *row = minidvodbProcessedRowAlloc(
     1217               pendingRow->minidvodb_id,
     1218               merge_order,
     1219               dtime_resort,
     1220               dtime_relphot,
     1221               dtime_merge,
     1222               epoch,
     1223               mergedvodb_path,
     1224               fault
     1225               );
     1226
     1227  if (!minidvodbProcessedInsertObject(config->dbh, row)) {
     1228    // rollback
     1229    if (!psDBRollback(config->dbh)) {
     1230      psError(PS_ERR_UNKNOWN, false, "database error");
     1231    }
     1232    psError(PS_ERR_UNKNOWN, false, "database error");
     1233    psFree(row);
     1234    psFree(pendingRow);
     1235    return false;
     1236  }
     1237
     1238
     1239
     1240
     1241  //this finds the # of merged things (for the merge order)
     1242  psString query3 = NULL;
     1243  psStringAppend(&query3, "select count(*) from minidvodbRun join minidvodbProcessed using (minidvodb_id) where state = 'merged' and minidvodb_group = '%s';", minidvodb_group);
     1244
     1245  if (!p_psDBRunQuery(config->dbh, query3)) {
     1246    // rollback
     1247    if (!psDBRollback(config->dbh)) {
     1248      psError(PS_ERR_UNKNOWN, false, "database error");
     1249    }
     1250    psError(PS_ERR_UNKNOWN, false, "database error");
     1251    psFree(query3);
     1252    return false;
     1253  }
     1254  psArray *output2 = p_psDBFetchResult(config->dbh);
     1255  if (!output2) {
     1256    psError(PS_ERR_UNKNOWN, false, "database error");
     1257    return false;
     1258  }
     1259  if (!psArrayLength(output2)) {
     1260    psTrace("addtool", PS_LOG_INFO, "no rows found");
     1261    psFree(output);
     1262    return true;
     1263  }
     1264  bool status;
     1265  psS64 m_order = psMetadataLookupS64(&status, output2->data[0], "count(*)");
     1266  if (!status) {
     1267
     1268    psAbort("failed to lookup value for count column");
     1269    return false;
     1270  }
     1271  psString final = NULL;
     1272  psStringAppend(&final, "%" PRIu64, m_order);
     1273    //return false;
     1274  psFree(query3);
     1275  psFree(output2);
     1276
     1277
     1278
     1279  //update the merge_order
     1280
     1281  psString query4 = NULL;
     1282  psStringAppend(&query4, "update minidvodbProcessed set merge_order = %"PRIu64,m_order);
     1283  psStringAppend(&query4," where minidvodb_id = %" PRIu64,  minidvodb_id);
     1284  printf("%s", query4);
     1285  if (!p_psDBRunQuery(config->dbh, query4)) {
     1286    // rollback
     1287    if (!psDBRollback(config->dbh)) {
     1288      psError(PS_ERR_UNKNOWN, false, "database error");
     1289    }
     1290    psError(PS_ERR_UNKNOWN, false, "database error");
     1291    psFree(query4);
     1292    return false;
     1293  }
     1294  //
     1295psFree(query4);
     1296
     1297
     1298
     1299// since there is only one exp per 'new' set mindvodbRun.state = 'merged'
     1300
     1301  psString query2 = NULL ;
     1302  psStringAppend(&query2, "UPDATE minidvodbRun SET state = 'merged' WHERE minidvodb_id = %'" PRIu64, row->minidvodb_id);
     1303
     1304  if (!p_psDBRunQuery(config->dbh, query2)) {
     1305    // rollback
     1306    if (!psDBRollback(config->dbh)) {
     1307      psError(PS_ERR_UNKNOWN, false, "database error");
     1308    }
     1309    psError(PS_ERR_UNKNOWN, false, "database error");
     1310    psFree(query2);
     1311    return false;
     1312  }
     1313
     1314
     1315
     1316
     1317
     1318
     1319
     1320
     1321  psFree(row);
     1322  psFree(pendingRow);
     1323
     1324
     1325
     1326
     1327
     1328  //commit the changes
     1329  if (!psDBCommit(config->dbh)) {
     1330    psError(PS_ERR_UNKNOWN, false, "database error");
     1331    return false;
     1332  }
     1333
     1334
     1335  //print the merge_order (why not!)
     1336  printf("%s", final);
     1337  psFree(final);
     1338
     1339  return true;
     1340}
     1341
     1342
     1343
     1344static bool listminidvodbprocessedMode(pxConfig *config) {
     1345  psMetadata *where = psMetadataAlloc();
     1346  PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbProcessed.minidvodb_id", "==");
     1347  PXOPT_COPY_STR(config->args, where, "-minidvodb_name", "minidvodbProcessed.minidvodb_name", "==");
     1348  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "minidvodbProcessed.minidvodb_group", "==");
     1349  PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     1350  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     1351  PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
     1352  if (!psListLength(where->list)) {
     1353        psFree(where);
     1354        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1355        return false;
     1356    }
     1357
     1358  psString query = pxDataGet("addtool_find_minidvodbprocessed.sql");
     1359    if (!query) {
     1360        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1361        return false;
     1362    }
     1363
     1364if (psListLength(where->list)) {
     1365        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1366        psStringAppend(&query, " WHERE %s", whereClause);
     1367        psFree(whereClause);
     1368    }
     1369
     1370// we either add AND (condition) or WHERE (condition):
     1371    if (where->list && faulted) {
     1372        // list only faulted rows
     1373        psStringAppend(&query, " %s", " AND minidvodbProcessed.fault != 0");
     1374    }
     1375    if (where->list && !faulted) {
     1376        // don't list faulted rows
     1377        psStringAppend(&query, " %s", " AND minidvodbProcessed.fault = 0");
     1378    }
     1379    if (!where->list && faulted) {
     1380        // list only faulted rows
     1381        psStringAppend(&query, " %s", " WHERE minidvodbProcessed.fault != 0");
     1382    }
     1383    if (!where->list && !faulted) {
     1384        // don't list faulted rows
     1385        psStringAppend(&query, " %s", " WHERE minidvodbProcessed.fault = 0");
     1386    }
     1387    psFree(where);
     1388
     1389    // order by epoch
     1390    psStringAppend(&query, " ORDER BY minidvodb_id");
     1391
     1392    // treat limit == 0 as "no limit"
     1393    if (limit) {
     1394        psString limitString = psDBGenerateLimitSQL(limit);
     1395        psStringAppend(&query, " %s", limitString);
     1396        psFree(limitString);
     1397    }
     1398
     1399    if (!p_psDBRunQuery(config->dbh, query)) {
     1400      psError(PS_ERR_UNKNOWN, false, "database error ");
     1401        psFree(query);
     1402        return false;
     1403    }
     1404    psFree(query);
     1405
     1406    psArray *output = p_psDBFetchResult(config->dbh);
     1407    if (!output) {
     1408        psError(PS_ERR_UNKNOWN, false, "database error");
     1409        return false;
     1410    }
     1411    if (!psArrayLength(output)) {
     1412        psTrace("addtool", PS_LOG_INFO, "no rows found");
     1413        psFree(output);
     1414        return true;
     1415    }
     1416
     1417    // negate simple so the default is true
     1418    if (!ippdbPrintMetadatas(stdout, output, "minidvodbProcessed", !simple)) {
     1419        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1420        psFree(output);
     1421        return false;
     1422    }
     1423
     1424    psFree(output);
     1425
     1426return true;
     1427}
     1428
     1429
     1430
     1431static bool revertminidvodbprocessedMode(pxConfig *config) {
     1432  psMetadata *where = psMetadataAlloc();
     1433  PS_ASSERT_PTR_NON_NULL(config, false);
     1434  PXOPT_COPY_S64(config->args, where, "-minidvodb_id", "minidvodbProcessed.minidvodb_id", "==");
     1435  PXOPT_COPY_STR(config->args, where, "-minidvodb_group", "addRun.minidvodb_group", "==");
     1436
     1437  if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
     1438    psFree(where);
     1439    psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1440    return false;
     1441  }
     1442
     1443  if (!psDBTransaction(config->dbh)) {
     1444      psError(PS_ERR_UNKNOWN, false, "database error");
     1445      psFree(where);
     1446      return false;
     1447  }
     1448
     1449  {
     1450    psString query = pxDataGet("addtool_revertminidvodbprocessed.sql");
     1451    if (!query) {
     1452      // rollback
     1453      if (!psDBRollback(config->dbh)) {
     1454        psError(PS_ERR_UNKNOWN, false, "database error");
     1455      }
     1456      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1457      psFree(where);
     1458      return false;
     1459    }
     1460
     1461    // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
     1462    if (where && psListLength(where->list)) {
     1463      psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1464      psStringAppend(&query, " AND %s", whereClause);
     1465      psFree(whereClause);
     1466    }
     1467
     1468    if (!p_psDBRunQuery(config->dbh, query)) {
     1469      // rollback
     1470      if (!psDBRollback(config->dbh)) {
     1471        psError(PS_ERR_UNKNOWN, false, "database error");
     1472      }
     1473      psError(PS_ERR_UNKNOWN, false, "database error");
     1474      psFree(query);
     1475      psFree(where);
     1476            return false;
     1477    }
     1478    psFree(query);
     1479  }
     1480  psFree(where);
     1481
     1482  if (!psDBCommit(config->dbh)) {
     1483    psError(PS_ERR_UNKNOWN, false, "database error");
     1484    return false;
     1485  }
     1486
     1487  return true;
     1488}
     1489
     1490
     1491
     1492static bool updateminidvodbprocessedMode(pxConfig *config) {
     1493  PS_ASSERT_PTR_NON_NULL(config, false);
     1494  psMetadata *where = psMetadataAlloc();
     1495
     1496  PXOPT_LOOKUP_U64(minidvodb_id,  config->args, "-minidvodb_id", true, false);
     1497  PXOPT_LOOKUP_U64(merge_order,  config->args, "-set_merge_order", false, false);
     1498  PXOPT_LOOKUP_S16(fault,  config->args, "-set_fault", false, false);
     1499  PXOPT_LOOKUP_F32(dtime_relphot,  config->args, "-set_dtime_relphot", false, false);
     1500  PXOPT_LOOKUP_F32(dtime_resort,  config->args, "-set_dtime_resort", false, false);
     1501  PXOPT_LOOKUP_F32(dtime_merge,  config->args, "-set_dtime_merge", false, false);
     1502
     1503  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",     "minidvodbProcessed.minidvodb_id", "==");
     1504  PXOPT_COPY_STR(config->args, where, "-minidvodb_name",     "minidvodbRun.minidvodb_name", "==");
     1505
     1506
     1507  if (!psListLength(where->list)) {
     1508    psFree(where);
     1509    psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1510    return false;
     1511  }
     1512
     1513  psString query = psStringCopy("UPDATE minidvodbProcessed JOIN minidvodbRun USING (minidvodb_id) SET ");
     1514  int cnt = 0;
     1515  psString comma = ",";
     1516  if (fault) {
     1517    psStringAppend(&query, " fault = %d", fault);
     1518  cnt++;
     1519  }
     1520
     1521  if (merge_order) {
     1522    if (cnt) {
     1523      psStringAppend(&query, "%s", comma);
     1524    }
     1525
     1526    psStringAppend(&query, " merge_order = %" PRId64, merge_order);
     1527    cnt++;
     1528  }
     1529
     1530  if (dtime_relphot) {
     1531    if (cnt) {
     1532      psStringAppend(&query, "%s", comma);
     1533    }
     1534    psStringAppend(&query, " dtime_relphot = %f", dtime_relphot);
     1535    cnt++;
     1536  }
     1537
     1538  if (dtime_resort) {
     1539    if (cnt) {
     1540      psStringAppend(&query, "%s", comma);
     1541    }
     1542    psStringAppend(&query, " dtime_resort = %f", dtime_resort);
     1543    cnt++;
     1544  }
     1545
     1546  if (dtime_merge) {
     1547    if (cnt) {
     1548      psStringAppend(&query, "%s", comma);
     1549    }
     1550    psStringAppend(&query, " dtime_merge = %f", dtime_merge);
     1551    cnt++;
     1552 }
     1553
     1554  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1555  psStringAppend(&query, " WHERE %s", whereClause);
     1556
     1557  if (!p_psDBRunQuery(config->dbh, query)) {
     1558   psError(PS_ERR_UNKNOWN, false, "database error");
     1559   psFree(query);
     1560   return false;
     1561  }
     1562
     1563  psFree(query);
     1564  psFree(where);
     1565
     1566  return true;
     1567}
     1568
     1569
Note: See TracChangeset for help on using the changeset viewer.