/[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 666 by torben, Fri Apr 23 15:28:34 2010 UTC revision 760 by torben, Thu May 27 09:45:44 2010 UTC
# Line 4  import java.io.IOException; Line 4  import java.io.IOException;
4  import java.io.PrintWriter;  import java.io.PrintWriter;
5  import java.net.InetSocketAddress;  import java.net.InetSocketAddress;
6  import java.util.ArrayList;  import java.util.ArrayList;
 import java.util.Collections;  
7  import java.util.Date;  import java.util.Date;
8  import java.util.Locale;  import java.util.Locale;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
# Line 30  public class ShowStats extends HttpServl Line 29  public class ShowStats extends HttpServl
29    
30          String login;          String login;
31          String password;          String password;
32            
33            MemcachedClient memcache = null;
34    
35    
36          @Override          @Override
# Line 38  public class ShowStats extends HttpServl Line 39  public class ShowStats extends HttpServl
39    
40                  login = getServletContext().getInitParameter("login");                  login = getServletContext().getInitParameter("login");
41                  password = getServletContext().getInitParameter("password");                  password = getServletContext().getInitParameter("password");
42                    
43                    try {
44                            memcache = new MemcachedClient(new InetSocketAddress("localhost", 11211));
45                    } catch (IOException e) {
46                            throw new ServletException(e);
47                    }
48          }          }
49    
50          protected String doLookup(String appId) throws IOException {          protected String doLookup(String query) throws IOException {
51                  MemcachedClient c = new MemcachedClient(new InetSocketAddress("localhost", 11211));                  
   
52    
53                  String key = "marketstats:" + appId;                  String key = "marketstats:" + query.replace(' ', '_');
54                  String response = (String) c.get(key);                  String response = (String) memcache.get(key);
55    
56                  if (response == null) {                  if (response == null) {
57                          response = doLookupWorker(appId);                          response = doLookupWorker(query);
58                          c.set(key, TIMEOUT, response);                          memcache.set(key, TIMEOUT, response);
59                          response += "<!-- new lookup -->";                          response += "<!-- new lookup -->";
60                  } else {                  } else {
61                          response += "<!-- from memcached -->";                          response += "<!-- from memcached -->";
# Line 57  public class ShowStats extends HttpServl Line 63  public class ShowStats extends HttpServl
63                  return response;                  return response;
64          }          }
65                    
66          private void loadComments(String appId, MarketSession session, String locale, ArrayList<CommentBean> commentBeans) {                      private void loadComments(String appId, MarketSession session, ArrayList<CommentBean> commentBeans) {          
67                  CommentCallback commentsCb = new CommentCallback();                  CommentCallback commentsCb = new CommentCallback();
68                  commentsCb.setList( commentBeans );                  commentsCb.setList( commentBeans );
69    
70                  commentsCb.setLocale( locale );                  session.setLocale( Locale.ROOT );
71                  session.setLocale( new Locale(locale) );                  
72                                    
73                                    
74                  int start = 0;                  int start = 0;
# Line 71  public class ShowStats extends HttpServl Line 77  public class ShowStats extends HttpServl
77                          if (start > 0)                                                    if (start > 0)                          
78                                  count = Math.min(10, commentsCb.getEntryCount() );                                  count = Math.min(10, commentsCb.getEntryCount() );
79                                                    
80                          //log.warning("count=" + count + " start=" + start + " " + locale);                          //log.warning("count=" + count + " start=" + start + " entryCount=" + commentsCb.getEntryCount() );
81                          CommentsRequest commentsRequest = CommentsRequest.newBuilder()                          CommentsRequest commentsRequest = CommentsRequest.newBuilder()
82                          .setAppId(appId)                          .setAppId(appId)
83                          .setStartIndex(start)                          .setStartIndex(start)
# Line 82  public class ShowStats extends HttpServl Line 88  public class ShowStats extends HttpServl
88                          session.flush();                          session.flush();
89                          start +=10;                          start +=10;
90                                                    
91                          if (start >= 20)                          if (start >= 200)
92                                  break; //emergency brake                                  break; //emergency brake
93                                                    
94                  } while ( start < commentsCb.getEntryCount() );                  } while ( start < commentsCb.getEntryCount() );
95          }          }
96    
97    
98          protected String doLookupWorker(String appId) {          protected String doLookupWorker(String query) {
99                  final StringBuilder sb = new StringBuilder();                  final StringBuilder sb = new StringBuilder();
100    
101    
# Line 98  public class ShowStats extends HttpServl Line 104  public class ShowStats extends HttpServl
104    
105    
106                  AppsRequest appsRequest = AppsRequest.newBuilder()                  AppsRequest appsRequest = AppsRequest.newBuilder()
107                  .setAppId(appId)                  .setQuery(query)
108                  .setStartIndex(0).setEntriesCount(10)                  .setStartIndex(0).setEntriesCount(10)
109                  .setWithExtendedInfo(true)                  .setWithExtendedInfo(true)
110                  .build();                  .build();
# Line 113  public class ShowStats extends HttpServl Line 119  public class ShowStats extends HttpServl
119    
120                  session.append(appsRequest, appsCb);                  session.append(appsRequest, appsCb);
121                  session.flush();                  session.flush();
122                    
123                    String appId = appsCb.getAppId();
124                    
125                    if (appId != null) {
126    
127                  loadComments(appId, session, "da", commentBeans);                          loadComments(appId, session, commentBeans);
128                  loadComments(appId, session, "en", commentBeans);          
129                            sb.append("-----------------------------------------------------------------\n");
130                  Collections.sort(commentBeans);                          for (CommentBean c : commentBeans) {
131                                    sb.append("User: " + c.author + "\n");
132                  sb.append("-----------------------------------------------------------------\n");                                  sb.append("Rating: " + c.rating + "\n");
133                  for (CommentBean c : commentBeans) {                                  sb.append("Time: " + new Date(c.time).toString() + "\n");
134                          sb.append("User: " + c.author + " ("  + c.locale + ")\n");                                  sb.append( c.text + "\n");
135                          sb.append("Rating: " + c.rating + "\n");                                  sb.append("\n");
136                          sb.append("Time: " + new Date(c.time).toString() + "\n");          
137                          sb.append( c.text + "\n");                          }
138                          sb.append("\n");                          sb.append("Comments: " + commentBeans.size() + "\n");
   
139                  }                  }
140                  sb.append("Comments: " + commentBeans.size() );                  sb.append("Cache.Timeout=" + TIMEOUT/60 + " minutes");          
141    
142    
143                  return sb.toString();                  return sb.toString();
144          }          }
145    
146          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
147                  String appId = request.getParameter("appId");                  String query = request.getParameter("query");
148    
149                  response.setContentType("text/html");                  response.setContentType("text/html");
150                  PrintWriter out = response.getWriter();                  PrintWriter out = response.getWriter();
151                  out.print( "<html><body><pre>" + doLookup(appId) + "</pre></body></html>" );                  out.print( "<html><body><pre>" + doLookup(query) + "</pre></body></html>" );
152          }          }
153    
154  }  }

Legend:
Removed from v.666  
changed lines
  Added in v.760

  ViewVC Help
Powered by ViewVC 1.1.20