--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/04/23 15:28:34 666 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/07/05 08:18:02 950 @@ -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,11 +23,16 @@ 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; String password; + + MemcachedClient memcache = null; @Override @@ -38,31 +41,59 @@ login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); + + try { + memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + } catch (IOException e) { + throw new ServletException(e); + } } - protected String doLookup(String appId) throws IOException { - MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + @Override + public void destroy() { + super.destroy(); + + memcache.shutdown(3, TimeUnit.SECONDS); + memcache = null; + } + protected AppBean doLookup(String query) throws IOException { + - String key = "marketstats:" + appId; - String response = (String) c.get(key); + String key = "marketstats:" + query.replace(' ', '_'); + //String response = (String) memcache.get(key); + AppBean response = (AppBean) memcache.get(key); if (response == null) { - response = doLookupWorker(appId); - c.set(key, TIMEOUT, response); - response += ""; - } else { - response += ""; - } + response = doLookupWorker(query); + if (response != null) { + memcache.set(key, APP_TIMEOUT, response); + } + } return response; } - private void loadComments(String appId, MarketSession session, String locale, 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 ); - commentsCb.setLocale( locale ); - session.setLocale( new Locale(locale) ); + session.setLocale( Locale.ROOT ); + int start = 0; @@ -71,25 +102,28 @@ if (start > 0) count = Math.min(10, commentsCb.getEntryCount() ); - //log.warning("count=" + count + " start=" + start + " " + locale); - CommentsRequest commentsRequest = CommentsRequest.newBuilder() - .setAppId(appId) - .setStartIndex(start) - .setEntriesCount(count) - .build(); - + //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() ); + 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 >= 20) + if (start >= MAXCOMMENTS) break; //emergency brake } while ( start < commentsCb.getEntryCount() ); + + return commentBeans; } + - protected String doLookupWorker(String appId) { + protected AppBean doLookupWorker(String query) { final StringBuilder sb = new StringBuilder(); @@ -98,48 +132,43 @@ AppsRequest appsRequest = AppsRequest.newBuilder() - .setAppId(appId) + .setQuery(query) .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(); - - loadComments(appId, session, "da", commentBeans); - loadComments(appId, session, "en", commentBeans); - - Collections.sort(commentBeans); - - sb.append("-----------------------------------------------------------------\n"); - for (CommentBean c : commentBeans) { - sb.append("User: " + c.author + " (" + c.locale + ")\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() ); - - - return sb.toString(); + + return appsCb.getResult(); + } + + 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 appId = request.getParameter("appId"); - - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(appId) + "
" ); + String query = request.getParameter("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); + } } }