Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.64 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Managers\XXX;
  4.  
  5. use Illuminate\Support\Facades\Storage;
  6.  
  7. class XXX {
  8.  
  9.     /**
  10.      * @param $country
  11.      * @param $from
  12.      * @param $to
  13.      * @return array
  14.      */
  15.     public static function retriveData($country, $from, $to) {
  16.         $folder = 'xxx';
  17.         $path = $folder . '\\' . $country . $from . $to;
  18.         if (Storage::exists($path)) {
  19.             $content = Storage::get($path);
  20.             return json_decode($content, true);
  21.         }
  22.  
  23.         if ($country == 'hrv') {
  24.             $elastic_search = new ElasticXXXSearchHRV($country, $from, $to);
  25.         } else {
  26.             $elastic_search = new ElasticXXXSearchExceptHRV($country, $from, $to);
  27.         }
  28.    
  29.         $result = $elastic_search->searchAllHits();
  30.        
  31.         if (count($result) == 0) return [];
  32.         $columns = [
  33.             'env_code' => 'env_code',
  34.             'load_int' => 'load_int',
  35.             'view' => 'view',
  36.             'timestamp' => 'timestamp'
  37.         ];
  38.         foreach ($result as $page) {
  39.             if ($country == 'hrv') {
  40.                 $loadInt = $page->source()->duration();
  41.             } else {
  42.                 $loadInt = $page->source()->loadInt();
  43.             }
  44.             $array_to_add = [
  45.                 'env_code' => $page->source()->envCode(),
  46.                 'load_int' => $loadInt,
  47.                 'timestamp' => $page->source()->timestamp()
  48.             ];
  49.             if ($country != 'hrv') {
  50.                 $array_to_add['view'] = $page->source()->view();
  51.             }
  52.             $records[] = $array_to_add;
  53.         }
  54.        
  55.         usort($records, function ($a, $b) {
  56.             return $a['load_int'] <=> $b['load_int'];
  57.         });
  58.  
  59.         $temp_array = $records;
  60.         $five_percents = count($temp_array) * 0.05;
  61.         while ($five_percents-- > 0) {
  62.             array_pop($temp_array);
  63.         }
  64.  
  65.         $load_ints = array_column($temp_array, "load_int");
  66.         $average = round(array_sum($load_ints) / count($load_ints));
  67.         // dd('die');
  68.         array_unshift($records, $columns);
  69.         $data['buckets'] = $result->getSearchResponses()->getAggregations()['load_int_range']['buckets'];
  70.         $data['records'] = $records;
  71.         $data['average'] = $average;
  72.         $data['min'] = $temp_array[0]['load_int'];
  73.         $data['max'] = $temp_array[count($temp_array) - 1]['load_int'];
  74.  
  75.         if (!in_array($folder, Storage::directories())) {
  76.             Storage::makeDirectory($folder);
  77.         }
  78.         Storage::put($folder . '\\' . $country . $from . $to, json_encode($data));
  79.         // dump(json_encode($data));
  80.  
  81.         // return $data;
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement