<commit_before>/*
 * Copyright (c) 2008-2010, Hazel Ltd. All Rights Reserved.
 *
 * 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 com.hazelcast.monitor.server;

import com.hazelcast.monitor.client.event.ChangeEventType;
import com.hazelcast.monitor.client.event.InstanceStatistics;
import com.hazelcast.monitor.server.event.ChangeEventGenerator;
import com.hazelcast.monitor.server.event.InstanceStatisticsGenerator;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import static com.hazelcast.monitor.server.HazelcastServiceImpl.getSessionObject;

public abstract class InstanceChartGenerator extends HttpServlet {
    static void increaseRange(NumberAxis axis) {
        Range range = axis.getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        double diff = upper - lower;
        axis.setRange(lower - diff * 0.3, upper + diff * 0.3);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        SessionObject sessionObject = getSessionObject(req.getSession());
        List<? super InstanceStatistics> list = getPastStatistics(name, sessionObject);
        if (list == null) {
            return;
        }
        JFreeChart chart;
        if ("size".equals(type)) {
            chart = generateSizeChart(list);
        } else {
            chart = generateOperationStatsChart(list);
        }
        try {
            OutputStream out = response.getOutputStream();
            response.setContentType("image/png");
            ChartUtilities.writeChartAsPNG(out, chart, 390, 250);
        } catch (IOException ignore) {
        }
    }

    private List<? super InstanceStatistics> getPastStatistics(String name, SessionObject sessionObject) {
        List<? super InstanceStatistics> list = null;
        for (ChangeEventGenerator eventGenerator : sessionObject.eventGenerators) {
            if (eventGenerator.getChangeEventType().equals(getChangeEventType())) {
                InstanceStatisticsGenerator msg = (InstanceStatisticsGenerator) eventGenerator;
                if (!msg.getName().equals(name)) {
                    continue;
                }
                list = msg.getPastStatistics();
            }
        }
        return list;
    }

    abstract ChangeEventType getChangeEventType();

    public JFreeChart generateOperationStatsChart(List<? super InstanceStatistics> list) {
        TimeSeries ts = new TimeSeries("operations per second");
        for (int i = 0; i < list.size(); i++) {
            InstanceStatistics instanceStatistics = (InstanceStatistics) list.get(i);
            ts.addOrUpdate(new Second(instanceStatistics.getCreatedDate()), instanceStatistics.getTotalOPS() / 1000);
        }
        TimeSeriesCollection timeDataset = new TimeSeriesCollection();
        timeDataset.addSeries(ts);
        JFreeChart chart =
                ChartFactory.createTimeSeriesChart(null, "time", "throughput (x1000)", timeDataset, true, true, true);
        XYPlot plot = (XYPlot) chart.getPlot();
        increaseRange((NumberAxis) plot.getRangeAxis(0));
        return chart;
    }

    public JFreeChart generateSizeChart(List<? super InstanceStatistics> list) {
        TimeSeries ts = new TimeSeries("size");
        for (int i = 0; i < list.size(); i++) {
            InstanceStatistics instanceStatistics = (InstanceStatistics) list.get(i);
            ts.addOrUpdate(new Second(instanceStatistics.getCreatedDate()), new Double(instanceStatistics.getSize() / (double) 1000));
        }
        TimeSeriesCollection timeDataset = new TimeSeriesCollection();
        timeDataset.addSeries(ts);
        JFreeChart chart =
                ChartFactory.createTimeSeriesChart(null, "time", "size (x1000)", timeDataset, true, true, true);
        XYPlot plot = (XYPlot) chart.getPlot();
        NumberAxis sizeAxis = (NumberAxis) plot.getRangeAxis(0);
        increaseRange(sizeAxis);
        afterPlot(list, chart, plot);
        return chart;
    }

    protected abstract void afterPlot(List<? super InstanceStatistics> list, JFreeChart chart, XYPlot plot);
}
<commit_msg>throughput less than 1000 is seen as 0. Fixed by casting to double<commit_after>/*
 * Copyright (c) 2008-2010, Hazel Ltd. All Rights Reserved.
 *
 * 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 com.hazelcast.monitor.server;

import com.hazelcast.monitor.client.event.ChangeEventType;
import com.hazelcast.monitor.client.event.InstanceStatistics;
import com.hazelcast.monitor.server.event.ChangeEventGenerator;
import com.hazelcast.monitor.server.event.InstanceStatisticsGenerator;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import static com.hazelcast.monitor.server.HazelcastServiceImpl.getSessionObject;

public abstract class InstanceChartGenerator extends HttpServlet {
    static void increaseRange(NumberAxis axis) {
        Range range = axis.getRange();
        double lower = range.getLowerBound();
        double upper = range.getUpperBound();
        double diff = upper - lower;
        axis.setRange(lower - diff * 0.3, upper + diff * 0.3);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        SessionObject sessionObject = getSessionObject(req.getSession());
        List<? super InstanceStatistics> list = getPastStatistics(name, sessionObject);
        if (list == null) {
            return;
        }
        JFreeChart chart;
        if ("size".equals(type)) {
            chart = generateSizeChart(list);
        } else {
            chart = generateOperationStatsChart(list);
        }
        try {
            OutputStream out = response.getOutputStream();
            response.setContentType("image/png");
            ChartUtilities.writeChartAsPNG(out, chart, 390, 250);
        } catch (IOException ignore) {
        }
    }

    private List<? super InstanceStatistics> getPastStatistics(String name, SessionObject sessionObject) {
        List<? super InstanceStatistics> list = null;
        for (ChangeEventGenerator eventGenerator : sessionObject.eventGenerators) {
            if (eventGenerator.getChangeEventType().equals(getChangeEventType())) {
                InstanceStatisticsGenerator msg = (InstanceStatisticsGenerator) eventGenerator;
                if (!msg.getName().equals(name)) {
                    continue;
                }
                list = msg.getPastStatistics();
            }
        }
        return list;
    }

    abstract ChangeEventType getChangeEventType();

    public JFreeChart generateOperationStatsChart(List<? super InstanceStatistics> list) {
        TimeSeries ts = new TimeSeries("operations per second");
        for (int i = 0; i < list.size(); i++) {
            InstanceStatistics instanceStatistics = (InstanceStatistics) list.get(i);
            ts.addOrUpdate(new Second(instanceStatistics.getCreatedDate()), (double)instanceStatistics.getTotalOPS() / 1000);
        }
        TimeSeriesCollection timeDataset = new TimeSeriesCollection();
        timeDataset.addSeries(ts);
        JFreeChart chart =
                ChartFactory.createTimeSeriesChart(null, "time", "throughput (x1000)", timeDataset, true, true, true);
        XYPlot plot = (XYPlot) chart.getPlot();
        increaseRange((NumberAxis) plot.getRangeAxis(0));
        return chart;
    }

    public JFreeChart generateSizeChart(List<? super InstanceStatistics> list) {
        TimeSeries ts = new TimeSeries("size");
        for (int i = 0; i < list.size(); i++) {
            InstanceStatistics instanceStatistics = (InstanceStatistics) list.get(i);
            ts.addOrUpdate(new Second(instanceStatistics.getCreatedDate()), new Double(instanceStatistics.getSize() / (double) 1000));
        }
        TimeSeriesCollection timeDataset = new TimeSeriesCollection();
        timeDataset.addSeries(ts);
        JFreeChart chart =
                ChartFactory.createTimeSeriesChart(null, "time", "size (x1000)", timeDataset, true, true, true);
        XYPlot plot = (XYPlot) chart.getPlot();
        NumberAxis sizeAxis = (NumberAxis) plot.getRangeAxis(0);
        increaseRange(sizeAxis);
        afterPlot(list, chart, plot);
        return chart;
    }

    protected abstract void afterPlot(List<? super InstanceStatistics> list, JFreeChart chart, XYPlot plot);
}

{'fraction_non_alphanumeric': 0.09886181920797667, 'fraction_numerical': 0.008371743015708775, 'mean_word_length': 3.2257551669316373, 'pattern_counts': {'":': 0, '<': 19, '<?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': '2148435', 'n_tokens_mistral': 2846, 'n_tokens_neox': 2752, 'n_words': 728}