Skip to content
Snippets Groups Projects
importFromV1_0toV1_1.php 6.01 KiB
Newer Older
root's avatar
root committed
#!/usr/bin/php
<?php
namespace SmartData;
require_once( __DIR__ . '/../Backend.php');

$time_interval = Time::MONTH;

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// Domain: SmartLisha
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//$domain = 'smartlisha';
//$begin = strtotime("1 January 2017")*1000*1000;
//$end = strtotime("23 September 2018")*1000*1000;
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Temperature, 741868840, 679816441, 25300, 0, $begin, $end, 0);
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Luminous_Intensity, 741868320, 679816251, 25300, 0, $begin, $end, 6);
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Distance, 741868880, 679816793, 25227, 0, $begin, $end, 0);
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Power, 741868770, 679816011, 25285, 0, $begin, $end, 0); // Outlet 0
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Power, 741868305, 679816231, 25285, 0, $begin, $end, 0); // Outlet 1

//$domain = 'smartlisha_bc';
//$series = new Series(SmartData::STATIC_VERSION, 6553604, 100, 100, 100, 0, $begin, $end, 0); // Blockchain commits

//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// Domain: hydrology
//+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
$domain = 'hydro';
//$end = strtotime("2018/10/03")*1000*1000;
//$begin = strtotime("2018/06/27")*1000*1000; //*since: 27/06/2018
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374672400, -423762500, -293721000, 0, $begin, $end, 0); //Exutorio
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Amount_of_Substance, 374672400, -423762500, -293721000, 0, $begin, $end, 1);

//$begin = strtotime("2018/07/06")*1000*1000; //*since: 06/07/2018
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374642900, -423784200, -293727600, 0, $begin, $end, 0); //CCJ
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374619700, -423795900, -293741100, 0, $begin, $end, 0); //Aplicacao

//$begin = strtotime("2017/08/09")*1000*1000; //*since: 09/08/2017
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374665000, -423767000, -293725000, 0, $begin, $end, 0); //BU

//$begin = strtotime("2018/06/29")*1000*1000; //*since: 29/06/2018
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Water_Flow,          374677500, -423772800, -293700300, 0, $begin, $end, 0); //HU01
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Water_Flow,          374677600, -423772700, -293700200, 0, $begin, $end, 0); //HU02
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Water_Flow,          374672600, -423773000, -293706400, 0, $begin, $end, 0); //HU03
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Water_Flow,          374690300, -423766300, -293692900, 0, $begin, $end, 0); //HU05
//$series = new Series(SmartData::STATIC_VERSION, TypicalUnit::Water_Flow,          374695800, -423766400, -293685900, 0, $begin, $end, 0); //HU06

// $begin = strtotime("2018/07/02")*1000*1000;
// $end = strtotime("2018/07/04")*1000*1000;
// $series    = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374690300, -423766300, -293692900, 0, $begin, $end, 0); //Exutorio
// $series_to = new Series(SmartData::STATIC_VERSION, TypicalUnit::Length,              374672400, -423762500, -293721000, 0, $begin, $end, 0); //Exutorio

// $series    = new Series(SmartData::STATIC_VERSION, TypicalUnit::Amount_of_Substance, 374690300, -423766300, -293692900, 0, $begin, $end, 1); //Exutorio
// $series_to = new Series(SmartData::STATIC_VERSION, TypicalUnit::Amount_of_Substance, 374672400, -423762500, -293721000, 0, $begin, $end, 1); //Exutorio

//$unit = Unit::interpret(2224441636);
//echo " ".TypicalUnit::Length."\n";
//echo $unit."\n";

exit;

$cred = new Credentials($domain, Config::config()::CASSANDRA_SUPERUSER, Config::config()::CASSANDRA_SUPERPASS);

//$__FORCE_SCREEN_LOG = true;

echo "------------------------------------------------------------\n";
echo "- Importing Series from domain $domain\n";
echo "- Unit : ".$series->unit()."\n";
echo "- Region : ({$series->x},{$series->y},{$series->z})->{$series->r}\n";
echo "- Dev : {$series->dev}\n";
echo '- From '.Time::time_to_date($begin).' to '.Time::time_to_date($end)."\n";
echo "------------------------------------------------------------\n";

$t0 = $begin;
$t1 = $begin + $time_interval;

$zero = 0;
$cc = 0;

while($t0 < $end){
    $series->t0 = $t0;
    $series->t1 = $t1;
    echo 'Sub-series from '.Time::time_to_date($t0).' to '.Time::time_to_date($t1)."\n";

    $__FORCE_VERSION = Config_Common::VERSION_1_0;
    $from_bd = new Backend_V1_0($cred, true);

    $result = $from_bd->query($series);
    $count = count($result['series']);
    if($count > 0){
        $batch = array();
        echo "Preparing ...\n";
        foreach ($result['series'] as $key => $value) {
            $smartdata = new StaticSmartData (
                $series->unit,
                $value['value'],
                $value['error'],
                $value['confidence'],
                $value['x'], // TODO: pay attencion here
                $value['y'],
                $value['z'],
                $value['timestamp'],
                $series->dev //$value['dev']
            );
            array_push($batch, $smartdata);
        }

        $__FORCE_VERSION = Config_Common::VERSION_1_1;
        $to_db = new Backend_V1_1($cred, true);

        $series = $series_to ?? $series;
        echo "Creating data series...\n";
        if(!$to_db->create($series))
            die ("Create error");

        echo "Inserting batch...\n";
        if(!$to_db->insertBatch($series, ...$batch)) {
            die ("Insertion Failed!\n");
        } else {
            echo "Inserted...\n";
        }
    }

    $t0 += $time_interval;
    $t1 += $time_interval;
}