<commit_before>const html = require('choo/html')
const moment = require('moment')
const hash = require('object-hash')
const createChart = require('./chart')
const form = require('./form')
const { chevron } = require('../icons')
const { className, getPerformance } = require('../utils')
const { __ } = require('../../locale')

const SELECTED_COOPERATIVE = /cooperative:(\w+)/

module.exports = function createWrapper (name) {
  const chart = createChart(name)

  return function render (header, center, cooperative, actions, state, emit) {
    if (typeof window === 'undefined') {
      return empty(html`
        <div>
          <div class="Chart-loading u-hiddenNoScript">${__('Fetching data')}</div>
          <div class="u-hiddenHasScript">
            ${__('There would be a pretty chart here, had you enabled JavaScript')}
          </div>
        </div>
      `)
    }

    const { page, inEdit } = state.chart
    const { granularity, items, normalized, type, compare } = state.consumptions

    /**
     * Figure out where (when) to center the graph
     */

    const offset = Math.round(moment(center).diff(Date.now(), granularity, true))
    const now = offset < -6
      ? moment(center).add(page + 5, granularity)
      : moment(center).add(page + Math.abs(offset), granularity)

    /**
     * Lookup cached data
     */

    const series = []
    const queries = getQueries(now, cooperative, state).reduce((missing, query) => {
      const cached = items[hash(query)]

      if (cached) {
        series.push({ name: query.name, values: cached })
        return missing
      }

      return missing.concat([query])
    }, [])

    /**
     * Fetch any data that might be missing and render loading state
     */

    let isLoading = false
    if (queries.length && !state.error) {
      isLoading = true
      emit('consumptions:fetch', queries)
    }

    /**
     * Compose Monthly values into years
     */

    if (granularity === 'year') {
      series.forEach(set => { set.values = composeYears(set.values) })
    }

    /**
     * Find the shortest series set
     */

    const max = series.reduce((max, set) => {
      return !max || (set.values.length < max) ? set.values.length : max
    }, 0)

    /**
     * Ensure all series sets are the same length
     */

    series.forEach(set => { set.values = set.values.slice(max * -1) })

    const hasLater = moment(now).isBefore(Date.now(), granularity)
    const hasEarlier = series.length && series[0].values.length

    return html`
      <div class="${className('Chart', { 'is-loading': isLoading })}">
        <div class="Chart-header">
          <div class="Chart-title">
            ${header}
          </div>
          <div class="Chart-filter"><commit_msg>Fix pagination back button appearing when it shouldn't<commit_after>const html = require('choo/html')
const moment = require('moment')
const hash = require('object-hash')
const createChart = require('./chart')
const form = require('./form')
const { chevron } = require('../icons')
const { className, getPerformance } = require('../utils')
const { __ } = require('../../locale')

const SELECTED_COOPERATIVE = /cooperative:(\w+)/

module.exports = function createWrapper (name) {
  const chart = createChart(name)

  return function render (header, center, cooperative, actions, state, emit) {
    if (typeof window === 'undefined') {
      return empty(html`
        <div>
          <div class="Chart-loading u-hiddenNoScript">${__('Fetching data')}</div>
          <div class="u-hiddenHasScript">
            ${__('There would be a pretty chart here, had you enabled JavaScript')}
          </div>
        </div>
      `)
    }

    const { page, inEdit } = state.chart
    const { granularity, items, normalized, type, compare } = state.consumptions

    /**
     * Figure out where (when) to center the graph
     */

    const offset = Math.round(moment(center).diff(Date.now(), granularity, true))
    const now = offset < -6
      ? moment(center).add(page + 5, granularity)
      : moment(center).add(page + Math.abs(offset), granularity)

    /**
     * Lookup cached data
     */

    const series = []
    const queries = getQueries(now, cooperative, state).reduce((missing, query) => {
      const cached = items[hash(query)]

      if (cached) {
        series.push({ name: query.name, values: cached })
        return missing
      }

      return missing.concat([query])
    }, [])

    /**
     * Fetch any data that might be missing and render loading state
     */

    let isLoading = false
    if (queries.length && !state.error) {
      isLoading = true
      emit('consumptions:fetch', queries)
    }

    /**
     * Compose Monthly values into years
     */

    if (granularity === 'year') {
      series.forEach(set => { set.values = composeYears(set.values) })
    }

    /**
     * Find the shortest series set
     */

    const max = series.reduce((max, set) => {
      return !max || (set.values.length < max) ? set.values.length : max
    }, 0)

    /**
     * Ensure all series sets are the same length
     */

    series.forEach(set => { set.values = set.values.slice(max * -1) })

    const hasLater = moment(now).isBefore(Date.now(), granularity)
    const period = getPeriod(granularity, now)
    const expected = moment(period.to).diff(period.from, granularity)
    const hasEarlier = series.length && (series[0].values.length >= expected)

    return html`
      <div class="${className('Chart', { 'is-loading': isLoading })}">
        <div class="Chart-header">
          <div class="Chart-title">
            ${header}
          </div>
          <div class="Chart-filter">
{'fraction_non_alphanumeric': 0.1497345132743363, 'fraction_numerical': 0.0017699115044247787, 'mean_word_length': 2.5833861762840837, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 34, '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': '4013604', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1616, 'n_words': 435}