-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[graphql] Add basic Node data to the GraphQL result
- Loading branch information
Showing
12 changed files
with
370 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
java/server/src/org/openqa/selenium/graphql/selenium-grid-schema.graphqls
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
java/server/src/org/openqa/selenium/grid/graphql/Grid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
java/server/src/org/openqa/selenium/grid/graphql/Types.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.