--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/07/05 06:17:05 949 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/07/05 08:18:02 950 @@ -1,10 +1,8 @@ package dk.thoerup.marketstats; import java.io.IOException; -import java.io.PrintWriter; import java.net.InetSocketAddress; import java.util.ArrayList; -import java.util.Date; import java.util.Locale; import java.util.concurrent.TimeUnit; import java.util.logging.Logger; @@ -25,8 +23,9 @@ public class ShowStats extends HttpServlet { private static final long serialVersionUID = 1L; - static final int MAXCOMMENTS = 50; - static final int TIMEOUT = 30*60; + static final int MAXCOMMENTS = 30; + static final int APP_TIMEOUT = 30*60; + static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4; static final Logger log = Logger.getLogger(ShowStats.class.getName()); @@ -58,23 +57,38 @@ memcache = null; } - protected String doLookup(String query) throws IOException { + protected AppBean doLookup(String query) throws IOException { String key = "marketstats:" + query.replace(' ', '_'); - String response = (String) memcache.get(key); + //String response = (String) memcache.get(key); + AppBean response = (AppBean) memcache.get(key); if (response == null) { response = doLookupWorker(query); - memcache.set(key, TIMEOUT, response); - response += ""; - } else { - response += ""; - } + if (response != null) { + memcache.set(key, APP_TIMEOUT, response); + } + } return response; } - private void loadComments(String appId, MarketSession session, ArrayList commentBeans) { + CommentsRequest buildCommentRequest(String appId, int start, int count) { + CommentsRequest commentsRequest = CommentsRequest.newBuilder() + .setAppId(appId) + .setStartIndex(start) + .setEntriesCount(count) + .build(); + + return commentsRequest; + } + + private ArrayList loadComments(String appId) { + + ArrayList commentBeans = new ArrayList(); + MarketSession session = new MarketSession(); + session.login(login,password); + CommentCallback commentsCb = new CommentCallback(); commentsCb.setList( commentBeans ); @@ -89,24 +103,27 @@ count = Math.min(10, commentsCb.getEntryCount() ); //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); - CommentsRequest commentsRequest = CommentsRequest.newBuilder() - .setAppId(appId) - .setStartIndex(start) - .setEntriesCount(count) - .build(); - + CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); session.append(commentsRequest, commentsCb); + start +=10; + + CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count); + session.append(commentsRequest2, commentsCb); session.flush(); + start +=10; if (start >= MAXCOMMENTS) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); + + return commentBeans; } + - protected String doLookupWorker(String query) { + protected AppBean doLookupWorker(String query) { final StringBuilder sb = new StringBuilder(); @@ -119,47 +136,39 @@ .setStartIndex(0).setEntriesCount(10) .setWithExtendedInfo(true) .build(); - - - AppsCallback appsCb = new AppsCallback() ; - appsCb.setStringBuffer(sb); - - ArrayList commentBeans = new ArrayList(); - - - + + AppsCallback appsCb = new AppsCallback(); session.append(appsRequest, appsCb); session.flush(); - String appId = appsCb.getAppId(); - - 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"); + return appsCb.getResult(); + } - } - sb.append("Comments: " + commentBeans.size() + "\n"); - } - sb.append("\n"); - - - return sb.toString(); + public ArrayList getComments(AppBean app) { + String key = "marketstats:comments:" + app.getId(); + ArrayList comments = (ArrayList) memcache.get(key); + + if (comments == null) { + comments = loadComments(app.getId()); + memcache.set(key, COMMENT_TIMEOUT, comments); + } + return comments; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = request.getParameter("query"); - - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(query) + "
" ); + + AppBean app = doLookup(query); + if (app != null) { + request.setAttribute("app", app); + + ArrayList comments = getComments(app); + request.setAttribute("comments", comments); + + getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response); + } else { + response.getWriter().print("No app found with query=" + query); + } } }