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.logging.Logger; |
import java.util.logging.Logger; |
47 |
} |
} |
48 |
} |
} |
49 |
|
|
50 |
protected String doLookup(String appId) throws IOException { |
protected String doLookup(String query) throws IOException { |
51 |
|
|
52 |
|
|
53 |
String key = "marketstats:" + appId; |
String key = "marketstats:" + query.replace(' ', '_'); |
54 |
String response = (String) memcache.get(key); |
String response = (String) memcache.get(key); |
55 |
|
|
56 |
if (response == null) { |
if (response == null) { |
57 |
response = doLookupWorker(appId); |
response = doLookupWorker(query); |
58 |
memcache.set(key, TIMEOUT, response); |
memcache.set(key, TIMEOUT, response); |
59 |
response += "<!-- new lookup -->"; |
response += "<!-- new lookup -->"; |
60 |
} else { |
} else { |
63 |
return response; |
return response; |
64 |
} |
} |
65 |
|
|
66 |
private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) { |
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) { |
67 |
CommentCallback commentsCb = new CommentCallback(); |
CommentCallback commentsCb = new CommentCallback(); |
68 |
commentsCb.setList( commentBeans ); |
commentsCb.setList( commentBeans ); |
69 |
|
|
70 |
commentsCb.setLocale( locale ); |
session.setLocale( Locale.ROOT ); |
71 |
session.setLocale( new Locale(locale) ); |
|
72 |
|
|
73 |
|
|
74 |
int start = 0; |
int start = 0; |
77 |
if (start > 0) |
if (start > 0) |
78 |
count = Math.min(10, commentsCb.getEntryCount() ); |
count = Math.min(10, commentsCb.getEntryCount() ); |
79 |
|
|
80 |
//log.warning("count=" + count + " start=" + start + " " + locale); |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
81 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
82 |
.setAppId(appId) |
.setAppId(appId) |
83 |
.setStartIndex(start) |
.setStartIndex(start) |
88 |
session.flush(); |
session.flush(); |
89 |
start +=10; |
start +=10; |
90 |
|
|
91 |
if (start >= 20) |
if (start >= 200) |
92 |
break; //emergency brake |
break; //emergency brake |
93 |
|
|
94 |
} while ( start < commentsCb.getEntryCount() ); |
} while ( start < commentsCb.getEntryCount() ); |
95 |
} |
} |
96 |
|
|
97 |
|
|
98 |
protected String doLookupWorker(String appId) { |
protected String doLookupWorker(String query) { |
99 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
100 |
|
|
101 |
|
|
104 |
|
|
105 |
|
|
106 |
AppsRequest appsRequest = AppsRequest.newBuilder() |
AppsRequest appsRequest = AppsRequest.newBuilder() |
107 |
.setAppId(appId) |
.setQuery(query) |
108 |
.setStartIndex(0).setEntriesCount(10) |
.setStartIndex(0).setEntriesCount(10) |
109 |
.setWithExtendedInfo(true) |
.setWithExtendedInfo(true) |
110 |
.build(); |
.build(); |
119 |
|
|
120 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
121 |
session.flush(); |
session.flush(); |
122 |
|
|
123 |
|
String appId = appsCb.getAppId(); |
124 |
|
|
125 |
|
if (appId != null) { |
126 |
|
|
127 |
loadComments(appId, session, "da", commentBeans); |
loadComments(appId, session, commentBeans); |
128 |
loadComments(appId, session, "en", commentBeans); |
|
129 |
|
sb.append("-----------------------------------------------------------------\n"); |
130 |
Collections.sort(commentBeans); |
for (CommentBean c : commentBeans) { |
131 |
|
sb.append("User: " + c.author + "\n"); |
132 |
sb.append("-----------------------------------------------------------------\n"); |
sb.append("Rating: " + c.rating + "\n"); |
133 |
for (CommentBean c : commentBeans) { |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
134 |
sb.append("User: " + c.author + " (" + c.locale + ")\n"); |
sb.append( c.text + "\n"); |
135 |
sb.append("Rating: " + c.rating + "\n"); |
sb.append("\n"); |
136 |
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
|
137 |
sb.append( c.text + "\n"); |
} |
138 |
sb.append("\n"); |
sb.append("Comments: " + commentBeans.size() + "\n"); |
|
|
|
139 |
} |
} |
140 |
sb.append("Comments: " + commentBeans.size() + "\n"); |
sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n"); |
|
sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes"); |
|
141 |
|
|
142 |
|
|
143 |
return sb.toString(); |
return sb.toString(); |
144 |
} |
} |
145 |
|
|
146 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
147 |
String appId = request.getParameter("appId"); |
String query = request.getParameter("query"); |
148 |
|
|
149 |
response.setContentType("text/html"); |
response.setContentType("text/html"); |
150 |
PrintWriter out = response.getWriter(); |
PrintWriter out = response.getWriter(); |
151 |
out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" ); |
out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" ); |
152 |
} |
} |
153 |
|
|
154 |
} |
} |