<commit_before>/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.lagdisplay;

import com.dmdirc.ClientModule.GlobalConfig;
import com.dmdirc.FrameContainer;
import com.dmdirc.ServerState;
import com.dmdirc.actions.ActionManager;
import com.dmdirc.actions.CoreActionType;
import com.dmdirc.addons.lagdisplay.LagDisplayModule.LagDisplaySettingsDomain;
import com.dmdirc.addons.ui_swing.MainFrame;
import com.dmdirc.addons.ui_swing.SelectionListener;
import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
import com.dmdirc.interfaces.ActionListener;
import com.dmdirc.interfaces.Connection;
import com.dmdirc.interfaces.actions.ActionType;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.util.collections.RollingList;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

/**
 * Manages the lifecycle of the lag display plugin.
 */
@Singleton
public class LagDisplayManager implements ActionListener, ConfigChangeListener, SelectionListener {

    /** Frame to listen to selection events on. */
    // TODO: Selection/focus management should be behind an interface
    private final MainFrame mainFrame;
    /** Status bar to add panels to. */
    private final SwingStatusBar statusBar;
    private final Provider<LagDisplayPanel> panelProvider;
    /** The settings domain to use. */
    private final String domain;
    /** Config to read global settings from. */
    private final AggregateConfigProvider globalConfig;
    /** A cache of ping times. */
    private final Map<Connection, String> pings = new WeakHashMap<>();
    /** Ping history. */
    private final Map<Connection, RollingList<Long>> history = new HashMap<>();
    /** Whether or not to show a graph in the info popup. */
    private boolean showGraph = true;
    /** Whether or not to show labels on that graph. */
    private boolean showLabels = true;
    /** The length of history to keep per-server. */
    private int historySize = 100;
    /** The panel currently in use. Null before {@link #load()} or after {@link #unload()}. */
    private LagDisplayPanel panel;

    @Inject
    public LagDisplayManager(
            final MainFrame mainFrame,
            final SwingStatusBar statusBar,
            final Provider<LagDisplayPanel> panelProvider,
            @LagDisplaySettingsDomain final String domain,
            @GlobalConfig final AggregateConfigProvider globalConfig) {
        this.mainFrame = mainFrame;
        this.statusBar = statusBar;
        this.panelProvider = panelProvider;
        this.domain = domain;
        this.globalConfig = globalConfig;
    }

    public void load() {
        statusBar.addComponent(panel);
        mainFrame.addSelectionListener(this);

        globalConfig.addChangeListener(domain, this);
        readConfig();
        ActionManager.getActionManager().registerListener(this,
                CoreActionType.SERVER_GOTPING, CoreActionType.SERVER_NOPING,
                CoreActionType.SERVER_DISCONNECTED,
                CoreActionType.SERVER_PINGSENT, CoreActionType.SERVER_NUMERIC);

        panel = panelProvider.get();
    }

    public void unload() {
        mainFrame.removeSelectionListener(this);
        statusBar.removeComponent(panel);
        globalConfig.removeListener(this);
        ActionManager.getActionManager().unregisterListener(this);

        panel = null;
    }

    /** Reads the plugin's global configuration settings. */
    private void readConfig() {
        showGraph = globalConfig.getOptionBool(domain, "graph");
        showLabels = globalConfig.getOptionBool(domain, "labels");
        historySize = globalConfig.getOptionInt(domain, "history");
    }

    /**
     * Retrieves the history of the specified server. If there is no history, a new list is added to
     * the history map and returned.
     *
     * @param connection The connection whose history is being requested<commit_msg>Create the lag display panel before adding it.<commit_after>/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.lagdisplay;

import com.dmdirc.ClientModule.GlobalConfig;
import com.dmdirc.FrameContainer;
import com.dmdirc.ServerState;
import com.dmdirc.actions.ActionManager;
import com.dmdirc.actions.CoreActionType;
import com.dmdirc.addons.lagdisplay.LagDisplayModule.LagDisplaySettingsDomain;
import com.dmdirc.addons.ui_swing.MainFrame;
import com.dmdirc.addons.ui_swing.SelectionListener;
import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
import com.dmdirc.addons.ui_swing.components.statusbar.SwingStatusBar;
import com.dmdirc.interfaces.ActionListener;
import com.dmdirc.interfaces.Connection;
import com.dmdirc.interfaces.actions.ActionType;
import com.dmdirc.interfaces.config.AggregateConfigProvider;
import com.dmdirc.interfaces.config.ConfigChangeListener;
import com.dmdirc.util.collections.RollingList;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

/**
 * Manages the lifecycle of the lag display plugin.
 */
@Singleton
public class LagDisplayManager implements ActionListener, ConfigChangeListener, SelectionListener {

    /** Frame to listen to selection events on. */
    // TODO: Selection/focus management should be behind an interface
    private final MainFrame mainFrame;
    /** Status bar to add panels to. */
    private final SwingStatusBar statusBar;
    private final Provider<LagDisplayPanel> panelProvider;
    /** The settings domain to use. */
    private final String domain;
    /** Config to read global settings from. */
    private final AggregateConfigProvider globalConfig;
    /** A cache of ping times. */
    private final Map<Connection, String> pings = new WeakHashMap<>();
    /** Ping history. */
    private final Map<Connection, RollingList<Long>> history = new HashMap<>();
    /** Whether or not to show a graph in the info popup. */
    private boolean showGraph = true;
    /** Whether or not to show labels on that graph. */
    private boolean showLabels = true;
    /** The length of history to keep per-server. */
    private int historySize = 100;
    /** The panel currently in use. Null before {@link #load()} or after {@link #unload()}. */
    private LagDisplayPanel panel;

    @Inject
    public LagDisplayManager(
            final MainFrame mainFrame,
            final SwingStatusBar statusBar,
            final Provider<LagDisplayPanel> panelProvider,
            @LagDisplaySettingsDomain final String domain,
            @GlobalConfig final AggregateConfigProvider globalConfig) {
        this.mainFrame = mainFrame;
        this.statusBar = statusBar;
        this.panelProvider = panelProvider;
        this.domain = domain;
        this.globalConfig = globalConfig;
    }

    public void load() {
        panel = panelProvider.get();
        statusBar.addComponent(panel);
        mainFrame.addSelectionListener(this);
        globalConfig.addChangeListener(domain, this);
        readConfig();
        ActionManager.getActionManager().registerListener(this,
                CoreActionType.SERVER_GOTPING, CoreActionType.SERVER_NOPING,
                CoreActionType.SERVER_DISCONNECTED,
                CoreActionType.SERVER_PINGSENT, CoreActionType.SERVER_NUMERIC);
    }

    public void unload() {
        statusBar.removeComponent(panel);
        mainFrame.removeSelectionListener(this);
        globalConfig.removeListener(this);
        ActionManager.getActionManager().unregisterListener(this);
        panel = null;
    }

    /** Reads the plugin's global configuration settings. */
    private void readConfig() {
        showGraph = globalConfig.getOptionBool(domain, "graph");
        showLabels = globalConfig.getOptionBool(domain, "labels");
        historySize = globalConfig.getOptionInt(domain, "history");
    }

    /**
     * Retrieves the history of the specified server. If there is no history, a new list is added to
     * the history map and returned.
     *
     * @param connection The connection whose history is being requested
{'fraction_non_alphanumeric': 0.08096590909090909, 'fraction_numerical': 0.0020833333333333333, 'mean_word_length': 3.623905429071804, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1787690', 'n_tokens_mistral': 2833, 'n_tokens_neox': 2715, 'n_words': 936}