function createGraphsFromCombos($combo, $var_vals, $i, $sid_vars, $genesis_base, $rparams, $templateid, $graph_num, $update)
{
    global $graphs_to_update;
    $genesis = $genesis_base;
    if ($i >= count($var_vals)) {
        $vals_combo = explode(",", $combo);
        for ($j = 0; $j < count($vals_combo); $j++) {
            $rparams[$sid_vars[$j]] = $vals_combo[$j];
            $genesis .= "," . $sid_vars[$j] . "=" . $vals_combo[$j];
        }
        $template = new Reconnoiter_GraphTemplate($templateid);
        $db = Reconnoiter_DB::getDB();
        $graph_json = $template->getNewGraphJSON($rparams);
        $graph_json = stripslashes($graph_json);
        $graph_json = json_decode($graph_json, true);
        $graph_json['genesis'] = $genesis;
        // Check to see if this title exists
        $titlerow = $db->getGraphByTitle($title);
        // if it does, append $graph_num to it
        if ($titlerow['graphid']) {
            $graph_json['title'] = $graph_json['title'] . $graph_num;
        }
        $grow = $db->getGraphByGenesis($genesis);
        if ($grow['graphid']) {
            if ($update) {
                $graph_json['id'] = $grow['graphid'];
                $graph_id = $db->saveGraph($graph_json);
            }
            $graphs_to_update[] = array('graphid' => $grow['graphid'], 'title' => $grow['title']);
            return;
        }
        if ($update) {
            $graph_id = $db->saveGraph($graph_json);
            $graph_json['id'] = $graph_id;
            $graph_id = $db->saveGraph($graph_json);
        }
        return;
    } else {
        foreach ($var_vals[$i] as $vval) {
            $graph_num++;
            createGraphsFromCombos($combo ? "{$combo},{$vval}" : $vval, $var_vals, $i + 1, $sid_vars, $genesis_base, $rparams, $templateid, $graph_num, $update);
        }
    }
}
 public function find_sids($json)
 {
     $this->num_sids = 0;
     $sids = array();
     $vs = Reconnoiter_GraphTemplate::find_variables($json);
     //vs will be multiD array containing variable names, their types (text/sid) and metric
     //names and metric types
     foreach ($vs as $v => $d) {
         if (isset($d['SID'])) {
             $this->num_sids++;
             $sql = "select m.* from noit.check_currently as m";
             $binds = array();
             $t = 1;
             $sids[$v] = array();
             foreach ($d['SID'] as $m) {
                 $sql = "{$sql}\n                    join noit.metric_name_summary t{$t}\n                      on (    m.sid = t{$t}.sid\n                          and t{$t}.metric_name = ?\n                          and t{$t}.metric_type = ?)";
                 $binds[] = $m[0];
                 $binds[] = $m[1];
                 $t++;
             }
             if (count($binds)) {
                 $db = Reconnoiter_DB::getDB();
                 $sth = $db->prepare($sql);
                 $sth->execute($binds);
                 while ($row = $sth->fetch()) {
                     $sids[$v][] = $row;
                 }
             }
         }
     }
     return $sids;
 }
} else {
    if ($_REQUEST[$l1]) {
        $want = $l2;
    } else {
        if ($_REQUEST[$l2]) {
            $want = $l3;
        } else {
            $want = "we are screwed";
        }
    }
}
$templateid;
$template;
if (!empty($_REQUEST[$l1])) {
    $templateid = $_REQUEST[$l1];
    $template = new Reconnoiter_GraphTemplate($templateid);
}
$bag = array();
if ($want == 'templateid') {
    foreach ($db->get_templates() as $item) {
        $params = array();
        $params['templateid'] = $item['templateid'];
        $params['title'] = $item['title'];
        $params['json'] = $item['json'];
        $jitem = array('id' => $item['templateid'], 'text' => $item['title'], 'hasChildren' => true, 'classes' => $want, 'params' => $params);
        $bag[] = $jitem;
    }
} else {
    if ($want == 'targetname') {
        $sidvars = array();
        $textvars = array();