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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20