<commit_before>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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 uk.co.reecedunn.intellij.plugin.marklogic.ui.log

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import uk.co.reecedunn.intellij.plugin.marklogic.api.Connection
import uk.co.reecedunn.intellij.plugin.marklogic.api.LogRequestBuilder
import uk.co.reecedunn.intellij.plugin.marklogic.log.MarkLogicLogFile
import uk.co.reecedunn.intellij.plugin.marklogic.server.LogType
import uk.co.reecedunn.intellij.plugin.marklogic.server.MarkLogicAppServer
import uk.co.reecedunn.intellij.plugin.marklogic.ui.settings.MarkLogicSettings
import uk.co.reecedunn.intellij.plugin.marklogic.server.MarkLogicServer
import uk.co.reecedunn.intellij.plugin.marklogic.ui.server.MarkLogicServerCellRenderer

import javax.swing.*
import java.io.IOException

class MarkLogicLogViewUI(private val mProject: Project) : LogViewActions {
    private inner class SettingsListener : MarkLogicSettings.Listener, Disposable {
        override fun serversChanged() {
            val settings = MarkLogicSettings.getInstance()

            mServer!!.removeAllItems()
            for (server in settings.servers) {
                mServer!!.addItem(server)
            }
        }

        override fun dispose() {
        }
    }

    private var mConnection: Connection? = null
    private var mLogBuilder: LogRequestBuilder? = null

    private var mActions: MarkLogicLogViewToolbar? = null
    private var mActionToolbar: JComponent? = null

    private var mPanel: JPanel? = null
    private var mLogText: JTextArea? = null
    private var mServer: JComboBox<MarkLogicServer>? = null
    private var mAppServer: JComboBox<MarkLogicAppServer>? = null

    val panel get(): JComponent? =
        mPanel

    private fun createUIComponents() {
        mActions = MarkLogicLogViewToolbar(this)
        mActionToolbar = mActions!!.component

        mLogText = JTextArea()
        mLogText!!.isEditable = false

        mServer = ComboBox()
        mServer!!.renderer = MarkLogicServerCellRenderer()
        mServer!!.addActionListener { e -> serverSelectionChanged() }

        mAppServer = ComboBox()
        mAppServer!!.addActionListener { e -> appserverSelectionChanged() }

        val settings = MarkLogicSettings.getInstance()
        val listener = SettingsListener()
        settings.addListener(listener, listener)
        listener.serversChanged()
    }

    private fun serverSelectionChanged() {
        val server = mServer?.selectedItem as? MarkLogicServer
        if (server == null) {
            mLogText!!.text = ""
            return
        }

        ApplicationManager.getApplication().executeOnPooledThread {
            mAppServer!!.removeAllItems()
            for (appserver in server.appservers) {
                mAppServer!!.addItem(appserver)
            }
        }

        mConnection = Connection.newConnection(
            server.hostname,
            server.adminPort,
            server.username,
            server.password,
            Connection.REST)

        mLogBuilder = mConnection!!.createLogRequestBuilder()
        appserverSelectionChanged()
    }

    private fun appserverSelectionChanged() {
        val appserver = mAppServer?.selectedItem as? MarkLogicAppServer
        if (mLogBuilder == null || appserver == null) return

        mLogBuilder!!.logFile = appserver.logfile(LogType.ERROR_LOG, 0)
        ApplicationManager.getApplication().executeOnPooledThread(refreshAction())
    }

    override fun refreshAction(): Runnable {
        return Runnable {
            try {
                val items = mLogBuilder!!.build().run().items
                mLogText!!.text = ""
                MarkLogicLogFile.parse(items[0].content).forEach { entry ->
                    val separator = if (entry.continuation) '+' else ' '
                    if (entry.appserver == null) {
                        mLogText!!.append("${entry.date} ${entry.time} ${entry.level}:${separator}${entry.message.content}\n")
                    } else {
                        mLogText!!.append("${entry.date} ${entry.time} ${entry.level}: ${entry.appserver}: ${entry.message.content}\n")
                    }
                }
                mLogText!!.caretPosition = mLogText!!.document.length
            } catch (e: IOException) {
                mLogText!!.text = e.message
            }
        }
    }
}<commit_msg>Log View: Filter the logs to the selected App-Server for MarkLogic <= 8.0.<commit_after>/*
 * Copyright (C) 2017 <NAME>
 *
 * 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 uk.co.reecedunn.intellij.plugin.marklogic.ui.log

import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.ComboBox
import uk.co.reecedunn.intellij.plugin.marklogic.api.Connection
import uk.co.reecedunn.intellij.plugin.marklogic.api.LogRequestBuilder
import uk.co.reecedunn.intellij.plugin.marklogic.log.MarkLogicLogFile
import uk.co.reecedunn.intellij.plugin.marklogic.server.LogType
import uk.co.reecedunn.intellij.plugin.marklogic.server.MarkLogicAppServer
import uk.co.reecedunn.intellij.plugin.marklogic.ui.settings.MarkLogicSettings
import uk.co.reecedunn.intellij.plugin.marklogic.server.MarkLogicServer
import uk.co.reecedunn.intellij.plugin.marklogic.ui.server.MarkLogicServerCellRenderer

import javax.swing.*
import java.io.IOException

class MarkLogicLogViewUI(private val mProject: Project) : LogViewActions {
    private inner class SettingsListener : MarkLogicSettings.Listener, Disposable {
        override fun serversChanged() {
            val settings = MarkLogicSettings.getInstance()

            mServer!!.removeAllItems()
            for (server in settings.servers) {
                mServer!!.addItem(server)
            }
        }

        override fun dispose() {
        }
    }

    private var mConnection: Connection? = null
    private var mLogBuilder: LogRequestBuilder? = null

    private var mActions: MarkLogicLogViewToolbar? = null
    private var mActionToolbar: JComponent? = null

    private var mPanel: JPanel? = null
    private var mLogText: JTextArea? = null
    private var mServer: JComboBox<MarkLogicServer>? = null
    private var mAppServer: JComboBox<MarkLogicAppServer>? = null

    val panel get(): JComponent? =
        mPanel

    private fun createUIComponents() {
        mActions = MarkLogicLogViewToolbar(this)
        mActionToolbar = mActions!!.component

        mLogText = JTextArea()
        mLogText!!.isEditable = false

        mServer = ComboBox()
        mServer!!.renderer = MarkLogicServerCellRenderer()
        mServer!!.addActionListener { e -> serverSelectionChanged() }

        mAppServer = ComboBox()
        mAppServer!!.addActionListener { e -> appserverSelectionChanged() }

        val settings = MarkLogicSettings.getInstance()
        val listener = SettingsListener()
        settings.addListener(listener, listener)
        listener.serversChanged()
    }

    private fun serverSelectionChanged() {
        val server = mServer?.selectedItem as? MarkLogicServer
        if (server == null) {
            mLogText!!.text = ""
            return
        }

        ApplicationManager.getApplication().executeOnPooledThread {
            mAppServer!!.removeAllItems()
            for (appserver in server.appservers) {
                mAppServer!!.addItem(appserver)
            }
        }

        mConnection = Connection.newConnection(
            server.hostname,
            server.adminPort,
            server.username,
            server.password,
            Connection.REST)

        mLogBuilder = mConnection!!.createLogRequestBuilder()
        appserverSelectionChanged()
    }

    private fun appserverSelectionChanged() {
        val appserver = mAppServer?.selectedItem as? MarkLogicAppServer
        if (mLogBuilder == null || appserver == null) return

        mLogBuilder!!.logFile = appserver.logfile(LogType.ERROR_LOG, 0)
        ApplicationManager.getApplication().executeOnPooledThread(refreshAction())
    }

    override fun refreshAction(): Runnable {
        return Runnable {
            try {
                val appserver = (mAppServer?.selectedItem as? MarkLogicAppServer)?.let {
                    if (it === MarkLogicAppServer.SYSTEM) null else it.appserver
                }
                val server = (mServer?.selectedItem as? MarkLogicServer)?.version?.split('-')?.get(0)?.toDouble() ?: 6.0

                val items = mLogBuilder!!.build().run().items
                mLogText!!.text = ""
                MarkLogicLogFile.parse(items[0].content).forEach { entry ->
                    if (server >= 9.0) {
                        val separator = if (entry.continuation) '+' else ' '
                        mLogText!!.append("${entry.date} ${entry.time} ${entry.level}:${separator}${entry.message.content}\n")
                    } else if (appserver == entry.appserver) {
                        mLogText!!.append("${entry.date} ${entry.time} ${entry.level}: ${entry.message.content}\n")
                    }
                }
                mLogText!!.caretPosition = mLogText!!.document.length
            } catch (e: IOException) {
                mLogText!!.text = e.message
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.0984870111333143, 'fraction_numerical': 0.002569226377390808, 'mean_word_length': 2.616655196145905, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, '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': '143795', 'n_tokens_mistral': 2931, 'n_tokens_neox': 2831, 'n_words': 634}