Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x1244 add numStored to graphql location schema #462

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.ac.sanger.sccp.stan.model.store;

import com.fasterxml.jackson.annotation.JsonIgnore;
import uk.ac.sanger.sccp.stan.model.Address;

import java.util.Objects;
Expand All @@ -15,18 +16,21 @@ public class BasicLocation {
private String name;
private Address address;
private Integer addressIndex;
private int numStored;
private int numChildren;

public BasicLocation() {}

public BasicLocation(String barcode, Address address) {
this(barcode, null, address, null);
this(barcode, null, address, null, 0, 0);
}

public BasicLocation(String barcode, String name, Address address, Integer addressIndex) {
public BasicLocation(String barcode, String name, Address address, Integer addressIndex, int numStored, int numChildren) {
this.barcode = barcode;
this.name = name;
this.address = address;
this.addressIndex = addressIndex;
this.numStored = numStored;
}

public String getBarcode() {
Expand Down Expand Up @@ -61,6 +65,29 @@ public void setAddressIndex(Integer addressIndex) {
this.addressIndex = addressIndex;
}

/** The number of items directly stored in this location */
public int getNumStored() {
return this.numStored;
}

public void setNumStored(int numStored) {
this.numStored = numStored;
}

/** The number of locations directly inside this location */
public int getNumChildren() {
return this.numChildren;
}

public void setNumChildren(int numChildren) {
this.numChildren = numChildren;
}

@JsonIgnore
public boolean isLeaf() {
return getNumChildren() == 0;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -69,7 +96,10 @@ public boolean equals(Object o) {
return (Objects.equals(this.barcode, that.barcode)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.address, that.address)
&& Objects.equals(this.addressIndex, that.addressIndex));
&& Objects.equals(this.addressIndex, that.addressIndex)
&& this.numStored==that.numStored
&& this.numChildren==that.numChildren
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.ac.sanger.sccp.stan.model.store;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.MoreObjects;
import uk.ac.sanger.sccp.stan.model.Address;

Expand All @@ -16,6 +17,8 @@ public class LinkedLocation {
private String barcode;
private String name;
private Address address;
private int numStored;
private int numChildren;

public String getBarcode() {
return this.barcode;
Expand Down Expand Up @@ -96,6 +99,27 @@ private static String sanitise(String string) {
return string;
}

public int getNumStored() {
return this.numStored;
}

public void setNumStored(int numStored) {
this.numStored = numStored;
}

public int getNumChildren() {
return this.numChildren;
}

public void setNumChildren(int numChildren) {
this.numChildren = numChildren;
}

@JsonIgnore
public boolean isLeaf() {
return getNumChildren() == 0;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -106,7 +130,10 @@ public boolean equals(Object o) {
protected boolean equalsLinkedLocation(LinkedLocation that) {
return (Objects.equals(this.barcode, that.barcode)
&& Objects.equals(this.name, that.name)
&& Objects.equals(this.address, that.address));
&& Objects.equals(this.address, that.address)
&& this.numStored == that.numStored
&& this.numChildren == that.numChildren
);
}

@Override
Expand All @@ -120,6 +147,8 @@ public String toString() {
.add("barcode", repr(barcode))
.add("name", repr(name))
.add("address", address)
.add("numStored", numStored)
.add("numChildren", numChildren)
.omitNullValues()
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -269,7 +270,7 @@ public UCMap<BasicLocation> loadBasicLocationsOfItems(Collection<String> itemBar
query = query.replace("[]", objectMapper.writeValueAsString(arrayNode));
GraphQLResponse response = storelightClient.postQuery(query, null);
checkErrors(response);
var objectData = response.getData();
ObjectNode objectData = response.getData();
ArrayNode storedData = (ArrayNode) objectData.get("stored");

return makeBasicLocations(storedData);
Expand Down Expand Up @@ -305,7 +306,9 @@ private UCMap<BasicLocation> makeBasicLocations(ArrayNode nodes) {
address = Address.valueOf(addressString);
}
Integer addressIndex = integerFromNode(sd.get("addressIndex"));
map.put(itemBarcode, new BasicLocation(locationBarcode, locationName, address, addressIndex));
int numStored = intFromNode(sd.get("numStored"), 0);
int numChildren = intFromNode(sd.get("numChildren"), 0);
map.put(itemBarcode, new BasicLocation(locationBarcode, locationName, address, addressIndex, numStored, numChildren));
}
return map;
}
Expand All @@ -324,6 +327,17 @@ private static Integer integerFromNode(JsonNode node) {
return (value < 0 ? null : (Integer) value);
}

/**
* Reads an int from the given node.
* If the node is null or non-numeric, returns the given default value.
* @param node the node to read
* @param defaultValue the value to return if an int cannot be read
* @return the int value from the node, or the default value
*/
private static int intFromNode(JsonNode node, int defaultValue) {
return (node==null ? defaultValue : node.asInt(defaultValue));
}

/**
* Checks if the labware can be stored.
* @param barcode the barcode being stored
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,10 @@ type Location {
direction: GridDirection
"""A combination of this location's name and its parents' names and barcodes."""
qualifiedNameWithFirstBarcode: String
"""The number of items directly stored in this location."""
numStored: Int!
"""Is this location a leaf (i.e. does not contain other locations)?"""
leaf: Boolean!
}

"""Information about a storage location, without links to other locations and items."""
Expand All @@ -914,6 +918,10 @@ type LinkedLocation {
customName: String
"""The row/column address (if any) of this location in its parent location."""
address: Address
"""The number of items directly stored in this location."""
numStored: Int!
"""Is this location a leaf (i.e. does not contain other locations)?"""
leaf: Boolean!
}

"""The result of a request to empty a location."""
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/storelight/editLocation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ mutation {
name
address
size { numRows numColumns }
children { barcode name address }
children { barcode name address numStored numChildren }
stored { barcode address }
parent { barcode name address }
parent { barcode name address numStored numChildren }
direction
numStored
numChildren
}
}
6 changes: 4 additions & 2 deletions src/main/resources/storelight/location.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
name
address
size { numRows numColumns}
children { barcode name address }
children { barcode name address numStored numChildren }
stored { barcode address }
parent { barcode name address }
parent { barcode name address numStored numChildren }
direction
qualifiedNameWithFirstBarcode
numStored
numChildren
}
}
6 changes: 4 additions & 2 deletions src/main/resources/storelight/storeBarcode.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ mutation {
name
address
size { numRows numColumns }
children { barcode name address }
children { barcode name address numStored numChildren }
stored { barcode address }
parent { barcode name address }
parent { barcode name address numStored numChildren }
direction
numStored
numChildren
}
}
}
6 changes: 4 additions & 2 deletions src/main/resources/storelight/stored.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
name
address
size { numRows numColumns}
children { barcode name address }
parent { barcode name address }
children { barcode name address numStored numChildren }
parent { barcode name address numStored numChildren }
stored { barcode address addressIndex }
direction
qualifiedNameWithFirstBarcode
numStored
numChildren
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/storelight/storedBasicLocation.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
barcode
address
addressIndex
location { barcode name }
location { barcode name numStored numChildren }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ public void testRelease() throws Exception {

stubStorelightUnstore(mockStorelightClient);
UCMap<BasicLocation> basicLocationMap = new UCMap<>(2);
basicLocationMap.put("STAN-001", new BasicLocation("STO-1", "Box 1", new Address(1,2), 4));
basicLocationMap.put("STAN-002", new BasicLocation("STO-1", "Box 1", new Address(3,4), null));
basicLocationMap.put("STAN-001", new BasicLocation("STO-1", "Box 1", new Address(1,2), 4, 0, 0));
basicLocationMap.put("STAN-002", new BasicLocation("STO-1", "Box 1", new Address(3,4), null, 0, 0));
stubStorelightBasicLocation(mockStorelightClient, basicLocationMap);

Object result = tester.post(mutation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -92,7 +94,7 @@ public void testStore() throws Exception {

List<Map<String, ?>> labwareListData = chainGet(result, "data", "labwareInLocation");
assertThat(labwareListData).hasSize(1);
Map<String, ?> labwareData = labwareListData.get(0);
Map<String, ?> labwareData = labwareListData.getFirst();
assertEquals("STAN-100", labwareData.get("barcode"));
assertEquals(sample.getId(), chainGet(labwareData, "slots", 0, "samples", 0, "id"));
}
Expand Down Expand Up @@ -207,6 +209,50 @@ public void testTransfer() throws Exception {
verifyStorelightQuery(mockStorelightClient, List.of("store", "STAN-100", "STAN-101", "STO-5"), user.getUsername());
}

@Transactional
@ParameterizedTest
@ValueSource(booleans={false,true})
public void testQueryLocation(boolean leaf) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode locationNode = objectMapper.createObjectNode()
.put("barcode", "STO-A")
.put("name", "Alpha: Beta")
.putNull("address")
.put("numStored", 0)
.put("numChildren", 1)
.set("children", objectMapper.createArrayNode()
.add(objectMapper.createObjectNode()
.put("barcode", "STO-B")
.put("name", "Gamma: Delta")
.put("address", "B3")
.put("numStored", 5)
.put("numChildren", leaf ? 0 : 3)
)
);
GraphQLResponse graphQLResponse = new GraphQLResponse(
objectMapper.createObjectNode().set("location", locationNode), null
);
when(mockStorelightClient.postQuery(anyString(), any())).thenReturn(graphQLResponse);
Object response = tester.post("query { location(locationBarcode: \"STO-A\") { " +
"barcode fixedName customName address numStored leaf " +
"children { barcode fixedName customName address numStored leaf } } }");
Map<String, ?> loc = chainGet(response, "data", "location");
assertEquals("STO-A", loc.get("barcode"));
assertEquals("Alpha", loc.get("fixedName"));
assertEquals("Beta", loc.get("customName"));
assertEquals(false, loc.get("leaf"));
assertNull(loc.get("address"));
assertEquals(0, loc.get("numStored"));
assertThat((List<?>) loc.get("children")).hasSize(1);
Map<String, ?> child = chainGet(loc, "children", 0);
assertEquals("STO-B", child.get("barcode"));
assertEquals("Gamma", child.get("fixedName"));
assertEquals("Delta", child.get("customName"));
assertEquals("B3", child.get("address"));
assertEquals(5, child.get("numStored"));
assertEquals(leaf, child.get("leaf"));
}

@Transactional
@Test
public void testStoragePath() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public void testRecordRelease(String locBarcode, Address address, Integer addres
if (locBarcode==null) {
loc = null;
} else {
loc = new BasicLocation(locBarcode, expectedName, address, addressIndex);
loc = new BasicLocation(locBarcode, expectedName, address, addressIndex, 0, 0);
}

final int releaseId = 10;
Expand Down
Loading
Loading