示例#1
0
 public function execute()
 {
     $action = JobAction::newFromContext($this->getContext());
     $action->doAction();
     $this->setAction($action);
     $this->content = $this->initContent();
 }
示例#2
0
 /**
  * @actionParam string sort: [optional] What to sort the results by.
  *  Must be one of "title", "id" or "creation". Defaults to "title".
  * @actionParam string sort_dir: [optional]
  *  Must be one of "asc" (ascending) or "desc" (decending). Defaults to "asc".
  */
 public function doAction()
 {
     $db = $this->getContext()->getDB();
     $request = $this->getContext()->getRequest();
     $sortField = $request->getVal('sort', 'title');
     $sortDir = $request->getVal('sort_dir', 'asc');
     if (!in_array($sortField, array('title', 'id', 'creation'))) {
         $this->setError('invalid-input', "Unknown sort `{$sortField}`.");
         return;
     }
     if (!in_array($sortDir, array('asc', 'desc'))) {
         $this->setError('invalid-input', "Unknown sort direction `{$sortDir}`.");
         return;
     }
     $sortDirQuery = '';
     switch ($sortDir) {
         case 'asc':
             $sortDirQuery = 'ASC';
             break;
         case 'desc':
             $sortDirQuery = 'DESC';
             break;
     }
     $sortFieldQuery = '';
     switch ($sortField) {
         case 'title':
             $sortFieldQuery = "ORDER BY display_title {$sortDirQuery}";
             break;
         case 'id':
             $sortFieldQuery = "ORDER BY id {$sortDirQuery}";
             break;
         case 'creation':
             $sortFieldQuery = "ORDER BY created {$sortDirQuery}";
             break;
     }
     $projects = array();
     $projectRows = $db->getRows("SELECT\n\t\t\t\tid,\n\t\t\t\tdisplay_title,\n\t\t\t\tcreated\n\t\t\tFROM projects\n\t\t\t{$sortFieldQuery};");
     if ($projectRows) {
         foreach ($projectRows as $projectRow) {
             // Get information about the latest job (if any)
             $jobRow = $db->getRow(str_queryf('SELECT id FROM jobs WHERE project_id = %s ORDER BY id DESC LIMIT 1;', $projectRow->id));
             if (!$jobRow) {
                 $job = false;
             } else {
                 $jobAction = JobAction::newFromContext($this->getContext()->createDerivedRequestContext(array('item' => $jobRow->id)));
                 $jobAction->doAction();
                 $job = $jobAction->getData();
             }
             $project = array('id' => $projectRow->id, 'displayTitle' => $projectRow->display_title, 'job' => $job);
             self::addTimestampsTo($project, $projectRow->created, 'created');
             $projects[] = $project;
         }
     }
     $this->setData($projects);
 }
示例#3
0
 public function doAction()
 {
     $db = $this->getContext()->getDB();
     $request = $this->getContext()->getRequest();
     $userName = $request->getVal("item");
     if (!$userName) {
         $this->setError("missing-parameters");
         return;
     }
     $userID = $db->getOne(str_queryf("SELECT id FROM users WHERE name = %s;", $userName));
     $userID = intval($userID);
     if (!$userID) {
         $this->setError("invalid-input", "User does not exist");
         return;
     }
     $uaIndex = BrowserInfo::getSwarmUAIndex();
     // Active clients
     $activeClients = array();
     $clientRows = $db->getRows(str_queryf("SELECT\n\t\t\t\tuseragent_id,\n\t\t\t\tuseragent,\n\t\t\t\tcreated\n\t\t\tFROM\n\t\t\t\tclients\n\t\t\tWHERE user_id = %u\n\t\t\tAND   updated > %s\n\t\t\tORDER BY created DESC;", $userID, swarmdb_dateformat(strtotime("1 minutes ago"))));
     if ($clientRows) {
         foreach ($clientRows as $clientRow) {
             $bi = BrowserInfo::newFromContext($this->getContext(), $clientRow->useragent);
             $activeClient = array("uaID" => $clientRow->useragent_id, "uaRaw" => $bi->getRawUA(), "uaData" => $bi->getSwarmUaItem(), "uaBrowscap" => $bi->getBrowscap());
             self::addTimestampsTo($activeClient, $clientRow->created, "connected");
             $activeClients[] = $activeClient;
         }
     }
     // Recent jobs
     $recentJobs = array();
     // List of all user agents used in recent jobs
     // This is as helper allow creating proper gaps when iterating
     // over jobs.
     $userAgents = array();
     $jobRows = $db->getRows(str_queryf("SELECT\n\t\t\t\tid,\n\t\t\t\tname\n\t\t\tFROM\n\t\t\t\tjobs\n\t\t\tWHERE jobs.user_id = %u\n\t\t\tORDER BY jobs.created DESC\n\t\t\tLIMIT 15;", $userID));
     if ($jobRows) {
         $uaRunStatusStrength = array_flip(array("passed", "new", "progress", "failed", "timedout", "error"));
         foreach ($jobRows as $jobRow) {
             $jobID = intval($jobRow->id);
             $jobActionContext = $this->getContext()->createDerivedRequestContext(array("action" => "job", "item" => $jobID), "GET");
             $jobAction = JobAction::newFromContext($jobActionContext);
             $jobAction->doAction();
             if ($jobAction->getError()) {
                 $this->setError($jobAction->getError());
                 return;
             }
             $jobActionData = $jobAction->getData();
             // Add user agents array of this job to the overal user agents list.
             // php array+ automatically fixes clashing keys. The values are always the same
             // so it doesn't matter whether or not it overwrites.
             $userAgents += $jobActionData["userAgents"];
             // The summerized status for each user agent run
             // of this job. e.g. if all are new except one,
             // then it will be on "progress", if all are complete
             // then the worst failure is put in the summary
             $uaSummary = array();
             $uaNotNew = array();
             $uaHasIncomplete = array();
             $uaStrongestStatus = array();
             foreach ($jobActionData["runs"] as $run) {
                 foreach ($run["uaRuns"] as $uaID => $uaRun) {
                     if ($uaRun["runStatus"] !== "new" && !in_array($uaID, $uaNotNew)) {
                         $uaNotNew[] = $uaID;
                     }
                     if ($uaRun["runStatus"] === "new" || $uaRun["runStatus"] === "progress") {
                         if (!in_array($uaID, $uaHasIncomplete)) {
                             $uaHasIncomplete[] = $uaID;
                         }
                     }
                     if (!isset($uaStrongestStatus[$uaID]) || $uaRunStatusStrength[$uaRun["runStatus"]] > $uaRunStatusStrength[$uaStrongestStatus[$uaID]]) {
                         $uaStrongestStatus[$uaID] = $uaRun["runStatus"];
                     }
                     $uaSummary[$uaID] = !in_array($uaID, $uaNotNew) ? "new" : (in_array($uaID, $uaHasIncomplete) ? "progress" : $uaStrongestStatus[$uaID]);
                 }
             }
             $recentJobs[] = array("id" => $jobID, "name" => $jobRow->name, "url" => swarmpath("job/{$jobID}", "fullurl"), "uaSummary" => $uaSummary);
         }
     }
     natcaseksort($userAgents);
     $this->setData(array("userName" => $userName, "activeClients" => $activeClients, "recentJobs" => $recentJobs, "uasInJobs" => $userAgents));
 }
示例#4
0
    /**
     * @actionParam string item: Project ID.
     */
    public function doAction()
    {
        $conf = $this->getContext()->getConf();
        $db = $this->getContext()->getDB();
        $request = $this->getContext()->getRequest();
        $projectID = $request->getVal('item');
        if (!$projectID) {
            $this->setError('missing-parameters');
            return;
        }
        // Note: The job list is reverse chronologic (descending).
        // To query the "next" page, you get the jobs with an id
        // that is lower than the last entry on the current page.
        // Parameters for navigation of job list in this project
        $dir = $request->getVal('dir', '');
        $offset = $request->getInt('offset');
        $limit = $request->getInt('limit', $this->defaultLimit);
        if (!in_array($dir, array('', 'back')) || $limit < 1 || $limit > 100) {
            $this->setError('invalid-input');
            return;
        }
        // Get project info
        $projectRow = $db->getRow(str_queryf('SELECT
				id,
				display_title,
				site_url,
				updated,
				created
			FROM projects
			WHERE id = %s;', $projectID));
        if (!$projectRow) {
            $this->setError('invalid-input', 'Project does not exist');
            return;
        }
        $conds = '';
        if ($offset) {
            if ($dir === 'back') {
                $conds = 'AND id > ' . intval($offset);
            } else {
                $conds = 'AND id < ' . intval($offset);
            }
        }
        // Get list of jobs
        $jobRows = $db->getRows(str_queryf('SELECT
				id,
				name
			FROM
				jobs
			WHERE project_id = %s
			' . $conds . '
			ORDER BY id ' . ($dir === 'back' ? 'ASC' : 'DESC') . '
			LIMIT %u;', $projectID, $limit + 1));
        $jobs = array();
        // List of all user agents used in recent jobs
        // This is as helper to allow easy creation of placeholder gaps in a UI
        // when iterating over jobs, because not all jobs have the same user agents.
        $userAgents = array();
        if (!$jobRows) {
            $pagination = array();
        } else {
            $pagination = $this->getPaginationData($dir, $offset, $limit, $jobRows, $projectID);
            if ($dir === 'back') {
                $jobRows = array_reverse($jobRows);
            }
            foreach ($jobRows as $jobRow) {
                $jobID = intval($jobRow->id);
                $jobAction = JobAction::newFromContext($this->getContext()->createDerivedRequestContext(array('item' => $jobID)));
                $jobAction->doAction();
                if ($jobAction->getError()) {
                    $this->setError($jobAction->getError());
                    return;
                }
                $jobActionData = $jobAction->getData();
                // Add user agents array of this job to the overal user agents list.
                // php array+ automatically fixes clashing keys. The values are always the same
                // so it doesn't matter whether or not it overwrites.
                $userAgents += $jobActionData['userAgents'];
                $jobs[] = array('info' => $jobActionData['info'], 'name' => $jobRow->name, 'summaries' => $jobActionData['uaSummaries']);
            }
        }
        uasort($userAgents, 'BrowserInfo::sortUaData');
        $projectInfo = (array) $projectRow;
        unset($projectInfo['updated'], $projectInfo['created']);
        self::addTimestampsTo($projectInfo, $projectRow->updated, 'updated');
        self::addTimestampsTo($projectInfo, $projectRow->created, 'created');
        $this->setData(array('info' => $projectInfo, 'jobs' => $jobs, 'pagination' => $pagination, 'userAgents' => $userAgents));
    }