Exemplo n.º 1
0
 public function recalcStreams()
 {
     $bannerStreams = BannerStream::all();
     echo '<table border="1"><tr><th>stream</th><th>banner</th><th>client</th><th>price</th><th>recalced price</th><th>difference</th></tr>';
     foreach ($bannerStreams as $bs) {
         $amount = $bs->amount;
         $recalcedAmount = StreamTimelog::whereStreamId($bs->stream_id)->sum('amount');
         if ($recalcedAmount > 0) {
             $diff = number_format(100 * $amount / $recalcedAmount, 2);
         } else {
             if ($amount > 0) {
                 $diff = 'failed';
             }
         }
         if ($diff == '100.00') {
             $diff = '';
             echo '<tr>';
         } else {
             $diff .= '%';
             echo '<tr style="font-weight: bold">';
         }
         echo '<tr>';
         echo '<td>stream#' . $bs->stream_id . '</td>';
         echo '<td>banner#' . $bs->banner_id . '</td>';
         echo '<td>client#' . $bs->banner->client_id . '</td>';
         echo '<td>' . $amount . '</td>';
         echo '<td>' . $recalcedAmount . '</td>';
         echo '<td>' . $diff . '</td>';
         echo '<tr>';
     }
     echo '</table>';
 }
Exemplo n.º 2
0
 public function addStreams()
 {
     $twitchers = User::whereType('twitcher')->get();
     $faker = $this->faker;
     $i = 0;
     foreach ($twitchers as $t) {
         try {
             $bannerTypes = $t->bannerTypes;
             if (count($bannerTypes) == 0) {
                 continue;
             }
             $bannerType = $bannerTypes->random();
             $banners = BannerMapper::activeTwitcher($t, $bannerType->id);
             if (count($banners) == 0) {
                 continue;
             }
             $streamDate = \Carbon\Carbon::createFromTimestamp($faker->dateTimeBetween($t->created_at)->getTimestamp());
             $stream = Stream::create(['user_id' => $t->id, 'time_start' => $streamDate]);
             BannerMapper::bannersToStream($stream, $banners);
             LogMapper::log('stream_start', $stream->id);
             foreach ($banners as $b) {
                 NotificationMapper::bannerStream($b, $stream);
             }
             $maxMinutes = rand(3, 30);
             for ($m = 0; $m < $maxMinutes; $m++) {
                 $startTime = $streamDate->getTimestamp();
                 $endTime = $streamDate->addMinutes(10)->getTimestamp();
                 $startTime = \Carbon\Carbon::createFromTimestamp($startTime);
                 $endTime = \Carbon\Carbon::createFromTimestamp($endTime);
                 $streamDate = $endTime;
                 $status = rand(0, 1);
                 if ($status == 1) {
                     //$uploadDir = '/assets/app/upload/t/';
                     $screenshot = $faker->imageUrl(640, 360);
                     //$screenshot = $uploadDir.basename($screenshot);
                     $viewers = rand(0, 100);
                     $response = (object) ['stream' => (object) ['viewers' => $viewers, 'preview' => (object) ['medium' => $faker->imageUrl(640, 360)]]];
                     $streamTimelog = StreamTimelog::create(['stream_id' => $stream->id, 'timeslot_start' => $startTime, 'timeslot_end' => $endTime, 'viewers' => $viewers, 'status' => 'live', 'screenshot' => $screenshot, 'response' => $response]);
                     $streamTimelog->calcAmount();
                     foreach ($banners as $b) {
                         BannerMapper::trackBanner($b, $stream, $streamTimelog);
                     }
                 } else {
                     $streamTimelog = StreamTimelog::create(['stream_id' => $stream->id, 'timeslot_start' => $startTime, 'timeslot_end' => $endTime, 'viewers' => 0, 'status' => 'died', 'screenshot' => '', 'response' => (object) []]);
                 }
             }
             $stream->time_end = $streamDate;
             $stream->save();
             $i++;
         } catch (\Exception $e) {
             dd($e->getTraceAsString());
         }
     }
 }
Exemplo n.º 3
0
 /**
  * @param Stream $stream
  * @param $response
  * @return StreamTimelog
  */
 public static function trackStream(Stream $stream, $response)
 {
     $prevTrackTime = Session::get('stream_lastPing', time());
     if ($response->stream != null) {
         $viewers = $response->stream->viewers;
         $screenshot = $response->stream->preview->large;
         if ($screenshot) {
             $filename = '/assets/app/upload/t/' . uniqid($stream->id . '_' . date('YmdHis')) . '.jpg';
             $screenshotFile = public_path($filename);
             if (copy($screenshot, $screenshotFile)) {
                 $screenshot = $filename;
             } else {
                 $screenshot = '';
             }
         } else {
             $screenshot = '';
         }
         $status = 'live';
     } else {
         $viewers = 0;
         $screenshot = '';
         $status = 'died';
     }
     $streamTimelog = StreamTimelog::create(['stream_id' => $stream->id, 'timeslot_start' => \Carbon\Carbon::createFromTimestamp($prevTrackTime), 'timeslot_end' => \Carbon\Carbon::now(), 'viewers' => $viewers, 'status' => $status, 'price' => $status, 'screenshot' => $screenshot, 'response' => $response]);
     $streamTimelog->calcAmount();
     Session::set('stream_lastPing', time());
     return $streamTimelog;
 }