Skip to content

Commit

Permalink
Updatemysampler (#21)
Browse files Browse the repository at this point in the history
* Implement hooks to select different sampling strategies

* Bump version, jmyapi, and release date
  • Loading branch information
apcarp committed Dec 12, 2023
1 parent a4b6db0 commit 8110736
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ plugins {
}

description = "MYA web-based query tool"
version = '6.0.2'
version = '6.1.0'

ext {
releaseDate = 'Sep 26 2023'
releaseDate = 'Dec 12, 2023'
}

tasks.withType(JavaCompile) {
Expand Down Expand Up @@ -35,7 +35,7 @@ configurations {
}

dependencies {
implementation 'org.jlab:jmyapi:8.0.0',
implementation 'org.jlab:jmyapi:9.0.0',
'org.eclipse.parsson:parsson:1.1.1'
providedCompile 'jakarta.servlet:jakarta.servlet-api:6.0.0'
testImplementation 'junit:junit:4.13.2'
Expand Down
48 changes: 43 additions & 5 deletions src/integration/java/org/jlab/myquery/MySamplerQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ public class MySamplerQueryTest {
@Test
public void basicUsageTest() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=")).build();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=&x=n")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpRequest request2 = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=&x=s")).build();
HttpResponse<String> response2 = client.send(request, HttpResponse.BodyHandlers.ofString());
HttpRequest request3 = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=")).build();
HttpResponse<String> response3 = client.send(request, HttpResponse.BodyHandlers.ofString());

assertEquals(200, response.statusCode());
assertEquals(200, response2.statusCode());
assertEquals(200, response3.statusCode());

String jsonString = """
{
Expand Down Expand Up @@ -63,14 +69,24 @@ public void basicUsageTest() throws IOException, InterruptedException {
}
}""";
String exp;
try(JsonReader r = Json.createReader(new StringReader(jsonString))) {
try (JsonReader r = Json.createReader(new StringReader(jsonString))) {
exp = r.readObject().toString();
}

try(JsonReader reader = Json.createReader(new StringReader(response.body()))) {
try (JsonReader reader = Json.createReader(new StringReader(response.body()))) {
JsonObject json = reader.readObject();
assertEquals(exp, json.toString());
}
try (JsonReader reader = Json.createReader(new StringReader(response2.body()))) {
JsonObject json = reader.readObject();
assertEquals(exp, json.toString());
}

try (JsonReader reader = Json.createReader(new StringReader(response3.body()))) {
JsonObject json = reader.readObject();
assertEquals(exp, json.toString());
}

}

@Test
Expand Down Expand Up @@ -1081,11 +1097,11 @@ public void multiChannelTest() throws IOException, InterruptedException {
}
}""";
String exp;
try(JsonReader r = Json.createReader(new StringReader(jsonString))) {
try (JsonReader r = Json.createReader(new StringReader(jsonString))) {
exp = r.readObject().toString();
}

try(JsonReader reader = Json.createReader(new StringReader(response.body()))) {
try (JsonReader reader = Json.createReader(new StringReader(response.body()))) {
JsonObject json = reader.readObject();
assertEquals(exp, json.toString());
}
Expand All @@ -1107,4 +1123,26 @@ public void jsonpTest() throws IOException, InterruptedException {
assertEquals(exp, result);
}
}


@Test
public void repeatUsageTest() throws IOException, InterruptedException {
// Check that we don't have some sort of resource exhaustion. Hit this bug in development and thought best to
// include explicit test for it.
HttpClient client = HttpClient.newHttpClient();
// Test the stream strategy
for (int i = 0; i < 20; i++) {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=&s=s")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}

// Test the n_queries strategy
for (int i = 0; i < 20; i++) {
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8080/myquery/mysampler?c=channel1&b=2019-08-12+23%3A59%3A00&n=5&s=15000&m=docker&f=6&v=&s=n")).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
}

}
}
22 changes: 15 additions & 7 deletions src/main/java/org/jlab/myquery/MySamplerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.jlab.mya.event.*;
import org.jlab.mya.stream.EventStream;
import org.jlab.mya.stream.LabeledEnumStream;
import org.jlab.mya.stream.MySamplerStream;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
Expand Down Expand Up @@ -60,6 +60,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
long sampleCount = -1;
String deployment = "ops";
Instant begin = null;
MySamplerStream.Strategy strategy = MySamplerStream.Strategy.N_QUERIES;

String c = request.getParameter("c"); // channel
String b = request.getParameter("b"); // begin
Expand All @@ -72,11 +73,20 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
String u = request.getParameter("u"); // formatAsMillisSinceEpoch
String a = request.getParameter("a"); // adjustMillisWithServerOffset
String v = request.getParameter("v"); // decimalFormatter (value precision)
String x = request.getParameter("x"); // Sampling strategy

boolean updatesOnly = (d != null);
boolean enumsAsStrings = (e != null);

try {

if (x != null && x.equals("s")) {
strategy = MySamplerStream.Strategy.STREAM;
} else if (x != null && x.equals("n")) {
strategy = MySamplerStream.Strategy.N_QUERIES;
} else if (x != null) {
throw new Exception("Unrecognized strategy: " + "'" + x + "'");
}

if (c == null || c.trim().isEmpty()) {
throw new Exception("Channel (c) is required");
}
Expand Down Expand Up @@ -182,7 +192,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
for(String channelName : channels) {
boolean error = processChannelRequest(service, deployment, gen, channelName, begin,
intervalMillis, sampleCount, updatesOnly, formatAsMillisSinceEpoch,
adjustMillisWithServerOffset, timestampFormatter, sigFigs, enumsAsStrings);
adjustMillisWithServerOffset, timestampFormatter, sigFigs, enumsAsStrings, strategy);
if (error) {
anyErrors = true;
}
Expand Down Expand Up @@ -212,7 +222,7 @@ private boolean processChannelRequest(MySamplerWebService service, String deploy
Instant begin, long intervalMillis, long sampleCount, boolean updatesOnly,
boolean formatAsMillisSinceEpoch, boolean adjustMillisWithServerOffset,
DateTimeFormatter timestampFormatter, short sigFigs,
boolean enumsAsStrings) throws ServletException {
boolean enumsAsStrings, MySamplerStream.Strategy strategy) throws ServletException {
gen.writeStartObject(channel);
boolean error = false;

Expand Down Expand Up @@ -248,9 +258,8 @@ private boolean processChannelRequest(MySamplerWebService service, String deploy
// Write out the channel's data
EventStream stream = null;
try {

// Cannot use try with resources since we may sometimes wrap stream in a LabeledEnumStream
stream = service.openEventStream(metadata, begin, intervalMillis, sampleCount, updatesOnly);
stream = service.openEventStream(metadata, begin, intervalMillis, sampleCount, updatesOnly, strategy);
if (enumsAsStrings && metadata.getMyaType() == MyaDataType.DBR_ENUM && enumLabels != null && !enumLabels.isEmpty()) {

stream = new LabeledEnumStream(stream, enumLabels);
Expand Down Expand Up @@ -279,7 +288,6 @@ private boolean processChannelRequest(MySamplerWebService service, String deploy

gen.writeEnd();
gen.write("returnCount", dataLength);

} catch(Exception ex) {
// Can't just return or else we leave a connection open
error = true;
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/org/jlab/myquery/MySamplerWebService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* This class provides features similar to the mySampler command line utility.
*
* @author adamc
*/
public class MySamplerWebService extends QueryWebService {
Expand All @@ -34,18 +35,31 @@ public List<ExtraInfo> findExtraInfo(Metadata metadata, String type, Instant beg


@SuppressWarnings("unchecked")
public <T extends Event> MySamplerStream<T> openEventStream(Metadata<T> metadata, Instant begin, long intervalMillis,
long sampleCount, boolean updatesOnly) throws SQLException, UnsupportedOperationException {
public <T extends Event> MySamplerStream<T> openEventStream(Metadata<T> metadata, Instant begin,
long intervalMillis, long sampleCount,
boolean updatesOnly, MySamplerStream.Strategy strategy)
throws SQLException, UnsupportedOperationException {

PointWebService pws = new PointWebService(nexus.getDeployment());
T priorEvent = (T) pws.findEvent(metadata, updatesOnly, begin, true, false, false);

Instant end = begin.plusMillis(intervalMillis * (sampleCount - 1));
EventStream<T> stream = nexus.openEventStream(metadata, begin, end, DataNexus.IntervalQueryFetchStrategy.STREAM, updatesOnly);

EventStream<T> stream = null;
MySamplerStream<T> out;
try {
out = MySamplerStream.getMySamplerStream(stream, begin, intervalMillis, sampleCount, priorEvent, updatesOnly, metadata.getType());
if (strategy == MySamplerStream.Strategy.STREAM) {
stream = nexus.openEventStream(metadata, begin, end,
DataNexus.IntervalQueryFetchStrategy.STREAM, updatesOnly);
out = MySamplerStream.getMySamplerStream(stream, begin, intervalMillis, sampleCount, priorEvent,
updatesOnly, metadata.getType());
} else if (strategy == MySamplerStream.Strategy.N_QUERIES) {
out = MySamplerStream.getMySamplerStream(begin, intervalMillis, sampleCount, updatesOnly,
metadata.getType(), nexus, metadata);
} else {
throw new IllegalArgumentException(("Unsupported strategy - " + strategy));
}

} catch (Exception ex) {
if (stream != null) {
try {
Expand Down
8 changes: 8 additions & 0 deletions src/main/webapp/mysampler-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ <h1>mysampler query</h1>
<label for="v">Significant figures (v): </label>
<input id="v" name="v" type="text" placeholder="default = 6"/>
</li>
<li>
<label for="x">Sample Strategy (v): </label>
<select id="x" name="x">
<option value="n">Many Events Per Sample</option>
<option value="s">Few Events Per Sample</option>
</select>
</li>

</ul>
</div>

Expand Down

0 comments on commit 8110736

Please sign in to comment.