PageRenderTime 87ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/testbed/Report.php

https://bitbucket.org/siosonel/flora
PHP | 215 lines | 158 code | 40 blank | 17 comment | 47 complexity | 14976325fc6481d200631c0c70b41036 MD5 | raw file
  1. <?php
  2. /*
  3. This class will capture reportable data by cycle, or summary data that are calculated as
  4. the simulation runs. It is not to be used to capture data that could be stored in one of
  5. the instantiated objects during a run, since those objects will be stored at the end of the
  6. run anyway.
  7. Edgar Sioson, 12/15/2011
  8. */
  9. class Report {
  10. function __construct($dEngs, $infras, $versionInfo='') {
  11. $this->dEngs = array_keys($dEngs);
  12. $this->infras = array_keys($infras);
  13. $this->maxCycle = $_GET['maxCycle'];
  14. $this->popN = $_GET['popN'];
  15. $this->locN = $_GET['locN'];
  16. if (stripos($_GET['print'],"byTick") !== false) $this->view_tick = 1;
  17. if (stripos($_GET['print'],"mem") !== false) $this->view_mem = 1;
  18. $this->cyclesLost = array();
  19. $this->lastCycle = 0;
  20. $defConstants = get_defined_constants(1);
  21. $this->defConstants = $defConstants['user'];
  22. $this->versionInfo = $versionInfo;
  23. $this->GETarray = $_GET;
  24. $this->period = 0;
  25. $this->log = array();
  26. $this->logId = 0;
  27. }
  28. function logData($linearTick, $cycleNum, $P, $L) {
  29. if ($_GET['log']) {
  30. foreach($P AS $pObj) $persons[] = array(
  31. "id"=>$pObj->id, "F" => $pObj->F, "prevAct" => $pObj->prevAct, "dEng" => $pObj->dEng, "brand" => $pObj->brand,
  32. "loss"=> $pObj->trackedLoss[$cycleNum] //+ $pObj->tickLoss/(99 * NUM_DIMENSIONS*CYCLE_PERIOD)
  33. );
  34. foreach($L AS $loc) $locs[] = array(
  35. "id"=>$loc->id, "rsrc" => $loc->rsrc, "qty" => $loc->qty, "waste" => $loc->waste, "brand" => $loc->brand
  36. );
  37. foreach($this->infras AS $sys) $infras[] = msg2way($sys,array("do" => "genReport"));
  38. $this->log["t$linearTick"] = array("P" => $persons, "L" => $locs, "I" => $infras);
  39. if (count($this->log) > 10) {
  40. if (!$this->logId) {
  41. if (!is_dir("results/". $_GET['runName'])) mkdir("results/". $_GET['runName']);
  42. if (!is_dir("results/". $_GET['runName'] ."/detail/")) mkdir("results/". $_GET['runName'] ."/detail");
  43. }
  44. file_put_contents("results/". $_GET['runName'] ."/detail/$this->logId", json_encode($this->log));
  45. $this->log = array();
  46. $this->logId++;
  47. }
  48. }
  49. if ($this->period == $cycleNum) {
  50. $this->byDEngine($cycleNum, $P);
  51. $this->period += SUMMARY_PERIOD;
  52. }
  53. }
  54. function byDEngine($cycleNum, $P) {
  55. $aliveNum = array();
  56. $aliveF = array();
  57. $deadNum = array();
  58. $deadF = array();
  59. $cycle_period = $cycleNum ? CYCLE_PERIOD : 1; //when cycleNum == 0, tickLoss should not be divided by whole cycle period
  60. foreach($P AS $person) {
  61. if (!$person->death) {
  62. $trackedLoss = $person->tickLoss / (99 * NUM_DIMENSIONS * $cycle_period * SUMMARY_PERIOD);
  63. $person->trackedLoss[$cycleNum] = $trackedLoss;
  64. $person->tickLoss = 0;
  65. $aliveF[$person->dEng] += array_sum($person->F);
  66. $aliveNum[$person->dEng] += 1;
  67. $deadNum[$person->dEng] += count($person->replacedOn); //might have to think about this some more
  68. $this->cyclesLost[$person->dEng] += $trackedLoss + count($person->replacedOn);
  69. }
  70. else {
  71. $deadF[$person->dEng] += array_sum($person->F);
  72. $deadNum[$person->dEng]++;
  73. $this->cyclesLost[$person->dEng] += 1;
  74. }
  75. };
  76. foreach($this->dEngs as $dEng) {
  77. if (!$aliveNum[$dEng]) $this->aliveNum[$dEng][$cycleNum] = 0;
  78. else $this->aliveNum[$dEng][$cycleNum] = $aliveNum[$dEng];
  79. if (!$deadNum[$dEng]) $this->deadNum[$dEng][$cycleNum] = 0;
  80. else $this->deadNum[$dEng][$cycleNum] = $deadNum[$dEng];
  81. $numBydEng = $aliveNum[$dEng] + $deadNum[$dEng]; if (!$numBydEng) exit("dEng with 0 Num: ". $dEng);
  82. //if ($cycleNum) $this->effCycles[$dEng][$cycleNum] = round(100*($cycleNum - $this->cyclesLost[$dEng]/$numBydEng)/$cycleNum);
  83. if ($cycleNum) $this->effCycles[$dEng][$cycleNum] = round(100*($cycleNum - $this->cyclesLost[$dEng]/$numBydEng)/$cycleNum);
  84. }
  85. if ($this->view_mem) echo "\nCycle number:$cycleNum mem_usage= ". memory_get_usage() ." ";
  86. if (is_dir("New folder")) exit("User interrupt: process is terminated.");
  87. $this->lastCycle = $cycleNum; //print_r($this); exit();
  88. }
  89. function viewObj($P,$L,$Accts,$Inven) {
  90. //echo "\nError: "; if (!$Error) echo "none\n"; else print_r($Error);
  91. /*if ($this->view_accts) {
  92. dispAccts("http://127.0.0.1/flora/infras/ocaup.php?ledger=k7&pwd=k7po&reconcile=1");
  93. dispAccts("decision/cache/accts_k3");
  94. }*/
  95. if (stripos($_GET['print'],"report") !== false) print_r($this);
  96. if (stripos($_GET['print'],"person") !== false) print_r($P);
  97. if (stripos($_GET['print'],"location") !== false) print_r($L);
  98. if (stripos($_GET['print'],"accts") !== false) print_r($Accts);
  99. if (stripos($_GET['print'],"inven") !== false) print_r($Inven);
  100. }
  101. function viewJSON($obj) {
  102. echo "\n";
  103. foreach($obj AS $member) {
  104. echo "\n". json_encode($member);
  105. }
  106. }
  107. function saveCSV($time, $runName,$Addr='') {
  108. $typesToSave = array("effCycles", "aliveNum");
  109. if ($runName != "__") {
  110. if (!is_dir("results/$runName")) mkdir("results/$runName");
  111. foreach ($this AS $type => $subReport) {
  112. if (in_array($type,$typesToSave)) {
  113. $csv = array();
  114. $cvsHeading = "tick";
  115. foreach($subReport AS $dEng => $arr) {
  116. $csvHeading .= ",$dEng";
  117. if (!$csv) {
  118. foreach($arr AS $key=>$val) $csv[] = "$key,$val";
  119. }
  120. else {
  121. foreach($csv AS &$val) {
  122. $key = strtok($val,",");
  123. $val .= ",". $arr[$key];
  124. }
  125. }
  126. }
  127. }
  128. if ($csv) {
  129. array_unshift($csv, $csvHeading);
  130. $csvStr = implode("\n",$csv);
  131. saveData("results/$runName/$type", $time, $csvStr);
  132. }
  133. }
  134. saveData("results/$runName/report", $time, json_encode($this));
  135. if ($Addr) saveData("results/$runName/addr", $time, json_encode($Addr));
  136. //if ($P) saveData("results/$runName/person", $time, json_encode($P));
  137. //saveData("results/$runName/config", $time, file_get_contents("config.php"));
  138. //saveData("results/$runName/config", $time, file_get_contents("loc.php"));
  139. if (file_exists("results/_versionInfo_")) $_versionInfo_ = json_decode(file_get_contents("results/_versionInfo_"));
  140. else $_versionInfo_ = new stdClass();
  141. if (!$_versionInfo_) exit("Invalid version info. [Report->saveCSV]");
  142. else if ($this->versionInfo['base']) $_versionInfo_->{$this->versionInfo['base']}->$time = $this->versionInfo['changes'];
  143. saveData("results/","_versionInfo_",json_encode($_versionInfo_));
  144. }
  145. }
  146. }
  147. function dispAct(&$Person,$linearTick,$cyclicTick,$decision) {
  148. echo "\nTick=$cyclicTick ($linearTick) id= $Person->id decision=". json_encode($decision) ." F= ".
  149. json_encode($Person->F) ." Holding: ". json_encode($Person->holding);
  150. }
  151. function saveData($dir,$name,$data) {
  152. if (!is_dir($dir)) mkdir($dir);
  153. if (substr($dir,-1) != "/") $dir .= "/";
  154. file_put_contents($dir. $name, $data);
  155. }
  156. function dispAccts($file) {
  157. if (stripos($file,"http://") === 0) {
  158. $reply = file_get_contents($file);
  159. $Data = json_decode($reply);
  160. if (!$Data OR $Data->status != "ok") echo "\n$reply\n";
  161. else {
  162. echo "\ntotal of $file account balances=". round($Data->net) ." ";
  163. echo "\nmetrics: ". print_r($Data->metrics);
  164. print_r($Data->Accts);
  165. }
  166. }
  167. else if (file_exists($file)) {
  168. $Accts = json_decode(file_get_contents($file));
  169. $total = -1*$Accts->initTokens;
  170. foreach($Accts AS $key=>$bal) { if (is_numeric($bal)) $total += $bal; }
  171. echo "total of $file account balances=". round($total) ." "; print_r($Accts);
  172. }
  173. }
  174. ?>