Search in sources :

Example 6 with DashboardDatasetRow

use of org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow in project dbeaver by serge-rider.

the class DashboardRendererTimeseries method updateDashboardData.

@Override
public void updateDashboardData(DashboardContainer container, Date lastUpdateTime, DashboardDataset dataset) {
    DashboardChartComposite chartComposite = getChartComposite(container);
    if (chartComposite.isDisposed()) {
        return;
    }
    JFreeChart chart = chartComposite.getChart();
    XYPlot plot = (XYPlot) chart.getPlot();
    TimeSeriesCollection chartDataset = (TimeSeriesCollection) plot.getDataset();
    if (container.getDashboardFetchType() == DashboardFetchType.stats) {
        // Clean previous data before stats update
        chartDataset.removeAllSeries();
    }
    long currentTime = System.currentTimeMillis();
    long secondsPassed = lastUpdateTime == null ? 1 : (currentTime - lastUpdateTime.getTime()) / 1000;
    if (secondsPassed <= 0) {
        secondsPassed = 1;
    }
    DashboardDatasetRow lastRow = (DashboardDatasetRow) chartComposite.getData("last_row");
    List<DashboardDatasetRow> rows = dataset.getRows();
    String[] srcSeries = dataset.getColumnNames();
    for (int i = 0; i < srcSeries.length; i++) {
        String seriesName = srcSeries[i];
        TimeSeries series = chartDataset.getSeries(seriesName);
        if (series == null) {
            series = new TimeSeries(seriesName);
            series.setMaximumItemCount(container.getDashboardMaxItems());
            series.setMaximumItemAge(container.getDashboardMaxAge());
            chartDataset.addSeries(series);
            plot.getRenderer().setSeriesStroke(chartDataset.getSeriesCount() - 1, plot.getRenderer().getBaseStroke());
        }
        switch(container.getDashboardCalcType()) {
            case value:
                {
                    int maxDP = 200;
                    Date startTime = null;
                    for (DashboardDatasetRow row : rows) {
                        if (startTime == null) {
                            startTime = row.getTimestamp();
                        } else {
                            if (container.getDashboardInterval() == DashboardInterval.second || container.getDashboardInterval() == DashboardInterval.millisecond) {
                                long diffSeconds = (row.getTimestamp().getTime() - startTime.getTime()) / 1000;
                                if (diffSeconds > maxDP) {
                                    // Too big difference between start and end points. Stop here otherwise we'll flood chart with too many ticks
                                    break;
                                }
                            }
                        }
                        Object value = row.getValues()[i];
                        if (value instanceof Number) {
                            series.addOrUpdate(makeDataItem(container, row), (Number) value);
                        }
                    }
                    break;
                }
            case delta:
                {
                    if (lastUpdateTime == null) {
                        return;
                    }
                    // System.out.println("LAST=" + lastUpdateTime + "; CUR=" + new Date());
                    for (DashboardDatasetRow row : rows) {
                        if (lastRow != null) {
                            Object prevValue = lastRow.getValues()[i];
                            Object newValue = row.getValues()[i];
                            if (newValue instanceof Number && prevValue instanceof Number) {
                                double deltaValue = ((Number) newValue).doubleValue() - ((Number) prevValue).doubleValue();
                                deltaValue /= secondsPassed;
                                if (container.getDashboardValueType() != DashboardValueType.decimal) {
                                    deltaValue = Math.round(deltaValue);
                                }
                                series.addOrUpdate(makeDataItem(container, row), deltaValue);
                            }
                        }
                    }
                    break;
                }
        }
    }
    if (!rows.isEmpty()) {
        chartComposite.setData("last_row", rows.get(rows.size() - 1));
    }
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) JFreeChart(org.jfree.chart.JFreeChart) Point(org.eclipse.swt.graphics.Point) Date(java.util.Date) XYPlot(org.jfree.chart.plot.XYPlot) DashboardChartComposite(org.jkiss.dbeaver.ui.dashboard.control.DashboardChartComposite)

Example 7 with DashboardDatasetRow

use of org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow in project dbeaver by dbeaver.

the class DashboardUpdater method transposeDataset.

private DashboardDataset transposeDataset(DashboardDataset dataset) {
    int oldColumnCount = dataset.getColumnNames().length;
    if (oldColumnCount < 2) {
        // Something went wrong
        return dataset;
    }
    // Column names don't matter. Get everything from rows.
    // First column in row is actually column name. The rest are row values (usually 1)
    List<String> colNamesFromRows = new ArrayList<>();
    List<DashboardDatasetRow> oldRows = dataset.getRows();
    Date oldTimestamp = oldRows.get(0).getTimestamp();
    DashboardDatasetRow[] newRows = new DashboardDatasetRow[oldColumnCount - 1];
    for (int i = 0; i < oldRows.size(); i++) {
        DashboardDatasetRow oldRow = oldRows.get(i);
        colNamesFromRows.add(CommonUtils.toString(oldRow.getValues()[0], String.valueOf(i + 1)));
        for (int colIndex = 1; colIndex < oldColumnCount; colIndex++) {
            DashboardDatasetRow newRow = newRows[colIndex - 1];
            if (newRow == null) {
                newRow = new DashboardDatasetRow(oldTimestamp, new Object[oldRows.size()]);
                newRows[colIndex - 1] = newRow;
            }
            newRow.getValues()[i] = oldRow.getValues()[colIndex];
        }
    }
    DashboardDataset newDataset = new DashboardDataset(colNamesFromRows.toArray(new String[0]));
    for (DashboardDatasetRow newRow : newRows) {
        newDataset.addRow(newRow);
    }
    return newDataset;
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset)

Example 8 with DashboardDatasetRow

use of org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow in project dbeaver by dbeaver.

the class DashboardUpdater method fetchDashboardData.

private void fetchDashboardData(DashboardContainer dashboard, DBCResultSet dbResults) throws DBCException {
    DBCResultSetMetaData meta = dbResults.getMeta();
    List<DBCAttributeMetaData> rsAttrs = meta.getAttributes();
    List<String> colNames = new ArrayList<>();
    String tsColName = null;
    for (DBCAttributeMetaData rsAttr : rsAttrs) {
        String colName = rsAttr.getLabel();
        if (CommonUtils.isEmpty(colName)) {
            colName = rsAttr.getName();
        }
        if (DashboardConstants.RS_COL_TIMESTAMP.equalsIgnoreCase(colName)) {
            tsColName = colName;
        } else {
            colNames.add(colName);
        }
    }
    DashboardDataset dataset = new DashboardDataset(colNames.toArray(new String[0]));
    while (dbResults.nextRow()) {
        Object[] values = new Object[colNames.size()];
        Date timestamp;
        if (tsColName != null) {
            timestamp = (Date) dbResults.getAttributeValue(tsColName);
        } else {
            timestamp = new Date();
        }
        for (int i = 0; i < colNames.size(); i++) {
            values[i] = dbResults.getAttributeValue(colNames.get(i));
        }
        dataset.addRow(new DashboardDatasetRow(timestamp, values));
        if (dataset.getRows().size() >= dashboard.getDashboardMaxItems()) {
            break;
        }
    }
    switch(dashboard.getDashboardFetchType()) {
        case rows:
            dataset = transposeDataset(dataset);
            break;
    }
    dashboard.updateDashboardData(dataset);
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset)

Aggregations

DashboardDatasetRow (org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow)8 DashboardDataset (org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset)6 Date (java.util.Date)2 JexlContext (org.apache.commons.jexl3.JexlContext)2 Point (org.eclipse.swt.graphics.Point)2 JFreeChart (org.jfree.chart.JFreeChart)2 XYPlot (org.jfree.chart.plot.XYPlot)2 DashboardChartComposite (org.jkiss.dbeaver.ui.dashboard.control.DashboardChartComposite)2