/[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 757 by torben, Thu May 27 08:51:59 2010 UTC revision 957 by torben, Mon Jul 5 09:28:40 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.Collections;  
 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 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 48  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                  String key = "marketstats:" + query;          protected AppBean lookupApp(String query) throws IOException {
62                  String response = (String) memcache.get(key);                  
63    
64                    String key = "marketstats:" + query.replace(' ', '_');
65                    //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();
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 79  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()                          CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
                         .setAppId(appId)  
                         .setStartIndex(start)  
                         .setEntriesCount(count)  
                         .build();  
                           
128                          session.append(commentsRequest, commentsCb);                          session.append(commentsRequest, commentsCb);
129                            start +=10;
130                            
131                            CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);                      
132                            session.append(commentsRequest2, commentsCb);
133                          session.flush();                          session.flush();
134                            
135                          start +=10;                          start +=10;
136                                                    
137                          if (start >= 200)                          if (start >= MAXCOMMENTS)
138                                  break; //emergency brake                                  break; //emergency brake
139                                                    
140                  } 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();  
141                                    
142                  String appId = appsCb.getAppId();                  return commentBeans;
143                            }
                 if (appId != null) {  
144    
                         loadComments(appId, session, commentBeans);  
145                    
                         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");  
146                    
147                          }          @SuppressWarnings("unchecked")
148                          sb.append("Comments: " + commentBeans.size() + "\n");          public ArrayList<CommentBean> getComments(AppBean app) {                
149                  }                  String key = "marketstats:comments:" + app.getId();
150                  sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes");                            ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
151    
152                    if (comments == null) {
153                  return sb.toString();                          comments = loadComments(app.getId());
154                            memcache.set(key, COMMENT_TIMEOUT, comments);
155                    }
156                    return comments;
157          }          }
158    
159          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
160                  String query = request.getParameter("query");                  String query = request.getParameter("query");
161                    
162                  response.setContentType("text/html");                  AppBean app = lookupApp(query);
163                  PrintWriter out = response.getWriter();                  if (app != null) {
164                  out.print( "<html><body><pre>" + doLookupWorker(query) + "</pre></body></html>" );                          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.757  
changed lines
  Added in v.957

  ViewVC Help
Powered by ViewVC 1.1.20