| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Tables\Builders\enso\DataImport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LaravelEnso\Tables\Contracts\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\Models\enso\dataimport\DataImport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Models\enso\dataimport\RejectedImport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DataImportTable implements Table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected const TemplatePath = __DIR__.'/../../../Templates/enso/dataimport/dataImports.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function query(): Builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         return DataImport::selectRaw(" | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             data_imports.id, data_imports.type, data_imports.status, files.original_name as name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             data_imports.successful, data_imports.failed, data_imports.created_at, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             TIME(data_imports.created_at) as time, people.name as createdBy, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             rejected_imports.id as rejectedId, {$this->rawDuration()} as duration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         ")->join('files', fn ($join) => $join | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             ->on('files.attachable_id', 'data_imports.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             ->where('files.attachable_type', DataImport::morphMapKey())) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ->join('users', 'files.created_by', '=', 'users.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->join('people', 'users.person_id', '=', 'people.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->leftJoin('rejected_imports', 'data_imports.id', '=', 'rejected_imports.data_import_id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             ->leftJoin('files as rejected_files', fn ($join) => $join | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 ->on('rejected_files.attachable_id', 'rejected_imports.id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 ->where('rejected_files.attachable_type', RejectedImport::morphMapKey())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function templatePath(): string | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         return static::TemplatePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private function rawDuration(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         switch (DB::getDriverName()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             case 'sqlite': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 return $this->sqliteDuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             case 'mysql': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 return $this->mysqlDuration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 return 'N/A'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private function sqliteDuration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $days = 'julianday(data_imports.updated_at) - julianday(data_imports.created_at)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $seconds = "({$days}) * 86400.0"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return "time({$seconds}, 'unixepoch')"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private function mysqlDuration() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $seconds = 'timestampdiff(second, data_imports.created_at, data_imports.updated_at)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return "sec_to_time({$seconds})"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |