📦 EqualifyEverything / equalify-scan-sim

📄 Sim.tsx · 239 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239import {
  Box,
  Button,
  Card,
  Container,
 
  Flex,
  Grid,
  Heading,
  Separator,
  Slider,
  Text,
} from "@radix-ui/themes";
import { useState } from "react";
import scanSim, { ResultsRecord } from "../inc/sim";
import { formatDuration, intervalToDuration } from "date-fns";
import { SparklinesLine, Tooltip } from "@lueton/react-sparklines";

export default function Sim() {
  // inputs
  const [scanTime, setScanTime] = useState([5]); // time per scan (s)
  const [workers, setWorkers] = useState([1]);
  const [customersCount, setCustomersCount] = useState([3]);
  const [averageCustomerRequestSize, setAverageCustomerRequestSize] = useState([
    5000,
  ]);
  const [simRuns, setSimRuns] = useState([1]);

  // data
  const [_1Rec, Set_1Rec] = useState(0);
  const [_500Rec, Set_500Rec] = useState(0);
  const [_5000Rec, Set_5000Rec] = useState(0);
  const [jobsCharts, setJobsCharts] = useState([] as number[][]);

  function calculateTimeAvg(data: Array<ResultsRecord[]>) {
    const average = (array: any[]) =>
      array.reduce((a, b) => a + b) / array.length;

    let _1RecArray = data.map((rec) => {
      return rec[0].val && rec[0].val > 0 ? rec[0].val : false;
    });
    Set_1Rec(average(_1RecArray));

    let _500RecArray = data.map((rec) => {
      return rec[1].val && rec[1].val > 0 ? rec[1].val : false;
    });
    Set_500Rec(average(_500RecArray));

    let _5000RecArray = data.map((rec) => {
      return rec[2].val && rec[2].val > 0 ? rec[2].val : false;
    });
    Set_5000Rec(average(_5000RecArray));
  }

  function populateCharts(jobsChart: number[][]) {
    setJobsCharts(jobsChart.slice(0, 16));
  }

  async function run() {
    const results = await scanSim(
      workers[0],
      customersCount[0],
      averageCustomerRequestSize[0],
      simRuns[0]
    );
    calculateTimeAvg(results.data);
    populateCharts(results.jobsChart);
    console.log(results);
  }

  return (
    <Flex>
      <Container size="1" p="3">
        <img
          alt="Equalify"
          width="201"
          height="62"
          src="https://equalify.app/wp-content/uploads/2024/04/Equalify-Logo.png"
        ></img>

        <Box pb="6">
          <Heading size={"2"} align={"left"}>
            Settings
          </Heading>
          <Separator
            style={{ width: "100%", marginTop: "1em", marginBottom: ".5em" }}
          />
          <Flex minWidth="300px" align={"center"} gap="4" pt="2">
            <Box
              width="120px"
              style={{
                fontSize: 12,
                lineHeight: 1.2,
                textAlign: "left",
                color: "#c8c8c8",
              }}
            >
              Avg Scan (s)
            </Box>
            <Slider
              min={0}
              max={10}
              value={scanTime}
              onValueChange={setScanTime}
              disabled
            ></Slider>
            {scanTime}
          </Flex>
          <Flex minWidth="300px" align={"center"} gap="4" pt="2">
            <Box
              width="120px"
              style={{ fontSize: 12, lineHeight: 1.2, textAlign: "left" }}
            >
              Workers
            </Box>
            <Slider
              min={1}
              max={50}
              value={workers}
              onValueChange={setWorkers}
            ></Slider>
            {workers}
          </Flex>
          <Flex minWidth="300px" align={"center"} gap="4" pt="2">
            <Box
              width="120px"
              style={{ fontSize: 12, lineHeight: 1.2, textAlign: "left" }}
            >
              Customers
            </Box>
            <Slider
              min={3}
              max={50}
              value={customersCount}
              onValueChange={setCustomersCount}
            ></Slider>
            {customersCount}
          </Flex>
          <Flex minWidth="300px" align={"center"} gap="4" pt="2">
            <Box
              width="120px"
              style={{ fontSize: 12, lineHeight: 1.2, textAlign: "left" }}
            >
              Avg Customer Request Size
            </Box>
            <Slider
              min={1}
              max={20000}
              value={averageCustomerRequestSize}
              onValueChange={setAverageCustomerRequestSize}
            ></Slider>
            {averageCustomerRequestSize}
          </Flex>
          <Separator
            style={{ width: "100%", marginTop: "1em", marginBottom: "1em" }}
          />
          <Flex minWidth="300px" align={"center"} gap="4" pt="2">
            <Box
              width="120px"
              style={{ fontSize: 12, lineHeight: 1.2, textAlign: "left" }}
            >
              # of Simulation Runs
            </Box>
            <Slider
              min={1}
              max={500}
              value={simRuns}
              onValueChange={setSimRuns}
            ></Slider>
            {simRuns}
          </Flex>
        </Box>
        <Box>
          <Button onClick={run}>Run</Button>
        </Box>
      </Container>
      <Container p="5" width="60vw">
        <Grid columns="3" gap="3" width="100%">
          <Card>
            <Text as="div" size="2" weight="bold">
              1 Scan
            </Text>
            <Text as="div" size="2" color="gray">
              {formatDuration(
                intervalToDuration({ start: 0, end: Math.round(_1Rec) * 1000 })
              )}
            </Text>
          </Card>
          <Card>
            <Text as="div" size="2" weight="bold">
              500 Scans
            </Text>
            <Text as="div" size="2" color="gray">
              {formatDuration(
                intervalToDuration({
                  start: 0,
                  end: Math.round(_500Rec) * 1000,
                })
              )}
            </Text>
          </Card>
          <Card>
            <Text as="div" size="2" weight="bold">
              5000 Scans
            </Text>
            <Text as="div" size="2" color="gray">
              {formatDuration(
                intervalToDuration({
                  start: 0,
                  end: Math.round(_5000Rec) * 1000,
                })
              )}
            </Text>
          </Card>
        </Grid>
        <Grid columns="4" gap="5" pt="5">
          {jobsCharts
            ? jobsCharts.map((chart, i) => (
                <Box style={{ borderLeft: "2px solid #efefef", borderBottom: "2px solid #efefef" }}>
                <SparklinesLine
                  data={chart}
                  key={i}
                  stroke="#155f1b"
                  fill="none"
                  curved
                  strokeWidth={2}
                  label="Scans"
                  
                >
                  <Tooltip />
                </SparklinesLine></Box>
              ))
            : ""}
        </Grid>
      </Container>
    </Flex>
  );
}