Project

General

Profile

Download (14.6 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2015 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.taxeditor.view.sessions;
10

    
11
import java.text.DecimalFormat;
12
import java.util.Collection;
13

    
14
import javax.annotation.PostConstruct;
15
import javax.inject.Inject;
16

    
17
import org.eclipse.core.commands.Command;
18
import org.eclipse.core.commands.ParameterizedCommand;
19
import org.eclipse.e4.core.commands.ECommandService;
20
import org.eclipse.e4.core.commands.EHandlerService;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.events.SelectionAdapter;
23
import org.eclipse.swt.events.SelectionEvent;
24
import org.eclipse.swt.layout.GridData;
25
import org.eclipse.swt.layout.GridLayout;
26
import org.eclipse.swt.widgets.Button;
27
import org.eclipse.swt.widgets.Composite;
28
import org.eclipse.swt.widgets.Label;
29
import org.eclipse.swt.widgets.Table;
30
import org.eclipse.swt.widgets.TableColumn;
31
import org.eclipse.swt.widgets.TableItem;
32
import org.eclipse.swt.widgets.Text;
33
import org.eclipse.wb.swt.SWTResourceManager;
34

    
35
import eu.etaxonomy.cdm.api.cache.CdmPermanentCacheBase;
36
import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
37
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
38
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
39
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
40
import eu.etaxonomy.taxeditor.session.NullSession;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42
import net.sf.ehcache.Cache;
43
import net.sf.ehcache.CacheManager;
44
import net.sf.ehcache.statistics.FlatStatistics;
45

    
46
/**
47
 * @author cmathew
48
 * @date 16 Feb 2015
49
 */
50
public class SessionsViewPart implements ICdmEntitySessionManagerObserver {
51

    
52
    public static final String ID = "eu.etaxonomy.taxeditor.view.sessions.SessionsViewPart"; //$NON-NLS-1$
53
    public static final String OPEN_INSPECT_SESSIONS_DIALOG_ID =  "eu.etaxonomy.taxeditor.store.open.InspectSessionsDialog";
54
    private Table tableSessions;
55

    
56
    private final String[] titles = { "Owner", "In Memory", "On Disk"};
57

    
58
    private final ICdmEntitySessionManager cdmEntitySessionManager;
59
    private Text txtNoOfSessions;
60
    private Text txtNoOfCaches;
61
    private Text txtPermanentInMemory;
62
    private Text txtCdmModelInMemory;
63
    private Text txtCdmModelOnDisk;
64
    private Text txtPermanentOnDisk;
65

    
66
    @Inject
67
    private ECommandService commandService;
68

    
69
    @Inject
70
    private EHandlerService handlerService;
71

    
72
    @Inject
73
    public SessionsViewPart() {
74
        this.cdmEntitySessionManager = CdmStore.getCurrentSessionManager();
75
        if(cdmEntitySessionManager != null) {
76
            cdmEntitySessionManager.addSessionObserver(this);
77
        }
78
    }
79

    
80
    /**
81
     * Create contents of the view part.
82
     * @param parent
83
     */
84
    @SuppressWarnings("unused")
85
    @PostConstruct
86
    public void createPartControl(Composite parent) {
87
        Composite container = new Composite(parent, SWT.NONE);
88
        container.setLayout(new GridLayout(5, false));
89
        {
90
            Label lblNoOfSessions = new Label(container, SWT.NONE);
91
            lblNoOfSessions.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
92
            lblNoOfSessions.setAlignment(SWT.RIGHT);
93
            GridData gd_lblNoOfSessions = new GridData(SWT.RIGHT, SWT.FILL, false, false, 1, 1);
94
            gd_lblNoOfSessions.widthHint = 115;
95
            gd_lblNoOfSessions.minimumHeight = 30;
96
            lblNoOfSessions.setLayoutData(gd_lblNoOfSessions);
97
            lblNoOfSessions.setText("No. of Sessions : ");
98
        }
99
        {
100
            txtNoOfSessions = new Text(container, SWT.BORDER);
101
            txtNoOfSessions.setEditable(false);
102
            txtNoOfSessions.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
103
            GridData gd_txtNoOfSessions = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
104
            gd_txtNoOfSessions.widthHint = 50;
105
            txtNoOfSessions.setLayoutData(gd_txtNoOfSessions);
106
        }
107
        new Label(container, SWT.NONE);
108
        new Label(container, SWT.NONE);
109
        new Label(container, SWT.NONE);
110
        {
111
            Label lblNoOfCaches = new Label(container, SWT.CENTER);
112
            lblNoOfCaches.setAlignment(SWT.RIGHT);
113
            lblNoOfCaches.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
114
            GridData gd_lblNoOfCaches = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
115
            gd_lblNoOfCaches.widthHint = 150;
116
            lblNoOfCaches.setLayoutData(gd_lblNoOfCaches);
117
            lblNoOfCaches.setText("No. of entity caches : ");
118
        }
119
        {
120
            txtNoOfCaches = new Text(container, SWT.BORDER);
121
            txtNoOfCaches.setEditable(false);
122
            GridData gd_txtNoOfCaches = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
123
            gd_txtNoOfCaches.widthHint = 50;
124
            txtNoOfCaches.setLayoutData(gd_txtNoOfCaches);
125
        }
126
        new Label(container, SWT.NONE);
127
        new Label(container, SWT.NONE);
128
        new Label(container, SWT.NONE);
129

    
130
        {
131
            Label lblPermanentCache = new Label(container, SWT.CENTER);
132
            lblPermanentCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
133
            lblPermanentCache.setText("Permanent Cache, ");
134
            lblPermanentCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
135
            lblPermanentCache.setAlignment(SWT.RIGHT);
136
        }
137
        {
138
            Label lblPermanentInMemory = new Label(container, SWT.CENTER);
139
            lblPermanentInMemory.setText("in Memory : ");
140
            lblPermanentInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
141
            lblPermanentInMemory.setAlignment(SWT.RIGHT);
142
            lblPermanentInMemory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
143
        }
144
        {
145
            txtPermanentInMemory = new Text(container, SWT.BORDER);
146
            txtPermanentInMemory.setEditable(false);
147
            GridData gd_txtPermanentInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
148
            gd_txtPermanentInMemory.widthHint = 130;
149
            txtPermanentInMemory.setLayoutData(gd_txtPermanentInMemory);
150
        }
151
        {
152
            Label lblPermanentOnDisk = new Label(container, SWT.CENTER);
153
            lblPermanentOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
154
            lblPermanentOnDisk.setText("on Disk : ");
155
            lblPermanentOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
156
            lblPermanentOnDisk.setAlignment(SWT.RIGHT);
157
        }
158
        {
159
            txtPermanentOnDisk = new Text(container, SWT.BORDER);
160
            txtPermanentOnDisk.setEditable(false);
161
            txtPermanentOnDisk.setText("");
162
            GridData gd_txtPermanentOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
163
            gd_txtPermanentOnDisk.widthHint = 130;
164
            txtPermanentOnDisk.setLayoutData(gd_txtPermanentOnDisk);
165
        }
166
        {
167
            Label lblCdmModelCache = new Label(container, SWT.CENTER);
168
            lblCdmModelCache.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
169
            lblCdmModelCache.setText("CDM Model Cache, ");
170
            lblCdmModelCache.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
171
            lblCdmModelCache.setAlignment(SWT.RIGHT);
172
        }
173
        {
174
            Label lblCdmModelInMemory = new Label(container, SWT.CENTER);
175
            lblCdmModelInMemory.setText("in Memory : ");
176
            lblCdmModelInMemory.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
177
            lblCdmModelInMemory.setAlignment(SWT.RIGHT);
178
            GridData gd_lblCdmModelInMemory = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
179
            gd_lblCdmModelInMemory.widthHint = 86;
180
            lblCdmModelInMemory.setLayoutData(gd_lblCdmModelInMemory);
181
        }
182
        {
183
            txtCdmModelInMemory = new Text(container, SWT.BORDER);
184
            txtCdmModelInMemory.setEditable(false);
185
            GridData gd_txtCdmModelInMemory = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
186
            gd_txtCdmModelInMemory.widthHint = 130;
187
            txtCdmModelInMemory.setLayoutData(gd_txtCdmModelInMemory);
188
        }
189
        {
190
            Label lblCdmModelOnDisk = new Label(container, SWT.CENTER);
191
            lblCdmModelOnDisk.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
192
            lblCdmModelOnDisk.setText("on Disk : ");
193
            lblCdmModelOnDisk.setFont(SWTResourceManager.getFont("Ubuntu", 10, SWT.NORMAL));
194
            lblCdmModelOnDisk.setAlignment(SWT.RIGHT);
195
        }
196
        {
197
            txtCdmModelOnDisk = new Text(container, SWT.BORDER);
198
            txtCdmModelOnDisk.setEditable(false);
199
            txtCdmModelOnDisk.setText("");
200
            GridData gd_txtCdmModelOnDisk = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
201
            gd_txtCdmModelOnDisk.widthHint = 130;
202
            txtCdmModelOnDisk.setLayoutData(gd_txtCdmModelOnDisk);
203
        }
204
        {
205
            Button btnRefresh = new Button(container, SWT.NONE);
206
            btnRefresh.addSelectionListener(new SelectionAdapter() {
207
                @Override
208
                public void widgetSelected(SelectionEvent e) {
209
                    changed();
210
                }
211
            });
212
            GridData gd_btnRefresh = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
213
            gd_btnRefresh.widthHint = 100;
214
            btnRefresh.setLayoutData(gd_btnRefresh);
215
            btnRefresh.setText("Refresh");
216
        }
217
        {
218
            Button btnInspectActiveSession = new Button(container, SWT.NONE);
219
            btnInspectActiveSession.addSelectionListener(new SelectionAdapter() {
220
                @Override
221
                public void widgetSelected(SelectionEvent e) {
222
                    Command command = commandService.getCommand(OPEN_INSPECT_SESSIONS_DIALOG_ID);
223
                    handlerService.executeHandler(ParameterizedCommand.generateCommand(command, null));
224
                }
225
            });
226
            btnInspectActiveSession.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));
227
            btnInspectActiveSession.setText("Inspect Active Session");
228
        }
229
        new Label(container, SWT.NONE);
230
        new Label(container, SWT.NONE);
231

    
232
        {
233
            tableSessions = new Table(container, SWT.BORDER | SWT.FULL_SELECTION);
234
            GridData gd_tableSessions = new GridData(SWT.FILL, SWT.FILL, true, true, 5, 1);
235
            gd_tableSessions.widthHint = 769;
236
            tableSessions.setLayoutData(gd_tableSessions);
237
            tableSessions.setHeaderVisible(true);
238
            tableSessions.setLinesVisible(true);
239
        }
240

    
241
        createActions();
242
    }
243

    
244
    /**
245
     * Create the actions.
246
     */
247
    private void createActions() {
248
        for (String title : titles) {
249
            TableColumn column = new TableColumn(tableSessions, SWT.NULL);
250
            column.setWidth(200);
251
            column.setText(title);
252
        }
253
        changed();
254

    
255
    }
256

    
257
    @Override
258
    public void changed() {
259
        if(tableSessions.isDisposed()) {
260
            return;
261
        }
262
        tableSessions.removeAll();
263
        if(cdmEntitySessionManager != null) {
264
            Collection<ICdmEntitySession> sessions = cdmEntitySessionManager.getSessions();
265
            txtNoOfSessions.setText(String.valueOf(sessions.size()));
266
            for(ICdmEntitySession session : sessions) {
267
                addSession(session);
268
            }
269
            ICdmEntitySession nullSession = cdmEntitySessionManager.getNullSession();
270
            if(nullSession != null) {
271
                addSession(nullSession);
272
            }
273
            for (int i=0; i<titles.length; i++) {
274
                tableSessions.getColumn (i).pack ();
275
            }
276
        }
277

    
278
        CdmRemoteCacheManager instance = CdmRemoteCacheManager.INSTANCE();
279
        int cdmEnityCacheSize = CacheManager.create().getCacheNames().length;
280
        txtNoOfCaches.setText(String.valueOf(cdmEnityCacheSize));
281

    
282
        Cache permanentCache = CacheManager.create().getCache(CdmPermanentCacheBase.PERMANENT_CACHE_NAME);
283
        txtPermanentInMemory.setText("");
284
        txtPermanentOnDisk.setText("");
285
        if(permanentCache != null) {
286
        	FlatStatistics liveCacheStatistics = permanentCache.getStatistics();
287
            txtPermanentInMemory.setText(getInMemoryStats(liveCacheStatistics));
288
            txtPermanentOnDisk.setText(getOnDiskStats(liveCacheStatistics));
289
        }
290

    
291
        Cache cdmModelGetMethodsCache = instance.getCdmModelGetMethodsCache();
292
        txtCdmModelInMemory.setText("");
293
        txtCdmModelOnDisk.setText("");
294
        if(cdmModelGetMethodsCache != null) {
295
        	FlatStatistics liveCacheStatistics = cdmModelGetMethodsCache.getStatistics();
296
            txtCdmModelInMemory.setText(getInMemoryStats(liveCacheStatistics));
297
            txtCdmModelOnDisk.setText(getOnDiskStats(liveCacheStatistics));
298
        }
299
    }
300

    
301
    private void addSession(ICdmEntitySession session) {
302

    
303
        TableItem item = new TableItem(tableSessions, SWT.NULL);
304

    
305
        String activePrefix = "";
306
        if(session.isActive()) {
307
            activePrefix = "*";
308
        }
309
        String sessionOwnerString = "";
310
        if(session instanceof NullSession) {
311
            sessionOwnerString = "Null Session";
312
        } else {
313
            if(session.getOwner() != null) {
314
                sessionOwnerString = session.getOwner().toString();
315
            }
316
        }
317
        item.setText(0, activePrefix + sessionOwnerString);
318
        FlatStatistics cacheStatistics = session.getCacheStatistics();
319
        item.setText(1, getInMemoryStats(cacheStatistics));
320
        item.setText(2, getOnDiskStats(cacheStatistics));
321
    }
322

    
323
    private String getInMemoryStats(FlatStatistics statistics) {
324
        if(statistics != null) {
325
            long localHeapSize = statistics.getLocalHeapSize();
326

    
327
            long localHeapSizeInBytes = statistics.getLocalHeapSizeInBytes();
328
            String localHeapSizeInBytesStr =
329
                    (new DecimalFormat("#.##")).format(localHeapSizeInBytes/1048576.0);
330
            return String.valueOf(localHeapSize) + " objects / " + localHeapSizeInBytesStr + " Mb";
331
        }
332
        return "";
333
    }
334

    
335
    private String getOnDiskStats(FlatStatistics statistics) {
336
        if(statistics != null) {
337
            long localDiskSize = statistics.getLocalDiskSize();
338

    
339
            long localDiskSizeInBytes = statistics.getLocalDiskSizeInBytes();
340
            String localDiskSizeInBytesStr =
341
                    (new DecimalFormat("#.##")).format(localDiskSizeInBytes/1048576.0);
342
            return String.valueOf(localDiskSize) + " objects / " + localDiskSizeInBytesStr + " Mb";
343
        }
344
        return "";
345
    }
346

    
347

    
348
}
(3-3/3)