<commit_before>import React, {PropTypes} from 'react'
import _ from 'lodash'
import {fetchTimeSeriesAsync} from 'shared/actions/timeSeries'

const {
  arrayOf,
  bool,
  element,
  func,
  number,
  oneOfType,
  shape,
  string,
} = PropTypes

const AutoRefresh = ComposedComponent => {
  const wrapper = React.createClass({
    propTypes: {
      children: element,
      autoRefresh: number.isRequired,
      templates: arrayOf(
        shape({
          type: string.isRequired,
          label: string.isRequired,
          tempVar: string.isRequired,
          query: shape({
            db: string,
            rp: string,
            influxql: string,
          }),
          values: arrayOf(
            shape({
              type: string.isRequired,
              value: string.isRequired,
              selected: bool,
            })
          ).isRequired,
        })
      ),
      queries: arrayOf(
        shape({
          host: oneOfType([string, arrayOf(string)]),
          text: string,
        }).isRequired
      ).isRequired,
      editQueryStatus: func,
    },

    getInitialState() {
      return {
        lastQuerySuccessful: false,
        timeSeries: [],
      }
    },

    componentDidMount() {
      const {queries, autoRefresh} = this.props
      this.executeQueries(queries)
      if (autoRefresh) {
        this.intervalID = setInterval(
          () => this.executeQueries(queries),
          autoRefresh
        )
      }
    },

    componentWillReceiveProps(nextProps) {
      const shouldRefetch = this.queryDifference(
        this.props.queries,
        nextProps.queries
      ).length

      if (shouldRefetch) {
        this.executeQueries(nextProps.queries)
      }

      if (this.props.autoRefresh !== nextProps.autoRefresh || shouldRefetch) {
        clearInterval(this.intervalID)

        if (nextProps.autoRefresh) {
          this.intervalID = setInterval(
            () => this.executeQueries(nextProps.queries),
            nextProps.autoRefresh
          )
        }
      }
    },

    queryDifference(left, right) {
      const leftStrs = left.map(q => `${q.host}${q.text}`)
      const rightStrs = right.map(q => `${q.host}${q.text}`)<commit_msg>AutoRefresh refetches data when temp vars change<commit_after>import React, {PropTypes} from 'react'
import _ from 'lodash'
import {fetchTimeSeriesAsync} from 'shared/actions/timeSeries'

const {
  arrayOf,
  bool,
  element,
  func,
  number,
  oneOfType,
  shape,
  string,
} = PropTypes

const AutoRefresh = ComposedComponent => {
  const wrapper = React.createClass({
    propTypes: {
      children: element,
      autoRefresh: number.isRequired,
      templates: arrayOf(
        shape({
          type: string.isRequired,
          label: string.isRequired,
          tempVar: string.isRequired,
          query: shape({
            db: string,
            rp: string,
            influxql: string,
          }),
          values: arrayOf(
            shape({
              type: string.isRequired,
              value: string.isRequired,
              selected: bool,
            })
          ).isRequired,
        })
      ),
      queries: arrayOf(
        shape({
          host: oneOfType([string, arrayOf(string)]),
          text: string,
        }).isRequired
      ).isRequired,
      editQueryStatus: func,
    },

    getInitialState() {
      return {
        lastQuerySuccessful: false,
        timeSeries: [],
      }
    },

    componentDidMount() {
      const {queries, autoRefresh} = this.props
      this.executeQueries(queries)
      if (autoRefresh) {
        this.intervalID = setInterval(
          () => this.executeQueries(queries),
          autoRefresh
        )
      }
    },

    componentWillReceiveProps(nextProps) {
      const queriesDidUpdate = this.queryDifference(
        this.props.queries,
        nextProps.queries
      ).length

      const tempVarsDidUpdate = !_.isEqual(
        this.props.templates,
        nextProps.templates
      )

      const shouldRefetch = queriesDidUpdate || tempVarsDidUpdate

      if (shouldRefetch) {
        this.executeQueries(nextProps.queries)
      }

      if (this.props.autoRefresh !== nextProps.autoRefresh || shouldRefetch) {
        clearInterval(this.intervalID)

        if (nextProps.autoRefresh) {
          this.intervalID = setInterval(
            () => this.executeQueries(nextProps.queries),
            nextProps.autoRefresh
          )
        }
      }
    },

    queryDifference(left, right) {
      const leftStrs = left.map(q => `${q.host}${q.text}`)
      const rightStrs = right.map(q => `${q.host}${q.text}`)
{'fraction_non_alphanumeric': 0.10933448573898012, 'fraction_numerical': 0.0, 'mean_word_length': 1.9223484848484849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '117874', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1320, 'n_words': 254}