/testbed/Report.php
PHP | 215 lines | 158 code | 40 blank | 17 comment | 47 complexity | 14976325fc6481d200631c0c70b41036 MD5 | raw file
- <?php
- /*
- This class will capture reportable data by cycle, or summary data that are calculated as
- the simulation runs. It is not to be used to capture data that could be stored in one of
- the instantiated objects during a run, since those objects will be stored at the end of the
- run anyway.
-
- Edgar Sioson, 12/15/2011
- */
-
- class Report {
- function __construct($dEngs, $infras, $versionInfo='') {
- $this->dEngs = array_keys($dEngs);
- $this->infras = array_keys($infras);
-
- $this->maxCycle = $_GET['maxCycle'];
- $this->popN = $_GET['popN'];
- $this->locN = $_GET['locN'];
-
- if (stripos($_GET['print'],"byTick") !== false) $this->view_tick = 1;
- if (stripos($_GET['print'],"mem") !== false) $this->view_mem = 1;
-
- $this->cyclesLost = array();
- $this->lastCycle = 0;
-
- $defConstants = get_defined_constants(1);
- $this->defConstants = $defConstants['user'];
- $this->versionInfo = $versionInfo;
- $this->GETarray = $_GET;
-
- $this->period = 0;
- $this->log = array();
- $this->logId = 0;
- }
-
- function logData($linearTick, $cycleNum, $P, $L) {
- if ($_GET['log']) {
- foreach($P AS $pObj) $persons[] = array(
- "id"=>$pObj->id, "F" => $pObj->F, "prevAct" => $pObj->prevAct, "dEng" => $pObj->dEng, "brand" => $pObj->brand,
- "loss"=> $pObj->trackedLoss[$cycleNum] //+ $pObj->tickLoss/(99 * NUM_DIMENSIONS*CYCLE_PERIOD)
- );
-
- foreach($L AS $loc) $locs[] = array(
- "id"=>$loc->id, "rsrc" => $loc->rsrc, "qty" => $loc->qty, "waste" => $loc->waste, "brand" => $loc->brand
- );
-
- foreach($this->infras AS $sys) $infras[] = msg2way($sys,array("do" => "genReport"));
-
- $this->log["t$linearTick"] = array("P" => $persons, "L" => $locs, "I" => $infras);
-
- if (count($this->log) > 10) {
- if (!$this->logId) {
- if (!is_dir("results/". $_GET['runName'])) mkdir("results/". $_GET['runName']);
- if (!is_dir("results/". $_GET['runName'] ."/detail/")) mkdir("results/". $_GET['runName'] ."/detail");
- }
-
- file_put_contents("results/". $_GET['runName'] ."/detail/$this->logId", json_encode($this->log));
- $this->log = array();
- $this->logId++;
- }
- }
-
- if ($this->period == $cycleNum) {
- $this->byDEngine($cycleNum, $P);
- $this->period += SUMMARY_PERIOD;
- }
- }
-
- function byDEngine($cycleNum, $P) {
- $aliveNum = array();
- $aliveF = array();
- $deadNum = array();
- $deadF = array();
- $cycle_period = $cycleNum ? CYCLE_PERIOD : 1; //when cycleNum == 0, tickLoss should not be divided by whole cycle period
-
- foreach($P AS $person) {
- if (!$person->death) {
- $trackedLoss = $person->tickLoss / (99 * NUM_DIMENSIONS * $cycle_period * SUMMARY_PERIOD);
- $person->trackedLoss[$cycleNum] = $trackedLoss;
- $person->tickLoss = 0;
-
- $aliveF[$person->dEng] += array_sum($person->F);
- $aliveNum[$person->dEng] += 1;
- $deadNum[$person->dEng] += count($person->replacedOn); //might have to think about this some more
- $this->cyclesLost[$person->dEng] += $trackedLoss + count($person->replacedOn);
- }
- else {
- $deadF[$person->dEng] += array_sum($person->F);
- $deadNum[$person->dEng]++;
- $this->cyclesLost[$person->dEng] += 1;
- }
- };
-
- foreach($this->dEngs as $dEng) {
- if (!$aliveNum[$dEng]) $this->aliveNum[$dEng][$cycleNum] = 0;
- else $this->aliveNum[$dEng][$cycleNum] = $aliveNum[$dEng];
-
- if (!$deadNum[$dEng]) $this->deadNum[$dEng][$cycleNum] = 0;
- else $this->deadNum[$dEng][$cycleNum] = $deadNum[$dEng];
-
- $numBydEng = $aliveNum[$dEng] + $deadNum[$dEng]; if (!$numBydEng) exit("dEng with 0 Num: ". $dEng);
-
- //if ($cycleNum) $this->effCycles[$dEng][$cycleNum] = round(100*($cycleNum - $this->cyclesLost[$dEng]/$numBydEng)/$cycleNum);
- if ($cycleNum) $this->effCycles[$dEng][$cycleNum] = round(100*($cycleNum - $this->cyclesLost[$dEng]/$numBydEng)/$cycleNum);
- }
-
- if ($this->view_mem) echo "\nCycle number:$cycleNum mem_usage= ". memory_get_usage() ." ";
- if (is_dir("New folder")) exit("User interrupt: process is terminated.");
- $this->lastCycle = $cycleNum; //print_r($this); exit();
- }
-
- function viewObj($P,$L,$Accts,$Inven) {
- //echo "\nError: "; if (!$Error) echo "none\n"; else print_r($Error);
- /*if ($this->view_accts) {
- dispAccts("http://127.0.0.1/flora/infras/ocaup.php?ledger=k7&pwd=k7po&reconcile=1");
- dispAccts("decision/cache/accts_k3");
- }*/
-
- if (stripos($_GET['print'],"report") !== false) print_r($this);
- if (stripos($_GET['print'],"person") !== false) print_r($P);
- if (stripos($_GET['print'],"location") !== false) print_r($L);
- if (stripos($_GET['print'],"accts") !== false) print_r($Accts);
- if (stripos($_GET['print'],"inven") !== false) print_r($Inven);
- }
-
- function viewJSON($obj) {
- echo "\n";
- foreach($obj AS $member) {
- echo "\n". json_encode($member);
- }
- }
-
- function saveCSV($time, $runName,$Addr='') {
- $typesToSave = array("effCycles", "aliveNum");
-
- if ($runName != "__") {
- if (!is_dir("results/$runName")) mkdir("results/$runName");
-
- foreach ($this AS $type => $subReport) {
- if (in_array($type,$typesToSave)) {
- $csv = array();
- $cvsHeading = "tick";
-
- foreach($subReport AS $dEng => $arr) {
- $csvHeading .= ",$dEng";
-
- if (!$csv) {
- foreach($arr AS $key=>$val) $csv[] = "$key,$val";
- }
- else {
- foreach($csv AS &$val) {
- $key = strtok($val,",");
- $val .= ",". $arr[$key];
- }
- }
- }
- }
-
- if ($csv) {
- array_unshift($csv, $csvHeading);
- $csvStr = implode("\n",$csv);
- saveData("results/$runName/$type", $time, $csvStr);
- }
- }
-
-
- saveData("results/$runName/report", $time, json_encode($this));
- if ($Addr) saveData("results/$runName/addr", $time, json_encode($Addr));
- //if ($P) saveData("results/$runName/person", $time, json_encode($P));
- //saveData("results/$runName/config", $time, file_get_contents("config.php"));
- //saveData("results/$runName/config", $time, file_get_contents("loc.php"));
-
- if (file_exists("results/_versionInfo_")) $_versionInfo_ = json_decode(file_get_contents("results/_versionInfo_"));
- else $_versionInfo_ = new stdClass();
-
- if (!$_versionInfo_) exit("Invalid version info. [Report->saveCSV]");
- else if ($this->versionInfo['base']) $_versionInfo_->{$this->versionInfo['base']}->$time = $this->versionInfo['changes'];
-
- saveData("results/","_versionInfo_",json_encode($_versionInfo_));
- }
- }
- }
-
- function dispAct(&$Person,$linearTick,$cyclicTick,$decision) {
- echo "\nTick=$cyclicTick ($linearTick) id= $Person->id decision=". json_encode($decision) ." F= ".
- json_encode($Person->F) ." Holding: ". json_encode($Person->holding);
- }
-
- function saveData($dir,$name,$data) {
- if (!is_dir($dir)) mkdir($dir);
- if (substr($dir,-1) != "/") $dir .= "/";
- file_put_contents($dir. $name, $data);
- }
-
- function dispAccts($file) {
- if (stripos($file,"http://") === 0) {
- $reply = file_get_contents($file);
- $Data = json_decode($reply);
- if (!$Data OR $Data->status != "ok") echo "\n$reply\n";
- else {
- echo "\ntotal of $file account balances=". round($Data->net) ." ";
- echo "\nmetrics: ". print_r($Data->metrics);
- print_r($Data->Accts);
- }
- }
- else if (file_exists($file)) {
- $Accts = json_decode(file_get_contents($file));
- $total = -1*$Accts->initTokens;
-
- foreach($Accts AS $key=>$bal) { if (is_numeric($bal)) $total += $bal; }
- echo "total of $file account balances=". round($total) ." "; print_r($Accts);
- }
- }
-
- ?>