001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.devutils.inspector;
018
019import java.text.SimpleDateFormat;
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.List;
023
024import org.apache.wicket.Application;
025import org.apache.wicket.devutils.DevUtilsPage;
026import org.apache.wicket.markup.html.basic.Label;
027import org.apache.wicket.markup.html.image.NonCachingImage;
028import org.apache.wicket.markup.html.link.Link;
029import org.apache.wicket.markup.html.list.ListItem;
030import org.apache.wicket.markup.html.list.PageableListView;
031import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
032import org.apache.wicket.model.IModel;
033import org.apache.wicket.model.Model;
034import org.apache.wicket.protocol.http.IRequestLogger;
035import org.apache.wicket.protocol.http.IRequestLogger.SessionData;
036import org.apache.wicket.protocol.http.RequestLogger;
037import org.apache.wicket.protocol.http.WebApplication;
038import org.apache.wicket.util.lang.Bytes;
039
040/**
041 * @author jcompagner
042 */
043public class LiveSessionsPage extends DevUtilsPage
044{
045        private static final long serialVersionUID = 1L;
046
047        /**
048         * Construct.
049         */
050        public LiveSessionsPage()
051        {
052                add(new NonCachingImage("bug"));
053
054                add(new ApplicationView("application", Application.get()));
055
056                Link<Void> link = new Link<Void>("togglelink")
057                {
058                        private static final long serialVersionUID = 1L;
059
060                        @Override
061                        public void onClick()
062                        {
063                                WebApplication webApplication = (WebApplication)Application.get();
064                                webApplication.getRequestLoggerSettings().setRequestsWindowSize(500);
065                                boolean enabled = webApplication.getRequestLoggerSettings()
066                                        .isRequestLoggerEnabled();
067                                webApplication.getRequestLoggerSettings().setRequestLoggerEnabled(!enabled);
068                        }
069                };
070                link.add(new Label("toggletext", new Model<String>()
071                {
072                        private static final long serialVersionUID = 1L;
073
074                        @Override
075                        public String getObject()
076                        {
077                                WebApplication webApplication = (WebApplication)Application.get();
078                                IRequestLogger requestLogger = webApplication.getRequestLogger();
079                                if (requestLogger == null)
080                                {
081                                        return "Enable request recording";
082                                }
083                                else
084                                {
085                                        return "Disable request recording";
086                                }
087                        }
088                }));
089                add(link);
090                add(new Label("totalSessions", new Model<Integer>()
091                {
092                        private static final long serialVersionUID = 1L;
093
094                        @Override
095                        public Integer getObject()
096                        {
097                                return getRequestLogger().getTotalCreatedSessions();
098                        }
099                }));
100                add(new Label("peakSessions", new Model<Integer>()
101                {
102                        private static final long serialVersionUID = 1L;
103
104                        @Override
105                        public Integer getObject()
106                        {
107                                return getRequestLogger().getPeakSessions();
108                        }
109                }));
110                add(new Label("liveSessions", new Model<Integer>()
111                {
112                        private static final long serialVersionUID = 1L;
113
114                        @Override
115                        public Integer getObject()
116                        {
117                                return getRequestLogger().getPeakSessions();
118                        }
119                }));
120
121                IModel<List<SessionData>> sessionModel = new IModel<List<SessionData>>()
122                {
123                        private static final long serialVersionUID = 1L;
124
125                        @Override
126                        public List<SessionData> getObject()
127                        {
128                                return new ArrayList<>(
129                                        Arrays.asList(getRequestLogger().getLiveSessions()));
130                        }
131                };
132                PageableListView<SessionData> listView = new PageableListView<SessionData>("sessions",
133                        sessionModel, 50)
134                {
135                        private static final long serialVersionUID = 1L;
136
137                        private final SimpleDateFormat sdf = new SimpleDateFormat("dd MMM hh:mm:ss.SSS");
138
139                        @Override
140                        protected void populateItem(final ListItem<SessionData> item)
141                        {
142                                final SessionData sd = item.getModelObject();
143                                Link<Void> link = new Link<Void>("id")
144                                {
145                                        private static final long serialVersionUID = 1L;
146
147                                        /**
148                                         * @see org.apache.wicket.markup.html.link.Link#onClick()
149                                         */
150                                        @Override
151                                        public void onClick()
152                                        {
153                                                setResponsePage(new RequestsPage(sd));
154                                        }
155                                };
156                                link.add(new Label("id", new Model<>(sd.getSessionId())));
157                                item.add(link);
158                                item.add(new Label("lastRequestTime", new Model<>(sdf.format(sd.getLastActive()))));
159                                item.add(new Label("requestCount", new Model<>(sd.getNumberOfRequests())));
160                                item.add(new Label("requestsTime", new Model<>(sd.getTotalTimeTaken())));
161                                item.add(new Label("sessionSize",
162                                                () -> sd.getSessionSize() >= 0 ? Bytes.bytes(sd.getSessionSize()) : "-"));
163                        }
164                };
165                add(listView);
166
167                PagingNavigator navigator = new PagingNavigator("navigator", listView);
168                add(navigator);
169        }
170
171        IRequestLogger getRequestLogger()
172        {
173                WebApplication webApplication = (WebApplication)Application.get();
174                IRequestLogger requestLogger = webApplication.getRequestLogger();
175
176                if (requestLogger == null)
177                        requestLogger = new RequestLogger();
178                return requestLogger;
179        }
180}