Example #1
0
function getMaxPercentageForTheNight()
{
    $today = time();
    $dateSMYSQL = es(date("Y-m-d 00:00", $today));
    // midnight
    $dateEMYSQL = es(date("Y-m-d 09:00", $today));
    // 9am
    $query = "SELECT\n                timestamp,\n                FLOOR(100*wind/demand) AS percent\n              FROM\n                wind_vs_demand\n              WHERE\n                timestamp BETWEEN '{$dateSMYSQL}' AND '{$dateEMYSQL}'\n              ORDER BY\n                percent DESC\n              LIMIT 1";
    return fetchAssoc(query($query));
}
Example #2
0
function getCurrentData()
{
    // get latest data
    // inner select grabs highest % tweeted today
    // only check today's readings
    $hour = date('H');
    if ($hour < 12) {
        $start = time();
        $end = time();
        $dateSMYSQL = es(date("Y-m-d 00:00", $start));
        // today 00
        $dateEMYSQL = es(date("Y-m-d 12:00", $end));
        // today 12
    } else {
        $start = time();
        $end = time() + 3600 * 24;
        $dateSMYSQL = es(date("Y-m-d 12:00", $start));
        // today 12
        $dateEMYSQL = es(date("Y-m-d 00:00", $end));
        // tomorrow 00
    }
    $query = "SELECT\n                timestamp,\n                FLOOR(100*wind/demand) AS percent,\n                (SELECT percentage FROM tweets WHERE timestamp BETWEEN '{$dateSMYSQL}' AND '{$dateEMYSQL}' ORDER BY percentage DESC LIMIT 1) AS day_max\n              FROM\n                wind_vs_demand\n              WHERE\n                timestamp BETWEEN '{$dateSMYSQL}' AND '{$dateEMYSQL}'\n              ORDER BY\n                timestamp DESC\n              LIMIT 1";
    return fetchAssoc(query($query));
}
Example #3
0
    /**
    	Recibe un Array con parametros y Retorna una tabla con la cantidad de registros solicitados 
    	y los campos de la query, incluyendo numeros de paginacion.
    	
    	string render( Array);
    	
    	Campos del array:
    	Array(
    		'query' => Consulta SQL que retornará datos
    		'campo_id' => campo id de la tabla
    		['ocultos'] => Campos que se mantendran ocultos separados por coma => <input type="hidden" name="[campo]_[id]" value="[valor]">
    		['limite'] => Cantidad de registros por página
    		['orden'] => Campo por el cual se debe ordenar, acepta DESC
    		['numeros'] => 'falso' para no mostrar numeros de página
    		['acciones'] => acciones separadas por coma retorna: <td><a class="[accion]" href="javascript: [accion]([id]);"></a></td>
    		['condiciones'] => condiciones para el select en formato [campo1="valor" AND campo2 = "valor2"]
    		['tabla'] => clase para la tabla
    	)
    	**/
    function render($atr = null)
    {
        //identifica query
        if ($atr['query']) {
            $query = $atr['query'];
        } else {
            echo 'ERROR';
        }
        if ($atr['campo_id']) {
            $campo_id = $atr['campo_id'];
        } else {
            $campo_id = 'id';
        }
        //datos de la tabla
        if ($atr['tabla']) {
            $clase = ' class="' . $atr['tabla'] . '" id="' . $atr['tabla'] . '"';
        }
        //identifica condiciones
        if ($atr['condiciones']) {
            $condiciones = 'WHERE ' . $atr['condiciones'];
        } else {
            $condiciones = $this->condiciones;
        }
        //identifica cantidad de registros a mostrar y paginas totales
        if ($atr['limite']) {
            $this->inicio = ($this->pagina - 1) * $atr['limite'];
            $this->limite = $atr['limite'];
            $limite = 'LIMIT ' . $this->inicio . ',' . $atr['limite'];
        } else {
            $this->inicio = ($this->pagina - 1) * $this->limite;
            $limite = 'LIMIT ' . $this->inicio . ',' . $this->limite;
        }
        //identifica si existe un campo para ordenar
        if ($atr['orden']) {
            $orden = 'ORDER BY ' . $atr['orden'];
        } else {
            if ($this->orden != '') {
                $orden = 'ORDER BY ' . $this->orden;
            }
        }
        //identifica si existen campos ocultos
        if ($atr['ocultos']) {
            $ocultos = explode(',', $atr['ocultos']);
            foreach ($ocultos as $index => $oculto) {
                $oculto[$index] = trim($oculto);
            }
        } else {
            $ocultos = array();
        }
        $sql = "SELECT * FROM ({$query}) AS TABLA {$condiciones} {$orden} {$limite}";
        $this->query = $sql;
        //echo $sql;
        $cantidad = fetchAssoc("SELECT COUNT(*) AS cant FROM ({$query}) AS TABLA {$condiciones}");
        $this->cantidad = $cantidad[0]['cant'];
        $res = fetchAssoc($sql);
        $op_tbody = '<tbody>';
        $thead = false;
        $colspan = 0;
        foreach ($res as $re) {
            $reg_id = $re[$campo_id];
            $op_tbody .= '<tr>';
            if (!$thead) {
                $op_thead = "<thead>";
            }
            foreach ($re as $c => $v) {
                if (!in_array($c, $ocultos)) {
                    $op_tbody .= "<td>{$v}</td>";
                    $desc = '';
                    if (!$thead) {
                        if ($this->orden == $c) {
                            $desc = ' DESC';
                        }
                        $op_thead .= '<th><a href="javascript: orden(\'' . $c . $desc . '\');">' . $c . '</a></th>';
                        $colspan++;
                    }
                } else {
                    $op_tbody .= '<input type="hidden" name="' . $c . $reg_id . '" value="' . $v . '">';
                }
            }
            //identifica si existen acciones
            if ($atr['acciones']) {
                $acciones = explode(',', $atr['acciones']);
                foreach ($acciones as $accion) {
                    $accion = trim($accion);
                    if (!$thead) {
                        $op_thead .= '<th>' . trim($accion) . '</th>';
                        $colspan++;
                    }
                    $op_tbody .= '<td class="' . $accion . '"><a href="javascript: ' . $accion . '(' . $reg_id . ');" class="' . $accion . '">' . $accion . '</a></td>';
                }
            }
            $op_tbody .= '</tr>';
            if (!$thead) {
                $op_thead .= "</thead>";
                $thead = true;
            }
        }
        $op_tbody .= '</tbody>';
        $js = '
		<script>
			function orden(campo){
				document.frm.orden.value = campo;	
				document.frm.submit();
			}
			function pagina(numero){
				document.frm.pagina.value = numero;	
				document.frm.submit();
			}
			
		</script>';
        $op = $js;
        if (!$atr['numeros'] || $atr['numeros'] != 'falso') {
            $op_tfoot = '<tfoot><tr><td class="numeros" colspan="' . $colspan . '">';
            $op_tfoot .= $this->numeros();
            $op_tfoot .= '</td></tr></tfoot>';
        }
        $op .= '<table' . $clase . '>' . $op_thead . $op_tbody . $op_tfoot . '</table>';
        $frm = '<form name="frm" method="post">
					<input type="hidden" name="orden" value="' . $this->orden . '">
					<input type="hidden" name="pagina" value="' . $this->pagina . '">	
				</form>';
        $op .= $frm;
        return $op;
    }
Example #4
0
function getUserInfo($id)
{
    dbConnect();
    $id = mysql_real_escape_string($id);
    $r = query("SELECT * FROM users WHERE id = '{$id}'");
    if ($row = fetchAssoc($r)) {
        return $row;
    } else {
        return false;
    }
}
Example #5
0
        if ($delta <= 3600 * 24 * 30 * 12) {
            // date range < 1 month : show daily average
            $time = "DATE(timestamp) AS time";
            $group_by = ' GROUP BY time';
        } else {
            // date range < 1 year : show weekly average
            $time = "CONCAT(YEAR(timestamp), '-', WEEK(timestamp)) AS time";
            $group_by = ' GROUP BY time';
        }
    }
}
$select = $time . $select;
$end = es($end);
$start = es($start);
$dateSMYSQL = es(date("Y-m-d H:i", $start));
$dateEMYSQL = es(date("Y-m-d H:i", $end));
$query = "SELECT\n              {$select}\n            FROM\n              wind_vs_demand\n            WHERE\n              timestamp BETWEEN '{$dateSMYSQL}' AND '{$dateEMYSQL}'\n              {$group_by}\n            ORDER BY\n              timestamp";
$wind = array();
$results = query($query);
while ($item = fetchAssoc($results)) {
    $windItem = array();
    $windItem['x'] = strtotime($item['timestamp']);
    $windItem['y'] = intval($item['wind']);
    $windItem['z'] = intval($item['wind'] / $item['demand'] * 100);
    array_push($wind, $windItem);
}
$graph = array(array('name' => "Wind", "color" => "#007232", "data" => $wind));
// echo $_GET['callback'] . "(";
echo json_encode($graph);
// echo ");";
//print_r($dbDebug);
Example #6
0
<?php

error_reporting(0);
require_once dirname(__FILE__) . '/../../inc/init.php';
$query = fetchAssoc(query("SELECT\n              timestamp,\n              demand,\n              wind\n            FROM\n              wind_vs_demand\n            ORDER BY\n              timestamp DESC\n            LIMIT 1"));
echo $query['wind'];