Skip to content

Commit b6cff46

Browse files
committed
Merge branch 'main' into demo
2 parents c39ca50 + f8b598f commit b6cff46

File tree

10 files changed

+150
-69
lines changed

10 files changed

+150
-69
lines changed

server/app/utils/requestHelper.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ func RunExecutor(ctx context.Context, request models.Request) error {
2121
var client httpRequest.HttpClient
2222
executor := executor.NewExecutor(request.ID, "")
2323
ctx, cancelCtx := context.WithCancel(ctx)
24-
go func() {
25-
<-time.After(time.Duration(request.Time) * time.Second)
26-
cancelCtx()
27-
}()
2824

2925
err := utils.RunWorker(request)
3026
if err != nil {
3127
log.Errorf("Error while sending request to Worker", err.Error())
3228
return err
3329
}
3430

31+
go func() {
32+
<-time.After(time.Duration(request.Time) * time.Second)
33+
cancelCtx()
34+
}()
35+
3536
go executor.Run(ctx, request)
3637
client, err = httpRequest.Initializer(request)
3738
if err != nil {

server/pkg/configs/fiber.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ func FiberConfig() fiber.Config {
1717
// Return Fiber configuration.
1818
return fiber.Config{
1919
ReadTimeout: time.Second * time.Duration(readTimeoutSecondsCount),
20-
// Prefork: true,
20+
Prefork: true,
2121
}
2222
}

server/pkg/utils/request.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"context"
55
"encoding/json"
6+
"net/http"
67
"strings"
78
"time"
89

@@ -30,28 +31,30 @@ func RunWorker(request models.Request) error {
3031
}
3132

3233
for _, element := range listServer {
33-
url := element.IP
34-
worker := GetWorkerLoad(request)
35-
request.ServerId = element.ID
36-
body, _ := json.Marshal(request)
37-
38-
if url != "" {
39-
worker.ServerId = element.ID
40-
url = strings.TrimSpace(strings.TrimSpace(url) + "/worker/request")
41-
headers := map[string]string{
42-
"Content-Type": "application/json",
43-
}
44-
res, err := Do("POST", url, body, headers)
45-
46-
if err != nil {
47-
worker.Status = true
48-
// worker.ServerId = element.ID
49-
worker.Error = err.Error()
50-
db.Provider.AddWorker(ctx, worker)
51-
} else if res.StatusCode == statusOK {
52-
worker.Status = false
53-
// worker.ServerId = element.ID
54-
db.Provider.AddWorker(ctx, worker)
34+
if element.Enabled {
35+
url := element.IP
36+
worker := GetWorkerLoad(request)
37+
request.ServerId = element.ID
38+
body, _ := json.Marshal(request)
39+
40+
if url != "" {
41+
worker.ServerId = element.ID
42+
url = strings.TrimSpace(strings.TrimSpace(url) + "/worker/request")
43+
headers := map[string]string{
44+
"Content-Type": "application/json",
45+
}
46+
res, err := Do(http.MethodPost, url, body, headers)
47+
48+
if err != nil {
49+
worker.Status = true
50+
// worker.ServerId = element.ID
51+
worker.Error = err.Error()
52+
db.Provider.AddWorker(ctx, worker)
53+
} else if res.StatusCode == statusOK {
54+
worker.Status = false
55+
// worker.ServerId = element.ID
56+
db.Provider.AddWorker(ctx, worker)
57+
}
5558
}
5659
}
5760
}

ui/src/assets/pause.svg

Lines changed: 6 additions & 0 deletions
Loading

ui/src/assets/play.svg

Lines changed: 2 additions & 0 deletions
Loading

ui/src/pages/Dashboard/AddEditRequest/OtherOptions/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ export function OtherOption() {
1919
const [open, setOpen] = useState(false);
2020
return (
2121
<Box>
22-
<Button colorScheme="tomato" size="md" onClick={() => setOpen(true)}>
22+
<Button
23+
colorScheme="primary"
24+
variant="outline"
25+
size="md"
26+
onClick={() => setOpen(true)}
27+
>
2328
Options
2429
</Button>
2530
<CustomModal onClose={() => setOpen(false)} isOpen={open}>

ui/src/pages/Dashboard/AddEditRequest/selectedRequest.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from "react";
1+
import { ReactNode, useEffect, useState } from "react";
22
import {
33
Box,
44
Divider,
@@ -33,7 +33,13 @@ import { InputWrap } from "./InputArea/inputWrap";
3333
import Method from "./method";
3434
import { OtherOption } from "./OtherOptions";
3535

36-
function CustomizeToolTipInfo({ text }: { text: string }) {
36+
export function CustomizeToolTipInfo({
37+
text,
38+
children,
39+
}: {
40+
text: string;
41+
children?: ReactNode;
42+
}) {
3743
return (
3844
<Tooltip
3945
hasArrow
@@ -43,7 +49,7 @@ function CustomizeToolTipInfo({ text }: { text: string }) {
4349
padding="10px"
4450
fontWeight="500"
4551
>
46-
<InfoOutlineIcon boxSize={3} cursor="pointer" margin={2} />
52+
{children || <InfoOutlineIcon boxSize={3} cursor="pointer" margin={2} />}
4753
</Tooltip>
4854
);
4955
}

ui/src/pages/Dashboard/LoadRequest/RequestRespons/childComponent.tsx

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ function PreviewHtml(props: { html: string }) {
1515
<iframe title="Response" srcDoc={DOMPurify.sanitize(html)} height={300} />
1616
);
1717
}
18+
const parsed = (data: any) => {
19+
try {
20+
const parsedBody = JSON.parse(data);
21+
return parsedBody;
22+
} catch {
23+
return null;
24+
}
25+
};
1826

1927
function RequestBody({
2028
body = "",
@@ -32,37 +40,39 @@ function RequestBody({
3240
/>
3341
);
3442
if (contentType.includes("application/json")) {
35-
const parsedBody = JSON.parse(body);
36-
return (
37-
<JSONInput
38-
id="a_unique_id"
39-
placeholder={parsedBody || {}}
40-
theme="light_mitsuketa_tribute"
41-
onKeyPressUpdate
42-
waitAfterKeyPress={1000}
43-
height="200px"
44-
width="100%"
45-
colors={{
46-
default: "black",
47-
background: "#edf2f7",
48-
string: "blue",
49-
keys: "#800000",
50-
error: "red",
51-
}}
52-
style={{
53-
body: {
54-
display: "flex",
55-
overflowX: "auto",
56-
},
57-
outerBox: {
58-
border: "1px solid #e2e8f0",
59-
},
60-
contentBox: {
61-
flex: "0 0 auto",
62-
},
63-
}}
64-
/>
65-
);
43+
const parsedBody = parsed(body);
44+
if (parsedBody) {
45+
return (
46+
<JSONInput
47+
id="a_unique_id"
48+
placeholder={parsedBody || {}}
49+
theme="light_mitsuketa_tribute"
50+
onKeyPressUpdate
51+
waitAfterKeyPress={1000}
52+
height="200px"
53+
width="100%"
54+
colors={{
55+
default: "black",
56+
background: "#edf2f7",
57+
string: "blue",
58+
keys: "#800000",
59+
error: "red",
60+
}}
61+
style={{
62+
body: {
63+
display: "flex",
64+
overflowX: "auto",
65+
},
66+
outerBox: {
67+
border: "1px solid #e2e8f0",
68+
},
69+
contentBox: {
70+
flex: "0 0 auto",
71+
},
72+
}}
73+
/>
74+
);
75+
}
6676
}
6777
return (
6878
<Card p={2}>

ui/src/pages/Server/index.tsx

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,33 @@ import {
1313
Text,
1414
Badge,
1515
Spinner as SP,
16+
Image,
1617
} from "@chakra-ui/react";
1718
import { format } from "date-fns";
1819
import { useDispatch, useSelector } from "react-redux";
1920
import { useEffect, useState } from "react";
2021
import { CheckIcon, CopyIcon, DeleteIcon, EditIcon } from "@chakra-ui/icons";
21-
import { motion } from "framer-motion";
2222
import {
2323
addOrEditServer,
24+
editServerAction,
2425
getAllServerAction,
2526
selectDeleteRequest,
2627
} from "../../store/stress/server/actions";
27-
import { Server } from "../../store/stress/server/types";
28+
import {
29+
AddServerRequestPayload,
30+
Server,
31+
} from "../../store/stress/server/types";
32+
import Play from "../../assets/play.svg";
33+
import Pause from "../../assets/pause.svg";
2834

29-
import { getServerList } from "../../store/stress/server/selectors";
35+
import {
36+
getAddServerState,
37+
getServerList,
38+
} from "../../store/stress/server/selectors";
3039
import Spinner from "../../components/Spinner";
3140
import { DeleteDialog } from "./DeleteRequest";
3241
import AddOrEditComp from "./AddEdit";
42+
import { CustomizeToolTipInfo } from "../Dashboard/AddEditRequest/selectedRequest";
3343

3444
const pagination = {
3545
limit: 10,
@@ -63,8 +73,11 @@ function TableBody({
6373
active,
6474
port,
6575
token,
76+
enabled,
6677
} = server;
6778
const dispatch = useDispatch();
79+
const [selectedServer, setSelected] = useState<Server | null>(null);
80+
const { loading } = useSelector(getAddServerState);
6881
const onEdit = (action: "ADD" | "EDIT", serverDetails?: Server) => {
6982
dispatch(
7083
addOrEditServer({
@@ -74,8 +87,23 @@ function TableBody({
7487
);
7588
};
7689

90+
useEffect(() => {
91+
if (!loading && selectedServer) {
92+
setSelected(null);
93+
}
94+
}, [loading]);
95+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
96+
const changeServerState = (serverState: Server) => {
97+
const payload = {
98+
...serverState,
99+
enabled: !server.enabled,
100+
};
101+
setSelected(serverState);
102+
dispatch(editServerAction(payload as AddServerRequestPayload));
103+
};
104+
77105
return (
78-
<motion.tr key={id} layout transition={{ duration: 0.5 }}>
106+
<Tr key={id}>
79107
<Td>{alias}</Td>
80108
<Td>{description}</Td>
81109

@@ -99,14 +127,30 @@ function TableBody({
99127
</Td>
100128
<Td>
101129
<Stack direction="row" display="flex" alignContent="center">
130+
{loading && selectedServer?.id === id ? (
131+
<SP />
132+
) : (
133+
<CustomizeToolTipInfo
134+
text={enabled ? "Status: Running" : "Status: Stopped"}
135+
>
136+
<Image
137+
width="18px"
138+
height="18px"
139+
src={enabled ? Pause : Play}
140+
alt="loadster"
141+
cursor="pointer"
142+
onClick={() => changeServerState(server)}
143+
/>
144+
</CustomizeToolTipInfo>
145+
)}
146+
<EditIcon cursor="pointer" onClick={() => onEdit("EDIT", server)} />
102147
<DeleteIcon
103148
cursor="pointer"
104149
onClick={() => dispatch(selectDeleteRequest(server))}
105150
/>
106-
<EditIcon cursor="pointer" onClick={() => onEdit("EDIT", server)} />
107151
</Stack>
108152
</Td>
109-
</motion.tr>
153+
</Tr>
110154
);
111155
}
112156

@@ -125,7 +169,9 @@ export default function ServerBoard() {
125169
};
126170

127171
useEffect(() => {
128-
dispatch(getAllServerAction(pagination));
172+
if (!data && !loading) {
173+
dispatch(getAllServerAction(pagination));
174+
}
129175
}, []);
130176

131177
const onCopy = (text) => {

ui/src/store/stress/server/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export interface Server {
4040
description: string;
4141
ip?: string;
4242
port?: number;
43+
enabled: boolean;
4344
active?: boolean;
4445
created_at: number;
4546
updated_at: number;
@@ -60,6 +61,7 @@ export interface AddServerRequestPayload {
6061
description: string;
6162
ip?: string;
6263
port?: number;
64+
enabled?: boolean;
6365
}
6466

6567
export interface AddServerRequestResponse {

0 commit comments

Comments
 (0)