Skip to content

Commit 12ac752

Browse files
Merge pull request #67 from oslabs-beta/ms/ValidateUserInput
Ms/validate user input
2 parents cef3610 + 8cc2455 commit 12ac752

File tree

6 files changed

+290
-87
lines changed

6 files changed

+290
-87
lines changed

ksqLight/server/server.js

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ const RealTimeType = new GraphQLObjectType({
3535
resolve: (parent, args, context) => parent[1]
3636
}
3737
})
38+
});
39+
40+
const ValidationType = new GraphQLObjectType({
41+
name: 'inputValidation',
42+
description: 'Object indicating input validity status and error message',
43+
fields: () => ({
44+
isValid: { type: GraphQLBoolean },
45+
error: { type: GraphQLString }
46+
})
3847
})
3948

4049
//---------------Root Query Types----------------
@@ -53,7 +62,7 @@ const RootQueryType = new GraphQLObjectType({
5362
prometheusURL: { type: GraphQLNonNull(GraphQLString)}
5463
},
5564
resolve: (parent, {start, end, resolution, metric, prometheusURL}) => {
56-
if (prometheusURL[prometheusURL.length] === '/') prometheusURL = prometheusURL.slice(0, prometheusURL.length);
65+
if (prometheusURL[prometheusURL.length - 1] === '/') prometheusURL = prometheusURL.slice(0, prometheusURL.length - 1);
5766

5867
return axios.get(`${prometheusURL}/api/v1/query_range?step=${resolution}s&end=${end}&start=${start}&query=${queryTypes[metric]}`)
5968
.then(res => {
@@ -125,6 +134,65 @@ const RootQueryType = new GraphQLObjectType({
125134
}
126135
}
127136
},
137+
isValidPrometheusURL: {
138+
type: ValidationType,
139+
description: 'Object representing whether provided Prometheus URL points to valid Prometheus server and any errors',
140+
args: {
141+
prometheusURL: { type: GraphQLNonNull(GraphQLString)}
142+
},
143+
resolve: async (parent, { prometheusURL }) => {
144+
if (prometheusURL[prometheusURL.length - 1] === '/') prometheusURL = prometheusURL.slice(0, prometheusURL.length - 1);
145+
146+
return axios.get(`${prometheusURL}/api/v1/status/buildinfo`)
147+
.then(res => ({
148+
isValid: true,
149+
error: null
150+
}))
151+
.catch(error => ({
152+
isValid: false,
153+
error: error.message
154+
}));
155+
}
156+
},
157+
isValidKsqlDBURL: {
158+
type: ValidationType,
159+
description: 'Object representing whether provided ksqlDB URL points to valid Prometheus server and any errors',
160+
args: {
161+
ksqlDBURL: { type: GraphQLNonNull(GraphQLString)}
162+
},
163+
resolve: (parent, { ksqlDBURL }) => {
164+
if (ksqlDBURL[ksqlDBURL.length - 1] === '/') ksqlDBURL = ksqlDBURL.slice(0, ksqlDBURL.length - 1);
165+
166+
return axios.get(`${ksqlDBURL}/clusterStatus`)
167+
.then(res => ({
168+
isValid: true,
169+
error: null
170+
}))
171+
.catch(error => ({
172+
isValid: false,
173+
error: error.message
174+
}));
175+
}
176+
},
177+
isValidDuration: {
178+
type: GraphQLBoolean,
179+
description: 'Boolean representing whether Prometheus server accepts user duration.',
180+
args: {
181+
metric: { type: GraphQLNonNull(GraphQLString)},
182+
start: { type: GraphQLNonNull(GraphQLInt)},
183+
end: { type: GraphQLNonNull(GraphQLInt)},
184+
resolution: { type: GraphQLNonNull(GraphQLInt)},
185+
prometheusURL: { type: GraphQLNonNull(GraphQLString)}
186+
},
187+
resolve: (parent, { start, end, resolution, metric, prometheusURL }) => {
188+
if (prometheusURL[prometheusURL.length - 1] === '/') prometheusURL = prometheusURL.slice(0, prometheusURL.length - 1);
189+
190+
191+
return axios.get(`${prometheusURL}/api/v1/query_range?step=${resolution}s&end=${end}&start=${start}&query=${queryTypes[metric]}`)
192+
.then(res => true)
193+
.catch(error => false);
194+
}
195+
}
128196
})
129197
});
130198

ksqLight/src/components/Chart.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

ksqLight/src/components/Homepage.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import React from "react";
2-
import { useState } from "react";
3-
import { Typography, Grid, Toolbar, Container, CssBaseline, Box } from "@mui/material";
4-
import { Chart } from "./Chart.js";
2+
import { Grid, CssBaseline, Box, CardContent } from "@mui/material";
53
import LineChart from "./LineChart.js";
6-
import { LivenessCard } from "./MetricCard.js";
74

85
export const Homepage = ({ showQueries, showMessages, showErrors, metricsState }) => {
9-
const [content, setContent] = useState('Chart placeholder');
10-
116
const queriesCharts = [
127
["runningQueries", "Number of Running Queries"],
138
["createdQueries", "Number of Created Queries"],
@@ -33,32 +28,10 @@ export const Homepage = ({ showQueries, showMessages, showErrors, metricsState }
3328
["errorQueries", "Number of Error Queries"],
3429
["pendingErrorQueries", "Number of Pending Error Queries"],
3530
]
36-
const queryTypes = [
37-
["runningQueries", "Number of Running Queries"],
38-
["rebalancingQueries", "Number of Rebalancing Queries"],
39-
["pendingShutdownQueries", "Number of Pending Shutdown Queries"],
40-
["pendingErrorQueries", "Number of Pending Error Queries"],
41-
["numPersistentQueries", "Number of Persistent Queries"],
42-
["numIdleQueries", "Number of Idle Queries"],
43-
["numActiveQueries", "Number of Active Queries"],
44-
["notRunningQueries", "Number of Not Running Queries"],
45-
["messagesProducedPerSec", "Number of Messages Produced Per Second"],
46-
["messagesConsumedTotal", "Number of Messages Consumed"],
47-
["messagesConsumedPerSec", "Number of Messages Consumed Per Second"],
48-
["messagesConsumedMin", "Number of Messages Consumed Min"],
49-
["messagesConsumedMax", "Number of Messages Consumed Max"],
50-
["messagesConsumedAvg", "Number of Messages Consumed Average"],
51-
// ["livenessIndicator", "Liveness Indicator"],
52-
["errorRate", "Error Rate"],
53-
["errorQueries", "Number of Error Queries"],
54-
["createdQueries", "Number of Created Queries"],
55-
["bytesConsumedTotal", "Number of Bytes Consumed Total"],
56-
];
5731

5832
return (
5933
<Box>
6034
<CssBaseline />
61-
<Typography color="primary">Hi Welcome back</Typography>
6235
<Grid container spacing={4} sx={{}}>
6336
{showQueries &&
6437
queriesCharts.map(([query, description], index) =>
@@ -69,12 +42,11 @@ export const Homepage = ({ showQueries, showMessages, showErrors, metricsState }
6942
<LineChart description={description} metric={query} metricsState={metricsState} key={index} />
7043
)
7144
}
72-
{/* {showErrors &&
45+
{showErrors &&
7346
errorCharts.map(([query, description], index) =>
74-
<LineChart description={description} metric={query} key={index} />
75-
)
76-
} */}
77-
{/* {queryTypes.map(([query, description], index) => <LineChart description={description} metric={query} key={index} />)} */}
47+
<LineChart description={description} metric={query} metricsState={metricsState} key={index} />
48+
).concat([<Grid item xl={3}><CardContent sx={{ width: "300px" }}></CardContent></Grid>])
49+
}
7850
</Grid>
7951
</Box>
8052
)

ksqLight/src/components/LineChart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default function LineChart({ metric, description, metricsState }) {
3737

3838
initialData = client.query({
3939
query: gql`
40-
query testQuery {
40+
query fetchMetric {
4141
ksqlDBMetrics(prometheusURL: "${metricsState.prometheusURL}" metric: "${metric}", resolution: ${metricsState.refreshRate}, start: ${unixStart}, end: ${unixEnd}) {
4242
x,
4343
y

0 commit comments

Comments
 (0)