patternjavaMinor
Serializing data from two URLs in the same object efficiently
Viewed 0 times
sametheefficientlyserializingobjecttwofromdataurls
Problem
This is a follow-up to: Serializing JSON data coming from two URLs in the same object
I have two URLs (
```
{
"aggRecords": {
"reportRecords": [
{
"min": 0,
"max": 12,
"avg": 0.3699187,
"count": 246,
"sumSq": 571,
"stddev": 1.4779372,
"median": 0,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "PostProcessing",
"Type": "PostProcessing"
},
"value": 91
},
{
"min": 0,
"max": 23,
"avg": 2.3991289E-4,
"count": 1463031,
"sumSq": 3071,
"stddev": 0.045814946,
"median": 0,
"percentileMap": {
"95": 0
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "ResourceContext",
"Type": "ResourceContext"
},
"value": 351
},
{
"min": 0,
"max": 1209,
"avg": 1.9203402,
"count": 7344636,
"sumSq": 71832774,
"stddev": 2.4683187,
"median": 2,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "DataSync",
I have two URLs (
urlA and urlB) and they both give me a JSON response back in the same JSON format. Below is my JSON string which I am getting back by calling from urlA. I have shortened it down by having only three reportRecords for understanding purposes. In general, it might have more than ~500 reportRecords.```
{
"aggRecords": {
"reportRecords": [
{
"min": 0,
"max": 12,
"avg": 0.3699187,
"count": 246,
"sumSq": 571,
"stddev": 1.4779372,
"median": 0,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "PostProcessing",
"Type": "PostProcessing"
},
"value": 91
},
{
"min": 0,
"max": 23,
"avg": 2.3991289E-4,
"count": 1463031,
"sumSq": 3071,
"stddev": 0.045814946,
"median": 0,
"percentileMap": {
"95": 0
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "ResourceContext",
"Type": "ResourceContext"
},
"value": 351
},
{
"min": 0,
"max": 1209,
"avg": 1.9203402,
"count": 7344636,
"sumSq": 71832774,
"stddev": 2.4683187,
"median": 2,
"percentileMap": {
"95": 4
},
"metricName": "TransactionDuration",
"dimensions": {
"env": "dev",
"pool": "titan",
"Name": "DataSync",
Solution
The
I don't really see the point of this:
Do you really want to print out the
I don't know of a rule against cloning and passing around
I would do like this:
This means of course that you'll have to change the
Here, appending the
I would extract the duplicated parts to a temp variable:
Sometimes you call a hostname variable as
main method is doing too much. It would be better to extract its content to a method, with a meaningful name.I don't really see the point of this:
List hostMetricsList = new ArrayList();
for (String hostName : hostNames) {
// ...
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);
hostMetricsList.add(hostMetrics);
}
System.out.println(hostMetricsList);Do you really want to print out the
HostMetrics as a list? Printing them one by one is not good enough?for (String hostName : hostNames) {
// ...
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);
System.out.println(hostMetrics);
}I don't know of a rule against cloning and passing around
Calendar objects, but it just doesn't seem natural. Instead of this:Calendar startDate = new GregorianCalendar();
startDate.set(Calendar.MINUTE, 0);
Calendar endDate = (Calendar) startDate.clone();
startDate.set(Calendar.HOUR_OF_DAY, 0);
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);I would do like this:
Calendar cal = new GregorianCalendar();
cal.set(Calendar.MINUTE, 0);
Date startDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 0);
Date endDate = cal.getTime();
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);This means of course that you'll have to change the
HostMetrics constructor to take Date params instead of Calendar, but I think that's a good thing.Here, appending the
startTime and endTime parameters is duplicated:String urlA = BASE_URL + hostName + TRANSACTION_METRIC + "&startTime=" + startTime
+ "&endTime=" + endTime;
String urlB = BASE_URL + hostName + EVENT_METRIC + "&startTime=" + startTime
+ "&endTime=" + endTime;I would extract the duplicated parts to a temp variable:
String extraParams = "&startTime=" + startTime + "&endTime=" + endTime;
String urlA = BASE_URL + hostName + TRANSACTION_METRIC + extraParams;
String urlB = BASE_URL + hostName + EVENT_METRIC + extraParams;Sometimes you call a hostname variable as
hostname, sometimes hostName. One day you might mix them up. I recommend to use hostname everywhere consistently.Code Snippets
List<HostMetrics> hostMetricsList = new ArrayList<HostMetrics>();
for (String hostName : hostNames) {
// ...
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);
hostMetricsList.add(hostMetrics);
}
System.out.println(hostMetricsList);for (String hostName : hostNames) {
// ...
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);
System.out.println(hostMetrics);
}Calendar startDate = new GregorianCalendar();
startDate.set(Calendar.MINUTE, 0);
Calendar endDate = (Calendar) startDate.clone();
startDate.set(Calendar.HOUR_OF_DAY, 0);
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);Calendar cal = new GregorianCalendar();
cal.set(Calendar.MINUTE, 0);
Date startDate = cal.getTime();
cal.set(Calendar.HOUR_OF_DAY, 0);
Date endDate = cal.getTime();
HostMetrics hostMetrics = new HostMetrics(hostName, startDate, endDate);String urlA = BASE_URL + hostName + TRANSACTION_METRIC + "&startTime=" + startTime
+ "&endTime=" + endTime;
String urlB = BASE_URL + hostName + EVENT_METRIC + "&startTime=" + startTime
+ "&endTime=" + endTime;Context
StackExchange Code Review Q#61578, answer score: 2
Revisions (0)
No revisions yet.