<commit_before>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2020 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * 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 org.sleuthkit.autopsy.casemodule.datasourcesummary;

import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JPanel;
import org.sleuthkit.datamodel.DataSource;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import java.text.DecimalFormat;
import javax.swing.JLabel;
import org.openide.util.NbBundle.Messages;
import org.sleuthkit.autopsy.coreutils.FileTypeUtils;

/**
 * A Pie Chart that shows file mime types in a data source.
 */
class FileTypePieChart extends JPanel {
    private static final Font DEFAULT_FONT = new JLabel().getFont(); 
    private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
    
    private final DefaultPieDataset dataset = new DefaultPieDataset();
    private DataSource dataSource;

    /**
     * Default constructor for the pie chart.
     */
    @Messages({
        "DataSourceSummaryCountsPanel.byMimeTypeLabel.text=Files by MIME Type",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.audio.row=Audio",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.documents.row=Documents",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.executables.row=Executables",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.images.row=Images",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.videos.row=Videos"
    })
    FileTypePieChart() {
        // Create chart
        JFreeChart chart = ChartFactory.createPieChart(
                Bundle.DataSourceSummaryCountsPanel_byMimeTypeLabel_text(),
                dataset,
                true,
                true,
                false);
        
        chart.setBackgroundPaint(null);
        chart.getLegend().setItemFont(DEFAULT_FONT);
        chart.getTitle().setFont(DEFAULT_HEADER_FONT);
        
        PiePlot plot = ((PiePlot) chart.getPlot());
        
        //Format Label
        PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(
                "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%"));
        
        plot.setLabelGenerator(labelGenerator);
        plot.setLabelFont(DEFAULT_FONT);

        plot.setBackgroundPaint(null);
        plot.setOutlinePaint(null);

        
        // Create Panel
        ChartPanel panel = new ChartPanel(chart);
        this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);
    }

    /**
     * The datasource currently used as the model with this pie chart.
     *
     * @return The datasource currently being used as the model in this pie chart.
     */
    DataSource getDataSource() {
        return dataSource;
    }

    /**
     * Sets datasource to visualize in the pie chart.
     *
     * @param dataSource The datasource to use in this pie chart.
     */
    void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.dataset.clear();

        if (dataSource != null) {
            this.dataset.setValue(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_images_row(),
                    getCount(dataSource, FileTypeUtils.FileTypeCategory.IMAGE));
            this.dataset.setValue(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_videos_row(),
                    getCount(dataSource, FileTypeUtils.FileTypeCategory.VIDEO));
            this.dataset.setValue(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_audio_row(),
                    getCount(dataSource, FileTypeUtils.FileTypeCategory.AUDIO));
            this.dataset.setValue(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_documents_row(),
                    getCount(dataSource, FileTypeUtils.FileTypeCategory.DOCUMENTS));
            this.dataset.setValue(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_executables_row(),
                    getCount(dataSource, FileTypeUtils.FileTypeCategory.EXECUTABLE));
        }
    }

    /**
     * Retrieves the counts of files of a particular mime type for a particular
     * DataSource.
     *
     * @param dataSource The DataSource.
     * @param category   The mime type category.
     *
     * @return The count.
     */
    private static Long getCount(DataSource dataSource, FileTypeUtils.FileTypeCategory category) {
        return DataSourceInfoUtilities.getCountOfFilesForMimeTypes(dataSource, category.getMediaTypes());
    }
}
<commit_msg>updates so empty are excluded<commit_after>/*
 * Autopsy Forensic Browser
 *
 * Copyright 2020 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * 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 org.sleuthkit.autopsy.casemodule.datasourcesummary;

import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JPanel;
import org.sleuthkit.datamodel.DataSource;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import java.text.DecimalFormat;
import javax.swing.JLabel;
import org.openide.util.NbBundle.Messages;
import org.sleuthkit.autopsy.coreutils.FileTypeUtils;

/**
 * A Pie Chart that shows file mime types in a data source.
 */
class FileTypePieChart extends JPanel {
    private static final Font DEFAULT_FONT = new JLabel().getFont(); 
    private static final Font DEFAULT_HEADER_FONT = new Font(DEFAULT_FONT.getName(), DEFAULT_FONT.getStyle(), (int) (DEFAULT_FONT.getSize() * 1.5));
    
    private final DefaultPieDataset dataset = new DefaultPieDataset();
    private DataSource dataSource;

    /**
     * Default constructor for the pie chart.
     */
    @Messages({
        "DataSourceSummaryCountsPanel.byMimeTypeLabel.text=Files by MIME Type",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.audio.row=Audio",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.documents.row=Documents",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.executables.row=Executables",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.images.row=Images",
        "DataSourceSummaryCountsPanel.FilesByMimeTypeTableModel.videos.row=Videos"
    })
    FileTypePieChart() {
        // Create chart
        JFreeChart chart = ChartFactory.createPieChart(
                Bundle.DataSourceSummaryCountsPanel_byMimeTypeLabel_text(),
                dataset,
                true,
                true,
                false);
        
        chart.setBackgroundPaint(null);
        chart.getLegend().setItemFont(DEFAULT_FONT);
        chart.getTitle().setFont(DEFAULT_HEADER_FONT);
        
        PiePlot plot = ((PiePlot) chart.getPlot());
        
        //Format Label
        PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(
                "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%"));
        
        plot.setLabelGenerator(labelGenerator);
        plot.setLabelFont(DEFAULT_FONT);

        plot.setBackgroundPaint(null);
        plot.setOutlinePaint(null);

        
        // Create Panel
        ChartPanel panel = new ChartPanel(chart);
        this.setLayout(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);
    }

    /**
     * The datasource currently used as the model with this pie chart.
     *
     * @return The datasource currently being used as the model in this pie chart.
     */
    DataSource getDataSource() {
        return dataSource;
    }

    /**
     * Sets datasource to visualize in the pie chart.
     *
     * @param dataSource The datasource to use in this pie chart.
     */
    void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
        this.dataset.clear();

        if (dataSource != null) {
            addIfPresent(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_images_row(),
                    FileTypeUtils.FileTypeCategory.IMAGE);
           addIfPresent(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_videos_row(), 
                   FileTypeUtils.FileTypeCategory.VIDEO);
            addIfPresent(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_audio_row(),
                    FileTypeUtils.FileTypeCategory.AUDIO);
            addIfPresent(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_documents_row(),
                    FileTypeUtils.FileTypeCategory.DOCUMENTS);
            addIfPresent(Bundle.DataSourceSummaryCountsPanel_FilesByMimeTypeTableModel_executables_row(),
                    FileTypeUtils.FileTypeCategory.EXECUTABLE);
        }
    }
    
    /**
     * Adds count for file type category if there is a value.  Uses fields 'dataSource' and 'dataset'.
     * @param label The label for this pie slice.
     * @param category The category for the pie slice.
     */
    private void addIfPresent(String label, FileTypeUtils.FileTypeCategory category) {
        if (this.dataSource == null) {
            return;
        }
        
        Long count = getCount(this.dataSource, category);
        if (count != null && count > 0) {
            this.dataset.setValue(label, count);
        }
    }

    /**
     * Retrieves the counts of files of a particular mime type for a particular
     * DataSource.
     *
     * @param dataSource The DataSource.
     * @param category   The mime type category.
     *
     * @return The count.
     */
    private static Long getCount(DataSource dataSource, FileTypeUtils.FileTypeCategory category) {
        return DataSourceInfoUtilities.getCountOfFilesForMimeTypes(dataSource, category.getMediaTypes());
    }
}

{'fraction_non_alphanumeric': 0.08552456839309429, 'fraction_numerical': 0.0029216467463479417, 'mean_word_length': 3.0868306801736614, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '7324304', 'n_tokens_mistral': 3158, 'n_tokens_neox': 3033, 'n_words': 724}