提交 6dacf48d 编写于 作者: S Serge Rider

#4209 Dashboard updater

上级 def85027
......@@ -298,17 +298,20 @@ public class CommonUtils {
}
}
}
public static long toLong(@Nullable Object object) {
return toLong(object, 0);
}
public static long toLong(@Nullable Object object, long defValue) {
if (object == null) {
return 0;
return defValue;
} else if (object instanceof Number) {
return ((Number) object).longValue();
} else {
try {
return Long.parseLong(toString(object));
} catch (NumberFormatException e) {
return -1;
return defValue;
}
}
}
......
......@@ -630,7 +630,7 @@
<extension point="org.jkiss.dbeaver.dashboard">
<dashboard id="postgresql.sessionCount" label="Server sessions" type="histogram" group="Standard" refresh="2" calc="value" fetch="columns" showByDefault="true">
<dashboard id="postgresql.sessionCount" label="Server sessions" type="histogram" group="Standard" updatePeriod="2000" calc="value" fetch="columns" showByDefault="true">
<datasource id="postgresql"/>
<query>
SELECT
......@@ -639,7 +639,7 @@
(SELECT count(*) as "Total" FROM pg_catalog.pg_stat_activity sa)
</query>
</dashboard>
<dashboard id="postgresql.transactionCount" label="Transactions per second" group="Standard" type="histogram" refresh="2" calc="delta" fetch="columns" showByDefault="true">
<dashboard id="postgresql.transactionCount" label="Transactions per second" group="Standard" type="histogram" updatePeriod="2000" calc="delta" fetch="columns" showByDefault="true">
<datasource id="postgresql"/>
<query>
SELECT
......
......@@ -27,19 +27,25 @@ import org.eclipse.swt.widgets.Text;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.ui.dashboard.model.*;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset;
import org.jkiss.dbeaver.ui.dashboard.registry.DashboardDescriptor;
import java.util.Date;
import java.util.List;
public class DashboardItem extends Composite implements DashboardContainer {
public static final int DEFAULT_HEIGHT = 200;
private DashboardList groupContainer;
private DashboardDescriptor dashboardDescriptor;
private Date lastUpdateTime;
public DashboardItem(DashboardList parent, DashboardDescriptor dashboardDescriptor) {
super(parent, SWT.BORDER);
parent.addItem(this);
addDisposeListener(e -> parent.removeItem(this));
this.groupContainer = parent;
groupContainer.addItem(this);
addDisposeListener(e -> groupContainer.removeItem(this));
GridLayout layout = new GridLayout(1, true);
layout.marginHeight = 0;
......@@ -62,10 +68,6 @@ public class DashboardItem extends Composite implements DashboardContainer {
}
}
public DashboardList getParent() {
return (DashboardList) super.getParent();
}
public int getDefaultHeight() {
return DEFAULT_HEIGHT;
}
......@@ -75,11 +77,9 @@ public class DashboardItem extends Composite implements DashboardContainer {
}
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
DashboardList list = getParent();
int defHeight = getDefaultHeight();
int defWidth = getDefaultWidth();
Point areaSize = list.getSize();
Point areaSize = groupContainer.getSize();
if (areaSize.x <= defWidth || areaSize.y <= defHeight) {
return new Point(defWidth, defHeight);
}
......@@ -95,8 +95,8 @@ public class DashboardItem extends Composite implements DashboardContainer {
if (areaSize.x > areaSize.y) {
// Horizontal
totalHeight = defHeight;
for (DashboardItem item : list.getItems()) {
if (totalWidth > 0) totalWidth += list.getItemSpacing();
for (DashboardItem item : groupContainer.getItems()) {
if (totalWidth > 0) totalWidth += groupContainer.getItemSpacing();
totalWidth += item.getDefaultWidth();
}
if (totalWidth < areaSize.x) {
......@@ -107,8 +107,8 @@ public class DashboardItem extends Composite implements DashboardContainer {
} else {
// Vertical
totalWidth = defWidth;
for (DashboardItem item : list.getItems()) {
if (totalHeight > 0) totalHeight += list.getItemSpacing();
for (DashboardItem item : groupContainer.getItems()) {
if (totalHeight > 0) totalHeight += groupContainer.getItemSpacing();
totalHeight += item.getDefaultHeight();
}
if (totalHeight < areaSize.y) {
......@@ -131,6 +131,11 @@ public class DashboardItem extends Composite implements DashboardContainer {
}
}
@Override
public String getDashboardId() {
return dashboardDescriptor.getId();
}
@Override
public String getDashboardTitle() {
return dashboardDescriptor.getLabel();
......@@ -158,12 +163,12 @@ public class DashboardItem extends Composite implements DashboardContainer {
@Override
public DBPDataSourceContainer getDataSourceContainer() {
return getParent().getDataSourceContainer();
return groupContainer.getDataSourceContainer();
}
@Override
public DashboardGroupContainer getGroup() {
return getParent();
return groupContainer;
}
@Override
......@@ -171,4 +176,20 @@ public class DashboardItem extends Composite implements DashboardContainer {
return dashboardDescriptor.getQueries();
}
@Override
public Date getLastUpdateTime() {
return lastUpdateTime;
}
@Override
public void updateDashboardData(DashboardDataset dataset) {
lastUpdateTime = new Date();
}
@Override
public long getUpdatePeriod() {
return dashboardDescriptor.getUpdatePeriod();
}
}
......@@ -20,9 +20,8 @@ import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardContainer;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardGroupContainer;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardViewContainer;
import org.jkiss.dbeaver.ui.dashboard.registry.DashboardDescriptor;
import org.jkiss.dbeaver.ui.dashboard.registry.DashboardRegistry;
......@@ -33,13 +32,13 @@ public class DashboardList extends Composite implements DashboardGroupContainer
private static final int ITEM_SPACING = 5;
private IDataSourceContainerProvider provider;
private DashboardViewContainer viewContainer;
private List<DashboardItem> items = new ArrayList<>();
public DashboardList(Composite parent, IDataSourceContainerProvider provider) {
public DashboardList(Composite parent, DashboardViewContainer viewContainer) {
super(parent, SWT.NONE);
this.provider = provider;
this.viewContainer = viewContainer;
RowLayout layout = new RowLayout();
layout.spacing = getItemSpacing();
......@@ -49,9 +48,13 @@ public class DashboardList extends Composite implements DashboardGroupContainer
this.setLayout(layout);
}
DBPDataSourceContainer getDataSourceContainer() {
return viewContainer.getDataSourceContainer();
}
@Override
public DBPDataSourceContainer getDataSourceContainer() {
return provider.getDataSourceContainer();
public DashboardViewContainer getView() {
return viewContainer;
}
public List<DashboardItem> getItems() {
......@@ -59,7 +62,8 @@ public class DashboardList extends Composite implements DashboardGroupContainer
}
void createDefaultDashboards() {
List<DashboardDescriptor> dashboards = DashboardRegistry.getInstance().getDashboards(provider.getDataSourceContainer(), true);
List<DashboardDescriptor> dashboards = DashboardRegistry.getInstance().getDashboards(
viewContainer.getDataSourceContainer(), true);
for (DashboardDescriptor dd : dashboards) {
DashboardItem item = new DashboardItem(this, dd);
}
......
......@@ -24,14 +24,16 @@ import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.DBPEvent;
import org.jkiss.dbeaver.model.DBPEventListener;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardGroupContainer;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardViewContainer;
import java.util.Collections;
import java.util.List;
public class DashboardViewManager implements DBPEventListener, IDataSourceContainerProvider {
public class DashboardViewManager implements DBPEventListener, IDataSourceContainerProvider, DashboardViewContainer {
private final DBPDataSourceContainer dataSourceContainer;
private DashboardList dashContainer;
......@@ -44,6 +46,8 @@ public class DashboardViewManager implements DBPEventListener, IDataSourceContai
if (!this.dataSourceContainer.isConnected()) {
//DataSourceConnectHandler
}
// Activate updater
}
public void dispose() {
......@@ -88,7 +92,14 @@ public class DashboardViewManager implements DBPEventListener, IDataSourceContai
return dataSourceContainer;
}
public List<? extends DashboardGroupContainer> getDashboardGroups() {
@Override
public List<? extends DashboardGroupContainer> getGroups() {
return Collections.singletonList(dashContainer);
}
@Override
public DBCExecutionContext getExecutionContext() {
return dataSourceContainer.getDataSource().getDefaultInstance().getDefaultContext(false);
}
}
......@@ -21,6 +21,7 @@ import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.jkiss.dbeaver.model.impl.preferences.BundlePreferenceStore;
import org.jkiss.dbeaver.model.preferences.DBPPreferenceStore;
import org.jkiss.dbeaver.runtime.DBeaverNotifications;
import org.jkiss.dbeaver.ui.dashboard.view.DashboardUpdateJob;
import org.jkiss.dbeaver.ui.notifications.NotificationUtils;
import org.osgi.framework.BundleContext;
......@@ -43,6 +44,8 @@ public class UIDashboardActivator extends AbstractUIPlugin {
DBeaverNotifications.setHandler(NotificationUtils::sendNotification);
plugin = this;
preferences = new BundlePreferenceStore(getBundle());
DashboardUpdateJob.startUpdating();
}
@Override
......
......@@ -17,7 +17,9 @@
package org.jkiss.dbeaver.ui.dashboard.model;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset;
import java.util.Date;
import java.util.List;
/**
......@@ -25,6 +27,8 @@ import java.util.List;
*/
public interface DashboardContainer {
String getDashboardId();
String getDashboardTitle();
String getDashboardDescription();
......@@ -41,4 +45,12 @@ public interface DashboardContainer {
List<? extends DashboardQuery> getQueryList();
Date getLastUpdateTime();
void updateDashboardData(DashboardDataset dataset);
/**
* Dashboard update period in seconds
*/
long getUpdatePeriod();
}
......@@ -25,7 +25,7 @@ import java.util.List;
*/
public interface DashboardGroupContainer {
DBPDataSourceContainer getDataSourceContainer();
DashboardViewContainer getView();
List<? extends DashboardContainer> getItems();
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.dashboard.model;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.IDataSourceContainerProvider;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import java.util.List;
/**
* Dashboard view container
*/
public interface DashboardViewContainer extends IDataSourceContainerProvider {
List<? extends DashboardGroupContainer> getGroups();
DBCExecutionContext getExecutionContext();
}
......@@ -48,6 +48,7 @@ public class DashboardDescriptor extends AbstractContextDescriptor
private float widthRatio;
private DashboardCalcType calcType;
private DashboardFetchType fetchType;
private long updatePeriod;
private static class DataSourceMapping {
private final String dataSourceProvider;
......@@ -104,6 +105,7 @@ public class DashboardDescriptor extends AbstractContextDescriptor
this.widthRatio = (float) CommonUtils.toDouble(config.getAttribute("ratio"), 1.5); // Default ratio is 2 to 3
this.calcType = CommonUtils.valueOf(DashboardCalcType.class, config.getAttribute("calc"), DashboardCalcType.value);
this.fetchType = CommonUtils.valueOf(DashboardFetchType.class, config.getAttribute("fetch"), DashboardFetchType.columns);
this.updatePeriod = CommonUtils.toLong(config.getAttribute("updatePeriod"), 5000); // Default ratio is 2 to 3
for (IConfigurationElement ds : config.getChildren("datasource")) {
dataSourceMappings.add(new DataSourceMapping(ds));
......@@ -163,6 +165,10 @@ public class DashboardDescriptor extends AbstractContextDescriptor
return queries;
}
public long getUpdatePeriod() {
return updatePeriod;
}
public boolean matches(DBPDataSourceContainer dataSource) {
for (DataSourceMapping dsm : dataSourceMappings) {
if (!dsm.matches(dataSource)) {
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.dashboard.view;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.utils.GeneralUtils;
/**
* Job which runs every second and updates necessary dashboards
*/
public class DashboardUpdateJob extends AbstractJob {
private static final Log log = Log.getLog(DashboardUpdateJob.class);
private static final int JOB_DELAY = 1000;
private DashboardUpdateJob() {
super("Dashboard update");
}
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try {
new DashboardUpdater().updateDashboards(monitor);
} catch (Exception e) {
log.error("Error running dashboard updater", e);
}
if (!DBWorkbench.getPlatform().isShuttingDown()) {
schedule(JOB_DELAY);
}
return Status.OK_STATUS;
}
public static void startUpdating() {
new DashboardUpdateJob().schedule(JOB_DELAY);
}
}
\ No newline at end of file
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2019 Serge Rider (serge@jkiss.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ui.dashboard.view;
import org.eclipse.ui.*;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.exec.*;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.dashboard.control.DashboardViewManager;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardContainer;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardGroupContainer;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardQuery;
import org.jkiss.dbeaver.ui.dashboard.model.DashboardViewContainer;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DashboardUpdater {
private static final Log log = Log.getLog(DashboardUpdater.class);
public void updateDashboards(DBRProgressMonitor monitor) {
for (DashboardContainer dashboard : getDashboardsToUpdate()) {
updateDashboard(monitor, dashboard);
}
}
private void updateDashboard(DBRProgressMonitor monitor, DashboardContainer dashboard) {
if (!dashboard.getDataSourceContainer().isConnected() || DBWorkbench.getPlatform().isShuttingDown()) {
return;
}
List<? extends DashboardQuery> queries = dashboard.getQueryList();
DashboardViewContainer view = dashboard.getGroup().getView();
try (DBCSession session = view.getExecutionContext().openSession(
monitor, DBCExecutionPurpose.UTIL, "Read dashboard '" + dashboard.getDashboardTitle() + "' data"))
{
for (DashboardQuery query : queries) {
try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, query.getQueryText(), false, false, false)) {
if (dbStat.executeStatement()) {
try (DBCResultSet dbResults = dbStat.openResultSet()) {
fetchDashboardData(dashboard, dbResults);
}
}
}
}
} catch (Exception e) {
log.debug("Error updating dashboard " + dashboard.getDashboardId(), e);
}
}
private void fetchDashboardData(DashboardContainer dashboard, DBCResultSet dbResults) throws DBCException {
while (dbResults.nextRow()) {
}
}
public List<DashboardContainer> getDashboardsToUpdate() {
List<DashboardContainer> dashboards = new ArrayList<>();
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
for (IWorkbenchPage page : window.getPages()) {
for (IViewReference view : page.getViewReferences()) {
if (view.getId().equalsIgnoreCase(DashboardView.VIEW_ID)) {
IWorkbenchPart part = view.getPart(false);
if (part instanceof DashboardView) {
getViewDashboards((DashboardView) part, dashboards);
}
}
}
}
}
return dashboards;
}
private void getViewDashboards(DashboardView view, List<DashboardContainer> dashboards) {
long currentTime = System.currentTimeMillis();
DashboardViewManager viewManager = view.getDashboardViewManager();
if (!viewManager.getDataSourceContainer().isConnected()) {
return;
}
for (DashboardGroupContainer group : viewManager.getGroups()) {
for (DashboardContainer dashboard : group.getItems()) {
Date lastUpdateTime = dashboard.getLastUpdateTime();
if (lastUpdateTime == null || (currentTime - lastUpdateTime.getTime()) >= dashboard.getUpdatePeriod()) {
dashboards.add(dashboard);
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册