/**
 * \brief Create the new ars tables and populate them from the job/jobqueue data.
 *
 * \param $DryRun Do not insert the ars records into the db.  Just print the insert statements.
 *        The ars table will still be created if it doesn't exist.
 *
 * \return 0 on success, 1 on failure
 **/
function Migrate_14_20($DryRun)
{
    global $PG_CONN;
    /* array of agent_name, ars file name for tables to create */
    $ARSarray = array("pkgagent" => "pkgagent_ars", "copyright" => "copyright_ars", "mimetype" => "mimetype_ars", "unpack" => "ununpack_ars", "ununpack" => "ununpack_ars");
    foreach ($ARSarray as $agent_name => $ARStablename) {
        /* Create the ars table if it doesn't exist */
        CreateARStable($ARStablename, $DryRun);
        /* Get the agent_pk */
        $agent_pk = GetAgentKey($agent_name, "");
        /* Select the jobqueue records for this agent */
        $sql = "select distinct job_upload_fk, jq_type, jq_starttime, jq_endtime from jobqueue\n              join job on jq_job_fk=job_pk where jq_type='{$agent_name}' and (jq_end_bits=1) order by jq_type";
        $result = pg_query($PG_CONN, $sql);
        DBCheckResult($result, $sql, __FILE__, __LINE__);
        /* Loop through jobqueue records inserting ars table rows */
        while ($row = pg_fetch_assoc($result)) {
            $upload_fk = $row['job_upload_fk'];
            /* prevent duplicate insert */
            $sql = "select ars_pk from {$ARStablename} where agent_fk={$agent_pk} and upload_fk={$upload_fk} and ars_success=true";
            $checkrec = pg_query($PG_CONN, $sql);
            DBCheckResult($checkrec, $sql, __FILE__, __LINE__);
            $num_rows = pg_num_rows($checkrec);
            pg_free_result($checkrec);
            if ($num_rows > 0) {
                continue;
            }
            /* add ars rec */
            $sql = "insert into {$ARStablename} (agent_fk, upload_fk, ars_success, ars_starttime, ars_endtime)\n                  values ({$agent_pk}, {$upload_fk}, true, '{$row['jq_starttime']}', '{$row['jq_endtime']}')";
            if ($DryRun) {
                echo "DryRun: {$sql}\n";
            } else {
                $insresult = pg_query($PG_CONN, $sql);
                DBCheckResult($insresult, $sql, __FILE__, __LINE__);
                pg_free_result($insresult);
            }
        }
        pg_free_result($result);
    }
    return 0;
}
Example #2
0
/**
 * \brief Check the ARS table to see if an agent has successfully scanned an upload.
 *
 * \param $upload_pk - the upload will be checked
 * \param $AgentName - Agent name, eg "nomos"
 * \param $AgentDesc - Agent description, eg "license scanner"
 * \param $AgentARSTableName - Agent ars table name, eg "nomos_ars"
 *
 * \returns:
 * - 0 = no
 * - 1 = yes, from latest agent version
 * - 2 = yes, from older agent version (does not apply to adj2nest)
 */
function CheckARS($upload_pk, $AgentName, $AgentDesc, $AgentARSTableName)
{
    /* get the latest agent_pk */
    $Latest_agent_pk = GetAgentKey($AgentName, $AgentDesc);
    /* get last agent pk with successful results */
    $Last_successful_agent_pk = LatestAgentpk($upload_pk, $AgentARSTableName);
    if (!empty($Latest_agent_pk) and !empty($Last_successful_agent_pk) and $Latest_agent_pk == $Last_successful_agent_pk) {
        return 1;
    }
    if (!empty($Latest_agent_pk) and !empty($Last_successful_agent_pk)) {
        return 2;
    }
    return 0;
}
 /**
  * \brief Check if the upload has results from this agent.
  *
  * \param $upload_pk
  *
  * \returns
  * 0 = no
  * 1 = yes, from latest agent version
  * 2 = yes, from older agent version
  */
 function AgentHasResults($upload_pk)
 {
     global $SysConf;
     /* Get the users.default_bucketpool_fk */
     $user_pk = $SysConf['auth']['UserId'];
     /* Unless the user is authenticated, we can't do anything. */
     if (empty($user_pk)) {
         return 0;
     }
     /* Get users default bucketpool so we know which bucketpool to use. */
     $usersRec = GetSingleRec("users", "where user_pk='{$user_pk}'");
     $default_bucketpool_fk = $usersRec['default_bucketpool_fk'];
     if (empty($default_bucketpool_fk)) {
         return 0;
     }
     /* get the latest nomos agent_pk */
     $Latest_nomos_agent_pk = GetAgentKey("nomos", "Nomos license scanner");
     /* get the latest bucket agent_pk */
     $Latest_bucket_agent_pk = GetAgentKey($this->AgentName, "Bucket scanner");
     if (empty($Latest_nomos_agent_pk) || empty($Latest_bucket_agent_pk)) {
         return 0;
     }
     // no any nomos or bucket agent in agent table
     /* see if the latest nomos and bucket agents have scaned this upload for this bucketpool */
     $bucket_arsRec = GetSingleRec("bucket_ars", "where bucketpool_fk='{$default_bucketpool_fk}' and upload_fk='{$upload_pk}' and nomosagent_fk='{$Latest_nomos_agent_pk}' and agent_fk='{$Latest_bucket_agent_pk}' and ars_success='true'");
     if (!empty($bucket_arsRec)) {
         return 1;
     }
     /* see if older nomos and/or bucket agents have scaned this upload for this bucketpool */
     $bucket_arsRec = GetSingleRec("bucket_ars", "where bucketpool_fk='{$default_bucketpool_fk}' and upload_fk='{$upload_pk}' and ars_success='true'");
     if (!empty($bucket_arsRec)) {
         return 2;
     }
     return 0;
 }