| 5068 |
varun.gupt |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
4 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
5 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
6 |
|
|
|
7 |
import java.io.IOException;
|
|
|
8 |
import java.text.DateFormat;
|
|
|
9 |
import java.text.ParseException;
|
|
|
10 |
import java.text.SimpleDateFormat;
|
|
|
11 |
import java.util.ArrayList;
|
|
|
12 |
import java.util.Date;
|
|
|
13 |
import java.util.HashMap;
|
|
|
14 |
import java.util.List;
|
|
|
15 |
import java.util.Map;
|
|
|
16 |
|
|
|
17 |
import javax.servlet.ServletOutputStream;
|
|
|
18 |
import javax.servlet.http.HttpServletRequest;
|
|
|
19 |
import javax.servlet.http.HttpServletResponse;
|
|
|
20 |
|
|
|
21 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
22 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
23 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
24 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
25 |
import org.apache.thrift.TException;
|
|
|
26 |
import org.apache.thrift.transport.TTransportException;
|
|
|
27 |
import org.slf4j.Logger;
|
|
|
28 |
import org.slf4j.LoggerFactory;
|
|
|
29 |
|
|
|
30 |
import com.opensymphony.xwork2.ValidationAwareSupport;
|
|
|
31 |
|
|
|
32 |
public class OrderMonitorController extends ValidationAwareSupport implements ServletRequestAware, ServletResponseAware {
|
|
|
33 |
|
|
|
34 |
private static Logger logger = LoggerFactory.getLogger(ProductNotificationsController.class);
|
|
|
35 |
private final DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
36 |
private HttpServletRequest request;
|
|
|
37 |
private HttpServletResponse response;
|
|
|
38 |
|
|
|
39 |
private long startTimestamp, endTimestamp;
|
|
|
40 |
|
|
|
41 |
private Map<String, Long> orderStatusDistribution;
|
|
|
42 |
|
|
|
43 |
public String index() {
|
|
|
44 |
Date startDate, endDate;
|
|
|
45 |
try {
|
|
|
46 |
if (request.getParameter("start") != null && request.getParameter("end") != null) {
|
|
|
47 |
startDate = dateFormat.parse(request.getParameter("start"));
|
|
|
48 |
endDate = dateFormat.parse(request.getParameter("end"));
|
|
|
49 |
|
|
|
50 |
} else {
|
|
|
51 |
startDate = new Date();
|
|
|
52 |
startDate.setHours(0);
|
|
|
53 |
startDate.setMinutes(0);
|
|
|
54 |
startDate.setSeconds(0);
|
|
|
55 |
|
|
|
56 |
endDate = new Date();
|
|
|
57 |
}
|
|
|
58 |
startTimestamp = startDate.getTime();
|
|
|
59 |
endTimestamp = endDate.getTime();
|
|
|
60 |
|
|
|
61 |
logger.info("Start: " + startDate + "\tEnd: " + endDate);
|
|
|
62 |
|
|
|
63 |
TransactionClient tsc = new TransactionClient();
|
|
|
64 |
in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
|
|
|
65 |
|
|
|
66 |
Map<Long, Long> orderDistribution = new HashMap<Long, Long>();
|
|
|
67 |
orderDistribution.putAll(transactionClient.getStatusDistributionOfOrders(startDate.getTime(), endDate.getTime()));
|
|
|
68 |
logger.info(orderDistribution.toString());
|
|
|
69 |
|
|
|
70 |
orderStatusDistribution = new HashMap<String, Long>();
|
|
|
71 |
|
|
|
72 |
for (long orderStatusId: orderDistribution.keySet()) {
|
|
|
73 |
String statusName = OrderStatus.findByValue((int)orderStatusId).name();
|
|
|
74 |
long count = orderDistribution.get(orderStatusId);
|
|
|
75 |
logger.info("\t" + orderStatusId + " " + statusName + " " + count);
|
|
|
76 |
orderStatusDistribution.put(statusName, count);
|
|
|
77 |
}
|
|
|
78 |
logger.info(orderStatusDistribution.toString());
|
|
|
79 |
|
|
|
80 |
} catch (ParseException e) {
|
|
|
81 |
logger.error(e.getStackTrace().toString());
|
|
|
82 |
addActionError("Please enter valid start and end date");
|
|
|
83 |
|
|
|
84 |
} catch (TTransportException e) {
|
|
|
85 |
logger.error(e.getStackTrace().toString());
|
|
|
86 |
addActionError(e.getMessage());
|
|
|
87 |
|
|
|
88 |
} catch (TransactionServiceException e) {
|
|
|
89 |
logger.error(e.getStackTrace().toString());
|
|
|
90 |
addActionError(e.getMessage());
|
|
|
91 |
|
|
|
92 |
} catch (TException e) {
|
|
|
93 |
logger.error(e.getStackTrace().toString());
|
|
|
94 |
addActionError(e.getMessage());
|
|
|
95 |
}
|
|
|
96 |
return "index";
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public HttpHeaders getOrderIds() {
|
|
|
100 |
long status = (long)OrderStatus.valueOf(request.getParameter("status")).getValue();
|
|
|
101 |
String[] timestamps = request.getParameter("timestamps").split("-");
|
|
|
102 |
|
|
|
103 |
TransactionClient tsc;
|
|
|
104 |
try {
|
|
|
105 |
tsc = new TransactionClient();
|
|
|
106 |
in.shop2020.model.v1.order.TransactionService.Client transactionClient = tsc.getClient();
|
|
|
107 |
List<Long> orderIds = transactionClient.getOrderIdsForStatus(status, Long.parseLong(timestamps[0]), Long.parseLong(timestamps[1]));
|
|
|
108 |
|
| 5069 |
varun.gupt |
109 |
response.setContentType("text/comma-separated-values");
|
|
|
110 |
response.setHeader("Content-disposition", "inline; filename=OrderIds-" + request.getParameter("status") + ".csv");
|
| 5068 |
varun.gupt |
111 |
|
|
|
112 |
StringBuilder sb = new StringBuilder();
|
|
|
113 |
|
|
|
114 |
for (long orderId: orderIds) {
|
|
|
115 |
sb.append(Long.toString(orderId));
|
|
|
116 |
sb.append("\n");
|
|
|
117 |
}
|
|
|
118 |
ServletOutputStream sos = response.getOutputStream();
|
|
|
119 |
sos.write(sb.toString().getBytes());
|
|
|
120 |
sos.flush();
|
|
|
121 |
|
|
|
122 |
} catch (TTransportException e) {
|
|
|
123 |
logger.error("", e);
|
|
|
124 |
|
|
|
125 |
} catch (NumberFormatException e) {
|
|
|
126 |
logger.error("", e);
|
|
|
127 |
|
|
|
128 |
} catch (TransactionServiceException e) {
|
|
|
129 |
logger.error("", e);
|
|
|
130 |
|
|
|
131 |
} catch (TException e) {
|
|
|
132 |
logger.error("", e);
|
|
|
133 |
|
|
|
134 |
} catch (IOException e) {
|
|
|
135 |
logger.error("", e);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return new DefaultHttpHeaders();
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public Map<String, Long> getOrderStatusDistribution() {
|
|
|
142 |
return this.orderStatusDistribution;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
public String getPath() {
|
|
|
146 |
return this.request.getServletPath();
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public String getStartEndTimestamp() {
|
|
|
150 |
return "" + startTimestamp + "-" + endTimestamp;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
@Override
|
|
|
154 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
155 |
this.request = request;
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
@Override
|
|
|
159 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
160 |
this.response = response;
|
|
|
161 |
}
|
|
|
162 |
}
|