activity.ajax 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?php
#
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
chdir("..");
include_once("webtask.php");
chdir("apt");
include_once("profile_defs.php");
include_once("instance_defs.php");
include_once("aggregate_defs.php");

$portals = array();
if ($TBMAINSITE) {
    $portals["www.aptlab.net"]         = "APT";
    $portals["www.cloudlab.us"]        = "Cloud";
    $portals["www.emulab.net"]         = "Emulab";
    $portals["www.phantomnet.org"]     = "Phantom";
    $portals["www.powderwireless.net"] = "Powder";
}
else {
    $portals[$APTHOST] = "Emulab";
}

function Do_Search()
{
    global $this_user, $urn_mapping;
    global $ajax_args, $portals, $APTHOST;
    $isadmin     = (ISADMIN() || ISFOREIGN_ADMIN() ? 1 : 0);
    $results     = array();

    if (!$isadmin) {
        if (isset($ajax_args["target_user"])) {
Leigh Stoller's avatar
Leigh Stoller committed
52
            $target_user = User::LookupByUid($ajax_args["target_user"]);
53 54 55 56 57 58 59 60 61 62
            if (!$target_user) {
                SPITAJAX_ERROR(1, "No such user");
                return;
            }
            if (!$target_user->SameUser($this_user)) {
                SPITAJAX_ERROR(1, "Not enough permission to view info!");
                return;
            }
        }
        elseif (isset($ajax_args["target_project"])) {
63
            $target_project = Project::Lookup($ajax_args["target_project"]);
64 65 66 67 68 69 70 71 72 73 74 75
            if (!$target_project) {
                SPITAJAX_ERROR(1, "No such project");
                return;
            }
            $approved = 0;
            
            if (!$target_project->IsMember($this_user, $approved) &&
                $approved) {
                SPITAJAX_ERROR(1, "Not enough permission to view this info!");
                return;
            }
        }
Leigh Stoller's avatar
Leigh Stoller committed
76 77 78
        else {
            $target_user = $this_user;
        }
79
    }
80 81 82 83 84 85 86 87 88 89 90 91 92 93
    elseif (isset($ajax_args["target_project"])) {
        $target_project = Project::Lookup($ajax_args["target_project"]);
        if (!$target_project) {
            SPITAJAX_ERROR(1, "No such project");
            return;
        }
    }  
    elseif (isset($ajax_args["target_user"])) {
        $target_user = User::LookupByUid($ajax_args["target_user"]);
        if (!$target_user) {
            SPITAJAX_ERROR(1, "No such user");
            return;
        }
    }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    $whereclause = "";

    if (isset($target_project)) {
        $target_idx   = $target_project->pid_idx();
        $whereclause = "where h.pid_idx='$target_idx'";
    }
    elseif (isset($target_user)) {
        $target_idx  = $target_user->idx();
        $whereclause = "where h.creator_idx='$target_idx'";
    }
    if (isset($ajax_args["portalonly"])) {
        if ($whereclause != "") {
            $whereclause = "$whereclause and ";
        }
        else {
            $whereclause = "where ";
        }
        $whereclause .= "servername='$APTHOST' ";
    }
    if (!(isset($ajax_args["min"]) && isset($ajax_args["max"]))) {
        SPITAJAX_ERROR(1, "Must provide min and max");
        return;
    }
    $min = $ajax_args["min"];
    $max = $ajax_args["max"];
    if (!is_numeric($min) || $min < 0) {
        SPITAJAX_ERROR(1, "Bad value for min");
        return;
    }
    if (!is_numeric($max) || $max < 0 || $max < $min) {
        SPITAJAX_ERROR(1, "Bad value for max");
        return;
    }
    # Hand off.
    if ($isadmin && isset($ajax_args["IP"])) {
        return SearchForIP($ajax_args["IP"], $min, $max);
    }
    if ($whereclause != "") {
        $whereclause = "$whereclause and ";
    }
    else {
        $whereclause = "where ";
    }
    if (isset($min)) {
        $whereclause .= "UNIX_TIMESTAMP(h.started) > $min ";
        if (isset($max)) {
            $whereclause .= "and ";
        }
    }
    if (isset($max)) {
        $whereclause .= "UNIX_TIMESTAMP(h.started) < $max ";
    }
    $query_result =
        DBQueryFatal("select h.uuid,h.profile_version,h.created, ".
                 "    h.started,h.destroyed,h.servername, ".
		 "    h.creator,p.uuid as profile_uuid,h.pid,u.email, ".
                 "    h.physnode_count,h.virtnode_count,".
                 "    h.name as instance_name,p.name as profile_name, ".
                 "    truncate(h.physnode_count * ".
                 "      ((UNIX_TIMESTAMP(h.destroyed) - ".
                 "        UNIX_TIMESTAMP(h.started)) / 3600.0),2) as phours, ".
                 "    GROUP_CONCAT(aa.abbreviation) as clusters ".
		 "  from apt_instance_history as h ".
                 "left join apt_instance_aggregate_history as ia ".
                 "     on ia.uuid=h.uuid ".
                 "left join apt_aggregates as aa on aa.urn=ia.aggregate_urn ".
		 "left join apt_profile_versions as p on ".
		 "     p.profileid=h.profile_id and ".
		 "     p.version=h.profile_version ".
		 "left join geni.geni_users as u on u.uuid=h.creator_uuid ".
                 $whereclause . " " .
		 "group by h.uuid order by h.started desc");

    while ($row = mysql_fetch_array($query_result)) {
        $uuid      = $row["uuid"];
	$pname     = $row["profile_name"];
        $iname     = $row["instance_name"];
	$pproj     = $row["pid"];
	$puuid     = $row["profile_uuid"];
	$created   = DateStringGMT($row["created"]);
	$started   = DateStringGMT($row["started"]);
	$destroyed = DateStringGMT($row["destroyed"]);
	$creator   = $row["creator"];
	$email     = $row["email"];
        $pcount    = $row["physnode_count"];
        $vcount    = $row["virtnode_count"];
        $phours    = $row["phours"];
        $portal    = $portals[$row["servername"]];
        $clusters  = $row["clusters"];
        # Backwards compat.
        if (!isset($pproj)) {
            $pproj = "";
        }
        if (!isset($destroyed)) {
            $destroyed = "";
        }
        if (!isset($iname)) {
            $iname = "&nbsp;";
        }
        
	# If a guest user, use email instead.
	if (isset($email)) {
	    $creator = $email;
	}

        # Save space with array instead of hash.
	$instance =
            array($pname, $pproj, $puuid, $pcount, $vcount,
                  $creator, $started, $destroyed, $phours, $iname,
                  $uuid, $created, $portal, $clusters);
                          
	$results[] = $instance;
    }
    SPITAJAX_RESPONSE($results);    
}

function SearchForIP($ip, $min, $max)
{
212
    global $this_user, $portals, $urn_mapping;
213 214 215 216 217 218 219 220 221 222
    $this_uid = $this_user->uid();
    $results = array();
    
    if (!preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
        SPITAJAX_ERROR(1, "Invalid IP");
        return;
    }
    $webtask = WebTask::CreateAnonymous();
    $retval = SUEXEC($this_uid, "nobody",
		     "websearchip -t " . $webtask->task_id() .
223
                     " -s $min -e $max -b $ip",
224 225 226 227 228 229 230 231 232 233 234 235 236 237
		     SUEXEC_ACTION_IGNORE);
    $webtask->Refresh();
    
    if ($retval != 0) {
        if (!$webtask->exited() || $retval < 0) {
            SUEXECERROR(SUEXEC_ACTION_CONTINUE);
            SPITAJAX_ERROR(-1, "Internal error");
        }
        else {
            SPITAJAX_ERROR($webtask->exitcode(), $webtask->output());
        }
        $webtask->Delete();
        return;
    }
238
    $current   = $webtask->TaskValue("instance");
239 240 241
    $instances = $webtask->TaskValue("instancelist");
    $webtask->Delete();

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    if ($current) {
        #
        # This is a bit hokey, but it lets us show a current experiment
        # in the activity list when searching for an IP, which is handy.
        #
        $instance = Instance::Lookup($current);
        error_log("foo: $min, $max, " . strtotime($instance->started()));
        if ($instance &&
            (strtotime($instance->started()) > $min &&
             strtotime($instance->started()) < $max)) {
            $uuid      = $instance->uuid();
            $iname     = $instance->name();
            $pproj     = $instance->pid();
            $created   = DateStringGMT($instance->created());
            $started   = DateStringGMT($instance->started());
            $destroyed = null;
            $creator   = $instance->creator();
            $pcount    = $instance->physnode_count();
            $vcount    = $instance->virtnode_count();
            $portal    = $portals[$instance->servername()];
            $phours    = round(pcount *
                               ((time() -
                                 strtotime($instance->created())) / 3600.0));

            # Profile Info.
            $profile = Profile::Lookup($instance->profile_id(),
                                       $instance->profile_version());
            if ($profile) {
                $pname = $profile->name();
                $puuid = $profile->uuid();
            }
            $clusters = array();
            foreach ($instance->slivers() as $sliver) {
                $abbreviation = $urn_mapping[$sliver->aggregate_urn()];
                $clusters[]   = $abbreviation;
            }
            $clusters = join(",", $clusters);

            # Backwards compat.
            if (!isset($pproj)) {
                $pproj = "";
            }
            if (!isset($iname)) {
                $iname = "&nbsp;";
            }

            # Save space with array instead of hash.
            $instance =
                array($pname, $pproj, $puuid, $pcount, $vcount,
                      $creator, $started, $destroyed, $phours, $iname,
                      $uuid, $created, $portal, $clusters);
                          
            $results[] = $instance;
        }
    }

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
    foreach ($instances as $uuid) {
        $instance = InstanceHistory::Lookup($uuid);
        if (!$instance) {
            continue;
        }
        $iname     = $instance->name();
	$pproj     = $instance->pid();
	$created   = DateStringGMT($instance->created());
	$started   = DateStringGMT($instance->started());
	$destroyed = DateStringGMT($instance->destroyed());
	$creator   = $instance->creator();
        $pcount    = $instance->physnode_count();
        $vcount    = $instance->virtnode_count();
        $portal    = $portals[$instance->servername()];
        $phours    = round(pcount *
                           ((strtotime($instance->destroyed()) -
                             strtotime($instance->created())) / 3600.0));

        # Profile Info.
        $profile = Profile::Lookup($instance->profile_id(),
                                   $instance->profile_version());
        if ($profile) {
            $pname = $profile->name();
            $puuid = $profile->uuid();
        }
        $clusters = array();
        foreach ($instance->slivers() as $sliver) {
325 326
            $abbreviation = $urn_mapping[$sliver["aggregate_urn"]];
            $clusters[]   = $abbreviation;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
        }
        $clusters = join(",", $clusters);

        # Backwards compat.
        if (!isset($pproj)) {
            $pproj = "";
        }
        if (!isset($iname)) {
            $iname = "&nbsp;";
        }

        # Save space with array instead of hash.
	$instance =
            array($pname, $pproj, $puuid, $pcount, $vcount,
                  $creator, $started, $destroyed, $phours, $iname,
                  $uuid, $created, $portal, $clusters);
                          
	$results[] = $instance;
        
    }
    SPITAJAX_RESPONSE($results);    
}

?>