Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Managers\XXX;
- use Illuminate\Support\Facades\Storage;
- class XXX {
- /**
- * @param $country
- * @param $from
- * @param $to
- * @return array
- */
- public static function retriveData($country, $from, $to) {
- $folder = 'xxx';
- $path = $folder . '\\' . $country . $from . $to;
- if (Storage::exists($path)) {
- $content = Storage::get($path);
- return json_decode($content, true);
- }
- if ($country == 'hrv') {
- $elastic_search = new ElasticXXXSearchHRV($country, $from, $to);
- } else {
- $elastic_search = new ElasticXXXSearchExceptHRV($country, $from, $to);
- }
- $result = $elastic_search->searchAllHits();
- if (count($result) == 0) return [];
- $columns = [
- 'env_code' => 'env_code',
- 'load_int' => 'load_int',
- 'view' => 'view',
- 'timestamp' => 'timestamp'
- ];
- foreach ($result as $page) {
- if ($country == 'hrv') {
- $loadInt = $page->source()->duration();
- } else {
- $loadInt = $page->source()->loadInt();
- }
- $array_to_add = [
- 'env_code' => $page->source()->envCode(),
- 'load_int' => $loadInt,
- 'timestamp' => $page->source()->timestamp()
- ];
- if ($country != 'hrv') {
- $array_to_add['view'] = $page->source()->view();
- }
- $records[] = $array_to_add;
- }
- usort($records, function ($a, $b) {
- return $a['load_int'] <=> $b['load_int'];
- });
- $temp_array = $records;
- $five_percents = count($temp_array) * 0.05;
- while ($five_percents-- > 0) {
- array_pop($temp_array);
- }
- $load_ints = array_column($temp_array, "load_int");
- $average = round(array_sum($load_ints) / count($load_ints));
- // dd('die');
- array_unshift($records, $columns);
- $data['buckets'] = $result->getSearchResponses()->getAggregations()['load_int_range']['buckets'];
- $data['records'] = $records;
- $data['average'] = $average;
- $data['min'] = $temp_array[0]['load_int'];
- $data['max'] = $temp_array[count($temp_array) - 1]['load_int'];
- if (!in_array($folder, Storage::directories())) {
- Storage::makeDirectory($folder);
- }
- Storage::put($folder . '\\' . $country . $from . $to, json_encode($data));
- // dump(json_encode($data));
- // return $data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement