Skip to content

Commit

Permalink
[graphql] Add basic Node data to the GraphQL result
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jun 1, 2020
1 parent 7f4b8c8 commit 7cbb707
Show file tree
Hide file tree
Showing 12 changed files with 370 additions and 97 deletions.
18 changes: 0 additions & 18 deletions java/server/src/org/openqa/selenium/graphql/BUILD.bazel

This file was deleted.

This file was deleted.

6 changes: 5 additions & 1 deletion java/server/src/org/openqa/selenium/grid/graphql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ java_library(
"//java/server/src/org/openqa/selenium/grid:__subpackages__",
"//java/server/test/org/openqa/selenium/grid/graphql:__pkg__",
],
resources = [
"selenium-grid-schema.graphqls",
],
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote/http",
"//java/server/src/org/openqa/selenium/graphql",
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/distributor",
artifact("com.google.guava:guava"),
artifact("com.graphql-java:graphql-java"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

public class GraphqlHandler implements HttpHandler {

public static final String GRID_SCHEMA = "/org/openqa/selenium/graphql/selenium-grid-schema.graphqls";
public static final String GRID_SCHEMA = "/org/openqa/selenium/grid/graphql/selenium-grid-schema.graphqls";
public static final Json JSON = new Json();
private final Distributor distributor;
private final String publicUrl;
Expand Down Expand Up @@ -100,8 +100,10 @@ public HttpResponse execute(HttpRequest req) throws UncheckedIOException {

private RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring()
.scalar(Types.Uri)
.scalar(Types.Url)
.type("GridQuery", typeWiring -> typeWiring
.dataFetcher("grid", new GridData(publicUrl)))
.dataFetcher("grid", new GridData(distributor, publicUrl)))
.build();
}

Expand Down
67 changes: 67 additions & 0 deletions java/server/src/org/openqa/selenium/grid/graphql/Grid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.graphql;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.internal.Require;

import java.util.List;
import java.util.function.Supplier;

public class Grid {

private final String url;
private final Supplier<DistributorStatus> distributorStatus;

public Grid(Distributor distributor, String url) {
Require.nonNull("Distributor", distributor);
this.url = Require.nonNull("Grid's public URL", url);

this.distributorStatus = Suppliers.memoize(distributor::getStatus);
}

public String getUrl() {
return url;
}

public List<Node> getNodes() {
return distributorStatus.get().getNodes().stream()
.map(summary -> new Node(summary.getNodeId(), summary.getUri(), summary.isUp()))
.collect(ImmutableList.toImmutableList());
}

public int getTotalSlots() {
return distributorStatus.get().getNodes().stream()
.map(summary -> {
int slotCount = summary.getStereotypes().values().stream().mapToInt(i -> i).sum();
return Math.min(summary.getMaxSessionCount(), slotCount);
})
.mapToInt(i -> i)
.sum();
}

public int getUsedSlots() {
return distributorStatus.get().getNodes().stream()
.map(summary -> summary.getUsedStereotypes().values().stream().mapToInt(i -> i).sum())
.mapToInt(i -> i)
.sum();
}
}
13 changes: 7 additions & 6 deletions java/server/src/org/openqa/selenium/grid/graphql/GridData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.openqa.selenium.graphql.Grid;

import java.util.Objects;
import org.openqa.selenium.grid.distributor.Distributor;
import org.openqa.selenium.internal.Require;

public class GridData implements DataFetcher {
private final Distributor distributor;
private final String publicUrl;

public GridData(String publicUrl) {
this.publicUrl = Objects.requireNonNull(publicUrl);
public GridData(Distributor distributor, String publicUrl) {
this.distributor = Require.nonNull("Distributor", distributor);
this.publicUrl = Require.nonNull("Grid's public URL", publicUrl);
}

@Override
public Object get(DataFetchingEnvironment environment) {
return new Grid(publicUrl);
return new Grid(distributor, publicUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,34 @@
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.graphql;
package org.openqa.selenium.grid.graphql;

import org.openqa.selenium.json.JsonInput;
import org.openqa.selenium.internal.Require;

import java.util.Objects;
import java.net.URI;
import java.util.UUID;

public class Grid {
public class Node {

private final String url;
private final UUID id;
private final URI uri;
private final boolean isUp;

public Grid(String url) {
this.url = Objects.requireNonNull(url);
public Node(UUID id, URI uri, boolean isUp) {
this.id = Require.nonNull("Node id", id);
this.uri = Require.nonNull("Node uri", uri);
this.isUp = isUp;
}

public String getUrl() {
return url;
public UUID getId() {
return id;
}

private static Grid fromJson(JsonInput input) {
String url = null;

input.beginObject();
while (input.hasNext()) {
switch (input.nextName()) {
case "url":
url = input.nextString();
break;

default:
input.skipValue();
break;
}
}
input.endObject();
public URI getUri() {
return uri;
}

return new Grid(url);
public String getStatus() {
return isUp ? "UP" : "UNAVAILABLE";
}
}
152 changes: 152 additions & 0 deletions java/server/src/org/openqa/selenium/grid/graphql/Types.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.grid.graphql;

import graphql.language.StringValue;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

class Types {

static final GraphQLScalarType Url = urlType();
static final GraphQLScalarType Uri = uriType();

private static GraphQLScalarType uriType() {
return GraphQLScalarType.newScalar()
.name("Uri")
.coercing(new Coercing<URI, String>() {
@Override
public String serialize(Object o) throws CoercingSerializeException {
if (o instanceof String) {
return (String) o;
}

if (o instanceof URL || o instanceof URI) {
return String.valueOf(o);
}

throw new CoercingSerializeException("Unable to coerce " + o);
}

@Override
public URI parseValue(Object input) throws CoercingParseValueException {
if (input == null) {
return null;
}

if (input instanceof URI) {
return (URI) input;
}

try {
if (input instanceof String) {
return new URI((String) input);
}

if (input instanceof URL) {
return ((URL) input).toURI();
}
} catch (URISyntaxException e) {
throw new CoercingParseValueException("Unable to create URI: " + input, e);
}

throw new CoercingParseValueException("Unable to create URI: " + input);
}

@Override
public URI parseLiteral(Object input) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException("Cannot convert to URL: " + input);
}

try {
return new URI(((StringValue) input).getValue());
} catch (URISyntaxException e) {
throw new CoercingParseLiteralException("Unable to parse: " + input);
}
}
})
.build();
}

private static GraphQLScalarType urlType() {
return GraphQLScalarType.newScalar()
.name("Url")
.coercing(new Coercing<URL, String>() {
@Override
public String serialize(Object o) throws CoercingSerializeException {
if (o instanceof String) {
return (String) o;
}

if (o instanceof URL || o instanceof URI) {
return String.valueOf(o);
}

throw new CoercingSerializeException("Unable to coerce " + o);
}

@Override
public URL parseValue(Object input) throws CoercingParseValueException {
if (input == null) {
return null;
}

if (input instanceof URL) {
return (URL) input;
}

try {
if (input instanceof String) {
return new URL((String) input);
}

if (input instanceof URI) {
return ((URI) input).toURL();
}
} catch (MalformedURLException e) {
throw new CoercingParseValueException("Unable to create URL: " + input, e);
}

throw new CoercingParseValueException("Unable to create URL: " + input);
}

@Override
public URL parseLiteral(Object input) throws CoercingParseLiteralException {
if (!(input instanceof StringValue)) {
throw new CoercingParseLiteralException("Cannot convert to URL: " + input);
}

try {
return new URL(((StringValue) input).getValue());
} catch (MalformedURLException e) {
throw new CoercingParseLiteralException("Unable to parse: " + input);
}
}
})
.build();
}
}
Loading

0 comments on commit 7cbb707

Please sign in to comment.