You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by ps...@apache.org on 2008/03/10 04:08:49 UTC

svn commit: r635423 - in /labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web: ./ json/

Author: psmith
Date: Sun Mar  9 20:08:47 2008
New Revision: 635423

URL: http://svn.apache.org/viewvc?rev=635423&view=rev
Log:
Added search timings to the JSON result sent back after a search.

Replaced the old Event Context View controller with a JSON one.

Added Event Detail JSON controller which can provide a set of full detail events for a given page.
To get this, I added a Spring Factory bean that configures the JsonConfig needed to encode specific objects, since
the default JSON serialization form for LoggingEvent and PinpointContext isn't that useful (still some more property
data to expose here, but the majority is there).
`


Added:
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointEventContextViewRequest.java
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/JSONUtils.java
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONConfigFactory.java
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONSearchResultStreamer.java
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointSearchResultPaginationRequest.java
Modified:
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchContextController.java
    labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchResultContextViewController.java

Added: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointEventContextViewRequest.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointEventContextViewRequest.java?rev=635423&view=auto
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointEventContextViewRequest.java (added)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointEventContextViewRequest.java Sun Mar  9 20:08:47 2008
@@ -0,0 +1,44 @@
+package org.apache.logging.pinpoint.web;
+
+/**
+ * Represents a request to retrieve the Context View data for a given Event specified
+ * by an eventKey linked to a specific Context, including any Context view parameters (same thread, all threads etc.).
+ * 
+ * 
+ * @author psmith
+ *
+ */
+public class PinpointEventContextViewRequest {
+
+    private String contextName;
+    private String eventKey;
+    private ThreadView threadView = ThreadView.SAME_THREAD;
+
+    public static enum ThreadView {
+        SAME_THREAD, ALL_THREADS;
+    }
+
+    public final String getContextName() {
+        return contextName;
+    }
+
+    public final void setContextName(String contextName) {
+        this.contextName = contextName;
+    }
+
+    public final String getEventKey() {
+        return eventKey;
+    }
+
+    public final void setEventKey(String eventKey) {
+        this.eventKey = eventKey;
+    }
+
+    public final ThreadView getThreadView() {
+        return threadView;
+    }
+
+    public final void setThreadView(ThreadView threadView) {
+        this.threadView = threadView;
+    }
+}

Modified: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchContextController.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchContextController.java?rev=635423&r1=635422&r2=635423&view=diff
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchContextController.java (original)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchContextController.java Sun Mar  9 20:08:47 2008
@@ -11,6 +11,8 @@
 import org.apache.logging.pinpoint.search.PinpointSearcher;
 import org.apache.logging.pinpoint.search.PinpointSearcherCache;
 import org.apache.lucene.search.Query;
+import org.joda.time.Period;
+import org.joda.time.format.PeriodFormat;
 import org.springframework.validation.BindException;
 import org.springframework.web.servlet.ModelAndView;
 import org.springframework.web.servlet.mvc.AbstractCommandController;
@@ -75,8 +77,11 @@
         PinpointSearcher pinpointSearcher = getSearcherCache().getSearcher(
                 context);
 
+        long begin = System.currentTimeMillis();
         PinpointSearchResults searchResults = pinpointSearcher.search(query);
+        long end = System.currentTimeMillis();
 
+        Period period = new Period(begin, end);
         request.getSession().setAttribute(searchResults.getResultKey(),
                 searchResults);
 
@@ -87,9 +92,8 @@
          */
         modelAndView.addObject("searchKey", searchResults.getResultKey());
         modelAndView.addObject("numResults", searchResults.size());
-        //        TODO search timing?
-
-        modelAndView.addObject("searchModel", searchModel);
+        modelAndView.addObject("time", PeriodFormat.getDefault().print(period));
+        //        modelAndView.addObject("searchModel", searchModel);
         
         return modelAndView;
     }

Modified: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchResultContextViewController.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchResultContextViewController.java?rev=635423&r1=635422&r2=635423&view=diff
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchResultContextViewController.java (original)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/PinpointSearchResultContextViewController.java Sun Mar  9 20:08:47 2008
@@ -1,69 +1,70 @@
 package org.apache.logging.pinpoint.web;
 
-import java.io.IOException;
+import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang.math.NumberUtils;
-import org.apache.log4j.spi.LoggingEvent;
-import org.apache.logging.pinpoint.PinpointContext;
-import org.apache.logging.pinpoint.search.LocalConnectionFactory;
 import org.apache.logging.pinpoint.search.PinpointConnection;
-import org.apache.logging.pinpoint.search.PinpointSearchResults;
+import org.apache.logging.pinpoint.search.PinpointConnectionFactory;
 import org.apache.logging.pinpoint.search.PinpointSearchUtils;
-import org.apache.logging.pinpoint.search.PinpointSearcherCache;
-
+import org.apache.logging.pinpoint.web.PinpointEventContextViewRequest.ThreadView;
+import org.springframework.validation.BindException;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.mvc.AbstractCommandController;
+
+/**
+ * Given a request that identifies the Context and individual Event of interest, returns a stream
+ * of LoggingEvent details in JSON format that form the Context view, which may be restricted
+ * to the Thread that generated the event, or all events around a specified time range.
+ * 
+ * @author psmith
+ *
+ */
 public class PinpointSearchResultContextViewController extends
-        AbstractPinpointResultController {
+        AbstractCommandController {
+
 
-    public static final String RESULT_ON_PAGE = "resultIndexOnPage";
+    public PinpointSearchResultContextViewController() {
+        super(PinpointEventContextViewRequest.class);
+    }
 
-    private PinpointSearcherCache searcherCache;
-    private LocalConnectionFactory connectionFactory;
+    private PinpointConnectionFactory connectionFactory;
 
-    public final LocalConnectionFactory getConnectionFactory() {
+    public final PinpointConnectionFactory getConnectionFactory() {
         return connectionFactory;
     }
 
     public final void setConnectionFactory(
-            LocalConnectionFactory connectionFactory) {
+            PinpointConnectionFactory connectionFactory) {
         this.connectionFactory = connectionFactory;
     }
 
-    public final PinpointSearcherCache getSearcherCache() {
-        return searcherCache;
-    }
-
-    public final void setSearcherCache(PinpointSearcherCache searcherCache) {
-        this.searcherCache = searcherCache;
-    }
-
-    public void showInContext(HttpServletRequest req, HttpServletResponse res)
+    @Override
+    protected ModelAndView handle(HttpServletRequest request,
+            HttpServletResponse response, Object command, BindException errors)
             throws Exception {
-        int contextResultIndex = NumberUtils.toInt(req
-                .getParameter(RESULT_ON_PAGE));
-
-        PinpointSearchResults results = getResults(req);
-
-        PinpointContext context = results.getContext();
-        PinpointConnection connection = connectionFactory.connect(context);
+        PinpointEventContextViewRequest contextRequest = (PinpointEventContextViewRequest) command;
 
+        PinpointConnection connection = connectionFactory
+                .connect(contextRequest.getContextName());
         try {
-            PinpointSearchUtils utils = new PinpointSearchUtils(
-                    connectionFactory.connect(context));
-
-            Iterable<LoggingEvent> eventsInContext = connection.details(utils
-                    .showEventInContext(results.getPageResults().get(
-                            contextResultIndex), true));
+            ModelAndView modelAndView = new ModelAndView("jsonView");
+            PinpointSearchUtils searchUtils = new PinpointSearchUtils(
+                    connection);
+            boolean showAllThreads = contextRequest
+                    .getThreadView() == ThreadView.ALL_THREADS;
+            List<String> eventInContext = searchUtils.showEventInContext(
+                    contextRequest.getEventKey(), showAllThreads);
+            modelAndView.addObject("contextEvents", connection
+                    .details(eventInContext));
+            return modelAndView;
 
         } finally {
-            try {
-                connection.close();
-            } catch (IOException e) {
-                throw new RuntimeException(
-                        "Failed to close EventStore cleanly", e);
-            }
+            connection.close();
         }
     }
+
+
+    
 }

Added: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/JSONUtils.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/JSONUtils.java?rev=635423&view=auto
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/JSONUtils.java (added)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/JSONUtils.java Sun Mar  9 20:08:47 2008
@@ -0,0 +1,86 @@
+package org.apache.logging.pinpoint.web.json;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import net.sf.json.JSONObject;
+
+import org.apache.commons.beanutils.PropertyUtils;
+
+public class JSONUtils {
+
+    /**
+     * Creates a Collection of JSONObject based on the property name & value of 
+     * the specified Object collection.
+     * 
+     * In some cases when you don't want to use the standard JSon-lib serialization protocol, this might be required.
+     * 
+     * For example, if you just want to expose a List of String values sourced from a single property of a given Object
+     * class, then you can pass a list of those Objects, and the name of the property, plus the output name of the
+     * attribute name (in JSON name-spaces).
+     * @param jsonPropertyName
+     * @param objects
+     * @param propertyName
+     * @return
+     * @throws IllegalAccessException
+     * @throws InvocationTargetException
+     * @throws NoSuchMethodException
+     */
+    public Collection<JSONObject> wrapStringsForJSON(String jsonPropertyName,
+            Collection<?> objects, String propertyName)
+            throws IllegalAccessException, InvocationTargetException,
+            NoSuchMethodException {
+
+        //        TODO assert that the object class has the property name
+        
+        
+        Collection<JSONObject> jsonObjects = new ArrayList<JSONObject>(objects
+                .size());
+        for (Object object : objects) {
+            Object property = PropertyUtils.getProperty(object, propertyName);
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put(jsonPropertyName, property);
+            jsonObjects.add(jsonObject);
+        }
+        return jsonObjects;
+
+    }
+    
+    /**
+     * Helper method that uses the propertyName of the source value you want to use as the JSON output attribute name.
+     * @param objects
+     * @param propertyName
+     * @return
+     * @throws IllegalAccessException
+     * @throws InvocationTargetException
+     * @throws NoSuchMethodException
+     */
+    public Collection<JSONObject> wrapStringsForJSON(Collection<?> objects,
+            String propertyName) throws IllegalAccessException,
+            InvocationTargetException, NoSuchMethodException {
+        return wrapStringsForJSON(propertyName, objects, propertyName);
+    }
+
+    /**
+     * Wraps a Collection of Strings into a Collection of JSONObjects with a specified
+     * JSON attribute name. 
+     * @param jsonPropertyName
+     * @param strings
+     * @return 
+     * @return
+     */
+    public Collection<JSONObject> wrapStrings(String jsonPropertyName,
+            Collection<String> strings) {
+        Collection<JSONObject> jsonObjects = new ArrayList<JSONObject>(strings
+                .size());
+        for (String string : strings) {
+
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put(jsonPropertyName, string);
+            jsonObjects.add(jsonObject);
+        }
+        return jsonObjects;
+
+    }
+}

Added: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONConfigFactory.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONConfigFactory.java?rev=635423&view=auto
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONConfigFactory.java (added)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONConfigFactory.java Sun Mar  9 20:08:47 2008
@@ -0,0 +1,63 @@
+package org.apache.logging.pinpoint.web.json;
+
+import java.text.DateFormat;
+import java.util.Date;
+
+import net.sf.json.JSONObject;
+import net.sf.json.JsonConfig;
+import net.sf.json.processors.JsonBeanProcessor;
+
+import org.apache.log4j.helpers.ISO8601DateFormat;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.logging.pinpoint.PinpointContext;
+import org.springframework.beans.factory.FactoryBean;
+
+public class PinpointJSONConfigFactory implements FactoryBean {
+
+    private DateFormat dtf = ISO8601DateFormat.getDateTimeInstance();
+    
+    public Object getObject() throws Exception {
+        JsonConfig config = new JsonConfig();
+        config.registerJsonBeanProcessor(PinpointContext.class,
+                new JsonBeanProcessor() {
+
+                    public JSONObject processBean(Object o, JsonConfig config) {
+                        PinpointContext ctx = (PinpointContext) o;
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.put("contextName", ctx.getContextName());
+                        return jsonObject;
+                    }
+                });
+        
+        config.registerJsonBeanProcessor(LoggingEvent.class,
+                new JsonBeanProcessor() {
+
+                    public JSONObject processBean(Object o, JsonConfig config) {
+                        LoggingEvent e = (LoggingEvent) o;
+                        JSONObject jsonObject = new JSONObject();
+                        jsonObject.element("logger", e.getLoggerName())
+                        .element("timeStamp",
+                                        dtf.format(new Date(e.getTimeStamp())))
+                                .element("level", e.getLevel().toString())
+                                .element("message", e.getRenderedMessage())
+                                .accumulateAll(
+                                        e.getProperties())
+                                        ;
+                        //                        TODO more event properties 
+                        return jsonObject;
+                    }
+                });
+        return config;
+    }
+
+    public Class<?> getObjectType() {
+        return JsonConfig.class;
+    }
+
+    public boolean isSingleton() {
+        return false;
+    }
+
+
+
+}

Added: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONSearchResultStreamer.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONSearchResultStreamer.java?rev=635423&view=auto
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONSearchResultStreamer.java (added)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointJSONSearchResultStreamer.java Sun Mar  9 20:08:47 2008
@@ -0,0 +1,69 @@
+package org.apache.logging.pinpoint.web.json;
+
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.logging.pinpoint.search.PinpointConnection;
+import org.apache.logging.pinpoint.search.PinpointConnectionFactory;
+import org.apache.logging.pinpoint.search.PinpointSearchResults;
+import org.springframework.validation.BindException;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.mvc.AbstractCommandController;
+
+/**
+ * Returns a JSON stream of {@link LoggingEvent} instances based on the previously executed
+ * search (represented by a searchKey, which should have been stored in the users Session).
+ * @author psmith
+ *
+ */
+public class PinpointJSONSearchResultStreamer extends AbstractCommandController {
+
+    private PinpointConnectionFactory pinpointConnectionFactory;
+
+    public final PinpointConnectionFactory getPinpointConnectionFactory() {
+        return pinpointConnectionFactory;
+    }
+
+    public final void setPinpointConnectionFactory(
+            PinpointConnectionFactory pinpointConnectionFactory) {
+        this.pinpointConnectionFactory = pinpointConnectionFactory;
+    }
+
+    public PinpointJSONSearchResultStreamer() {
+        super(PinpointSearchResultPaginationRequest.class);
+    }
+
+    @Override
+    protected ModelAndView handle(HttpServletRequest request,
+            HttpServletResponse response, Object command, BindException errors)
+            throws Exception {
+
+        PinpointSearchResultPaginationRequest paginationRequest = (PinpointSearchResultPaginationRequest) command;
+        ModelAndView modelAndView = new ModelAndView("jsonView");
+
+        PinpointSearchResults results = (PinpointSearchResults) request
+                .getSession().getAttribute(paginationRequest.getSearchKey());
+
+        //        TODO risk of thread-safety here.  might be better to add an explicit method on the PinpointSearchResult object to retrieve the subList
+        results.setCurrentPage(paginationRequest.getPageNumber());
+        List<String> pageResults = results.getPageResults();
+
+        //        TODO probably need a Connection cache here as well.  Should the PinpointSearcherCache be replaced with a Connection oriented one?
+        PinpointConnection connection = pinpointConnectionFactory
+                .connect(results.getContext().getContextName());
+
+        try {
+            List<LoggingEvent> details = connection.details(pageResults);
+            modelAndView.addObject("pageResults", details);
+
+        } finally {
+            connection.close();
+        }
+
+        return modelAndView;
+    }
+
+}

Added: labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointSearchResultPaginationRequest.java
URL: http://svn.apache.org/viewvc/labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointSearchResultPaginationRequest.java?rev=635423&view=auto
==============================================================================
--- labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointSearchResultPaginationRequest.java (added)
+++ labs/pinpoint/trunk/pinpoint-web/src/main/java/org/apache/logging/pinpoint/web/json/PinpointSearchResultPaginationRequest.java Sun Mar  9 20:08:47 2008
@@ -0,0 +1,25 @@
+package org.apache.logging.pinpoint.web.json;
+
+public class PinpointSearchResultPaginationRequest {
+
+    private String searchKey;
+    private int pageNumber;
+
+    public final String getSearchKey() {
+        return searchKey;
+    }
+
+    public final void setSearchKey(String searchKey) {
+        this.searchKey = searchKey;
+    }
+
+    public final int getPageNumber() {
+        return pageNumber;
+    }
+
+    public final void setPageNumber(int pageNumber) {
+        this.pageNumber = pageNumber;
+    }
+    
+    
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org