15 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
16 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
17 |
|
|
18 |
|
import dk.thoerup.traininfoservice.db.DBConnection; |
19 |
|
|
20 |
|
|
21 |
@WebServlet(urlPatterns={"/SavedStatistics"}) |
@WebServlet(urlPatterns={"/SavedStatistics"}) |
22 |
public class SavedStatistics extends HttpServlet { |
public class SavedStatistics extends HttpServlet { |
26 |
protected SavedStatBean convertRow(ResultSet res) throws SQLException { |
protected SavedStatBean convertRow(ResultSet res) throws SQLException { |
27 |
SavedStatBean bean = new SavedStatBean(); |
SavedStatBean bean = new SavedStatBean(); |
28 |
|
|
29 |
bean.statisticsdate = res.getString(1); |
bean.label = res.getString(1); |
30 |
bean.station = res.getString(2); |
bean.station = res.getString(2); |
31 |
bean.location = res.getString(3); |
bean.location = res.getString(3); |
32 |
bean.name = res.getString(4); |
bean.name = res.getString(4); |
42 |
|
|
43 |
} |
} |
44 |
|
|
45 |
|
List<SavedStatBean> convertResultset(ResultSet res) throws SQLException{ |
46 |
|
List<SavedStatBean> stats = new ArrayList<SavedStatBean>(); |
47 |
|
while (res.next()) { |
48 |
|
SavedStatBean bean = convertRow(res); |
49 |
|
stats.add(bean); |
50 |
|
|
51 |
|
} |
52 |
|
return stats; |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
57 |
|
|
59 |
try { |
try { |
60 |
conn = DBConnection.getConnection(); |
conn = DBConnection.getConnection(); |
61 |
Statement stmt = conn.createStatement(); |
Statement stmt = conn.createStatement(); |
62 |
List<SavedStatBean> stats = new ArrayList<SavedStatBean>(); |
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
String sqlAvg = "SELECT count(*) AS count, round(avg(location+name+favorites),1) AS station, round(avg(location),1) as location, round(avg(name),1) as name, round(avg(favorites),1) as favorites, " + |
String sqlAvg = "SELECT count(*) AS count, round(avg(location+name+favorites),1) AS station, round(avg(location),1) as location, round(avg(name),1) as name, round(avg(favorites),1) as favorites, " + |
67 |
"round(avg(departure),1) as departure, round(avg(depcache),1) as depcache, round(avg(deperror),1) as deperror, " + |
"round(avg(departure),1) as departure, round(avg(depcache),1) as depcache, round(avg(deperror),1) as deperror, " + |
68 |
"round(avg(timetable),1) as timetable, round(avg(timecache),1) as timecache, round(avg(timeerror),1) as timeerror " + |
"round(avg(timetable),1) as timetable, round(avg(timecache),1) as timecache, round(avg(timeerror),1) as timeerror " + |
69 |
"FROM trainstatistics"; |
"FROM trainstatistics " + |
70 |
|
"WHERE statisticsdate <> now()::date "; |
71 |
|
|
72 |
ResultSet resAvg = stmt.executeQuery(sqlAvg); |
ResultSet resAvg = stmt.executeQuery(sqlAvg); |
73 |
resAvg.next(); |
resAvg.next(); |
74 |
SavedStatBean avgBean = convertRow(resAvg); |
SavedStatBean avgBean = convertRow(resAvg); |
75 |
stats.add(avgBean); |
|
76 |
|
////// |
77 |
|
String sql30Avg = "SELECT count(*) AS count, round(avg(location+name+favorites),1) AS station, round(avg(location),1) as location, round(avg(name),1) as name, round(avg(favorites),1) as favorites, " + |
78 |
|
"round(avg(departure),1) as departure, round(avg(depcache),1) as depcache, round(avg(deperror),1) as deperror, " + |
79 |
|
"round(avg(timetable),1) as timetable, round(avg(timecache),1) as timecache, round(avg(timeerror),1) as timeerror " + |
80 |
|
"FROM trainstatistics " + |
81 |
|
"WHERE statisticsdate < now()::date AND statisticsdate >= (now()::date - interval '30 days')"; |
82 |
|
|
83 |
|
ResultSet res30Avg = stmt.executeQuery(sql30Avg); |
84 |
|
res30Avg.next(); |
85 |
|
SavedStatBean avg30Bean = convertRow(res30Avg); |
86 |
|
|
87 |
//////////////////////////////////////////////// |
//////////////////////////////////////////////// |
88 |
|
|
91 |
|
|
92 |
|
|
93 |
ResultSet res = stmt.executeQuery(SQL); |
ResultSet res = stmt.executeQuery(SQL); |
94 |
|
List<SavedStatBean> stats = convertResultset(res); |
95 |
|
|
96 |
|
|
97 |
|
////////////////////////////////////////////// |
98 |
|
|
99 |
|
SQL = "select statisticsdate,location+name+favorites AS station,location,name,favorites,departure,depcache,deperror,timetable,timecache,timeerror " + |
100 |
|
" rom trainstatistics order by departure - deperror desc limit 10"; |
101 |
while (res.next()) { |
res = stmt.executeQuery(SQL); |
|
SavedStatBean bean = convertRow(res); |
|
|
stats.add(bean); |
|
|
|
|
|
} |
|
102 |
|
|
103 |
|
List<SavedStatBean> top10 = convertResultset(res); |
104 |
|
|
105 |
|
|
106 |
|
conn.close(); |
107 |
|
|
108 |
|
|
109 |
// request.setAttribute("avg", avgBean); |
request.setAttribute("avg", avgBean); |
110 |
|
request.setAttribute("avg30", avg30Bean); |
111 |
request.setAttribute("stats", stats); |
request.setAttribute("stats", stats); |
112 |
|
request.setAttribute("top10", top10); |
113 |
request.getRequestDispatcher("/savedstatistics.jsp").forward(request, response); |
request.getRequestDispatcher("/savedstatistics.jsp").forward(request, response); |
114 |
|
|
115 |
|
|