--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/05/27 08:51:59 757 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/07/05 08:43:30 952 @@ -1,12 +1,10 @@ package dk.thoerup.marketstats; import java.io.IOException; -import java.io.PrintWriter; import java.net.InetSocketAddress; import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; import java.util.Locale; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import javax.servlet.ServletException; @@ -25,7 +23,10 @@ public class ShowStats extends HttpServlet { private static final long serialVersionUID = 1L; - 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()); String login; @@ -48,23 +49,68 @@ } } - protected String doLookup(String query) throws IOException { + @Override + public void destroy() { + super.destroy(); + + memcache.shutdown(3, TimeUnit.SECONDS); + memcache = null; + } + + protected AppBean lookupApp(String query) throws IOException { - String key = "marketstats:" + query; - String response = (String) memcache.get(key); + String key = "marketstats:" + query.replace(' ', '_'); + //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 += ""; - } + response = lookupAppWorker(query); + if (response != null) { + memcache.set(key, APP_TIMEOUT, response); + } + } return response; } + + protected AppBean lookupAppWorker(String query) { + final StringBuilder sb = new StringBuilder(); + + + MarketSession session = new MarketSession(); + session.login(login,password); + + + AppsRequest appsRequest = AppsRequest.newBuilder() + .setQuery(query) + .setStartIndex(0) + .setEntriesCount(1) + .setWithExtendedInfo(true) + .build(); + + AppsCallback appsCb = new AppsCallback(); + session.append(appsRequest, appsCb); + session.flush(); + + return appsCb.getResult(); + } + + CommentsRequest buildCommentRequest(String appId, int start, int count) { + CommentsRequest commentsRequest = CommentsRequest.newBuilder() + .setAppId(appId) + .setStartIndex(start) + .setEntriesCount(count) + .build(); + + return commentsRequest; + } - private void loadComments(String appId, MarketSession session, ArrayList commentBeans) { + private ArrayList loadComments(String appId) { + + ArrayList commentBeans = new ArrayList(); + MarketSession session = new MarketSession(); + session.login(login,password); + CommentCallback commentsCb = new CommentCallback(); commentsCb.setList( commentBeans ); @@ -79,77 +125,52 @@ 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 >= 200) + if (start >= MAXCOMMENTS) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); - } - - - protected String doLookupWorker(String query) { - final StringBuilder sb = new StringBuilder(); - - - MarketSession session = new MarketSession(); - session.login(login,password); - - - AppsRequest appsRequest = AppsRequest.newBuilder() - .setQuery(query) - .setStartIndex(0).setEntriesCount(10) - .setWithExtendedInfo(true) - .build(); - - - AppsCallback appsCb = new AppsCallback() ; - appsCb.setStringBuffer(sb); - - ArrayList commentBeans = new ArrayList(); - - - - session.append(appsRequest, appsCb); - session.flush(); - - String appId = appsCb.getAppId(); - if (appId != null) { + return commentBeans; + } - 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"); - } - sb.append("Comments: " + commentBeans.size() + "\n"); - } - sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes"); - - - return sb.toString(); + @SuppressWarnings("unchecked") + 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( "
" + doLookupWorker(query) + "
" ); + + AppBean app = lookupApp(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); + } } }