From 305e4bb17f5404e9dafaa7ba7917831b843702c7 Mon Sep 17 00:00:00 2001 From: Henry Whitaker Date: Sat, 10 Apr 2021 11:22:37 +0100 Subject: [PATCH] Order/location of columns is now variable --- app/Casts/CommaSeparatedArrayCast.php | 51 +++++++++++++ app/Helpers/SettingsHelper.php | 3 + app/Setting.php | 5 ++ ..._10_082758_add_visible_columns_setting.php | 38 ++++++++++ resources/js/components/Graphics/TableRow.js | 72 +++++++++++++++---- .../js/components/Graphics/TestsTable.js | 59 +++++++++++++-- 6 files changed, 209 insertions(+), 19 deletions(-) create mode 100644 app/Casts/CommaSeparatedArrayCast.php create mode 100644 database/migrations/2021_04_10_082758_add_visible_columns_setting.php diff --git a/app/Casts/CommaSeparatedArrayCast.php b/app/Casts/CommaSeparatedArrayCast.php new file mode 100644 index 00000000..c8331835 --- /dev/null +++ b/app/Casts/CommaSeparatedArrayCast.php @@ -0,0 +1,51 @@ +name, $this->shouldCast)) { + return $value; + } + + return explode(',', $value); + } + + /** + * Prepare the given value for storage. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $key + * @param mixed $value + * @param array $attributes + * @return mixed + */ + public function set($model, $key, $value, $attributes) + { + if (!in_array($model->name, $this->shouldCast)) { + return $value; + } + + return implode(',', $value); + } +} diff --git a/app/Helpers/SettingsHelper.php b/app/Helpers/SettingsHelper.php index 1ff03877..a314f4d9 100644 --- a/app/Helpers/SettingsHelper.php +++ b/app/Helpers/SettingsHelper.php @@ -168,6 +168,9 @@ class SettingsHelper 'telegram_bot_token' => SettingsHelper::settingIsEditable('telegram_bot_token'), 'telegram_chat_id' => SettingsHelper::settingIsEditable('telegram_chat_id'), ], + 'tables' => [ + 'visible_columns' => SettingsHelper::get('visible_columns')->value, + ], 'auth' => (bool)SettingsHelper::get('auth')->value ]; } diff --git a/app/Setting.php b/app/Setting.php index 78c78765..075fa304 100644 --- a/app/Setting.php +++ b/app/Setting.php @@ -2,6 +2,7 @@ namespace App; +use App\Casts\CommaSeparatedArrayCast; use App\Helpers\SettingsHelper; use Illuminate\Database\Eloquent\Model; @@ -17,4 +18,8 @@ class Setting extends Model ]; protected $table = 'settings'; + + protected $casts = [ + 'value' => CommaSeparatedArrayCast::class, + ]; } diff --git a/database/migrations/2021_04_10_082758_add_visible_columns_setting.php b/database/migrations/2021_04_10_082758_add_visible_columns_setting.php new file mode 100644 index 00000000..4d17b3dc --- /dev/null +++ b/database/migrations/2021_04_10_082758_add_visible_columns_setting.php @@ -0,0 +1,38 @@ + 'visible_columns', + 'value' => 'id,created_at,download,upload,ping', + 'description' => 'Columns visible in the "All Tests" table.' + ]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Setting::whereIn('name', [ + 'visible_columns', + ])->delete(); + } +} diff --git a/resources/js/components/Graphics/TableRow.js b/resources/js/components/Graphics/TableRow.js index 30180892..4e1f9d44 100644 --- a/resources/js/components/Graphics/TableRow.js +++ b/resources/js/components/Graphics/TableRow.js @@ -47,18 +47,62 @@ export default class TableRow extends Component { this.toggleShow(); } + getDataFields = () => { + let allFields = this.props.allFields; + let data = this.state.data; + let processedFields = []; + + for(var key in allFields) { + let field = allFields[key]; + + let value = data[key]; + + if(field.type === 'date') { + value = new Date(value).toLocaleString(); + } else if(field.type === 'bool') { + value = Boolean(value) ? field.if_true : field.if_false + } + + let final = { + name: key, + key: field.alias, + value: value, + type: field.type + }; + + processedFields.push(final); + } + + let visible = []; + let inModal = []; + + window.config.tables.visible_columns.forEach(column => { + visible.push(processedFields.find(x => x.name == column)); + }); + + inModal = processedFields.filter(el => { + return !visible.includes(el); + }); + + return { + visible: visible, + modal: inModal + }; + } + render() { var e = this.state.data; var show = this.state.show; + var fields = this.getDataFields(); if(e.failed != true) { return ( - {e.id} - {new Date(e.created_at).toLocaleString()} - {e.download} - {e.upload} - {e.ping} + {fields.visible.map((e, i) => { + return ( + {e.value} + ); + })} {e.server_host != null ? @@ -67,13 +111,17 @@ export default class TableRow extends Component { More info -

Server ID: {e.server_id}

-

Name: {e.server_name}

-

Host: {e.server_host}

-

URL: Speedtest.net

- {e.scheduled != undefined && -

Type: {e.scheduled == true ? 'scheduled' : 'manual'}

- } + {fields.modal.map((e, i) => { + if(e.type === 'url') { + return ( +

{e.key}: Speedtest.net

+ ); + } else { + return ( +

{e.key}: {e.value}

+ ); + } + })}
diff --git a/resources/js/components/Graphics/TestsTable.js b/resources/js/components/Graphics/TestsTable.js index 700398ab..9b137eb3 100644 --- a/resources/js/components/Graphics/TestsTable.js +++ b/resources/js/components/Graphics/TestsTable.js @@ -14,7 +14,51 @@ export default class TestsTable extends Component { data: [], showTable: false, refresh: true, - interval: null + interval: null, + allFields: { + id: { + type: 'int', + alias: 'ID' + }, + created_at: { + type: 'date', + alias: 'Time' + }, + download: { + type: 'float', + alias: 'Download (Mbit/s)' + }, + upload: { + type: 'float', + alias: 'Upload (Mbit/s)' + }, + ping: { + type: 'float', + alias: 'Ping (ms)' + }, + server_id: { + type: 'int', + alias: 'Server ID' + }, + server_name: { + type: 'string', + alias: 'Name' + }, + server_host: { + type: 'string', + alias: 'Host' + }, + url: { + type: 'url', + alias: 'URL' + }, + scheduled: { + type: 'bool', + alias: 'Type', + if_true: 'scheduled', + if_false: 'manual' + } + } } } @@ -84,6 +128,7 @@ export default class TestsTable extends Component { var data = this.state.data; var show = this.state.showTable; var refresh = this.state.refresh; + let allFields = this.state.allFields; if(data.length > 0) { return ( @@ -102,18 +147,18 @@ export default class TestsTable extends Component { - - - - - + {window.config.tables.visible_columns.map((e, i) => { + return ( + + ); + })} {data.map((e,i) => { return ( - + ); })}
IDTimeDownload (Mbit/s)Upload (Mbit/s)Ping (ms){allFields[e].alias}More