Skip to content

Commit

Permalink
Refactor search queries and extend time out
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Aug 22, 2022
1 parent 5579295 commit ab3c751
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public function search(Node $node, Request $request)

if ($request->show_available_ips)
{
return $builder->where('server_id', null)->get();
$builder = $builder->where('server_id', null);
}

return $builder->get();
return $builder->paginate($request->query('per_page') ?? 50);
}
}
18 changes: 17 additions & 1 deletion app/Http/Controllers/Admin/Templates/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,25 @@
namespace App\Http\Controllers\Admin\Templates;

use App\Http\Controllers\Controller;
use App\Models\Node;
use App\Models\Template;
use App\Services\Nodes\TemplateService;
use Illuminate\Http\Request;

class TemplateController extends Controller
{
//
public function __construct(private TemplateService $templateService)
{

}

public function index(Node $node)
{
return $this->templateService->setNode($node)->getTemplates();
}

public function show(Node $node, Template $template)
{
return $template;
}
}
2 changes: 1 addition & 1 deletion app/Jobs/Servers/ProcessInstallation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ProcessInstallation implements ShouldQueue
*
* @var int
*/
public $timeout = 420;
public $timeout = 1000;

/**
* The number of times the job may be attempted.
Expand Down
10 changes: 1 addition & 9 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use App\Rules\Network\Hostname;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Node extends Model
{
use HasFactory, Searchable;
use HasFactory;

protected $fillable = [
'name', 'cluster', 'hostname', 'token_id', 'secret', 'port'
Expand Down Expand Up @@ -37,11 +36,4 @@ public function addresses()
{
return $this->hasMany(IPAddress::class);
}

public function toSearchableArray()
{
return [
'name' => $this->name
];
}
}
10 changes: 1 addition & 9 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Server extends Model
{
use HasFactory, Searchable;
use HasFactory;

protected $fillable = [
'name',
Expand Down Expand Up @@ -37,11 +36,4 @@ public function template()
{
return $this->hasOne(Template::class);
}

public function toSearchableArray()
{
return [
'name' => $this->name
];
}
}
14 changes: 1 addition & 13 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use App\Models\Server;
use Laravel\Scout\Searchable;

/**
* @mixin \Eloquent
*/
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Searchable;
use HasApiTokens, HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
Expand Down Expand Up @@ -55,14 +53,4 @@ public function servers()
{
return $this->hasMany(Server::class);
}

/**
* @return array
*/
public function toSearchableArray()
{
return [
'name' => $this->name
];
}
}
21 changes: 17 additions & 4 deletions resources/js/api/admin/nodes/addresses/searchAddresses.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Address } from '@/api/admin/nodes/addresses/types'
import { PaginatedInterface } from '@/api/types/default'
import axios from 'axios'

export default (query: string, nodeId: number, showAvailableIps: boolean = false) => {
return axios.get<Address[]>(route('admin.nodes.show.addresses.search', nodeId), {
params: { 'filter[*]': query, show_available_ips: showAvailableIps },
})
export default (
query: string,
nodeId: number,
showAvailableIps: boolean = false
) => {
return axios.get<PaginatedInterface<Address[]>>(
route('admin.nodes.show.addresses.search', {
node: nodeId,
params: { 'filter[address]': query },
}),
{
params: {
show_available_ips: showAvailableIps,
},
}
)
}
3 changes: 2 additions & 1 deletion resources/js/api/admin/nodes/searchNodes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Node } from '@/api/admin/nodes/types'
import { PaginatedInterface } from '@/api/types/default'
import axios from 'axios'

export default (search: string) => {
return axios.get<Node[]>(
return axios.get<PaginatedInterface<Node[]>>(
route('admin.nodes.search', {
params: {
'filter[*]' : search,
Expand Down
3 changes: 2 additions & 1 deletion resources/js/api/admin/servers/searchServers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios'
import { Server as DefaultServer } from '@/api/server/types'
import { PaginatedInterface } from '@/api/types/default'

export interface Server extends DefaultServer {
node: {
Expand All @@ -9,7 +10,7 @@ export interface Server extends DefaultServer {
}

export default (search: string) => {
return axios.get<Server[]>(
return axios.get<PaginatedInterface<Server[]>>(
route('admin.servers.search', {
params: {
'filter[*]': search,
Expand Down
10 changes: 10 additions & 0 deletions resources/js/api/admin/servers/templates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ export interface Template {
created_at?: string
updated_at?: string
}

export interface ServerTemplate {
id: number
server_id: number
server: {
id: number
vmid: number
name: string
}
}
4 changes: 2 additions & 2 deletions resources/js/api/admin/users/searchUsers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { User } from '@/api/types/default'
import { PaginatedInterface, User } from '@/api/types/default'
import axios from 'axios'

export default (search: string) => {
return axios.get<User[]>(
return axios.get<PaginatedInterface<User[]>>(
route('admin.users.search', {
params: {
'filter[*]' : search,
Expand Down
11 changes: 1 addition & 10 deletions resources/js/api/server/settings/getTemplates.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { ServerTemplate } from '@/api/admin/servers/templates/types'
import axios from 'axios'

export interface ServerTemplate {
id: number
server_id: number
server: {
id: number
vmid: number
name: string
}
}

export default (serverId: number) => {
return axios.get<ServerTemplate[]>(route('servers.show.templates', serverId))
}
19 changes: 19 additions & 0 deletions resources/js/api/types/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ export interface AuthInterface {
export interface DefaultProps {
auth: AuthInterface
}

export interface LinkInterface {
back?: string
next?: string
}

export interface PaginationInterface {
total: number
count: number
per_page: number
current_page: number
total_pages: number
links: LinkInterface
}

export interface PaginatedInterface<T> {
data: T
meta: PaginationInterface
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const EditAddressModal = ({ node, address, open, setOpen }: Props) => {

const searchServers = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchServers(query)
const { data: { data } } = await getSearchServers(query)
setServers(
data.map((server) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const NewAddressModal = ({node, open, setOpen}: Props) => {

const searchServers = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchServers(query)
const { data: { data } } = await getSearchServers(query)
setServers(
data.map((server) => {
return {
Expand Down
10 changes: 5 additions & 5 deletions resources/js/pages/admin/servers/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import getSearchNodes from '@/api/admin/nodes/searchNodes'
import SelectItem from '@/components/SelectItem'
import getSearchUsers from '@/api/admin/users/searchUsers'
import { useQuery } from '@tanstack/react-query'
import getTemplates from '@/api/server/settings/getTemplates'
import getTemplates from '@/api/admin/nodes/templates/getTemplates'
import getSearchAddresses from '@/api/admin/nodes/addresses/searchAddresses'

interface Props extends DefaultProps {}
Expand Down Expand Up @@ -94,7 +94,7 @@ const Create = ({ auth }: Props) => {

const searchNodes = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchNodes(query)
const { data: { data } } = await getSearchNodes(query)
setNodes(
data.map((node) => {
return {
Expand All @@ -118,7 +118,7 @@ const Create = ({ auth }: Props) => {

const searchUsers = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchUsers(query)
const { data: { data } } = await getSearchUsers(query)
setUsers(
data.map((user) => {
return {
Expand All @@ -141,13 +141,13 @@ const Create = ({ auth }: Props) => {

const searchIps = useCallback(
debounce(async (query: string) => {
const { data: res } = await getSearchAddresses(
const { data: { data } } = await getSearchAddresses(
query,
dataRef.current.node_id as number,
true
)
setIps(
res.map((ip) => {
data.map((ip) => {
return {
label: ip.address,
value: ip.id.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const BasicSettings = () => {

const searchNodes = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchNodes(query)
const { data: { data } } = await getSearchNodes(query)
setNodes(
data.map((node) => {
return {
Expand All @@ -76,7 +76,7 @@ const BasicSettings = () => {

const searchUsers = useCallback(
debounce(async (query: string) => {
const { data } = await getSearchUsers(query)
const { data: { data } } = await getSearchUsers(query)
setUsers(
data.map((user) => {
return {
Expand Down
6 changes: 6 additions & 0 deletions routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Controllers\Admin\Servers\Settings;
use App\Http\Controllers\Admin\Users;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\Templates\TemplateController;

Route::get('/', [IndexController::class, 'index'])->name('admin.dashboard');

Expand All @@ -31,6 +32,11 @@
Route::get('/', [NodeController::class, 'show'])->name('admin.nodes.show');
Route::delete('/', [NodeController::class, 'destroy']);

Route::prefix('/templates')->group(function () {
Route::get('/', [TemplateController::class, 'index'])->name('admin.nodes.show.templates');
Route::get('/{template}', [TemplateController::class, 'show'])->name('admin.nodes.show.templates.show');
});

Route::prefix('/addresses')->group(function () {
Route::get('/', [AddressController::class, 'index'])->name('admin.nodes.show.addresses');
Route::post('/', [AddressController::class, 'store']);
Expand Down

0 comments on commit ab3c751

Please sign in to comment.