Skip to content

Commit

Permalink
Add SessionMap data
Browse files Browse the repository at this point in the history
  • Loading branch information
shucon authored and AutomatedTester committed Aug 21, 2020
1 parent 3326b90 commit 070db73
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -89,20 +90,23 @@ public static class NodeSummary {
private final int maxSessionCount;
private final Map<Capabilities, Integer> stereotypes;
private final Map<Capabilities, Integer> used;
private final Set<Session> activeSessions;

public NodeSummary(
UUID nodeId,
URI uri,
boolean up,
int maxSessionCount,
Map<Capabilities, Integer> stereotypes,
Map<Capabilities, Integer> usedStereotypes) {
Map<Capabilities, Integer> usedStereotypes,
Set <Session> activeSessions) {
this.nodeId = Require.nonNull("Node id", nodeId);
this.uri = Require.nonNull("URI", uri);
this.up = up;
this.maxSessionCount = maxSessionCount;
this.stereotypes = ImmutableMap.copyOf(Require.nonNull("Stereoytpes", stereotypes));
this.used = ImmutableMap.copyOf(Require.nonNull("User stereotypes", usedStereotypes));
this.activeSessions = activeSessions;
}

public UUID getNodeId() {
Expand All @@ -129,6 +133,10 @@ public Map<Capabilities, Integer> getUsedStereotypes() {
return used;
}

public Set<Session> getActiveSessions() {
return activeSessions;
}

public boolean hasCapacity() {
HashMap<Capabilities, Integer> all = new HashMap<>(stereotypes);
used.forEach((caps, count) -> all.computeIfPresent(caps, (ignored, curr) -> curr - count));
Expand Down Expand Up @@ -167,6 +175,7 @@ private static NodeSummary fromJson(JsonInput input) {
int maxSessionCount = 0;
Map<Capabilities, Integer> stereotypes = new HashMap<>();
Map<Capabilities, Integer> used = new HashMap<>();
Set<Session> activeSessions = new HashSet<>();

input.beginObject();
while (input.hasNext()) {
Expand Down Expand Up @@ -203,7 +212,7 @@ private static NodeSummary fromJson(JsonInput input) {

input.endObject();

return new NodeSummary(nodeId, uri, up, maxSessionCount, stereotypes, used);
return new NodeSummary(nodeId, uri, up, maxSessionCount, stereotypes, used, activeSessions);
}

private static Map<Capabilities, Integer> readCapabilityCounts(JsonInput input) {
Expand Down
7 changes: 7 additions & 0 deletions java/server/src/org/openqa/selenium/grid/data/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

Expand All @@ -39,10 +40,12 @@ public class Session {
private final SessionId id;
private final URI uri;
private final Capabilities capabilities;
private final LocalDateTime startTime;

public Session(SessionId id, URI uri, Capabilities capabilities) {
this.id = Require.nonNull("Session ID", id);
this.uri = Require.nonNull("Where the session is running", uri);
this.startTime = LocalDateTime.now();

this.capabilities = ImmutableCapabilities.copyOf(
Require.nonNull("Session capabilities", capabilities));
Expand All @@ -60,6 +63,10 @@ public Capabilities getCapabilities() {
return capabilities;
}

public LocalDateTime getStartTime() {
return startTime;
}

private Map<String, Object> toJson() {
// Deliberately shaped like the return value for the W3C New Session command's return value.
return ImmutableMap.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ java_library(
"//java/server/test/org/openqa/selenium:__subpackages__",
],
deps = [
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/events",
"//java/server/src/org/openqa/selenium/grid/component",
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/node",
artifact("com.google.guava:guava"),
"//java/server/src/org/openqa/selenium/concurrent",
"//java/client/src/org/openqa/selenium:core",
"//java/server/src/org/openqa/selenium/events",
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/node",
"//java/server/src/org/openqa/selenium/grid/node/remote",
"//java/server/src/org/openqa/selenium/grid/component",
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
import org.openqa.selenium.grid.data.CreateSessionResponse;
import org.openqa.selenium.grid.data.DistributorStatus;
import org.openqa.selenium.grid.data.NodeStatus;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.node.Node;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.remote.SessionId;

import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -148,11 +150,13 @@ public UUID getId() {
public DistributorStatus.NodeSummary asSummary() {
Map<Capabilities, Integer> stereotypes = new HashMap<>();
Map<Capabilities, Integer> used = new HashMap<>();
Set<Session> activeSessions = new HashSet<>();

slots.forEach(slot -> {
stereotypes.compute(slot.getStereotype(), (key, curr) -> curr == null ? 1 : curr + 1);
if (slot.getStatus() != AVAILABLE) {
used.compute(slot.getStereotype(), (key, curr) -> curr == null ? 1 : curr + 1);
activeSessions.add(slot.getCurrentSession());
}
});

Expand All @@ -162,7 +166,8 @@ public DistributorStatus.NodeSummary asSummary() {
getHostStatus() == UP,
maxSessionCount,
stereotypes,
used);
used,
activeSessions);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public long getLastSessionCreated() {
return lastStartedNanos;
}

public Session getCurrentSession() {
return currentSession;
}

public boolean isSupporting(Capabilities caps) {
// Simple implementation --- only checks current values
return registeredCapabilities.getCapabilityNames().stream()
Expand Down
12 changes: 10 additions & 2 deletions java/server/src/org/openqa/selenium/grid/graphql/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand All @@ -35,7 +36,6 @@ public class Grid {
public Grid(Distributor distributor, URI uri) {
Require.nonNull("Distributor", distributor);
this.uri = Require.nonNull("Grid's public URI", uri);

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

Expand All @@ -49,10 +49,18 @@ public List<Node> getNodes() {
summary.getUri(),
summary.isUp(),
summary.getMaxSessionCount(),
summary.getStereotypes()))
summary.getStereotypes(),
summary.getActiveSessions()))
.collect(ImmutableList.toImmutableList());
}

public int getSessionCount() {
return distributorStatus.get().getNodes().stream()
.map(summary -> summary.getUsedStereotypes().values().stream().mapToInt(i -> i).sum())
.mapToInt(i -> i)
.sum();
}

public int getTotalSlots() {
return distributorStatus.get().getNodes().stream()
.map(summary -> {
Expand Down
24 changes: 22 additions & 2 deletions java/server/src/org/openqa/selenium/grid/graphql/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.openqa.selenium.grid.graphql;

import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;

Expand All @@ -26,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public class Node {
Expand All @@ -36,18 +40,29 @@ public class Node {
private final int maxSession;
private final Map<Capabilities, Integer> capabilities;
private static final Json JSON = new Json();
private final Set<Session> activeSessions;


public Node(UUID id,
URI uri,
boolean isUp,
int maxSession,
Map<Capabilities, Integer> capabilities) {
Map<Capabilities, Integer> capabilities,
Set<Session> activeSessions) {
this.id = Require.nonNull("Node id", id);
this.uri = Require.nonNull("Node uri", uri);
this.isUp = isUp;
this.maxSession = Require.nonNull("Node maxSession", maxSession);
this.capabilities = Require.nonNull("Node capabilities", capabilities);
this.capabilities = Require.nonNull("Node capabilities", JSON.toJson(capabilities));
this.activeSessions = Require.nonNull("Active sessions", activeSessions);
}

public List<org.openqa.selenium.grid.graphql.Session> getSessions() {
return activeSessions.stream()
.map(session -> new org.openqa.selenium.grid.graphql.Session(session.getId().toString(),
session.getCapabilities(),
session.getStartTime()))
.collect(ImmutableList.toImmutableList());
}

public UUID getId() {
Expand All @@ -62,6 +77,11 @@ public int getMaxSession() {
return maxSession;
}

public List<String> getActiveSessionIds() {
return activeSessions.stream().map(session -> session.getId().toString())
.collect(ImmutableList.toImmutableList());
}

public String getCapabilities() {
List<Map> toReturn = new ArrayList<>();

Expand Down
52 changes: 52 additions & 0 deletions java/server/src/org/openqa/selenium/grid/graphql/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 org.openqa.selenium.Capabilities;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Session {

private final String id;
private final Capabilities capabilities;
private final LocalDateTime startTime;
private static final Json JSON = new Json();

public Session(String id, Capabilities capabilities, LocalDateTime startTime) {
this.id = Require.nonNull("Node id", id);
this.capabilities = Require.nonNull("Node capabilities", capabilities);
this.startTime = Require.nonNull("Session Start time", startTime);
}

public String getId() {
return id;
}

public String getCapabilities() {
return JSON.toJson(capabilities);
}

public String getStartTime() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
return dtf.format(startTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ enum Status {
UNAVAILABLE
}

type Session {
id: String!
capabilities: String!
startTime: String!
}

type Node {
id: ID!
uri: Uri!
status: Status!
maxSession: Int!
sessions: [Session]!
capabilities: String!
}

Expand All @@ -27,4 +34,5 @@ type Grid {
nodes: [Node!]!
totalSlots: Int
usedSlots: Int
sessionCount: Int!
}

0 comments on commit 070db73

Please sign in to comment.