55use App \Filament \Resources \ResultResource \Pages ;
66use App \Models \Result ;
77use App \Settings \GeneralSettings ;
8+ use Carbon \Carbon ;
9+ use Filament \Forms ;
10+ use Filament \Forms \Components \TextInput ;
11+ use Filament \Resources \Form ;
812use Filament \Resources \Resource ;
913use Filament \Resources \Table ;
1014use Filament \Tables ;
1115use Filament \Tables \Actions \Action ;
1216use Filament \Tables \Columns \IconColumn ;
1317use Filament \Tables \Columns \TextColumn ;
14- use Filament \ Tables \ Columns \ ViewColumn ;
18+ use Illuminate \ Database \ Eloquent \ Builder ;
1519
1620class ResultResource extends Resource
1721{
@@ -21,6 +25,71 @@ class ResultResource extends Resource
2125
2226 protected static ?string $ navigationLabel = 'Results ' ;
2327
28+ public static function form (Form $ form ): Form
29+ {
30+ $ settings = new GeneralSettings ();
31+
32+ return $ form
33+ ->schema ([
34+ Forms \Components \Grid::make ([
35+ 'default ' => 2 ,
36+ 'md ' => 3 ,
37+ ])->schema ([
38+ Forms \Components \Grid::make ([
39+ 'default ' => 2 ,
40+ 'md ' => 3 ,
41+ ])
42+ ->schema ([
43+ Forms \Components \TextInput::make ('id ' )
44+ ->label ('ID ' ),
45+ Forms \Components \TextInput::make ('created_at ' )
46+ ->label ('Created ' )
47+ ->afterStateHydrated (function (TextInput $ component , $ state ) use ($ settings ) {
48+ $ component ->state (Carbon::parse ($ state )->format ($ settings ->time_format ?? 'M j, Y G:i:s ' ));
49+ })
50+ ->columnSpan (2 ),
51+ Forms \Components \TextInput::make ('server_id ' )
52+ ->label ('Server ID ' ),
53+ Forms \Components \TextInput::make ('server_name ' )
54+ ->label ('Server name ' )
55+ ->columnSpan (2 ),
56+ Forms \Components \TextInput::make ('server_host ' )
57+ ->label ('Server host ' )
58+ ->columnSpan ([
59+ 'default ' => 2 ,
60+ 'md ' => 3 ,
61+ ]),
62+ Forms \Components \TextInput::make ('download ' )
63+ ->label ('Download (Mbps) ' )
64+ ->afterStateHydrated (function (TextInput $ component , $ state ) {
65+ $ component ->state (! blank ($ state ) ? formatBits (formatBytestoBits ($ state ), 3 , false ) : '' );
66+ }),
67+ Forms \Components \TextInput::make ('upload ' )
68+ ->label ('Upload (Mbps) ' )
69+ ->afterStateHydrated (function (TextInput $ component , $ state ) {
70+ $ component ->state (! blank ($ state ) ? formatBits (formatBytestoBits ($ state ), 3 , false ) : '' );
71+ }),
72+ Forms \Components \TextInput::make ('ping ' )
73+ ->label ('Ping (Ms) ' ),
74+ ])
75+ ->columnSpan (2 ),
76+ Forms \Components \Card::make ()
77+ ->schema ([
78+ Forms \Components \Checkbox::make ('successful ' ),
79+ Forms \Components \Checkbox::make ('scheduled ' ),
80+ ])
81+ ->columns (1 )
82+ ->columnSpan ([
83+ 'default ' => 2 ,
84+ 'md ' => 1 ,
85+ ]),
86+ ]),
87+ Forms \Components \Textarea::make ('data ' )
88+ ->rows (10 )
89+ ->columnSpan (2 ),
90+ ]);
91+ }
92+
2493 public static function table (Table $ table ): Table
2594 {
2695 $ settings = new GeneralSettings ();
@@ -29,59 +98,78 @@ public static function table(Table $table): Table
2998 ->columns ([
3099 TextColumn::make ('id ' )
31100 ->label ('ID ' ),
101+ TextColumn::make ('server ' )
102+ ->getStateUsing (fn (Result $ record ): string |null => ! blank ($ record ->server_id ) ? $ record ->server_id .' ( ' .$ record ->server_name .') ' : null )
103+ ->toggleable (),
104+ IconColumn::make ('successful ' )
105+ ->boolean ()
106+ ->toggleable (),
32107 IconColumn::make ('scheduled ' )
33108 ->boolean ()
34- ->toggleable ()
35- ->toggledHiddenByDefault (),
36- ViewColumn::make ('download ' )
37- ->view ('tables.columns.bits-column ' ),
38- ViewColumn::make ('upload ' )
39- ->view ('tables.columns.bits-column ' ),
109+ ->toggleable (),
110+ TextColumn::make ('download ' )
111+ ->label ('Download (Mbps) ' )
112+ ->getStateUsing (fn (Result $ record ): string |null => ! blank ($ record ->download ) ? formatBits (formatBytestoBits ($ record ->download ), 3 , false ) : null ),
113+ TextColumn::make ('upload ' )
114+ ->label ('Upload (Mbps) ' )
115+ ->getStateUsing (fn (Result $ record ): string |null => ! blank ($ record ->upload ) ? formatBits (formatBytestoBits ($ record ->upload ), 3 , false ) : null ),
40116 TextColumn::make ('ping ' )
117+ ->label ('Ping (Ms) ' )
41118 ->toggleable (),
42119 TextColumn::make ('download_jitter ' )
43- ->getStateUsing (function (Result $ record ): string |null {
44- return json_decode ($ record ->data , true )['download ' ]['latency ' ]['jitter ' ] ?? null ;
45- })
120+ ->getStateUsing (fn (Result $ record ): string |null => json_decode ($ record ->data , true )['download ' ]['latency ' ]['jitter ' ] ?? null )
46121 ->toggleable ()
47122 ->toggledHiddenByDefault (),
48123 TextColumn::make ('upload_jitter ' )
49- ->getStateUsing (function (Result $ record ): string |null {
50- return json_decode ($ record ->data , true )['upload ' ]['latency ' ]['jitter ' ] ?? null ;
51- })
124+ ->getStateUsing (fn (Result $ record ): string |null => json_decode ($ record ->data , true )['upload ' ]['latency ' ]['jitter ' ] ?? null )
52125 ->toggleable ()
53126 ->toggledHiddenByDefault (),
54127 TextColumn::make ('ping_jitter ' )
55- ->getStateUsing (function (Result $ record ): string |null {
56- return json_decode ($ record ->data , true )['ping ' ]['jitter ' ] ?? null ;
57- })
128+ ->getStateUsing (fn (Result $ record ): string |null => json_decode ($ record ->data , true )['ping ' ]['jitter ' ] ?? null )
58129 ->toggleable ()
59130 ->toggledHiddenByDefault (),
60- ViewColumn::make ('server_id ' )
61- ->label ('Server ID ' )
62- ->view ('tables.columns.server-column ' )
63- ->toggleable (),
64131 TextColumn::make ('created_at ' )
132+ ->label ('Created ' )
65133 ->dateTime ($ settings ->time_format ?? 'M j, Y G:i:s ' )
66- ->timezone ($ settings ->timezone ?? 'UTC ' ),
134+ ->timezone ($ settings ->timezone ?? 'UTC ' )
135+ ->sortable (),
67136 ])
68137 ->filters ([
69- //
138+ Tables \Filters \TernaryFilter::make ('scheduled ' )
139+ ->placeholder ('- ' )
140+ ->trueLabel ('Only scheduled speedtests ' )
141+ ->falseLabel ('Only manual speedtests ' )
142+ ->queries (
143+ true: fn (Builder $ query ) => $ query ->where ('scheduled ' , true ),
144+ false: fn (Builder $ query ) => $ query ->where ('scheduled ' , false ),
145+ blank: fn (Builder $ query ) => $ query ,
146+ ),
147+ Tables \Filters \TernaryFilter::make ('successful ' )
148+ ->placeholder ('- ' )
149+ ->trueLabel ('Only successful speedtests ' )
150+ ->falseLabel ('Only failed speedtests ' )
151+ ->queries (
152+ true: fn (Builder $ query ) => $ query ->where ('successful ' , true ),
153+ false: fn (Builder $ query ) => $ query ->where ('successful ' , false ),
154+ blank: fn (Builder $ query ) => $ query ,
155+ ),
70156 ])
71157 ->actions ([
72158 Tables \Actions \ActionGroup::make ([
73159 Action::make ('view result ' )
74160 ->label ('View on Speedtest.net ' )
75161 ->icon ('heroicon-o-link ' )
76- ->url (fn (Result $ record ): string => $ record ->url )
162+ ->url (fn (Result $ record ): string |null => optional ($ record )->url )
163+ ->hidden (fn (Result $ record ): bool => ! $ record ->is_successful )
77164 ->openUrlInNewTab (),
78- // Tables\Actions\ViewAction::make(),
165+ Tables \Actions \ViewAction::make (),
79166 Tables \Actions \DeleteAction::make (),
80167 ]),
81168 ])
82169 ->bulkActions ([
83170 Tables \Actions \DeleteBulkAction::make (),
84- ]);
171+ ])
172+ ->defaultSort ('created_at ' , 'desc ' );
85173 }
86174
87175 public static function getRelations (): array
0 commit comments