<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.apache.isis.client.kroviz.ui.panel

import io.kvision.chart.*
import io.kvision.chart.js.LegendItem
import io.kvision.core.Color
import io.kvision.core.CssSize
import io.kvision.core.UNIT
import io.kvision.panel.SimplePanel
import io.kvision.utils.obj
import org.apache.isis.client.kroviz.core.event.LogEntry
import org.apache.isis.client.kroviz.ui.dialog.EventLogDetail
import org.apache.isis.client.kroviz.ui.core.SessionManager
import kotlin.math.pow

@OptIn(kotlin.js.ExperimentalJsExport::class)
@JsExport
fun openLogEntry(i: Int) {
    val logEntry = SessionManager.getEventStore().log[i]
    EventLogDetail(logEntry).open()
}

@OptIn(kotlin.js.ExperimentalJsExport::class)
@JsExport
fun foo() = "Hello"

class EventBubbleChart() : SimplePanel() {
    private val model = SessionManager.getEventStore()
    private val logStart = model.getLogStartMilliSeconds()
    private var chart: Chart

    init {
        width = CssSize(90, UNIT.vw)
        chart = buildChart()
    }

    private fun buildChart(): Chart {
        return chart(
            configuration = buildConfiguration(),
        )
    }

    private fun buildConfiguration(): Configuration {
        val dataSetsList = listOf(buildDataSets())
        return Configuration(
            type = ChartType.BUBBLE,
            dataSets = dataSetsList,
            labels = buildLabels(),
            options = buildChartOptions(dataSetsList)
        )
    }

    // https://stackoverflow.com/questions/45249779/chart-js-bubble-chart-changing-dataset-labels
    private fun buildChartOptions(dataSetsList: List<DataSets>): ChartOptions {
        val chartOptions = ChartOptions(
            plugins = PluginsOptions(
                title = TitleOptions(
                    text = listOf<String>("Request Duration over Time by Request Density and Response Size"),
                    display = true
                ),
                tooltip = TooltipOptions(callbacks = toolTipCallback())
            ),
            onClick = js(
                "function(e) {"
                        + "var element = e.chart.getElementsAtEventForMode(e, 'nearest', {intersect: true}, true);"
                        + "if (element.length > 0) {"
                        + "var i = element[0].index;"
                        + "console.log(i);"
                        + "kroviz.org.apache.isis.client.kroviz.ui.panel.openLogEntry(i);"
                        + "alert(kroviz.org.apache.isis.client.kroviz.ui.panel.hello());"
                        + "}"
                        + "}"
            ),
            showLine = true,
            scales = mapOf(
                "x" to ChartScales(
                    title = ScaleTitleOptions(
                        text = "Time since Connect(ms)", display = true
                    )
                ),
                "y" to ChartScales(
                    title = ScaleTitleOptions(text = "duration in ms (log)", display = true),
                    type = ScalesType.LOGARITHMIC
                )
            )
        )
        return chartOptions
    }

    // https://www.youtube.com/watch?v=UxJ5d-HGhJA
    // https://en.wikipedia.org/wiki/Clarke%27s_three_laws -> #2
    // I would have appreciated a real API.
    private fun toolTipCallback(): TooltipCallback {<commit_msg>ISIS-2957 clicking bubble opens EventLogDetail<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.apache.isis.client.kroviz.ui.panel

import io.kvision.chart.*
import io.kvision.chart.js.LegendItem
import io.kvision.core.Color
import io.kvision.core.CssSize
import io.kvision.core.UNIT
import io.kvision.panel.SimplePanel
import io.kvision.utils.obj
import org.apache.isis.client.kroviz.core.event.LogEntry
import org.apache.isis.client.kroviz.ui.dialog.EventLogDetail
import org.apache.isis.client.kroviz.ui.core.SessionManager
import kotlin.math.pow

@OptIn(kotlin.js.ExperimentalJsExport::class)
@JsExport
fun openLogEntry(i: Int) {
    val logEntry = SessionManager.getEventStore().log[i]
    EventLogDetail(logEntry).open()
}

class EventBubbleChart() : SimplePanel() {
    private val model = SessionManager.getEventStore()
    private val logStart = model.getLogStartMilliSeconds()
    private var chart: Chart

    init {
        width = CssSize(90, UNIT.vw)
        chart = buildChart()
    }

    private fun buildChart(): Chart {
        return chart(
            configuration = buildConfiguration(),
        )
    }

    private fun buildConfiguration(): Configuration {
        val dataSetsList = listOf(buildDataSets())
        return Configuration(
            type = ChartType.BUBBLE,
            dataSets = dataSetsList,
            labels = buildLabels(),
            options = buildChartOptions(dataSetsList)
        )
    }

    // https://stackoverflow.com/questions/45249779/chart-js-bubble-chart-changing-dataset-labels
    private fun buildChartOptions(dataSetsList: List<DataSets>): ChartOptions {
        val chartOptions = ChartOptions(
            plugins = PluginsOptions(
                title = TitleOptions(
                    text = listOf<String>("Request Duration over Time by Request Density and Response Size"),
                    display = true
                ),
                tooltip = TooltipOptions(callbacks = toolTipCallback())
            ),
            onClick = js(
                "function(e) {"
                        + "var element = e.chart.getElementsAtEventForMode(e, 'nearest', {intersect: true}, true);"
                        + "if (element.length > 0) {"
                        + "var i = element[0].index;"
                        + "kroviz.org.apache.isis.client.kroviz.ui.panel.openLogEntry(i);"
                        + "}"
                        + "}"
            ),
            showLine = true,
            scales = mapOf(
                "x" to ChartScales(
                    title = ScaleTitleOptions(
                        text = "Time since Connect(ms)", display = true
                    )
                ),
                "y" to ChartScales(
                    title = ScaleTitleOptions(text = "duration in ms (log)", display = true),
                    type = ScalesType.LOGARITHMIC
                )
            )
        )
        return chartOptions
    }

    // https://www.youtube.com/watch?v=UxJ5d-HGhJA
    // https://en.wikipedia.org/wiki/Clarke%27s_three_laws -> #2
    // I would have appreciated a real API.
    private fun toolTipCallback(): TooltipCallback {
{'fraction_non_alphanumeric': 0.09565539372994322, 'fraction_numerical': 0.0054307578375709705, 'mean_word_length': 2.1431342125678823, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, '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': '2898508', 'n_tokens_mistral': 2261, 'n_tokens_neox': 2172, 'n_words': 575}