<?php

namespace MySQLMigrationBridge;

require_once __DIR__ . "/../../bootstrap.php";
require "common.php";
require "vendor/phpQuery/phpQuery-onefile.php";
$hbase_panel = \phpQuery::newDocumentHTML(file_get_contents("http://" . DB_HOST . ":60010/master-status"));
//print_r($hbase_panel);
$hbase_attr_table = null;
$hbase_panel["table"]->filter(":first")->addClass("table table-striped table-hover")->toReference($hbase_attr_table);
$hbase_tables = fetch_all_assoc(mysql_query("SHOW TABLES"));
$uri_param_table = isset($_GET["table"]) ? $_GET["table"] : null;
$uri_param_func = isset($_GET["func"]) ? $_GET["func"] : null;
$uri_param_query_post = isset($_GET["query_post"]) ? $_GET["query_post"] : null;
$colnames_post = isset($_POST["colname"]) ? $_POST["colname"] : null;
$tablenames_post = isset($_POST["tablename"]) ? $_POST["tablename"] : null;
$act = isset($_GET["act"]) ? $_GET["act"] : null;
if (!empty($act) && $act == "submit") {
    echo ";;;;;;;;;;;;;;;;;;;";
    print_r($colnames_post);
    print_r($tablenames_post);
    // put 'tbl1', 'row1', 'cf1:column1', 'v1'
    $cmd = "create '{$tablenames_post}', 'mysql'\n";
    foreach ($colnames_post as $col) {
        $cmd .= "put '{$tablenames_post}', 'row1', 'mysql:{$col}', ''";
    }
    //file_put_contents("tmp/create-table.txt", $cmd);
    //exec("hbase shell < tmp/create-table.txt");
    MMB::createTable($tablenames_post, "mysql");
    $cols = implode(",", $colnames_post);
<?php

date_default_timezone_set("America/Chicago");
$mysqli = new mysqli("localhost", "root", "", "yt_viewer");
$timestamp = mktime(0, 0, 0, date("m"), date("d") - 7, date("Y"));
$today = new DateTime(date("m/d/Y"));
$seven_date = new DateTime(date("m/d/Y", $timestamp));
$result = fetch_all_assoc($mysqli, "SELECT * FROM analytics");
foreach ($result as $key => $value) {
    list($year, $month, $day) = explode("-", $value['date']);
    $date = new DateTime(date("m/d/Y", mktime(0, 0, 0, $month, $day, $year)));
    if ($date < $seven_date || $date > $today || $date == new DateTime(date("m/d/Y", mktime(0, 0, 0, 12, 31, 9999)))) {
        unset($result[$key]);
    }
}
echo json_encode(array_reverse($result));
$mysqli->close();
function fetch_all_assoc($mysqli, $query)
{
    $result = $mysqli->query($query);
    $assoc = array();
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        $assoc[] = $row;
    }
    $result->close();
    return $assoc;
}