<commit_before>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2019 <NAME> (<EMAIL>)
 *
 * 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.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBUtils;
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.DashboardListViewer;
import org.jkiss.dbeaver.ui.dashboard.model.*;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow;
import org.jkiss.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;
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) {
        List<DashboardContainer> dashboards = getDashboardsToUpdate();

        updateDashboards(monitor, dashboards);

    }

    private void updateDashboards(DBRProgressMonitor monitor, List<DashboardContainer> dashboards) {
        for (DashboardContainer dashboard : dashboards) {
            DBPDataSource dataSource = dashboard.getDataSourceContainer().getDataSource();
            if (dataSource == null) {
                continue;
            }
            try {
                DBUtils.tryExecuteRecover(dashboards, dataSource, param -> {
                    try {
                        updateDashboard(monitor, dashboard);
                    } catch (Throwable e) {
                        throw new InvocationTargetException(e);
                    }
                });
            } catch (DBException e) {
                log.debug(e);
            }
        }
    }

    private void updateDashboard(DBRProgressMonitor monitor, DashboardContainer dashboard) throws DBCException {
        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"))
        {
            session.enableLogging(false);<commit_msg>#4209 Dashboards: updater fix (connection validate)<commit_after>/*
 * DBeaver - Universal Database Manager
 * Copyright (C) 2010-2019 <NAME> (<EMAIL>)
 *
 * 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.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.DBUtils;
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.DashboardListViewer;
import org.jkiss.dbeaver.ui.dashboard.model.*;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDataset;
import org.jkiss.dbeaver.ui.dashboard.model.data.DashboardDatasetRow;
import org.jkiss.utils.CommonUtils;

import java.lang.reflect.InvocationTargetException;
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) {
        List<DashboardContainer> dashboards = getDashboardsToUpdate();

        updateDashboards(monitor, dashboards);

    }

    private void updateDashboards(DBRProgressMonitor monitor, List<DashboardContainer> dashboards) {
        for (DashboardContainer dashboard : dashboards) {
            DBPDataSource dataSource = dashboard.getDataSourceContainer().getDataSource();
            if (dataSource == null) {
                continue;
            }
            try {
                DBUtils.tryExecuteRecover(dashboards, dataSource, param -> {
                    try {
                        updateDashboard(monitor, dashboard);
                    } catch (Throwable e) {
                        throw new InvocationTargetException(e);
                    }
                });
            } catch (DBException e) {
                log.debug(e);
            }
        }
    }

    private void updateDashboard(DBRProgressMonitor monitor, DashboardContainer dashboard) throws DBCException {
        if (!dashboard.getDataSourceContainer().isConnected() || DBWorkbench.getPlatform().isShuttingDown()) {
            return;
        }

        List<? extends DashboardQuery> queries = dashboard.getQueryList();
        DashboardViewContainer view = dashboard.getGroup().getView();
        DBCExecutionContext executionContext = view.getExecutionContext();
        if (executionContext == null) {
            return;
        }
        try (DBCSession session = executionContext.openSession(
            monitor, DBCExecutionPurpose.UTIL, "Read dashboard '" + dashboard.getDashboardTitle() + "' data"))
        {
            session.enableLogging(false);
{'fraction_non_alphanumeric': 0.09439209022091766, 'fraction_numerical': 0.004325660435655801, 'mean_word_length': 3.0086687306501547, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992871', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1758, 'n_words': 425}