|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine; |
| 18 | + |
| 19 | +import com.google.appengine.api.datastore.Cursor; |
| 20 | +import com.google.appengine.api.datastore.DatastoreService; |
| 21 | +import com.google.appengine.api.datastore.DatastoreServiceFactory; |
| 22 | +import com.google.appengine.api.datastore.Entity; |
| 23 | +import com.google.appengine.api.datastore.FetchOptions; |
| 24 | +import com.google.appengine.api.datastore.PreparedQuery; |
| 25 | +import com.google.appengine.api.datastore.Query; |
| 26 | +import com.google.appengine.api.datastore.Query.SortDirection; |
| 27 | +import com.google.appengine.api.datastore.QueryResultList; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.io.PrintWriter; |
| 31 | + |
| 32 | +import javax.servlet.ServletException; |
| 33 | +import javax.servlet.http.HttpServlet; |
| 34 | +import javax.servlet.http.HttpServletRequest; |
| 35 | +import javax.servlet.http.HttpServletResponse; |
| 36 | + |
| 37 | +public class StatsServlet extends HttpServlet { |
| 38 | + @Override |
| 39 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 40 | + throws ServletException, IOException { |
| 41 | + // [START stat_example] |
| 42 | + DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); |
| 43 | + Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity(); |
| 44 | + Long totalBytes = (Long) globalStat.getProperty("bytes"); |
| 45 | + Long totalEntities = (Long) globalStat.getProperty("count"); |
| 46 | + // [END stat_example] |
| 47 | + |
| 48 | + resp.setContentType("text/plain"); |
| 49 | + resp.setCharacterEncoding("UTF-8"); |
| 50 | + PrintWriter w = resp.getWriter(); |
| 51 | + w.printf("%d bytes\n%d entities\n", totalBytes, totalEntities); |
| 52 | + } |
| 53 | +} |
| 54 | +// [END cursors] |
0 commit comments