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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 664 - (hide annotations) (download)
Fri Apr 23 15:25:52 2010 UTC (14 years, 1 month ago) by torben
File size: 3961 byte(s)
Outcommented debug statements
1 torben 659 package dk.thoerup.marketstats;
2    
3     import java.io.IOException;
4     import java.io.PrintWriter;
5     import java.net.InetSocketAddress;
6 torben 662 import java.util.ArrayList;
7     import java.util.Collections;
8 torben 659 import java.util.Date;
9     import java.util.Locale;
10     import java.util.logging.Logger;
11    
12     import javax.servlet.ServletException;
13     import javax.servlet.http.HttpServlet;
14     import javax.servlet.http.HttpServletRequest;
15     import javax.servlet.http.HttpServletResponse;
16    
17     import net.spy.memcached.MemcachedClient;
18    
19     import com.gc.android.market.api.MarketSession;
20     import com.gc.android.market.api.model.Market.AppsRequest;
21     import com.gc.android.market.api.model.Market.CommentsRequest;
22    
23    
24 torben 664
25 torben 659 public class ShowStats extends HttpServlet {
26     private static final long serialVersionUID = 1L;
27    
28 torben 662 final int TIMEOUT = 30*60;
29 torben 659 static final Logger log = Logger.getLogger(ShowStats.class.getName());
30    
31     String login;
32     String password;
33    
34    
35     @Override
36     public void init() throws ServletException {
37     super.init();
38    
39     login = getServletContext().getInitParameter("login");
40     password = getServletContext().getInitParameter("password");
41     }
42    
43     protected String doLookup(String appId) throws IOException {
44     MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));
45    
46 torben 662
47 torben 659 String key = "marketstats:" + appId;
48     String response = (String) c.get(key);
49    
50     if (response == null) {
51     response = doLookupWorker(appId);
52     c.set(key, TIMEOUT, response);
53     response += "<!-- new lookup -->";
54     } else {
55     response += "<!-- from memcached -->";
56     }
57     return response;
58     }
59 torben 663
60     private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) {
61     CommentCallback commentsCb = new CommentCallback();
62     commentsCb.setList( commentBeans );
63 torben 659
64 torben 663 commentsCb.setLocale( locale );
65     session.setLocale( new Locale(locale) );
66    
67    
68     int start = 0;
69     do {
70     int count = 10;
71     if (start > 0)
72     count = Math.min(10, commentsCb.getEntryCount() );
73    
74 torben 664 //log.warning("count=" + count + " start=" + start + " " + locale);
75 torben 663 CommentsRequest commentsRequest = CommentsRequest.newBuilder()
76     .setAppId(appId)
77     .setStartIndex(start)
78     .setEntriesCount(count)
79     .build();
80    
81     session.append(commentsRequest, commentsCb);
82     session.flush();
83     start +=10;
84    
85     if (start >= 30)
86     break; //emergency brake
87    
88     } while ( start < commentsCb.getEntryCount() );
89     }
90 torben 659
91 torben 663
92 torben 659 protected String doLookupWorker(String appId) {
93     final StringBuilder sb = new StringBuilder();
94    
95    
96     MarketSession session = new MarketSession();
97     session.login(login,password);
98    
99 torben 662
100 torben 659 AppsRequest appsRequest = AppsRequest.newBuilder()
101     .setAppId(appId)
102     .setStartIndex(0).setEntriesCount(10)
103     .setWithExtendedInfo(true)
104     .build();
105    
106    
107 torben 663 AppsCallback appsCb = new AppsCallback() ;
108     appsCb.setStringBuffer(sb);
109 torben 659
110 torben 662 ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
111 torben 659
112    
113    
114     session.append(appsRequest, appsCb);
115     session.flush();
116 torben 662
117 torben 663 loadComments(appId, session, "da", commentBeans);
118     loadComments(appId, session, "en", commentBeans);
119 torben 659
120 torben 662 Collections.sort(commentBeans);
121    
122     sb.append("-----------------------------------------------------------------\n");
123     for (CommentBean c : commentBeans) {
124     sb.append("User: " + c.author + " (" + c.locale + ")\n");
125     sb.append("Rating: " + c.rating + "\n");
126     sb.append("Time: " + new Date(c.time).toString() + "\n");
127     sb.append( c.text + "\n");
128     sb.append("\n");
129    
130     }
131 torben 663 sb.append("Comments: " + commentBeans.size() );
132 torben 662
133    
134 torben 659 return sb.toString();
135     }
136    
137     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
138     String appId = request.getParameter("appId");
139 torben 662
140 torben 660 response.setContentType("text/html");
141 torben 659 PrintWriter out = response.getWriter();
142 torben 660 out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" );
143 torben 659 }
144    
145     }

  ViewVC Help
Powered by ViewVC 1.1.20