/[projects]/android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java
ViewVC logotype

Diff of /android/MarketStats/src/dk/thoerup/marketstats/ShowStats.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 762 by torben, Thu May 27 11:30:16 2010 UTC revision 1016 by torben, Thu Aug 12 10:01:24 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.marketstats;  package dk.thoerup.marketstats;
2    
3  import java.io.IOException;  import java.io.IOException;
 import java.io.PrintWriter;  
4  import java.net.InetSocketAddress;  import java.net.InetSocketAddress;
5  import java.util.ArrayList;  import java.util.ArrayList;
 import java.util.Date;  
6  import java.util.Locale;  import java.util.Locale;
7  import java.util.concurrent.TimeUnit;  import java.util.concurrent.TimeUnit;
8  import java.util.logging.Logger;  import java.util.logging.Logger;
9    
10  import javax.servlet.ServletException;  import javax.servlet.ServletException;
11    import javax.servlet.annotation.WebServlet;
12  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
13  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
# Line 21  import com.gc.android.market.api.model.M Line 20  import com.gc.android.market.api.model.M
20  import com.gc.android.market.api.model.Market.CommentsRequest;  import com.gc.android.market.api.model.Market.CommentsRequest;
21    
22    
23    @WebServlet(urlPatterns={"/ShowStats"})
24  public class ShowStats extends HttpServlet {  public class ShowStats extends HttpServlet {
25          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
26    
27          final int TIMEOUT = 30*60;          static final int MAXCOMMENTS = 30;
28            static final int APP_TIMEOUT = 30*60;
29            static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4;
30            
31          static final Logger log = Logger.getLogger(ShowStats.class.getName());          static final Logger log = Logger.getLogger(ShowStats.class.getName());
32    
33          String login;          String login;
# Line 56  public class ShowStats extends HttpServl Line 58  public class ShowStats extends HttpServl
58                  memcache = null;                  memcache = null;
59          }          }
60    
61          protected String doLookup(String query) throws IOException {          protected AppBean lookupApp(String query) throws IOException {
62                                    
63    
64                  String key = "marketstats:" + query.replace(' ', '_');                  String key = "marketstats:" + query.replace(' ', '_');
65                  String response = (String) memcache.get(key);                  //String response = (String) memcache.get(key);
66                    AppBean response = (AppBean) memcache.get(key);
67    
68                  if (response == null) {                  if (response == null) {
69                          response = doLookupWorker(query);                          response = lookupAppWorker(query);
70                          memcache.set(key, TIMEOUT, response);                          if (response != null) {
71                          response += "<!-- new lookup -->";                                  memcache.set(key, APP_TIMEOUT, response);
72                  } else {                          }
73                          response += "<!-- from memcached -->";                  }
                 }  
74                  return response;                  return response;
75          }          }
76    
77            protected AppBean lookupAppWorker(String query) {
78    
79                    MarketSession session = new MarketSession();
80                    session.login(login,password);
81    
82    
83                    AppsRequest appsRequest = AppsRequest.newBuilder()
84                    .setQuery(query)
85                    .setStartIndex(0)
86                    .setEntriesCount(1)
87                    .setWithExtendedInfo(true)
88                    .build();
89                    
90                    AppsCallback appsCb = new AppsCallback();
91                    session.append(appsRequest, appsCb);
92                    session.flush();
93                    
94                    return appsCb.getResult().get(0);
95            }      
96            
97            CommentsRequest buildCommentRequest(String appId, int start, int count) {
98                    CommentsRequest commentsRequest = CommentsRequest.newBuilder()
99                    .setAppId(appId)
100                    .setStartIndex(start)
101                    .setEntriesCount(count)
102                    .build();
103                    
104                    return commentsRequest;
105            }
106                    
107          private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {                    private ArrayList<CommentBean> loadComments(String appId) {
108                    
109                    ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
110                    MarketSession session = new MarketSession();
111                    session.login(login,password);
112                    
113                  CommentCallback commentsCb = new CommentCallback();                  CommentCallback commentsCb = new CommentCallback();
114                  commentsCb.setList( commentBeans );                  commentsCb.setList( commentBeans );
115    
# Line 87  public class ShowStats extends HttpServl Line 124  public class ShowStats extends HttpServl
124                                  count = Math.min(10, commentsCb.getEntryCount() );                                  count = Math.min(10, commentsCb.getEntryCount() );
125                                                    
126                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
127                          CommentsRequest commentsRequest = CommentsRequest.newBuilder()                          for (int i=0; i<5;i++) {
128                          .setAppId(appId)                                  CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
129                          .setStartIndex(start)                                  session.append(commentsRequest, commentsCb);
130                          .setEntriesCount(count)                                  start +=10;
131                          .build();                          }
132                                                    
                         session.append(commentsRequest, commentsCb);  
133                          session.flush();                          session.flush();
134                          start +=10;                                                  
135                                                    if (start >= MAXCOMMENTS)
                         if (start >= 200)  
136                                  break; //emergency brake                                  break; //emergency brake
137                                                    
138                  } while ( start < commentsCb.getEntryCount() );                  } 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<CommentBean> commentBeans = new ArrayList<CommentBean>();  
   
   
   
                 session.append(appsRequest, appsCb);  
                 session.flush();  
                   
                 String appId = appsCb.getAppId();  
139                                    
140                  if (appId != null) {                  return commentBeans;
141            }
142    
                         loadComments(appId, session, commentBeans);  
143                    
                         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");  
144                    
145                          }          @SuppressWarnings("unchecked")
146                          sb.append("Comments: " + commentBeans.size() + "\n");          public ArrayList<CommentBean> getComments(AppBean app) {                
147                  }                  String key = "marketstats:comments:" + app.getId();
148                  sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n");                          ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
149    
150                    if (comments == null) {
151                  return sb.toString();                          comments = loadComments(app.getId());
152                            memcache.set(key, COMMENT_TIMEOUT, comments);
153                    }
154                    return comments;
155          }          }
156    
157          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
158                  String query = request.getParameter("query");                  String query = "pname:" + request.getParameter("app");
159                    
160                  response.setContentType("text/html");                  request.setAttribute("extended", request.getParameter("extended"));
161                  PrintWriter out = response.getWriter();                  
162                  out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" );                  AppBean app = lookupApp(query);
163                    if (app != null) {
164                            request.setAttribute("app", app);
165                    
166                            ArrayList<CommentBean> comments = getComments(app);
167                            request.setAttribute("comments", comments);
168                    
169                            getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response);
170                    } else {
171                            response.getWriter().print("No app found with query=" + query);
172                    }
173          }          }
174    
175  }  }

Legend:
Removed from v.762  
changed lines
  Added in v.1016

  ViewVC Help
Powered by ViewVC 1.1.20