/[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 952 by torben, Mon Jul 5 08:43:30 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;
# Line 25  import com.gc.android.market.api.model.M Line 23  import com.gc.android.market.api.model.M
23  public class ShowStats extends HttpServlet {  public class ShowStats extends HttpServlet {
24          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
25    
26          final int TIMEOUT = 30*60;          static final int MAXCOMMENTS = 30;
27            static final int APP_TIMEOUT = 30*60;
28            static final int COMMENT_TIMEOUT = APP_TIMEOUT * 4;
29            
30          static final Logger log = Logger.getLogger(ShowStats.class.getName());          static final Logger log = Logger.getLogger(ShowStats.class.getName());
31    
32          String login;          String login;
# Line 48  public class ShowStats extends HttpServl Line 49  public class ShowStats extends HttpServl
49                  }                  }
50          }          }
51    
52          protected String doLookup(String query) throws IOException {          @Override
53            public void destroy() {
54                    super.destroy();
55                    
56                    memcache.shutdown(3, TimeUnit.SECONDS);
57                    memcache = null;
58            }
59    
60            protected AppBean lookupApp(String query) throws IOException {
61                                    
62    
63                  String key = "marketstats:" + query;                  String key = "marketstats:" + query.replace(' ', '_');
64                  String response = (String) memcache.get(key);                  //String response = (String) memcache.get(key);
65                    AppBean response = (AppBean) memcache.get(key);
66    
67                  if (response == null) {                  if (response == null) {
68                          response = doLookupWorker(query);                          response = lookupAppWorker(query);
69                          memcache.set(key, TIMEOUT, response);                          if (response != null) {
70                          response += "<!-- new lookup -->";                                  memcache.set(key, APP_TIMEOUT, response);
71                  } else {                          }
72                          response += "<!-- from memcached -->";                  }
                 }  
73                  return response;                  return response;
74          }          }
75    
76            protected AppBean lookupAppWorker(String query) {
77                    final StringBuilder sb = new StringBuilder();
78    
79    
80                    MarketSession session = new MarketSession();
81                    session.login(login,password);
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();
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 79  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()                          CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
                         .setAppId(appId)  
                         .setStartIndex(start)  
                         .setEntriesCount(count)  
                         .build();  
                           
129                          session.append(commentsRequest, commentsCb);                          session.append(commentsRequest, commentsCb);
130                            start +=10;
131                            
132                            CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);                      
133                            session.append(commentsRequest2, commentsCb);
134                          session.flush();                          session.flush();
135                            
136                          start +=10;                          start +=10;
137                                                    
138                          if (start >= 200)                          if (start >= MAXCOMMENTS)
139                                  break; //emergency brake                                  break; //emergency brake
140                                                    
141                  } 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();  
142                                    
143                  if (appId != null) {                  return commentBeans;
144            }
145    
                         loadComments(appId, session, commentBeans);  
146                    
                         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");  
147                    
148                          }          @SuppressWarnings("unchecked")
149                          sb.append("Comments: " + commentBeans.size() + "\n");          public ArrayList<CommentBean> getComments(AppBean app) {                
150                  }                  String key = "marketstats:comments:" + app.getId();
151                  sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes");                            ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
152    
153                    if (comments == null) {
154                  return sb.toString();                          comments = loadComments(app.getId());
155                            memcache.set(key, COMMENT_TIMEOUT, comments);
156                    }
157                    return comments;
158          }          }
159    
160          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
161                  String query = request.getParameter("query");                  String query = request.getParameter("query");
162                    
163                  response.setContentType("text/html");                  AppBean app = lookupApp(query);
164                  PrintWriter out = response.getWriter();                  if (app != null) {
165                  out.print( "<html><body><pre>" + doLookupWorker(query) + "</pre></body></html>" );                          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.757  
changed lines
  Added in v.952

  ViewVC Help
Powered by ViewVC 1.1.20