<commit_before>(function (timespanSelection , graphFactory, utils, dataSource) {
    var margin = {top: 10, right: 0, bottom: 30, left: 35},
        width = graphFactory.size - margin.left - margin.right,
        height = graphFactory.size - margin.top - margin.bottom;

    var x = d3.time.scale()
            .range([0, width]);

    var y = d3.scale.linear()
            .domain([0, 24])
            .range([height, 0]);

    var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

    var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left")
            .outerTickSize(0)
            .tickValues([0, 6, 9, 12, 15, 18])
            .tickFormat(function (d) {
                if (d < 12) {
                    return d + 'am';
                } else if (d === 12) {
                    return d + 'pm';
                } else if (d === 24) {
                    return '0am';
                } else {
                    return (d - 12) + 'pm';
                }
            });

    var timeOfDay = function (date) {
        return (date.getTime() - (date.getTimezoneOffset() * 60 * 1000)) % (24 * 60 * 60 * 1000) / (60 * 60 * 1000);
    };

    var startOfDay = function (date) {
        return new Date(date.getFullYear(), date.getMonth(), date.getDate());
    };

    var endOfDay = function (date) {
        var nextDay = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);

        return new Date(nextDay.getTime() - 1);
    };

    var phasesByDay = function (start, end) {
        var phases = [],
            startDate = new Date(start),
            endDate = end ? new Date(end) : undefined,
            endOfCurrentDay = endOfDay(startDate);

        if (endDate) {
            while (endOfCurrentDay < endDate) {
                phases.push({
                    start: startDate,
                    end: endOfCurrentDay
                });

                startDate = new Date(endOfCurrentDay.getTime() + 1);
                endOfCurrentDay = endOfDay(startDate);
            }
        }

        phases.push({
            start: startDate,
            end: endDate
        });

        return phases;
    };

    var flatten = function (listOfLists) {
        return listOfLists.reduce(function (a, b) { return a.concat(b); }, []);
    };

    var calculatePhasesByDay = function (data) {
        var lastEntry;

        return flatten(data.map(function (entry) {
            var greenPhases = [],
                start = entry.start;

            if (lastEntry) {
                greenPhases = phasesByDay(lastEntry.end + 1, entry.start - 1).map(function (phase) {
                    phase.color = 'green';
                    phase.duration = entry.start - lastEntry.end;
                    phase.phaseStart = new Date(lastEntry.end);
                    phase.phaseEnd = new Date(entry.start);
                    return phase;
                });
            }

            lastEntry = entry;

            var redPhases = phasesByDay(entry.start, entry.end).filter(function (phase) {
                return phase.end !== undefined;
            }).map(function (phase) {
                phase.color = 'red';
                phase.culprits = entry.culprits;
                phase.duration = entry.end - entry.start;
                phase.phaseStart = new Date(entry.start);
                phase.phaseEnd = new Date(entry.end);
                return phase;
            });

            return greenPhases.concat(redPhases);
        }));
    };

    var annotateDateAndTime = function (phases) {
        return phases.map(function (phase) {
            phase.startOfDay = startOfDay(phase.start);
            phase.endOfDay = endOfDay(phase.end);
            phase.startTime = timeOfDay(phase.start);
            phase.endTime = timeOfDay(phase.end);
            return phase;
        });
    };

    var isWeekend = function (date) {
        var dayOfWeek = date.getDay();
        return dayOfWeek === 0 || dayOfWeek === 6;
    };

    var shortTimeString = function (date, referenceDate) {
        if (startOfDay(date).valueOf() === startOfDay(referenceDate).valueOf()) {
            return date.toLocaleTimeString();
        } else {
            return date.toLocaleTimeString() + ' (' + date.toLocaleDateString() + ')';
        }
    };

    var saneDayTicks = function (axis, scale) {
        var dayCount = (x.domain()[1] - x.domain()[0]) / (24 * 60 * 60 * 1000);
        if (dayCount < 10) {
            axis.ticks(d3.time.days, 1);
        }
        return axis;
    };

    var axesPane,
        getOrCreateAxesPane = function (svg) {
            if (axesPane === undefined) {
                axesPane = svg
                    .append("g")
                    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

                axesPane.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + height + ")");

                axesPane.append("g")
                    .attr("class", "y axis")
                    .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", "-1.5em")
                    .attr("dx", "-1.5em")
                    .style("text-anchor", "end")
                    .text("Time of the day");
            }

            return axesPane;
        };

    var renderData = function (data, svg) {
        var phasesByDay = annotateDateAndTime(calculatePhasesByDay(data));

        if (!phasesByDay.length) {
            return;
        }

        x.domain([d3.min(phasesByDay, function(d) { return d.startOfDay; }),
                  d3.max(phasesByDay, function(d) { return d.endOfDay; })]);

        var g = getOrCreateAxesPane(svg);

        g.selectAll('.x.axis')
            .call(saneDayTicks(xAxis, x));
<commit_msg>Correctly update axes when selecting timespan for fail phases<commit_after>(function (timespanSelection , graphFactory, utils, dataSource) {
    var margin = {top: 10, right: 0, bottom: 30, left: 35},
        width = graphFactory.size - margin.left - margin.right,
        height = graphFactory.size - margin.top - margin.bottom;

    var x = d3.time.scale()
            .range([0, width]);

    var y = d3.scale.linear()
            .domain([0, 24])
            .range([height, 0]);

    var xAxis = d3.svg.axis()
            .scale(x)
            .orient("bottom");

    var yAxis = d3.svg.axis()
            .scale(y)
            .orient("left")
            .outerTickSize(0)
            .tickValues([0, 6, 9, 12, 15, 18])
            .tickFormat(function (d) {
                if (d < 12) {
                    return d + 'am';
                } else if (d === 12) {
                    return d + 'pm';
                } else if (d === 24) {
                    return '0am';
                } else {
                    return (d - 12) + 'pm';
                }
            });

    var timeOfDay = function (date) {
        return (date.getTime() - (date.getTimezoneOffset() * 60 * 1000)) % (24 * 60 * 60 * 1000) / (60 * 60 * 1000);
    };

    var startOfDay = function (date) {
        return new Date(date.getFullYear(), date.getMonth(), date.getDate());
    };

    var endOfDay = function (date) {
        var nextDay = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);

        return new Date(nextDay.getTime() - 1);
    };

    var phasesByDay = function (start, end) {
        var phases = [],
            startDate = new Date(start),
            endDate = end ? new Date(end) : undefined,
            endOfCurrentDay = endOfDay(startDate);

        if (endDate) {
            while (endOfCurrentDay < endDate) {
                phases.push({
                    start: startDate,
                    end: endOfCurrentDay
                });

                startDate = new Date(endOfCurrentDay.getTime() + 1);
                endOfCurrentDay = endOfDay(startDate);
            }
        }

        phases.push({
            start: startDate,
            end: endDate
        });

        return phases;
    };

    var flatten = function (listOfLists) {
        return listOfLists.reduce(function (a, b) { return a.concat(b); }, []);
    };

    var calculatePhasesByDay = function (data) {
        var lastEntry;

        return flatten(data.map(function (entry) {
            var greenPhases = [],
                start = entry.start;

            if (lastEntry) {
                greenPhases = phasesByDay(lastEntry.end + 1, entry.start - 1).map(function (phase) {
                    phase.color = 'green';
                    phase.duration = entry.start - lastEntry.end;
                    phase.phaseStart = new Date(lastEntry.end);
                    phase.phaseEnd = new Date(entry.start);
                    return phase;
                });
            }

            lastEntry = entry;

            var redPhases = phasesByDay(entry.start, entry.end).filter(function (phase) {
                return phase.end !== undefined;
            }).map(function (phase) {
                phase.color = 'red';
                phase.culprits = entry.culprits;
                phase.duration = entry.end - entry.start;
                phase.phaseStart = new Date(entry.start);
                phase.phaseEnd = new Date(entry.end);
                return phase;
            });

            return greenPhases.concat(redPhases);
        }));
    };

    var annotateDateAndTime = function (phases) {
        return phases.map(function (phase) {
            phase.startOfDay = startOfDay(phase.start);
            phase.endOfDay = endOfDay(phase.end);
            phase.startTime = timeOfDay(phase.start);
            phase.endTime = timeOfDay(phase.end);
            return phase;
        });
    };

    var isWeekend = function (date) {
        var dayOfWeek = date.getDay();
        return dayOfWeek === 0 || dayOfWeek === 6;
    };

    var shortTimeString = function (date, referenceDate) {
        if (startOfDay(date).valueOf() === startOfDay(referenceDate).valueOf()) {
            return date.toLocaleTimeString();
        } else {
            return date.toLocaleTimeString() + ' (' + date.toLocaleDateString() + ')';
        }
    };

    var saneDayTicks = function (axis, scale) {
        var dayCount = (x.domain()[1] - x.domain()[0]) / (24 * 60 * 60 * 1000);
        if (dayCount < 10) {
            axis.ticks(d3.time.days, 1);
        } else {
            axis.ticks(10);
        }
        return axis;
    };

    var axesPane,
        getOrCreateAxesPane = function (svg) {
            if (axesPane === undefined) {
                axesPane = svg
                    .append("g")
                    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

                axesPane.append("g")
                    .attr("class", "x axis")
                    .attr("transform", "translate(0," + height + ")");

                axesPane.append("g")
                    .attr("class", "y axis")
                    .append("text")
                    .attr("transform", "rotate(-90)")
                    .attr("y", 6)
                    .attr("dy", "-1.5em")
                    .attr("dx", "-1.5em")
                    .style("text-anchor", "end")
                    .text("Time of the day");
            }

            return axesPane;
        },
        removeAxesPane = function (svg) {
            svg.select('g').remove();
            axesPane = undefined;
        };

    var renderData = function (data, svg) {
        var phasesByDay = annotateDateAndTime(calculatePhasesByDay(data));

        if (!phasesByDay.length) {
            removeAxesPane(svg);

            return;
        }

        x.domain([d3.min(phasesByDay, function(d) { return d.startOfDay; }),
                  d3.max(phasesByDay, function(d) { return d.endOfDay; })]);

        var g = getOrCreateAxesPane(svg);

        g.selectAll('.x.axis')
            .call(saneDayTicks(xAxis, x));

{'fraction_non_alphanumeric': 0.13480718085106383, 'fraction_numerical': 0.015458776595744681, 'mean_word_length': 1.4647685374846375, 'pattern_counts': {'":': 0, '<': 9, '<?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': '2217235', 'n_tokens_mistral': 3664, 'n_tokens_neox': 3345, 'n_words': 741}