blob: b407cc67e9e1feaf4d67ffe3bb616683f3b2e150 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.operationProcessor;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.vespa.http.client.config.SessionParams;
import com.yahoo.vespa.http.client.core.communication.ClusterConnection;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
public class OperationStats {
private static JsonFactory jsonFactory = new JsonFactory();
private final String sessionParamsAsXmlString;
private List<ClusterConnection> clusters;
private IncompleteResultsThrottler throttler;
public OperationStats(
SessionParams sessionParams,
List<ClusterConnection> clusters,
IncompleteResultsThrottler throttler) {
this.sessionParamsAsXmlString = generateSessionParamsAsXmlString(sessionParams);
this.clusters = clusters;
this.throttler = throttler;
}
private String generateSessionParamsAsXmlString(final SessionParams sessionParams) {
final ObjectMapper objectMapper = new ObjectMapper();
final StringWriter stringWriter = new StringWriter();
try {
JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
objectMapper.writeValue(jsonGenerator, sessionParams);
return stringWriter.toString();
} catch (IOException e) {
return e.getMessage();
}
}
public String getStatsAsJson() {
try {
final StringWriter stringWriter = new StringWriter();
JsonGenerator jsonGenerator = jsonFactory.createGenerator(stringWriter);
jsonGenerator.writeStartObject();
jsonGenerator.writeArrayFieldStart("clusters");
for (ClusterConnection cluster : clusters) {
jsonGenerator.writeStartObject();
jsonGenerator.writeNumberField("clusterid", cluster.getClusterId());
jsonGenerator.writeFieldName("stats");
jsonGenerator.writeRawValue(cluster.getStatsAsJSon());
jsonGenerator.writeEndObject();
}
jsonGenerator.writeEndArray();
jsonGenerator.writeFieldName("sessionParams");
jsonGenerator.writeRawValue(sessionParamsAsXmlString);
jsonGenerator.writeFieldName("throttleDebugMessage");
jsonGenerator.writeRawValue("\"" + throttler.getDebugMessage() + "\"");
jsonGenerator.writeEndObject();
jsonGenerator.close();
return stringWriter.toString();
} catch (IOException e) {
return "{ \"Error\" : \""+ e.getMessage() + "\"}";
}
}
}
|