--- android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java 2010/04/23 12:39:31 660 +++ 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.Date; -import java.util.Formatter; -import java.util.List; +import java.util.ArrayList; import java.util.Locale; +import java.util.concurrent.TimeUnit; import java.util.logging.Logger; import javax.servlet.ServletException; @@ -17,26 +15,24 @@ import net.spy.memcached.MemcachedClient; import com.gc.android.market.api.MarketSession; -import com.gc.android.market.api.MarketSession.Callback; -import com.gc.android.market.api.model.Market.App; import com.gc.android.market.api.model.Market.AppsRequest; -import com.gc.android.market.api.model.Market.AppsResponse; -import com.gc.android.market.api.model.Market.Comment; import com.gc.android.market.api.model.Market.CommentsRequest; -import com.gc.android.market.api.model.Market.CommentsResponse; -import com.gc.android.market.api.model.Market.ResponseContext; + public class ShowStats extends HttpServlet { private static final long serialVersionUID = 1L; - final int TIMEOUT = 15*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 @@ -45,116 +41,136 @@ login = getServletContext().getInitParameter("login"); password = getServletContext().getInitParameter("password"); + + try { + memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + } catch (IOException e) { + throw new ServletException(e); + } + } + + @Override + public void destroy() { + super.destroy(); + + memcache.shutdown(3, TimeUnit.SECONDS); + memcache = null; } - protected String doLookup(String appId) throws IOException { - MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211)); + protected AppBean lookupApp(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 = lookupAppWorker(query); + if (response != null) { + memcache.set(key, APP_TIMEOUT, response); + } + } return response; } - - protected String doLookupWorker(String appId) { + protected AppBean lookupAppWorker(String query) { final StringBuilder sb = new StringBuilder(); MarketSession session = new MarketSession(); session.login(login,password); - session.setLocale( Locale.ENGLISH ); + AppsRequest appsRequest = AppsRequest.newBuilder() - .setAppId(appId) - .setStartIndex(0).setEntriesCount(10) + .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(0) - .setEntriesCount(10) + .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 ); + session.setLocale( Locale.ROOT ); + + + + int start = 0; + do { + int count = 10; + if (start > 0) + count = Math.min(10, commentsCb.getEntryCount() ); + + //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 >= MAXCOMMENTS) + break; //emergency brake + + } while ( start < commentsCb.getEntryCount() ); + + return commentBeans; + } - - Callback appsCb = new Callback() { - @Override - public void onResult(ResponseContext context, AppsResponse response) { - //System.out.println("Response : " + response); - //sb.append("Response: " + response + "\n"); - - Formatter form = new Formatter(sb); - App app = response.getApp(0); - sb.append( "

" + app.getTitle() + "

"); - sb.append("Ver: " + app.getVersion() + "(" + app.getVersionCode() + ")\n" ); - sb.append("Ratingcount: " + app.getRatingsCount() + "\n"); - - sb.append("Rating: " ); - double rating = Double.parseDouble( app.getRating() ); - form.format("%.4f", rating); - sb.append("\n"); - - sb.append("Downloads: " + app.getExtendedInfo().getDownloadsCountText() + "(" + app.getExtendedInfo().getDownloadsCount() + ")\n" ); - - sb.append("\n"); - - } - }; - - Callback commentsCb = new Callback() { - - @Override - public void onResult(ResponseContext context, CommentsResponse response) { - //System.out.println("Response : " + response); - //sb.append("Response: " + response + "\n"); - sb.append("--------------------------------------------------------------\n"); - sb.append("Total comments: " + response.getEntriesCount() +"\n\n"); - - - List cl = response.getCommentsList(); - for (Comment c : cl) { - sb.append("User: " + c.getAuthorName() + "\n"); - sb.append("Rating: " + c.getRating() + "\n"); - sb.append("Time: " + new Date(c.getCreationTime()).toString() + "\n"); - sb.append( c.getText() + "\n"); - sb.append("\n"); - sb.append(c.getUnknownFields().toString() ); - - } - sb.append( response.getUnknownFields().toString() ); - - - - } - - }; - session.append(appsRequest, appsCb); - session.append(commentsRequest, commentsCb); - session.flush(); - session.setLocale( new Locale("da") ); - session.append(commentsRequest, commentsCb); - session.flush(); - - 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"); + String query = request.getParameter("query"); + + AppBean app = lookupApp(query); + if (app != null) { + request.setAttribute("app", app); - response.setContentType("text/html"); - PrintWriter out = response.getWriter(); - out.print( "
" + doLookup(appId) + "
" ); + 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); + } } }