/[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 761 by torben, Thu May 27 11:26:05 2010 UTC revision 1205 by torben, Wed Dec 8 22:49:52 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;
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 20  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 47  public class ShowStats extends HttpServl Line 50  public class ShowStats extends HttpServl
50                  }                  }
51          }          }
52    
53          protected String doLookup(String query) throws IOException {          @Override
54            public void destroy() {
55                    super.destroy();
56                    
57                    memcache.shutdown(3, TimeUnit.SECONDS);
58                    memcache = null;
59            }
60    
61            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                    session.getContext().setAndroidId("dead00beef");
82    
83    
84                    AppsRequest appsRequest = AppsRequest.newBuilder()
85                    .setQuery(query)
86                    .setStartIndex(0)
87                    .setEntriesCount(1)
88                    .setWithExtendedInfo(true)
89                    .build();
90                    
91                    AppsCallback appsCb = new AppsCallback();
92                    session.append(appsRequest, appsCb);
93                    session.flush();
94                    
95                    return appsCb.getResult().get(0);
96            }      
97            
98            CommentsRequest buildCommentRequest(String appId, int start, int count) {
99                    CommentsRequest commentsRequest = CommentsRequest.newBuilder()
100                    .setAppId(appId)
101                    .setStartIndex(start)
102                    .setEntriesCount(count)
103                    .build();
104                    
105                    return commentsRequest;
106            }
107                    
108          private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {                    private ArrayList<CommentBean> loadComments(String appId) {
109                    
110                    ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
111                    MarketSession session = new MarketSession();
112                    session.login(login,password);
113                    
114                  CommentCallback commentsCb = new CommentCallback();                  CommentCallback commentsCb = new CommentCallback();
115                  commentsCb.setList( commentBeans );                  commentsCb.setList( commentBeans );
116    
# Line 78  public class ShowStats extends HttpServl Line 125  public class ShowStats extends HttpServl
125                                  count = Math.min(10, commentsCb.getEntryCount() );                                  count = Math.min(10, commentsCb.getEntryCount() );
126                                                    
127                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
128                          CommentsRequest commentsRequest = CommentsRequest.newBuilder()                          for (int i=0; i<5;i++) {
129                          .setAppId(appId)                                  CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
130                          .setStartIndex(start)                                  session.append(commentsRequest, commentsCb);
131                          .setEntriesCount(count)                                  start +=10;
132                          .build();                          }
133                                                    
                         session.append(commentsRequest, commentsCb);  
134                          session.flush();                          session.flush();
135                          start +=10;                                                  
136                                                    if (start >= MAXCOMMENTS)
                         if (start >= 200)  
137                                  break; //emergency brake                                  break; //emergency brake
138                                                    
139                  } 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();  
140                                    
141                  if (appId != null) {                  return commentBeans;
142            }
143    
                         loadComments(appId, session, commentBeans);  
144                    
                         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");  
145                    
146                          }          @SuppressWarnings("unchecked")
147                          sb.append("Comments: " + commentBeans.size() + "\n");          public ArrayList<CommentBean> getComments(AppBean app) {                
148                  }                  String key = "marketstats:comments:" + app.getId();
149                  sb.append("<!--Cache.Timeout=" + TIMEOUT/60 + " minutes-->\n");                          ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
150    
151                    if (comments == null) {
152                  return sb.toString();                          comments = loadComments(app.getId());
153                            memcache.set(key, COMMENT_TIMEOUT, comments);
154                    }
155                    return comments;
156          }          }
157    
158          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
159                  String query = request.getParameter("query");                  String query = "pname:" + request.getParameter("app");
160                    
161                  response.setContentType("text/html");                  request.setAttribute("extended", request.getParameter("extended"));
162                  PrintWriter out = response.getWriter();                  
163                  out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" );                  AppBean app = lookupApp(query);
164                    if (app != null) {
165                            request.setAttribute("app", app);
166                    
167                            ArrayList<CommentBean> comments = getComments(app);
168                            request.setAttribute("comments", comments);
169                    
170                            getServletContext().getRequestDispatcher("/statsview.jsp").forward(request, response);
171                    } else {
172                            response.getWriter().print("No app found with query=" + query);
173                    }
174          }          }
175    
176  }  }

Legend:
Removed from v.761  
changed lines
  Added in v.1205

  ViewVC Help
Powered by ViewVC 1.1.20