1 |
package dk.daoas.daoadresseservice.admin; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.PrintWriter; |
5 |
import java.sql.SQLException; |
6 |
import java.text.SimpleDateFormat; |
7 |
import java.util.Date; |
8 |
import java.util.List; |
9 |
|
10 |
import javax.servlet.ServletException; |
11 |
import javax.servlet.annotation.WebServlet; |
12 |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
15 |
|
16 |
import dk.daoas.daoadresseservice.AddressSearch; |
17 |
import dk.daoas.daoadresseservice.beans.Address; |
18 |
import dk.daoas.daoadresseservice.beans.LoggedAddress; |
19 |
import dk.daoas.daoadresseservice.beans.SearchResult; |
20 |
import dk.daoas.daoadresseservice.beans.SearchResult.Status; |
21 |
import dk.daoas.daoadresseservice.db.DatabaseLayer; |
22 |
import dk.daoas.daoadresseservice.util.TimingHelper; |
23 |
|
24 |
/** |
25 |
* Servlet implementation class CsvUdtraek |
26 |
*/ |
27 |
@WebServlet("/CsvUdtraek") |
28 |
public class CsvUdtraek extends HttpServlet { |
29 |
private static final long serialVersionUID = 1L; |
30 |
|
31 |
|
32 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
33 |
String type = request.getParameter("type"); |
34 |
String str = null; |
35 |
|
36 |
if (type != null) { |
37 |
switch( type ) { |
38 |
case "ikkedaekket": |
39 |
str = udtraekIkkeDaekket(); |
40 |
break; |
41 |
case "sammenlign": |
42 |
str = udtraekSammenligning(request); |
43 |
break; |
44 |
default: |
45 |
str = null; |
46 |
break; |
47 |
} |
48 |
} |
49 |
|
50 |
if (str != null) { |
51 |
response.setContentType("text/csv"); |
52 |
response.setHeader("Content-Disposition", "filename=" + type + ".csv"); |
53 |
response.getWriter().print(str); |
54 |
} else { |
55 |
PrintWriter w = response.getWriter(); |
56 |
response.setContentType("text/html"); |
57 |
w.print("Type parameter is blank or invalid value<br>"); |
58 |
w.print("Type: <a href='CsvUdtraek?type=ikkedaekket'>ikkedaekket</a> <br>"); |
59 |
w.print("Type: <a href='CsvUdtraek?type=sammenlign&antaldage=5'>sammenlign</a> <br>"); |
60 |
} |
61 |
} |
62 |
|
63 |
/* ******************************************* |
64 |
* |
65 |
*/ |
66 |
private String udtraekIkkeDaekket() { |
67 |
AddressSearch search = (AddressSearch) getServletContext().getAttribute("search"); |
68 |
if (search == null) { |
69 |
return "data struktur ikke indlæst"; |
70 |
} |
71 |
|
72 |
StringBuilder sb = new StringBuilder(); |
73 |
sb.append("gadeid;postnr;vejnavn;husnr;husnrbogstav;distributor\r\n"); |
74 |
|
75 |
for (Address addr : search.getNonCoveredAddresses() ) { |
76 |
sb.append( addr.gadeid ).append(";") |
77 |
.append( addr.postnr ).append(";") |
78 |
.append( addr.vejnavn ).append(";") |
79 |
.append( addr.husnr ).append(";") |
80 |
.append( addr.husnrbogstav ).append(";") |
81 |
.append( addr.distributor != null ? addr.distributor : "" ) |
82 |
.append( "\n" ); |
83 |
} |
84 |
|
85 |
return sb.toString(); |
86 |
} |
87 |
|
88 |
/* ******************************************* |
89 |
* |
90 |
*/ |
91 |
|
92 |
protected String udtraekSammenligning(HttpServletRequest request) throws IOException { |
93 |
|
94 |
AddressSearch search = (AddressSearch) getServletContext().getAttribute("search"); |
95 |
if (search == null) { |
96 |
return "data struktur ikke indlæst"; |
97 |
} |
98 |
|
99 |
int antal_dage = 2; |
100 |
|
101 |
if (request.getParameter("antaldage") != null ) { |
102 |
antal_dage = Integer.parseInt( request.getParameter("antaldage") ); |
103 |
} |
104 |
|
105 |
int antal_nyfejl_glok = 0; |
106 |
int antal_nyfejl_glfejl = 0; |
107 |
int antal_nyok_glfejl = 0; |
108 |
int antal_nyok_glgoogle = 0; |
109 |
int antal_nygoogle_glgoogle = 0; |
110 |
int antal_nyok_glok = 0; |
111 |
|
112 |
try { |
113 |
TimingHelper timer = new TimingHelper(); |
114 |
|
115 |
StringBuilder sb = new StringBuilder(); |
116 |
sb.append("besked;postnr;adresse;gl status;ny status;ny split;ny vej\n"); |
117 |
|
118 |
List<LoggedAddress> addrList = DatabaseLayer.getLoggedAdresses(antal_dage); |
119 |
|
120 |
for(LoggedAddress addr : addrList) { |
121 |
SearchResult res = search.search("" + addr.postnr, addr.adresse); |
122 |
|
123 |
if (res.status != Status.STATUS_OK) { //Ny service fandt ikke adressen) { |
124 |
|
125 |
if (addr.status != 10 ) {//Hvis gl service kunne finde adressen så er vi ude i en regression |
126 |
|
127 |
appendLogLine("ny fejler, gl ok", sb, addr, res); |
128 |
antal_nyfejl_glok++; |
129 |
|
130 |
} else { |
131 |
appendLogLine("ny fejler, gl fejler !!!", sb, addr, res); |
132 |
antal_nyfejl_glfejl++; |
133 |
} |
134 |
|
135 |
} else { |
136 |
if (addr.status == 10) { |
137 |
appendLogLine("ny OK, gl fejler", sb, addr, res); |
138 |
antal_nyok_glfejl++; |
139 |
|
140 |
} else if (addr.status == 12 ) { |
141 |
|
142 |
if (res.google == false && res.osm == false) { |
143 |
|
144 |
appendLogLine("ny Direkte, gl Google", sb, addr, res); |
145 |
antal_nyok_glgoogle++; |
146 |
} else { |
147 |
appendLogLine("ny Google, gl Google", sb, addr, res); |
148 |
antal_nygoogle_glgoogle++; |
149 |
} |
150 |
} else { |
151 |
antal_nyok_glok++; |
152 |
} |
153 |
} |
154 |
|
155 |
if (res.osm || res.google) { |
156 |
try { |
157 |
Thread.sleep(50);//Back-off external services |
158 |
} catch(InterruptedException e) { |
159 |
// |
160 |
} |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
|
166 |
|
167 |
sb.append("Forbrugt tid: ;").append( timer.getElapsed() ).append(";;;\n"); |
168 |
sb.append("SearchData genereret: ;").append( search.getStatistics().buildTime ).append(";;;\n"); |
169 |
sb.append("Sammenligning csv genereret: ;").append( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date() ) ).append(";;;\n"); |
170 |
|
171 |
|
172 |
sb.append("Antal dage: ;").append( antal_dage ).append(";;;\n"); |
173 |
sb.append("Antal adresser testet: ;").append(addrList.size()).append(";;;\n"); |
174 |
double sekunder = timer.getElapsed() / 1000.0; |
175 |
int adresserPrSekund = (int) Math.round( addrList.size() / sekunder ); |
176 |
sb.append("Antal adresser testet/s: ;").append(adresserPrSekund).append(";;;\n"); |
177 |
|
178 |
|
179 |
sb.append("Antal ny ok, gl ok: ;").append( antal_nyok_glok ).append(";;;\n"); |
180 |
sb.append("Antal ny ok, gl google: ;").append( antal_nyok_glgoogle ).append(";;;\n"); |
181 |
sb.append("Antal ny ok, gl fejl: ;").append( antal_nyok_glfejl ).append(";;;\n"); |
182 |
sb.append("Antal ny google, gl google: ;").append( antal_nygoogle_glgoogle ).append(";;;\n"); |
183 |
sb.append("Antal ny fejl, gl ok: ;").append( antal_nyfejl_glok ).append(";;;\n"); |
184 |
sb.append("Antal ny fejl, gl fejl: ;").append( antal_nyfejl_glfejl ).append(";;;\n"); |
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
return sb.toString(); |
191 |
|
192 |
} catch(SQLException e) { |
193 |
throw new IOException(e); |
194 |
} |
195 |
} |
196 |
|
197 |
private void appendLogLine(String msg, StringBuilder sb, LoggedAddress addr, SearchResult res) { |
198 |
|
199 |
sb.append(msg).append(";") |
200 |
.append(addr.postnr).append(";") |
201 |
.append(addr.adresse).append(";") |
202 |
.append(addr.status).append(";") |
203 |
.append(res.status).append(";") |
204 |
.append(res.splitResult).append(";") |
205 |
.append(res.vej) |
206 |
.append("\n"); |
207 |
|
208 |
} |
209 |
|
210 |
} |