/[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 660 by torben, Fri Apr 23 12:39:31 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.Date;  import java.util.ArrayList;
 import java.util.Formatter;  
 import java.util.List;  
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 17  import javax.servlet.http.HttpServletRes Line 15  import javax.servlet.http.HttpServletRes
15  import net.spy.memcached.MemcachedClient;  import net.spy.memcached.MemcachedClient;
16    
17  import com.gc.android.market.api.MarketSession;  import com.gc.android.market.api.MarketSession;
 import com.gc.android.market.api.MarketSession.Callback;  
 import com.gc.android.market.api.model.Market.App;  
18  import com.gc.android.market.api.model.Market.AppsRequest;  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;  
19  import com.gc.android.market.api.model.Market.CommentsRequest;  import com.gc.android.market.api.model.Market.CommentsRequest;
20  import com.gc.android.market.api.model.Market.CommentsResponse;  
 import com.gc.android.market.api.model.Market.ResponseContext;  
21    
22    
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 = 15*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;
33          String password;          String password;
34                    
35                    MemcachedClient memcache = null;
36    
37    
38          @Override          @Override
# Line 45  public class ShowStats extends HttpServl Line 41  public class ShowStats extends HttpServl
41    
42                  login = getServletContext().getInitParameter("login");                  login = getServletContext().getInitParameter("login");
43                  password = getServletContext().getInitParameter("password");                  password = getServletContext().getInitParameter("password");
44                    
45                    try {
46                            memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
47                    } catch (IOException e) {
48                            throw new ServletException(e);
49                    }
50            }
51    
52            @Override
53            public void destroy() {
54                    super.destroy();
55                    
56                    memcache.shutdown(3, TimeUnit.SECONDS);
57                    memcache = null;
58          }          }
59    
60          protected String doLookup(String appId) throws IOException {          protected AppBean lookupApp(String query) throws IOException {
                 MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));  
61                                    
62    
63                  String key = "marketstats:" + appId;                  String key = "marketstats:" + query.replace(' ', '_');
64                  String response = (String) c.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(appId);                          response = lookupAppWorker(query);
69                          c.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) {
         protected String doLookupWorker(String appId) {  
77                  final StringBuilder sb = new StringBuilder();                  final StringBuilder sb = new StringBuilder();
78    
79    
80                  MarketSession session = new MarketSession();                  MarketSession session = new MarketSession();
81                  session.login(login,password);                  session.login(login,password);
82                  session.setLocale( Locale.ENGLISH );  
83    
84                  AppsRequest appsRequest = AppsRequest.newBuilder()                  AppsRequest appsRequest = AppsRequest.newBuilder()
85                  .setAppId(appId)                  .setQuery(query)
86                  .setStartIndex(0).setEntriesCount(10)                  .setStartIndex(0)
87                    .setEntriesCount(1)
88                  .setWithExtendedInfo(true)                  .setWithExtendedInfo(true)
89                  .build();                  .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()                  CommentsRequest commentsRequest = CommentsRequest.newBuilder()
100                  .setAppId(appId)                  .setAppId(appId)
101                  .setStartIndex(0)                  .setStartIndex(start)
102                  .setEntriesCount(10)                  .setEntriesCount(count)
103                  .build();                  .build();
104                                    
105                    return commentsRequest;
106            }
107            
108            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();
115                    commentsCb.setList( commentBeans );
116    
117                    session.setLocale( Locale.ROOT );
118                    
119                    
120                    
121                    int start = 0;
122                    do {
123                            int count = 10;
124                            if (start > 0)                          
125                                    count = Math.min(10, commentsCb.getEntryCount() );
126                            
127                            //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
128                            CommentsRequest commentsRequest = buildCommentRequest(appId,start,count);                      
129                            session.append(commentsRequest, commentsCb);
130                            start +=10;
131                            
132                            CommentsRequest commentsRequest2 = buildCommentRequest(appId,start,count);                      
133                            session.append(commentsRequest2, commentsCb);
134                            session.flush();
135                            
136                            start +=10;
137                            
138                            if (start >= MAXCOMMENTS)
139                                    break; //emergency brake
140                            
141                    } while ( start < commentsCb.getEntryCount() );
142                    
143                    return commentBeans;
144            }
145    
146            
147                  Callback<AppsResponse> appsCb = new Callback<AppsResponse>() {          
148                          @Override          @SuppressWarnings("unchecked")
149                          public void onResult(ResponseContext context, AppsResponse response) {          public ArrayList<CommentBean> getComments(AppBean app) {                
150                                  //System.out.println("Response : " + response);                  String key = "marketstats:comments:" + app.getId();
151                                  //sb.append("Response: " + response + "\n");                  ArrayList<CommentBean>  comments =  (ArrayList<CommentBean>) memcache.get(key);
152    
153                                  Formatter form = new Formatter(sb);                  if (comments == null) {
154                                  App app = response.getApp(0);                          comments = loadComments(app.getId());
155                                  sb.append( "<h2>" + app.getTitle() + "</h2>");                          memcache.set(key, COMMENT_TIMEOUT, comments);
156                                  sb.append("Ver: " + app.getVersion() + "(" + app.getVersionCode() + ")\n"  );                  }
157                                  sb.append("Ratingcount: " + app.getRatingsCount() + "\n");                  return comments;
   
                                 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<CommentsResponse> commentsCb = new Callback<CommentsResponse>() {  
   
                         @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<Comment> 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();  
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 appId = request.getParameter("appId");                  String query = request.getParameter("query");
162                    
163                    AppBean app = lookupApp(query);
164                    if (app != null) {
165                            request.setAttribute("app", app);
166                                    
167                  response.setContentType("text/html");                          ArrayList<CommentBean> comments = getComments(app);
168                  PrintWriter out = response.getWriter();                          request.setAttribute("comments", comments);
169                  out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" );                  
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.660  
changed lines
  Added in v.952

  ViewVC Help
Powered by ViewVC 1.1.20