--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/04/26 07:57:45 669 +++ android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/08/12 10:01:24 1016 @@ -1,15 +1,14 @@ 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; +import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -21,11 +20,14 @@ import com.gc.android.market.api.model.Market.CommentsRequest; - +@WebServlet(urlPatterns={"/ShowStats"}) 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,28 +50,71 @@ } } - protected String doLookup(String appId) throws IOException { + @Override + public void destroy() { + super.destroy(); + memcache.shutdown(3, TimeUnit.SECONDS); + memcache = null; + } - String key = "marketstats:" + appId; - String response = (String) memcache.get(key); + protected AppBean lookupApp(String query) throws IOException { + + + String key = "marketstats:" + query.replace(' ', '_'); + //String response = (String) memcache.get(key); + AppBean response = (AppBean) memcache.get(key); if (response == null) { - response = doLookupWorker(appId); - 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) { + + 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().get(0); + } - 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; @@ -78,76 +123,53 @@ 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() ); + for (int i=0; i<5;i++) { + CommentsRequest commentsRequest = buildCommentRequest(appId,start,count); + session.append(commentsRequest, commentsCb); + start +=10; + } - session.append(commentsRequest, 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) { - final StringBuilder sb = new StringBuilder(); - - - MarketSession session = new MarketSession(); - session.login(login,password); - - - AppsRequest appsRequest = AppsRequest.newBuilder() - .setAppId(appId) - .setStartIndex(0).setEntriesCount(10) - .setWithExtendedInfo(true) - .build(); - - - AppsCallback appsCb = new AppsCallback() ; - appsCb.setStringBuffer(sb); - - ArrayList commentBeans = new ArrayList(); - - - - 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() + "\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 appId = request.getParameter("appId"); - - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(appId) + "
" ); + String query = "pname:" + request.getParameter("app"); + + request.setAttribute("extended", request.getParameter("extended")); + + 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); + } } }