<commit_before>import React, { Component } from 'react';
import { compose, graphql } from 'react-apollo';
import find from 'lodash.find';
import uniqBy from 'lodash.uniqby';
import get from 'lodash.get';
import moment from 'moment';

export const MetricNames = [
  'AVG_MEM_BYTES',
  'AVG_LOAD_PERCENT',
  'AGG_NETWORK_BYTES'
];

export const withServiceMetricsPolling = ({
  pollingInterval = 1000, // In milliseconds
  getPreviousEnd = () =>
    moment()
      .utc()
      .format()
}) => {
  return WrappedComponent => {
    return class extends Component {
      componentDidMount() {
        this._poll = setInterval(() => {
          const { loading, error, service, fetchMoreMetrics } = this.props;
          const previousEnd = getPreviousEnd(this.props);

          if (previousEnd) {
            fetchMoreMetrics(previousEnd);
          }
        }, pollingInterval); // TODO this is the polling interval - think about amount is the todo I guess...
      }

      componentWillUnmount() {
        clearInterval(this._poll);
      }

      render() {
        return <WrappedComponent {...this.props} />;
      }
    };
  };
};

export const withServiceMetricsGql = ({
  gqlQuery,
  graphDurationSeconds,
  updateIntervalSeconds,
  variables = () => ({}),
  props = () => ({})
}) => {
  const getPreviousMetrics = (
    previousResult,
    serviceId,
    instanceId,
    metricName
  ) => {
    const service = find(get(previousResult, 'deploymentGroup.services', []), [
      'id',
      serviceId
    ]);

    if (!service) {
      return [];
    }

    return service.instances
      .find(i => i.id === instanceId)
      .metrics.find(m => m.name === metricName).metrics;
  };

  const getNextResult = (previousResult, fetchNextResult) => {
    const deploymentGroup = fetchNextResult.deploymentGroup;

    return {
      deploymentGroup: {
        ...deploymentGroup,
        services: deploymentGroup.services.map(service => ({
          ...service,
          instances: service.instances.map(instance => ({
            ...instance,
            metrics: instance.metrics.map(metric => ({
              ...metric,
              metrics: uniqBy(
                getPreviousMetrics(
                  previousResult,
                  service.id,
                  instance.id,
                  metric.name<commit_msg>fix(cp-frontend): handle no previous metrics<commit_after>import React, { Component } from 'react';
import { compose, graphql } from 'react-apollo';
import find from 'lodash.find';
import uniqBy from 'lodash.uniqby';
import get from 'lodash.get';
import moment from 'moment';

export const MetricNames = [
  'AVG_MEM_BYTES',
  'AVG_LOAD_PERCENT',
  'AGG_NETWORK_BYTES'
];

export const withServiceMetricsPolling = ({
  pollingInterval = 1000, // In milliseconds
  getPreviousEnd = () =>
    moment()
      .utc()
      .format()
}) => {
  return WrappedComponent => {
    return class extends Component {
      componentDidMount() {
        this._poll = setInterval(() => {
          const { loading, error, service, fetchMoreMetrics } = this.props;
          const previousEnd = getPreviousEnd(this.props);

          if (previousEnd) {
            fetchMoreMetrics(previousEnd);
          }
        }, pollingInterval); // TODO this is the polling interval - think about amount is the todo I guess...
      }

      componentWillUnmount() {
        clearInterval(this._poll);
      }

      render() {
        return <WrappedComponent {...this.props} />;
      }
    };
  };
};

export const withServiceMetricsGql = ({
  gqlQuery,
  graphDurationSeconds,
  updateIntervalSeconds,
  variables = () => ({}),
  props = () => ({})
}) => {
  const getPreviousMetrics = (
    previousResult,
    serviceId,
    instanceId,
    metricName
  ) => {
    const services = get(previousResult, 'deploymentGroup.services', []);

    if (!services.length) {
      return [];
    }

    const service = find(services, [
      'id',
      serviceId
    ]);

    if (!service) {
      return [];
    }

    const instance = find(service.instances, ['id', instanceId]);

    if (!instance) {
      return [];
    }

    const metrics = find(instance.metrics, ['name', metricName]);

    if (!metrics) {
      return [];
    }

    return get(metrics, 'metrics', []);
  };

  const getNextResult = (previousResult, fetchNextResult) => {
    const deploymentGroup = fetchNextResult.deploymentGroup;

    return {
      deploymentGroup: {
        ...deploymentGroup,
        services: deploymentGroup.services.map(service => ({
          ...service,
          instances: service.instances.map(instance => ({
            ...instance,
            metrics: instance.metrics.map(metric => ({
              ...metric,
              metrics: uniqBy(
                getPreviousMetrics(
                  previousResult,
                  service.id,
                  instance.id,
                  metric.name
{'fraction_non_alphanumeric': 0.1277461350691619, 'fraction_numerical': 0.0016273393002441008, 'mean_word_length': 2.2434036939313984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '1791483', 'n_tokens_mistral': 1412, 'n_tokens_neox': 1361, 'n_words': 304}