Home > Mobile >  how to calculate percentage while getting data from api response
how to calculate percentage while getting data from api response

Time:12-16

I'm getting response from an api in Laravel using HTTP client. And It takes some time.I just want to calculate that how much percentage of data is fetched from api. And when progressbar of 100% complete it means data is completely fetched and ready to show. So any solution for this.

CodePudding user response:

if you are that flexible then you can do it this way ;) You can use queue job with livewire and tailwind css to achieve this goal.

  • install tailwind.
  • install livewire with this command composer require livewire/livewire.
  • run php artisan make:livewire YourClassWire to create livwire class, and you can find it under /App/Http/Livewire.
  • make YourClassWire somthing like this :

class YourClassWire extends Component
{
  public $visibility;
  // you need this to hide progress bar if it is 100% or  0 
  public $progress  ;
  // this will be your progress.
  public $total     ;
  // this is your total job
 protected $listeners = ['reresh']; // this will called from the view
  
  public function mount()
    {
     $this->total      = \DB::table('jobs')->count(); // you must  change this if you have multiple queue with different names
     $this->visibility = $this->total > 0 ? 'visible' : 'hidden' ; // we need it to show hide porgress ;
     $this->progress   = \DB::table('jobs')->count() * 100 / $this->total; // simple equation to get progress percentage
     $this->render(); // you now call render each time the view  refresh 
    }
    
    public function refresh()
    {
    // this function will be responsible from updating the progress
     $this->visibility = $this->total > 0 ? 'visible' : 'hidden' ; // we need it to show hide porgress ;
     $this->progress   = \DB::table('jobs')->count() * 100 / $this->total; // simple equation to get progress percentage.
    }
    public function render()
     {
               return view('livewire.your-class-wire',) // note this will created with command you executed earlier `php artisan make:livewire YourClassWire` and you can find it in view/livewire
               ->with('progress', $this->progress)
               ->with('visibility',$this->visibility);
     }
  }
now in your livewire view you can create your progress bar
  <div wire:poll.2s="refresh">
<div >
    <div >
    <!-- controll the width with the progress -->
        <div  class= "progressbar border-0 relative align-middle bg-gradient-to-r from-[#d72323] via-[#dddd03] to-[#248401] dark:bg-[#dcf5ff] h-1 rounded-full"  style="width: {{ $progress }}%">
            <div  >✈️
             <!-- now this text icon will move to the end based on your progress --> 
            </div>
        </div>
    </div>
</div>
</div>

I hope this guide you to what you want..success

CodePudding user response:

You could do some logic like:

get data from API
foreach dataset taken, do the calculation
after the calculation store/update model
get out of the foreach loop or take the next entry

or if you need to calculate average or something like that you can go

$emptyArray = [];
foreach($responses as $response) {
                $emptyArray [$response->id][] = $response->data; // data === type of integer
            }

$emptyArray_avg = [];
        foreach($emptyArray as $arrEl) {
            $emptyArray_avg[] = array_sum($arrEl)/count($arrEl);
        }

Note: If there are a lot of data in the JSON response, queues would be a good solution for it :)

Queues

  • Related