<commit_before>neon.ready(function () {
    var databaseName;
    var tableName;
    var filterKey;
    var chart;

    var HOURS_IN_WEEK = 168;
    var HOURS_IN_DAY = 24;

    var clientId = neon.eventing.messaging.getInstanceId();

    initialize();

    function initialize() {

        neon.query.SERVER_URL = $("#neon-server").val();

        neon.eventing.messaging.registerForNeonEvents({
            activeDatasetChanged: onDatasetChanged,
            filtersChanged: onFiltersChanged
        });

        neon.toggle.createOptionsPanel("#options-panel");
        initChart();
        restoreState();
    }

    function initChart() {
        chart = circularHeatChart()
            .segmentHeight(20)
            .innerRadius(20)
            .numSegments(24)
            .radialLabels(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
            .segmentLabels(["12am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "12pm", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"])
            .margin({top: 20, right: 20, bottom: 20, left: 20});
        queryForChartData();
    }

    function onFiltersChanged(message) {
        queryForChartData();
    }

    function onDatasetChanged(message) {
        databaseName = message.database;
        tableName = message.table;
        neon.query.registerForFilterKey(databaseName, tableName, function (filterResponse) {
            filterKey = filterResponse;
        });
        neon.query.getFieldNames(databaseName, tableName, neon.widget.CIRCULAR_HEAT, populateFromColumns);
    }

    function populateFromColumns(data) {
        neon.dropdown.populateAttributeDropdowns(data, 'date', queryForChartData);
    }

    function queryForChartData() {
        var dateField = getDateField();

        if (!dateField) {
            drawChart({data: []});
            return;
        }

        //TODO: NEON-603 Add support for dayOfWeek to query API
        var groupByDayClause = new neon.query.GroupByFunctionClause('dayOfWeek', dateField, 'day');
        var groupByHourClause = new neon.query.GroupByFunctionClause(neon.query.HOUR, dateField, 'hour');

        var query = new neon.query.Query()
            .selectFrom(databaseName, tableName)
            .groupBy(groupByDayClause, groupByHourClause)
            .where(dateField, '!=', null)
            .aggregate(neon.query.COUNT, null, 'count');

        var stateObject = buildStateObject(dateField, query);

        neon.query.executeQuery(query, drawChart);
        neon.query.saveState(clientId, stateObject);
    }

    function drawChart(queryResults) {
        rawData = queryResults.data;

        data = [];

        for (ii = 0; ii < HOURS_IN_WEEK; ++ii) {
            data[ii] = 0;
        }

        rawData.forEach(function (d) {
            data[(d.day - 1) * HOURS_IN_DAY + d.hour] = d.count;
        });

        d3.select('#circularheatchart')
            .selectAll('svg')
            .remove();

        d3.select('#circularheatchart')
            .selectAll('svg')
            .data([data])
            .enter()
            .append('svg')
            .call(chart);
    }

    function getDateField() {
        return $('#date option:selected').val();
    }

    function buildStateObject(dateField, query) {
        return {<commit_msg>NEON-629 -- Remove global variables.<commit_after>neon.ready(function () {
    var databaseName;
    var tableName;
    var filterKey;
    var chart;

    var HOURS_IN_WEEK = 168;
    var HOURS_IN_DAY = 24;

    var clientId = neon.eventing.messaging.getInstanceId();

    initialize();

    function initialize() {

        neon.query.SERVER_URL = $("#neon-server").val();

        neon.eventing.messaging.registerForNeonEvents({
            activeDatasetChanged: onDatasetChanged,
            filtersChanged: onFiltersChanged
        });

        neon.toggle.createOptionsPanel("#options-panel");
        initChart();
        restoreState();
    }

    function initChart() {
        chart = circularHeatChart()
            .segmentHeight(20)
            .innerRadius(20)
            .numSegments(24)
            .radialLabels(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"])
            .segmentLabels(["12am", "1am", "2am", "3am", "4am", "5am", "6am", "7am", "8am", "9am", "10am", "11am", "12pm", "1pm", "2pm", "3pm", "4pm", "5pm", "6pm", "7pm", "8pm", "9pm", "10pm", "11pm"])
            .margin({top: 20, right: 20, bottom: 20, left: 20});
        queryForChartData();
    }

    function onFiltersChanged(message) {
        queryForChartData();
    }

    function onDatasetChanged(message) {
        databaseName = message.database;
        tableName = message.table;
        neon.query.registerForFilterKey(databaseName, tableName, function (filterResponse) {
            filterKey = filterResponse;
        });
        neon.query.getFieldNames(databaseName, tableName, neon.widget.CIRCULAR_HEAT, populateFromColumns);
    }

    function populateFromColumns(data) {
        neon.dropdown.populateAttributeDropdowns(data, 'date', queryForChartData);
    }

    function queryForChartData() {
        var dateField = getDateField();

        if (!dateField) {
            drawChart({data: []});
            return;
        }

        //TODO: NEON-603 Add support for dayOfWeek to query API
        var groupByDayClause = new neon.query.GroupByFunctionClause('dayOfWeek', dateField, 'day');
        var groupByHourClause = new neon.query.GroupByFunctionClause(neon.query.HOUR, dateField, 'hour');

        var query = new neon.query.Query()
            .selectFrom(databaseName, tableName)
            .groupBy(groupByDayClause, groupByHourClause)
            .where(dateField, '!=', null)
            .aggregate(neon.query.COUNT, null, 'count');

        var stateObject = buildStateObject(dateField, query);

        neon.query.executeQuery(query, drawChart);
        neon.query.saveState(clientId, stateObject);
    }

    function drawChart(queryResults) {
        var rawData = queryResults.data;

        var data = [];

        for (var i = 0; i < HOURS_IN_WEEK; i++) {
            data[i] = 0;
        }

        _.each(rawData, function (element) {
            data[(element.day - 1) * HOURS_IN_DAY + element.hour] = element.count;
        });

        d3.select('#circularheatchart')
            .selectAll('svg')
            .remove();

        d3.select('#circularheatchart')
            .selectAll('svg')
            .data([data])
            .enter()
            .append('svg')
            .call(chart);
    }

    function getDateField() {
        return $('#date option:selected').val();
    }

    function buildStateObject(dateField, query) {
        return {
{'fraction_non_alphanumeric': 0.1374169741697417, 'fraction_numerical': 0.01726937269372694, 'mean_word_length': 2.3527956457199406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5759612', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2019, 'n_words': 389}