Пример #1
0
    /**
     * @param string|bool $name
     */
    protected function getActiveClients($name = false)
    {
        $context = $this->getContext();
        $db = $context->getDB();
        $nameQuery = $name ? 'AND name = \'' . $db->strEncode($name) . '\'' : '';
        $results = array();
        $rows = $db->getRows(str_queryf("SELECT\n\t\t\t\tid,\n\t\t\t\tname,\n\t\t\t\tuseragent,\n\t\t\t\tupdated,\n\t\t\t\tcreated\n\t\t\tFROM\n\t\t\t\tclients\n\t\t\tWHERE updated >= %s\n\t\t\t{$nameQuery}\n\t\t\tORDER BY created DESC;", swarmdb_dateformat(Client::getMaxAge($context))));
        if ($rows) {
            foreach ($rows as $row) {
                $bi = BrowserInfo::newFromContext($this->getContext(), $row->useragent);
                $resultRow = $db->getRow(str_queryf('SELECT
						id,
						run_id,
						client_id,
						status,
						total,
						fail,
						error,
						updated,
						created
					FROM runresults
					WHERE client_id = %u
					ORDER BY created DESC
					LIMIT 1;', $row->id));
                $client = array('id' => $row->id, 'name' => $row->name, 'uaID' => $bi->getSwarmUaID(), 'uaRaw' => $bi->getRawUA(), 'uaData' => $bi->getUaData(), 'viewUrl' => swarmpath("client/{$row->id}"), 'lastResult' => !$resultRow ? null : array('id' => intval($resultRow->id), 'viewUrl' => swarmpath("result/{$resultRow->id}"), 'status' => JobAction::getRunresultsStatus($resultRow)));
                self::addTimestampsTo($client, $row->created, 'connected');
                self::addTimestampsTo($client, $row->updated, 'pinged');
                $results[$row->id] = $client;
            }
        }
        return $results;
    }
Пример #2
0
 public function getBrowserInfo()
 {
     if ($this->browserInfo === null) {
         $ua = isset($_SERVER["HTTP_USER_AGENT"]) ? $_SERVER["HTTP_USER_AGENT"] : "";
         $this->browserInfo = BrowserInfo::newFromContext($this, $ua);
     }
     return $this->browserInfo;
 }
Пример #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: Client id.
     */
    public function doAction()
    {
        $context = $this->getContext();
        $db = $context->getDB();
        $request = $context->getRequest();
        $item = $request->getInt('item');
        if (!$item) {
            $this->setError('missing-parameters');
            return;
        }
        // Client information
        $row = $db->getRow(str_queryf('SELECT
				id,
				name,
				useragent,
				updated,
				created
			FROM
				clients
			WHERE id = %u;', $item));
        if (!$row) {
            $this->setError('invalid-input', 'Client not found');
            return;
        }
        $bi = BrowserInfo::newFromContext($context, $row->useragent);
        $info = array('id' => intval($row->id), 'name' => $row->name, 'viewUrl' => swarmpath("clients/{$row->name}"), 'uaID' => $bi->getSwarmUaID(), 'uaRaw' => $bi->getRawUA(), 'uaData' => $bi->getUaData(), 'sessionAge' => gmstrtotime($row->updated) - gmstrtotime($row->created));
        self::addTimestampsTo($info, $row->created, 'connected');
        self::addTimestampsTo($info, $row->updated, 'pinged');
        // Run results
        $results = array();
        $rows = $db->getRows(str_queryf('SELECT
				id,
				run_id,
				client_id,
				status,
				total,
				fail,
				error,
				updated,
				created
			FROM runresults
			WHERE client_id = %u
			ORDER BY created DESC;', $item));
        if ($rows) {
            foreach ($rows as $row) {
                $runRow = $jobRow = false;
                $result = array('id' => intval($row->id), 'viewUrl' => swarmpath("result/{$row->id}"), 'status' => JobAction::getRunresultsStatus($row));
                $runRow = $db->getRow(str_queryf('SELECT
						name,
						job_id
					FROM runs
					WHERE id = %u;', $row->run_id));
                if ($runRow) {
                    $jobRow = $db->getRow(str_queryf('SELECT
							id,
							name,
							project_id
						FROM
							jobs
						WHERE id = %u', $runRow->job_id));
                    if ($jobRow) {
                        $projectRow = $db->getRow(str_queryf('SELECT
								display_title
							FROM projects
							WHERE id = %s;', $jobRow->project_id));
                        $result['job'] = array('nameText' => strip_tags($jobRow->name), 'viewUrl' => swarmpath("job/{$jobRow->id}"));
                        $result['run'] = array('name' => $runRow->name);
                        $result['project'] = array('id' => $jobRow->project_id, 'display_title' => $projectRow->display_title, 'viewUrl' => swarmpath("project/{$jobRow->project_id}"));
                    }
                }
                // Runs and jobs could be deleted, results are preserved.
                if (!$jobRow) {
                    $result['job'] = null;
                    $result['run'] = null;
                    $result['project'] = null;
                }
                $results[] = $result;
            }
        }
        $this->setData(array('info' => $info, 'results' => $results));
    }