/[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 662 by torben, Fri Apr 23 14:13:39 2010 UTC revision 667 by torben, Sun Apr 25 20:53:06 2010 UTC
# Line 6  import java.net.InetSocketAddress; Line 6  import java.net.InetSocketAddress;
6  import java.util.ArrayList;  import java.util.ArrayList;
7  import java.util.Collections;  import java.util.Collections;
8  import java.util.Date;  import java.util.Date;
 import java.util.Formatter;  
9  import java.util.Locale;  import java.util.Locale;
10  import java.util.logging.Logger;  import java.util.logging.Logger;
11    
# Line 18  import javax.servlet.http.HttpServletRes Line 17  import javax.servlet.http.HttpServletRes
17  import net.spy.memcached.MemcachedClient;  import net.spy.memcached.MemcachedClient;
18    
19  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;  
20  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;  
21  import com.gc.android.market.api.model.Market.CommentsRequest;  import com.gc.android.market.api.model.Market.CommentsRequest;
22  import com.gc.android.market.api.model.Market.ResponseContext;  
23    
24    
25  public class ShowStats extends HttpServlet {  public class ShowStats extends HttpServlet {
# Line 34  public class ShowStats extends HttpServl Line 30  public class ShowStats extends HttpServl
30    
31          String login;          String login;
32          String password;          String password;
33            
34            MemcachedClient memcache = null;
35    
36    
37          @Override          @Override
# Line 42  public class ShowStats extends HttpServl Line 40  public class ShowStats extends HttpServl
40    
41                  login = getServletContext().getInitParameter("login");                  login = getServletContext().getInitParameter("login");
42                  password = getServletContext().getInitParameter("password");                  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 {          protected String doLookup(String appId) throws IOException {
52                  MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));                  
53    
54    
55                  String key = "marketstats:" + appId;                  String key = "marketstats:" + appId;
56                  String response = (String) c.get(key);                  String response = (String) memcache.get(key);
57    
58                  if (response == null) {                  if (response == null) {
59                          response = doLookupWorker(appId);                          response = doLookupWorker(appId);
60                          c.set(key, TIMEOUT, response);                          memcache.set(key, TIMEOUT, response);
61                          response += "<!-- new lookup -->";                          response += "<!-- new lookup -->";
62                  } else {                  } else {
63                          response += "<!-- from memcached -->";                          response += "<!-- from memcached -->";
64                  }                  }
65                  return response;                  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) {          protected String doLookupWorker(String appId) {
# Line 76  public class ShowStats extends HttpServl Line 111  public class ShowStats extends HttpServl
111                  .setWithExtendedInfo(true)                  .setWithExtendedInfo(true)
112                  .build();                  .build();
113    
                 CommentsRequest commentsRequest = CommentsRequest.newBuilder()  
                 .setAppId(appId)  
                 .setStartIndex(0)  
                 .setEntriesCount(10)  
                 .build();  
   
114    
115                    AppsCallback appsCb = new AppsCallback() ;
116                    appsCb.setStringBuffer(sb);
                 Callback<AppsResponse> appsCb = new Callback<AppsResponse>() {  
                         @Override  
                         public void onResult(ResponseContext context, AppsResponse response) {  
                                 //System.out.println("Response : " + response);  
                                 //sb.append("Response: " + response + "\n");  
   
                                 Formatter form = new Formatter(sb);  
                                 App app = response.getApp(0);  
                                 sb.append( "<h2>" + app.getTitle() + "</h2>");  
                                 sb.append("Ver: " + app.getVersion() + " (" + app.getVersionCode() + ")\n"  );  
                                 sb.append("Ratingcount: " + app.getRatingsCount() + "\n");  
   
                                 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");  
   
                         }  
                 };  
117    
118                  ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();                  ArrayList<CommentBean> commentBeans = new ArrayList<CommentBean>();
119    
                 CommentCallback commentsCb = new CommentCallback();  
                 commentsCb.setList( commentBeans );  
120    
                 commentsCb.setLocale("en");  
                 session.setLocale( Locale.ENGLISH );  
121    
122                  session.append(appsRequest, appsCb);                  session.append(appsRequest, appsCb);
                 session.append(commentsRequest, commentsCb);  
123                  session.flush();                  session.flush();
124    
125                  commentsCb.setLocale( "da" );                  loadComments(appId, session, "da", commentBeans);
126                  session.setLocale( new Locale("da") );                  loadComments(appId, session, "en", commentBeans);
                 session.append(commentsRequest, commentsCb);  
                 session.flush();  
127    
128                  Collections.sort(commentBeans);                  Collections.sort(commentBeans);
129    
# Line 137  public class ShowStats extends HttpServl Line 136  public class ShowStats extends HttpServl
136                          sb.append("\n");                          sb.append("\n");
137    
138                  }                  }
139                    sb.append("Comments: " + commentBeans.size() );
140    
141    
142                  return sb.toString();                  return sb.toString();

Legend:
Removed from v.662  
changed lines
  Added in v.667

  ViewVC Help
Powered by ViewVC 1.1.20