public function run(Request $request) { $postData = $request->all(); $date_end = $postData['date_end']; $date_end = \Helper::parseDate($date_end); $object_id = $postData['EnergyUnit']; $phase_type = $postData['ExtensionPhaseType']; $value_type = $postData['ExtensionValueType']; $data_source = $postData['ExtensionDataSource']; $table = $postData['EnergyUnit']; $mdl = \Helper::getModelName($table); $cb_update_db = $postData['cb_update_db']; $a = $postData['a']; $b = $postData['b']; $u = $postData['u']; $l = $postData['l']; $m = $postData['m']; $c1 = $postData['c1']; $c2 = $postData['c2']; $date_begin = $postData['date_begin']; $date_begin = \Helper::parseDate($date_begin); $date_from = $postData['f_from_date']; $date_from = \Helper::parseDate($date_from); $date_to = $postData['f_to_date']; $date_to = \Helper::parseDate($date_to); $from_date = $date_begin; $mkey = "_" . date("Ymdhis_") . rand(100, 1000); $data = ""; $continous = true; $lastT = null; if (array_key_exists('forecast', $postData)) { $txt_modify_data = $postData['forecast']; $ds = explode("\n", $txt_modify_data); foreach ($ds as $line) { if ($line) { $ls = explode(",", $line); if (count($ls) >= 2) { $t = trim($ls[0]); $v = trim($ls[1]); $data .= ($data ? "\r\n" : "") . "{$t},{$v}"; if ($lastT && $t - $lastT != 1 && $continous) { $continous = false; } $lastT = $t; } } } } else { $qData = $this->getDataSet($postData, null, null, $date_begin, null); $dataSet = $qData['dataSet']; foreach ($dataSet as $row) { $occur_date = $row->OCCUR_DATE; $time = $occur_date->diffInDays($from_date); $value = $row->V; $data .= ($data ? "\r\n" : "") . "{$time},{$value}"; if ($lastT && $time - $lastT != 1 && $continous) { $continous = false; } $lastT = $time; } } file_put_contents("data{$mkey}.txt", $data); //$end = '2013-08-29'; //$start = '2013-08-25'; /* $d1 = strtotime($date_from) - strtotime($date_begin); $d1 = floor($d1/(60*60*24)); $d2 = strtotime($date_to) - strtotime($date_begin); $d2 = floor($d2/(60*60*24)); */ $d1 = $date_from->timestamp - $date_begin->timestamp; $d1 = floor($d1 / (60 * 60 * 24)); $d2 = $date_to->timestamp - $date_begin->timestamp; $d2 = floor($d2 / (60 * 60 * 24)); $timeForecast = ""; for ($i = $d1; $i < $d2 + 1; $i++) { $timeForecast .= ($timeForecast ? "\r\n" : "") . $i; if ($lastT && $i - $lastT != 1 && $continous) { $continous = false; } $lastT = $i; } $sqls = []; $warning = ''; if (!$continous) { $warning = "Timing is not continuous"; } file_put_contents("t{$mkey}.txt", $timeForecast); // echo "<b>Time forecast:</b> ".$timeForecast."<br>"; if ($a === "0" || $a === "1") { $params = "{$a},{$b},0,0,0,0,0"; } else { if ($c2 > 0) { $params = "{$a},{$b},0,0,0,{$c1},{$c2}"; } else { $params = "{$a},{$b},{$l},{$u},{$m},{$c1},0"; } } file_put_contents("prop{$mkey}.txt", $params); $error = []; $results = []; if (!file_exists('pdforecast.exe')) { $error[] = "Exec file not found"; } else { if (file_exists("data{$mkey}.txt") && file_exists("t{$mkey}.txt") && file_exists("prop{$mkey}.txt")) { set_time_limit(300); exec("pdforecast.exe {$mkey}"); if (file_exists("error{$mkey}.txt")) { $error[] = file_get_contents("error{$mkey}.txt", true); } if (file_exists("forecast_q{$mkey}.csv")) { // echo "<b>Result:</b><br>"; $file = fopen("forecast_q{$mkey}.csv", "r"); $configuration = auth()->user()->getConfiguration(); $format = $configuration['time']['DATE_FORMAT_CARBON']; //'m/d/Y'; while (!feof($file)) { $line = fgets($file); // echo $line; // $result.= $line; $result = ['value' => $line]; if ($line) { $xs = explode(",", $line); if (count($xs >= 2)) { $x_time = trim($xs[0]); $x_value = trim($xs[1]); if ($x_time >= $d1) { // $x_time=($x_time)*60*60*24+strtotime($date_begin); $beginTimeStamp = $date_begin->timestamp; $x_time = $x_time * 60 * 60 * 24 + $beginTimeStamp; // $x_date=date('Y-m-d',$x_time); $x_date = Carbon::createFromTimestamp($x_time); // $x_date = $x_date->createFromTimestamp($x_time); // echo " ($x_date) "; $rxDate = $x_date ? $x_date->format($format) : $x_date; // $result.= " ($rxDate) "; $result['date'] = $rxDate; if ($cb_update_db == 'true') { $field = "EU_DATA_{$value_type}"; $field = strtoupper($field); $attributes = ['EU_ID' => $object_id, 'OCCUR_DATE' => $x_date, "FLOW_PHASE" => $phase_type]; $values = ['EU_ID' => $object_id, 'OCCUR_DATE' => $x_date, "FLOW_PHASE" => $phase_type, $field => $x_value]; \DB::enableQueryLog(); EnergyUnitDataForecast::updateOrCreate($attributes, $values); $result['sql'] = \Helper::logger(); } } } } $results[] = $result; // echo "<br>"; } \DB::disableQueryLog(); fclose($file); } else { // logError("Result file not found"); $error[] = "Result file not found"; } } else { // logError("Input files not found"); $error[] = "Input files not found"; } } $finalResults = ['data' => $data, 'warning' => $warning, 'params' => $params, 'time' => $timeForecast, 'result' => $results, 'error' => $error, 'key' => $mkey]; $this->cleanFiles($mkey); return response()->json($finalResults); }
public function run(Request $request) { $postData = $request->all(); $phase_type = $postData['ExtensionPhaseType']; $cb_update_db = $postData['cb_update_db']; $occur_date = $postData['date_begin']; $value_type = $postData['ExtensionValueType']; $occur_date = \Helper::parseDate($occur_date); $inputDataSet = $this->getInputDataSet($postData, $occur_date); $objdata = $inputDataSet['data']; $objinfo = $inputDataSet['info']; $mkey = ""; // $mkey = "_".date("Ymdhis_").rand(100,1000)/* ."hung_test" */; $preos = ""; $files = ['gas' => "{$preos}" . "prvap.exe", 'oil' => "{$preos}" . "prliq.exe", 'data' => "{$preos}" . "data{$mkey}.txt", 'm_ij' => "{$preos}" . "m_ij{$mkey}.txt", 'prop' => "{$preos}" . "prop{$mkey}.txt", 'error' => "{$preos}" . "error{$mkey}.txt", 'PR_single_V' => "{$preos}" . "PR_single_V{$mkey}.csv", 'PR_single_L' => "{$preos}" . "PR_single_L{$mkey}.csv"]; $cc = count($objdata); if ($cc <= 0) { return response('empty input data', 401); } //['error'=>"empty input data"]; $ele = array_values($objdata)[0]; $data = ""; $inputData = []; foreach ($ele as $key => $value) { $ss = []; foreach ($objdata as $source => $objValue) { if ($objValue[$key] !== "") { $ss[] = $objValue[$key]; $inputData[] = $objValue[$key] . " <- [{$source}][{$key}]"; } } if (count($ss) > 0) { $data .= ($data ? "\r\n" : "") . implode(",", $ss); } } file_put_contents($files['data'], $data); $error = []; $results = []; $sqls = []; //Gas $exe = $phase_type == 2 ? $files['gas'] : $files['oil']; if (!file_exists($exe)) { $error[] = "Exec {$exe} file not found"; } else { if (file_exists($files['data'])) { set_time_limit(300); exec("{$exe} {$mkey}"); if (file_exists($files['error'])) { $error[] = file_get_contents($files['error'], true); } if (file_exists($files['PR_single_V'])) { $fileName = $files['PR_single_V']; $file = fopen($fileName, "r"); $lastline = ""; $result = []; while (!feof($file)) { $line = fgets($file); $result[] = $line; if ($line) { if ($line) { $lastline = $line; } } } $results[$fileName] = $result; fclose($file); $fileName = $files['PR_single_L']; $file = fopen($fileName, "r"); $lastline = ""; $result = []; while (!feof($file)) { $line = fgets($file); $result[] = $line; if ($line) { if ($line) { $lastline = $line; } } } $results[$fileName] = $result; fclose($file); if ($lastline && $cb_update_db == 'true') { $xs = explode(",", $lastline); $i = 0; foreach ($xs as $svol) { if ($i < count($objinfo)) { $src = $objinfo[$i]["src"]; $pre = $objinfo[$i]["pre"]; $table = $src . "_DATA_VALUE"; $field = $pre . "_DATA_{$value_type}"; $field = strtoupper($field); $attributes = ["OCCUR_DATE" => $occur_date]; if ($src == "ENERGY_UNIT") { $attributes['FLOW_PHASE'] = $phase_type; } $attributes["{$pre}" . "_ID"] = $objinfo[$i]["obj_id"]; $values = $attributes; $values[$field] = $svol; $mdl = \Helper::getModelName($table); \DB::enableQueryLog(); $mdl::updateOrCreate($attributes, $values); $sqls[] = \Helper::logger(); } $i++; } \DB::disableQueryLog(); } } else { $error[] = "Result file not found"; } } else { $error[] = "Input files not found"; } } $finalResults = ['data' => $inputData, 'warning' => '', 'result' => $results, 'error' => $error, 'key' => $mkey, 'exe' => $exe, 'sqls' => $sqls]; // $this->cleanFiles($mkey); return response()->json($finalResults); }