4 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
5 |
import java.net.InetSocketAddress; |
import java.net.InetSocketAddress; |
6 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.Collections; |
|
7 |
import java.util.Date; |
import java.util.Date; |
8 |
import java.util.Locale; |
import java.util.Locale; |
9 |
|
import java.util.concurrent.TimeUnit; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
12 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
25 |
public class ShowStats extends HttpServlet { |
public class ShowStats extends HttpServlet { |
26 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
27 |
|
|
28 |
final int TIMEOUT = 30*60; |
static final int MAXCOMMENTS = 50; |
29 |
|
static final int TIMEOUT = 30*60; |
30 |
|
|
31 |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
32 |
|
|
33 |
String login; |
String login; |
34 |
String password; |
String password; |
35 |
|
|
36 |
|
MemcachedClient memcache = null; |
37 |
|
|
38 |
|
|
39 |
@Override |
@Override |
42 |
|
|
43 |
login = getServletContext().getInitParameter("login"); |
login = getServletContext().getInitParameter("login"); |
44 |
password = getServletContext().getInitParameter("password"); |
password = getServletContext().getInitParameter("password"); |
45 |
|
|
46 |
|
try { |
47 |
|
memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
48 |
|
} catch (IOException e) { |
49 |
|
throw new ServletException(e); |
50 |
|
} |
51 |
} |
} |
52 |
|
|
53 |
protected String doLookup(String appId) throws IOException { |
@Override |
54 |
MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); |
public void destroy() { |
55 |
|
super.destroy(); |
56 |
|
|
57 |
|
memcache.shutdown(3, TimeUnit.SECONDS); |
58 |
|
memcache = null; |
59 |
|
} |
60 |
|
|
61 |
|
protected String doLookup(String query) throws IOException { |
62 |
|
|
63 |
|
|
64 |
String key = "marketstats:" + appId; |
String key = "marketstats:" + query.replace(' ', '_'); |
65 |
String response = (String) c.get(key); |
String response = (String) memcache.get(key); |
66 |
|
|
67 |
if (response == null) { |
if (response == null) { |
68 |
response = doLookupWorker(appId); |
response = doLookupWorker(query); |
69 |
c.set(key, TIMEOUT, response); |
memcache.set(key, TIMEOUT, response); |
70 |
response += "<!-- new lookup -->"; |
response += "<!-- new lookup -->"; |
71 |
} else { |
} else { |
72 |
response += "<!-- from memcached -->"; |
response += "<!-- from memcached -->"; |
74 |
return response; |
return response; |
75 |
} |
} |
76 |
|
|
77 |
private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) { |
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) { |
78 |
CommentCallback commentsCb = new CommentCallback(); |
CommentCallback commentsCb = new CommentCallback(); |
79 |
commentsCb.setList( commentBeans ); |
commentsCb.setList( commentBeans ); |
80 |
|
|
81 |
commentsCb.setLocale( locale ); |
session.setLocale( Locale.ROOT ); |
82 |
session.setLocale( new Locale(locale) ); |
|
83 |
|
|
84 |
|
|
85 |
int start = 0; |
int start = 0; |
88 |
if (start > 0) |
if (start > 0) |
89 |
count = Math.min(10, commentsCb.getEntryCount() ); |
count = Math.min(10, commentsCb.getEntryCount() ); |
90 |
|
|
91 |
//log.warning("count=" + count + " start=" + start + " " + locale); |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
92 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
93 |
.setAppId(appId) |
.setAppId(appId) |
94 |
.setStartIndex(start) |
.setStartIndex(start) |
99 |
session.flush(); |
session.flush(); |
100 |
start +=10; |
start +=10; |
101 |
|
|
102 |
if (start >= 30) |
if (start >= MAXCOMMENTS) |
103 |
break; //emergency brake |
break; //emergency brake |
104 |
|
|
105 |
} while ( start < commentsCb.getEntryCount() ); |
} while ( start < commentsCb.getEntryCount() ); |
106 |
} |
} |
107 |
|
|
108 |
|
|
109 |
protected String doLookupWorker(String appId) { |
protected String doLookupWorker(String query) { |
110 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
111 |
|
|
112 |
|
|
115 |
|
|
116 |
|
|
117 |
AppsRequest appsRequest = AppsRequest.newBuilder() |
AppsRequest appsRequest = AppsRequest.newBuilder() |
118 |
.setAppId(appId) |
.setQuery(query) |
119 |
.setStartIndex(0).setEntriesCount(10) |
.setStartIndex(0).setEntriesCount(10) |
120 |
.setWithExtendedInfo(true) |
.setWithExtendedInfo(true) |
121 |
.build(); |
.build(); |
130 |
|
|
131 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
132 |
session.flush(); |
session.flush(); |
133 |
|
|
134 |
|
String appId = appsCb.getAppId(); |
135 |
|
|
136 |
|
if (appId != null) { |
137 |
|
|
138 |
loadComments(appId, session, "da", commentBeans); |
loadComments(appId, session, commentBeans); |
139 |
loadComments(appId, session, "en", commentBeans); |
|
140 |
|
sb.append("-----------------------------------------------------------------\n"); |
141 |
Collections.sort(commentBeans); |
for (CommentBean c : commentBeans) { |
142 |
|
sb.append("User: " + c.author + "\n"); |
143 |
sb.append("-----------------------------------------------------------------\n"); |
sb.append("Rating: " + c.rating + "\n"); |
144 |
for (CommentBean c : commentBeans) { |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
145 |
sb.append("User: " + c.author + " (" + c.locale + ")\n"); |
sb.append( c.text + "\n"); |
146 |
sb.append("Rating: " + c.rating + "\n"); |
sb.append("\n"); |
147 |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
|
148 |
sb.append( c.text + "\n"); |
} |
149 |
sb.append("\n"); |
sb.append("Comments: " + commentBeans.size() + "\n"); |
|
|
|
150 |
} |
} |
151 |
sb.append("Comments: " + commentBeans.size() ); |
sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n"); |
152 |
|
|
153 |
|
|
154 |
return sb.toString(); |
return sb.toString(); |
155 |
} |
} |
156 |
|
|
157 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
158 |
String appId = request.getParameter("appId"); |
String query = request.getParameter("query"); |
159 |
|
|
160 |
response.setContentType("text/html"); |
response.setContentType("text/html"); |
161 |
PrintWriter out = response.getWriter(); |
PrintWriter out = response.getWriter(); |
162 |
out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" ); |
out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" ); |
163 |
} |
} |
164 |
|
|
165 |
} |
} |