From 451cd699cb8186d109708e6c8a014c035a907079 Mon Sep 17 00:00:00 2001 From: Peter Deltchev Date: Sun, 20 Sep 2015 02:38:17 -0700 Subject: [PATCH] Fixed zeroed timestamps being recorded for statistics and added a script to fill in the missing data. --- app/Console/Commands/FixYearZeroLogs.php | 69 ++++++++++++++++++++++++ app/Console/Kernel.php | 1 + app/ResourceLogItem.php | 12 +++-- 3 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 app/Console/Commands/FixYearZeroLogs.php diff --git a/app/Console/Commands/FixYearZeroLogs.php b/app/Console/Commands/FixYearZeroLogs.php new file mode 100644 index 00000000..ca17e81d --- /dev/null +++ b/app/Console/Commands/FixYearZeroLogs.php @@ -0,0 +1,69 @@ +orderBy('id', 'asc')->get(); + $totalItems = $items->count(); + + // calculate the start and end of the logging gap + $lastGoodId = (int) $items[0]->id - 1; + $lastGoodItem = ResourceLogItem::find($lastGoodId); + + $lastGoodDate = $lastGoodItem->created_at; + $now = Carbon::now(); + + $secondsDifference = $now->diffInSeconds($lastGoodDate); + $oneInterval = $secondsDifference / $totalItems; + + $this->info('Correcting records...'); + $bar = $this->output->createProgressBar($totalItems); + + foreach ($items as $i => $item) { + $bar->advance(); + $dateOffset = (int) ($oneInterval * $i); + $item->created_at = $lastGoodDate->copy()->addSeconds($dateOffset); + $item->save(); + } + + $bar->finish(); + $this->line(''); + $this->info('All done!'); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 6e569828..ebb5d5ef 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -18,6 +18,7 @@ class Kernel extends ConsoleKernel \App\Console\Commands\ImportMLPMA::class, \App\Console\Commands\ClassifyMLPMA::class, \App\Console\Commands\RebuildTags::class, + \App\Console\Commands\FixYearZeroLogs::class, ]; /** diff --git a/app/ResourceLogItem.php b/app/ResourceLogItem.php index d361923a..bac5f905 100644 --- a/app/ResourceLogItem.php +++ b/app/ResourceLogItem.php @@ -3,14 +3,16 @@ namespace App; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Request; +use Carbon\Carbon; +use Auth; +use DB; +use Request; class ResourceLogItem extends Model { protected $table = 'resource_log_items'; public $timestamps = false; + protected $dates = ['created_at']; const VIEW = 1; const DOWNLOAD = 2; @@ -22,7 +24,7 @@ class ResourceLogItem extends Model $logItem = new ResourceLogItem(); $logItem->{$resourceIdColumn} = $resourceId; - $logItem->created_at = time(); + $logItem->created_at = Carbon::now(); $logItem->log_type = $logType; $logItem->track_format_id = $formatId; $logItem->ip_address = Request::getClientIp(); @@ -81,4 +83,4 @@ class ResourceLogItem extends Model ]); } } -} \ No newline at end of file +}