<commit_before>package charts.builder;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Year;
import org.jfree.data.xy.XYDataset;

import charts.CotsOutbreak;
import charts.Dimensions;
import charts.representations.Format;
import charts.representations.Representation;
import charts.spreadsheet.DataSource;

public class CotsOutbreakSpreadsheetBuilder extends DefaultSpreadsheetChartBuilder {

    public CotsOutbreakSpreadsheetBuilder() {
        super(ChartType.COTS_OUTBREAK);
    }

    private boolean isCotsOutbreakSpreadsheet(DataSource datasource) {
        try {
            return "TOTALOUTBREAKS".equalsIgnoreCase(datasource.select("B1").format("value"));
        } catch(Exception e) {
            return false;
        }
    }

    private XYDataset createDataset(DataSource datasource) {
        int i = 2;
        TimeSeries s1 = new TimeSeries("outbreaks");
        while(true) {
            try {
                String year = datasource.select("A"+i).format("value");
                String outbreaks = datasource.select("B"+i).format("value");
                if(StringUtils.isNotBlank(year) && StringUtils.isNotBlank(outbreaks)) {
                    double val = Double.parseDouble(outbreaks);
                    s1.add(new Year(parseYear(year)), val);
                } else {
                    break;
                }
                i++;
            } catch(Exception e) {
                break;
            }
        }
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        return dataset;
    }

    private int parseYear(String y) {
        y = StringUtils.strip(y);
        if(y.contains(".")) {
            y = StringUtils.substringBefore(y, ".");
        }
        return Integer.parseInt(y);
    }

    @Override
    boolean canHandle(DataSource datasource) {
        return isCotsOutbreakSpreadsheet(datasource);
    }

    @Override
    Chart build(DataSource datasource, final Region region,
        final Map<String, String[]> query) {
      if (region.equals(Region.GBR)) {
        XYDataset dataset = createDataset(datasource);
        final JFreeChart jfreechart = new CotsOutbreak().createChart(dataset);
        final Chart chart = new AbstractChart(query) {
          @Override
          public ChartDescription getDescription() {
            return new ChartDescription(ChartType.COTS_OUTBREAK, region);
          }
          @Override
          public Dimensions getChart() {
            return createDimensions(jfreechart, query);
          }
          @Override
          public String getCSV() throws UnsupportedFormatException {
            throw new Chart.UnsupportedFormatException();
          }
        };
        return chart;
      } else {
        return null;
      }
    }

}<commit_msg>Adding CSV for COTS outbreak chart.<commit_after>package charts.builder;

import java.io.IOException;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.data.time.Year;
import org.jfree.data.xy.XYDataset;
import org.supercsv.io.CsvListWriter;
import org.supercsv.prefs.CsvPreference;

import charts.CotsOutbreak;
import charts.Dimensions;
import charts.BeerCoaster.Category;
import charts.BeerCoaster.Indicator;
import charts.representations.Format;
import charts.representations.Representation;
import charts.spreadsheet.DataSource;

public class CotsOutbreakSpreadsheetBuilder extends DefaultSpreadsheetChartBuilder {

    public CotsOutbreakSpreadsheetBuilder() {
        super(ChartType.COTS_OUTBREAK);
    }

    private boolean isCotsOutbreakSpreadsheet(DataSource datasource) {
        try {
            return "TOTALOUTBREAKS".equalsIgnoreCase(datasource.select("B1").format("value"));
        } catch(Exception e) {
            return false;
        }
    }

    private TimeSeriesCollection createDataset(DataSource datasource) {
        int i = 2;
        TimeSeries s1 = new TimeSeries("outbreaks");
        while(true) {
            try {
                String year = datasource.select("A"+i).format("value");
                String outbreaks = datasource.select("B"+i).format("value");
                if(StringUtils.isNotBlank(year) && StringUtils.isNotBlank(outbreaks)) {
                    double val = Double.parseDouble(outbreaks);
                    s1.add(new Year(parseYear(year)), val);
                } else {
                    break;
                }
                i++;
            } catch(Exception e) {
                break;
            }
        }
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        return dataset;
    }

    private int parseYear(String y) {
        y = StringUtils.strip(y);
        if(y.contains(".")) {
            y = StringUtils.substringBefore(y, ".");
        }
        return Integer.parseInt(y);
    }

    @Override
    boolean canHandle(DataSource datasource) {
        return isCotsOutbreakSpreadsheet(datasource);
    }

    @Override
    Chart build(DataSource datasource, final Region region,
        final Map<String, String[]> query) {
      if (region.equals(Region.GBR)) {
        final TimeSeriesCollection dataset = createDataset(datasource);
        final JFreeChart jfreechart = new CotsOutbreak().createChart(dataset);
        final Chart chart = new AbstractChart(query) {
          @Override
          public ChartDescription getDescription() {
            return new ChartDescription(ChartType.COTS_OUTBREAK, region);
          }
          @Override
          public Dimensions getChart() {
            return createDimensions(jfreechart, query);
          }
          @SuppressWarnings("unchecked")
          @Override
          public String getCSV() {
            final StringWriter sw = new StringWriter();
            try {
              final CsvListWriter csv = new CsvListWriter(sw,
                  CsvPreference.STANDARD_PREFERENCE);
              final DateFormat yearOnly = new SimpleDateFormat("YYYY");
              csv.write("Year", "Outbreaks");
              final List<TimeSeriesDataItem> items =
                  dataset.getSeries(0).getItems();
              for (TimeSeriesDataItem i : items) {
                csv.write(
                  yearOnly.format(i.getPeriod().getEnd()),
                  i.getValue().intValue()+"");
              }
              csv.close();
            } catch (IOException e) {
              // How on earth would you get an IOException with a StringWriter?
              throw new RuntimeException(e);
            }
            return sw.toString();
          }
        };
        return chart;
      } else {
        return null;
      }
    }

}
{'fraction_non_alphanumeric': 0.09694163860830528, 'fraction_numerical': 0.001823793490460157, 'mean_word_length': 2.224332881049299, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5155565', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1896, 'n_words': 386}