1 |
package dk.thoerup.marketstats; |
package dk.thoerup.marketstats; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.io.PrintWriter; |
|
4 |
import java.net.InetSocketAddress; |
import java.net.InetSocketAddress; |
5 |
import java.util.ArrayList; |
import java.util.ArrayList; |
|
import java.util.Date; |
|
6 |
import java.util.Locale; |
import java.util.Locale; |
7 |
import java.util.concurrent.TimeUnit; |
import java.util.concurrent.TimeUnit; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
23 |
public class ShowStats extends HttpServlet { |
public class ShowStats extends HttpServlet { |
24 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
25 |
|
|
26 |
static final int MAXCOMMENTS = 50; |
static final int MAXCOMMENTS = 30; |
27 |
static final int TIMEOUT = 30*60; |
static final int APP_TIMEOUT = 30*60; |
28 |
|
static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4; |
29 |
|
|
30 |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
static final Logger log = Logger.getLogger(ShowStats.class.getName()); |
31 |
|
|
57 |
memcache = null; |
memcache = null; |
58 |
} |
} |
59 |
|
|
60 |
protected String doLookup(String query) throws IOException { |
protected AppBean doLookup(String query) throws IOException { |
61 |
|
|
62 |
|
|
63 |
String key = "marketstats:" + query.replace(' ', '_'); |
String key = "marketstats:" + query.replace(' ', '_'); |
64 |
String response = (String) memcache.get(key); |
//String response = (String) memcache.get(key); |
65 |
|
AppBean response = (AppBean) memcache.get(key); |
66 |
|
|
67 |
if (response == null) { |
if (response == null) { |
68 |
response = doLookupWorker(query); |
response = doLookupWorker(query); |
69 |
memcache.set(key, TIMEOUT, response); |
if (response != null) { |
70 |
response += "<!-- new lookup -->"; |
memcache.set(key, APP_TIMEOUT, response); |
71 |
} else { |
} |
72 |
response += "<!-- from memcached -->"; |
} |
|
} |
|
73 |
return response; |
return response; |
74 |
} |
} |
75 |
|
|
76 |
private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) { |
CommentsRequest buildCommentRequest(String appId, int start, int count) { |
77 |
|
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
78 |
|
.setAppId(appId) |
79 |
|
.setStartIndex(start) |
80 |
|
.setEntriesCount(count) |
81 |
|
.build(); |
82 |
|
|
83 |
|
return commentsRequest; |
84 |
|
} |
85 |
|
|
86 |
|
private ArrayList<CommentBean> loadComments(String appId) { |
87 |
|
|
88 |
|
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
89 |
|
MarketSession session = new MarketSession(); |
90 |
|
session.login(login,password); |
91 |
|
|
92 |
CommentCallback commentsCb = new CommentCallback(); |
CommentCallback commentsCb = new CommentCallback(); |
93 |
commentsCb.setList( commentBeans ); |
commentsCb.setList( commentBeans ); |
94 |
|
|
103 |
count = Math.min(10, commentsCb.getEntryCount() ); |
count = Math.min(10, commentsCb.getEntryCount() ); |
104 |
|
|
105 |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
//log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); |
106 |
CommentsRequest commentsRequest = CommentsRequest.newBuilder() |
CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); |
|
.setAppId(appId) |
|
|
.setStartIndex(start) |
|
|
.setEntriesCount(count) |
|
|
.build(); |
|
|
|
|
107 |
session.append(commentsRequest, commentsCb); |
session.append(commentsRequest, commentsCb); |
108 |
|
start +=10; |
109 |
|
|
110 |
|
CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count); |
111 |
|
session.append(commentsRequest2, commentsCb); |
112 |
session.flush(); |
session.flush(); |
113 |
|
|
114 |
start +=10; |
start +=10; |
115 |
|
|
116 |
if (start >= MAXCOMMENTS) |
if (start >= MAXCOMMENTS) |
117 |
break; //emergency brake |
break; //emergency brake |
118 |
|
|
119 |
} while ( start < commentsCb.getEntryCount() ); |
} while ( start < commentsCb.getEntryCount() ); |
120 |
|
|
121 |
|
return commentBeans; |
122 |
} |
} |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
protected String doLookupWorker(String query) { |
protected AppBean doLookupWorker(String query) { |
127 |
final StringBuilder sb = new StringBuilder(); |
final StringBuilder sb = new StringBuilder(); |
128 |
|
|
129 |
|
|
136 |
.setStartIndex(0).setEntriesCount(10) |
.setStartIndex(0).setEntriesCount(10) |
137 |
.setWithExtendedInfo(true) |
.setWithExtendedInfo(true) |
138 |
.build(); |
.build(); |
139 |
|
|
140 |
|
AppsCallback appsCb = new AppsCallback(); |
|
AppsCallback appsCb = new AppsCallback() ; |
|
|
appsCb.setStringBuffer(sb); |
|
|
|
|
|
ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>(); |
|
|
|
|
|
|
|
|
|
|
141 |
session.append(appsRequest, appsCb); |
session.append(appsRequest, appsCb); |
142 |
session.flush(); |
session.flush(); |
143 |
|
|
144 |
String appId = appsCb.getAppId(); |
return appsCb.getResult(); |
145 |
|
} |
|
if (appId != null) { |
|
|
|
|
|
loadComments(appId, session, commentBeans); |
|
|
|
|
|
sb.append("-----------------------------------------------------------------\n"); |
|
|
for (CommentBean c : commentBeans) { |
|
|
sb.append("User: " + c.author + "\n"); |
|
|
sb.append("Rating: " + c.rating + "\n"); |
|
|
sb.append("Time: " + new Date(c.time).toString() + "\n"); |
|
|
sb.append( c.text + "\n"); |
|
|
sb.append("\n"); |
|
146 |
|
|
147 |
} |
public ArrayList<CommentBean> getComments(AppBean app) { |
148 |
sb.append("Comments: " + commentBeans.size() + "\n"); |
String key = "marketstats:comments:" + app.getId(); |
149 |
} |
ArrayList<CommentBean> comments = (ArrayList<CommentBean>) memcache.get(key); |
150 |
sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n"); |
|
151 |
|
if (comments == null) { |
152 |
|
comments = loadComments(app.getId()); |
153 |
return sb.toString(); |
memcache.set(key, COMMENT_TIMEOUT, comments); |
154 |
|
} |
155 |
|
return comments; |
156 |
} |
} |
157 |
|
|
158 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
159 |
String query = request.getParameter("query"); |
String query = request.getParameter("query"); |
160 |
|
|
161 |
response.setContentType("text/html"); |
AppBean app = doLookup(query); |
162 |
PrintWriter out = response.getWriter(); |
if (app != null) { |
163 |
out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" ); |
request.setAttribute("app", app); |
164 |
|
|
165 |
|
ArrayList<CommentBean> comments = getComments(app); |
166 |
|
request.setAttribute("comments", comments); |
167 |
|
|
168 |
|
getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response); |
169 |
|
} else { |
170 |
|
response.getWriter().print("No app found with query=" + query); |
171 |
|
} |
172 |
} |
} |
173 |
|
|
174 |
} |
} |