Search in sources :

Example 1 with DashboardDataset

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

the class DashboardUpdater method fetchDashboardMapData.

private void fetchDashboardMapData(DBRProgressMonitor monitor, DashboardContainer dashboard) {
    MapQueryInfo mqi = getMapQueryData(dashboard);
    if (mqi == null) {
        return;
    }
    Map<String, Object> mapValue = mqi.mapValue;
    if (mapValue != null) {
        String[] mapKeys = dashboard.getMapKeys();
        String[] mapLabels = dashboard.getMapLabels();
        if (!ArrayUtils.isEmpty(mapKeys)) {
            if (ArrayUtils.isEmpty(mapLabels)) {
                mapLabels = mapKeys;
            }
            DashboardDataset dataset = new DashboardDataset(mapLabels);
            Object[] mapValues = new Object[mapKeys.length];
            for (int i = 0; i < mapKeys.length; i++) {
                Object value = mapValue.get(mapKeys[i]);
                Number numValue;
                if (value instanceof Number) {
                    numValue = (Number) value;
                } else {
                    numValue = CommonUtils.toDouble(value);
                }
                mapValues[i] = numValue;
            }
            Date timestamp = mqi.timestamp;
            if (timestamp == null) {
                timestamp = new Date();
            }
            dataset.addRow(new DashboardDatasetRow(timestamp, mapValues));
            dashboard.updateDashboardData(dataset);
        } else if (dashboard.getMapFormula() != null) {
            Map<String, Object> ciMap = new HashMap<>(mapValue.size());
            for (Map.Entry<String, Object> me : mapValue.entrySet()) {
                ciMap.put(me.getKey().toLowerCase(Locale.ENGLISH), me.getValue());
            }
            JexlContext context = new JexlContext() {

                @Override
                public Object get(String name) {
                    if (name.equals("map")) {
                        return ciMap;
                    } else if (name.equals("dashboard")) {
                        return dashboard;
                    }
                    return null;
                }

                @Override
                public void set(String name, Object value) {
                    log.warn("Set is not implemented in DBX model");
                }

                @Override
                public boolean has(String name) {
                    return name.equals("object") || name.equals("dashboard");
                }
            };
            Object result = dashboard.getMapFormula().evaluate(context);
            if (result instanceof Number) {
                String columnName = dashboard.getDashboardTitle();
                if (!ArrayUtils.isEmpty(mapLabels)) {
                    columnName = mapLabels[0];
                }
                DashboardDataset dataset = new DashboardDataset(new String[] { columnName });
                dataset.addRow(new DashboardDatasetRow(new Date(), new Object[] { result }));
                dashboard.updateDashboardData(dataset);
            } else {
                log.debug("Wrong expression result: " + result);
            }
        }
    }
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset) JexlContext(org.apache.commons.jexl3.JexlContext)

Example 2 with DashboardDataset

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

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)

Example 3 with DashboardDataset

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

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 4 with DashboardDataset

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

the class DashboardUpdater method fetchDashboardMapData.

private void fetchDashboardMapData(DBRProgressMonitor monitor, DashboardContainer dashboard) {
    MapQueryInfo mqi = getMapQueryData(dashboard);
    if (mqi == null) {
        return;
    }
    Map<String, Object> mapValue = mqi.mapValue;
    if (mapValue != null) {
        String[] mapKeys = dashboard.getMapKeys();
        String[] mapLabels = dashboard.getMapLabels();
        if (!ArrayUtils.isEmpty(mapKeys)) {
            if (ArrayUtils.isEmpty(mapLabels)) {
                mapLabels = mapKeys;
            }
            DashboardDataset dataset = new DashboardDataset(mapLabels);
            Object[] mapValues = new Object[mapKeys.length];
            for (int i = 0; i < mapKeys.length; i++) {
                Object value = mapValue.get(mapKeys[i]);
                Number numValue;
                if (value instanceof Number) {
                    numValue = (Number) value;
                } else {
                    numValue = CommonUtils.toDouble(value);
                }
                mapValues[i] = numValue;
            }
            Date timestamp = mqi.timestamp;
            if (timestamp == null) {
                timestamp = new Date();
            }
            dataset.addRow(new DashboardDatasetRow(timestamp, mapValues));
            dashboard.updateDashboardData(dataset);
        } else if (dashboard.getMapFormula() != null) {
            Map<String, Object> ciMap = new HashMap<>(mapValue.size());
            for (Map.Entry<String, Object> me : mapValue.entrySet()) {
                ciMap.put(me.getKey().toLowerCase(Locale.ENGLISH), me.getValue());
            }
            JexlContext context = new JexlContext() {

                @Override
                public Object get(String name) {
                    if (name.equals("map")) {
                        return ciMap;
                    } else if (name.equals("dashboard")) {
                        return dashboard;
                    }
                    return null;
                }

                @Override
                public void set(String name, Object value) {
                    log.warn("Set is not implemented in DBX model");
                }

                @Override
                public boolean has(String name) {
                    return name.equals("object") || name.equals("dashboard");
                }
            };
            Object result = dashboard.getMapFormula().evaluate(context);
            if (result instanceof Number) {
                String columnName = dashboard.getDashboardTitle();
                if (!ArrayUtils.isEmpty(mapLabels)) {
                    columnName = mapLabels[0];
                }
                DashboardDataset dataset = new DashboardDataset(new String[] { columnName });
                dataset.addRow(new DashboardDatasetRow(new Date(), new Object[] { result }));
                dashboard.updateDashboardData(dataset);
            } else {
                log.debug("Wrong expression result: " + result);
            }
        }
    }
}
Also used : DashboardDatasetRow(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow) DashboardDataset(org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset) JexlContext(org.apache.commons.jexl3.JexlContext)

Example 5 with DashboardDataset

use of org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset 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)

Aggregations

DashboardDataset (org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset)6 DashboardDatasetRow (org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow)6 JexlContext (org.apache.commons.jexl3.JexlContext)2