2021-07-19 20:34:10 +00:00
|
|
|
import { useContext } from 'react';
|
2021-01-05 16:17:44 +00:00
|
|
|
import useSWR from 'swr';
|
2021-07-19 20:37:09 +00:00
|
|
|
import { createContext } from '@/api/admin';
|
2021-08-02 15:54:13 +00:00
|
|
|
import http, { FractalResponseData, getPaginationSet, PaginatedResult } from '@/api/http';
|
|
|
|
import { Egg, rawDataToEgg } from '@/api/admin/eggs/getEgg';
|
2021-01-05 16:17:44 +00:00
|
|
|
import { Node, rawDataToNode } from '@/api/admin/nodes/getNodes';
|
|
|
|
import { User, rawDataToUser } from '@/api/admin/users/getUsers';
|
|
|
|
|
|
|
|
export interface Server {
|
|
|
|
id: number;
|
2021-05-20 22:00:46 +00:00
|
|
|
externalId: string | null
|
2021-01-05 16:17:44 +00:00
|
|
|
uuid: string;
|
|
|
|
identifier: string;
|
|
|
|
name: string;
|
|
|
|
description: string;
|
2021-05-20 22:00:46 +00:00
|
|
|
status: string;
|
|
|
|
|
|
|
|
limits: {
|
|
|
|
memory: number;
|
|
|
|
swap: number;
|
|
|
|
disk: number;
|
|
|
|
io: number;
|
|
|
|
cpu: number;
|
2021-08-02 17:53:04 +00:00
|
|
|
threads: string | null;
|
2021-05-20 22:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
featureLimits: {
|
|
|
|
databases: number;
|
|
|
|
allocations: number;
|
|
|
|
backups: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
ownerId: number;
|
|
|
|
nodeId: number;
|
|
|
|
allocationId: number;
|
|
|
|
nestId: number;
|
|
|
|
eggId: number;
|
|
|
|
|
|
|
|
container: {
|
|
|
|
startupCommand: string;
|
|
|
|
defaultStartup: string;
|
|
|
|
image: string;
|
|
|
|
environment: Map<string, string>;
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:17:44 +00:00
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
|
|
|
|
|
|
|
relations: {
|
2021-05-20 22:00:46 +00:00
|
|
|
egg: Egg | undefined;
|
2021-01-05 16:17:44 +00:00
|
|
|
node: Node | undefined;
|
|
|
|
user: User | undefined;
|
2021-07-19 20:34:10 +00:00
|
|
|
}
|
2021-01-05 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 23:38:39 +00:00
|
|
|
export const rawDataToServer = ({ attributes }: FractalResponseData): Server => ({
|
2021-01-05 16:17:44 +00:00
|
|
|
id: attributes.id,
|
|
|
|
externalId: attributes.external_id,
|
|
|
|
uuid: attributes.uuid,
|
|
|
|
identifier: attributes.identifier,
|
|
|
|
name: attributes.name,
|
|
|
|
description: attributes.description,
|
2021-05-20 22:00:46 +00:00
|
|
|
status: attributes.status,
|
|
|
|
|
|
|
|
limits: {
|
|
|
|
memory: attributes.limits.memory,
|
|
|
|
swap: attributes.limits.swap,
|
|
|
|
disk: attributes.limits.disk,
|
|
|
|
io: attributes.limits.io,
|
|
|
|
cpu: attributes.limits.cpu,
|
|
|
|
threads: attributes.limits.threads,
|
|
|
|
},
|
|
|
|
|
|
|
|
featureLimits: {
|
|
|
|
databases: attributes.feature_limits.databases,
|
|
|
|
allocations: attributes.feature_limits.allocations,
|
|
|
|
backups: attributes.feature_limits.backups,
|
|
|
|
},
|
|
|
|
|
|
|
|
ownerId: attributes.owner_id,
|
|
|
|
nodeId: attributes.node_id,
|
|
|
|
allocationId: attributes.allocation_id,
|
|
|
|
nestId: attributes.nest_id,
|
|
|
|
eggId: attributes.egg_id,
|
|
|
|
|
|
|
|
container: {
|
|
|
|
startupCommand: attributes.container.startup_command,
|
|
|
|
defaultStartup: '',
|
|
|
|
image: attributes.container.image,
|
|
|
|
environment: attributes.container.environment,
|
|
|
|
},
|
|
|
|
|
2021-01-05 16:17:44 +00:00
|
|
|
createdAt: new Date(attributes.created_at),
|
|
|
|
updatedAt: new Date(attributes.updated_at),
|
|
|
|
|
|
|
|
relations: {
|
2021-05-20 22:00:46 +00:00
|
|
|
egg: attributes.relationships?.egg !== undefined ? rawDataToEgg(attributes.relationships.egg as FractalResponseData) : undefined,
|
2021-01-05 16:17:44 +00:00
|
|
|
node: attributes.relationships?.node !== undefined ? rawDataToNode(attributes.relationships.node as FractalResponseData) : undefined,
|
|
|
|
user: attributes.relationships?.user !== undefined ? rawDataToUser(attributes.relationships.user as FractalResponseData) : undefined,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-05-19 02:53:42 +00:00
|
|
|
export interface Filters {
|
2021-07-14 22:43:59 +00:00
|
|
|
id?: string;
|
2021-05-19 02:53:42 +00:00
|
|
|
uuid?: string;
|
|
|
|
name?: string;
|
|
|
|
/* eslint-disable camelcase */
|
2021-08-02 15:54:13 +00:00
|
|
|
owner_id?: string;
|
|
|
|
node_id?: string;
|
2021-05-19 02:53:42 +00:00
|
|
|
external_id?: string;
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:34:10 +00:00
|
|
|
export const Context = createContext<Filters>();
|
2021-01-05 16:17:44 +00:00
|
|
|
|
2021-01-06 16:52:22 +00:00
|
|
|
export default (include: string[] = []) => {
|
2021-05-19 02:53:42 +00:00
|
|
|
const { page, filters, sort, sortDirection } = useContext(Context);
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
if (filters !== null) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
|
|
|
// @ts-ignore
|
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort !== null) {
|
|
|
|
// @ts-ignore
|
|
|
|
params.sort = (sortDirection ? '-' : '') + sort;
|
|
|
|
}
|
2021-01-05 16:17:44 +00:00
|
|
|
|
2021-05-19 02:53:42 +00:00
|
|
|
return useSWR<PaginatedResult<Server>>([ 'servers', page, filters, sort, sortDirection ], async () => {
|
|
|
|
const { data } = await http.get('/api/application/servers', { params: { include: include.join(','), page, ...params } });
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
return ({
|
2021-01-06 23:38:39 +00:00
|
|
|
items: (data.data || []).map(rawDataToServer),
|
2021-01-05 16:17:44 +00:00
|
|
|
pagination: getPaginationSet(data.meta.pagination),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|