All files / modules/75-cf/components/FlagActivation/views TabEvaluations.tsx

96.77% Statements 60/62
69.44% Branches 50/72
93.75% Functions 15/16
96.61% Lines 57/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193              3x 3x 3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x 3x 3x 3x   3x                 3x 16x   16x           3x 2x 2x 2x   2x 2x                       2x       2x 2x 6x   6x 4x   2x     6x   2x 6x   6x     6x     6x     6x       2x 16x       2x 4x   32x 90x           4x 32x                           32x     4x             4x   2x   2x                                                   2x 4x     2x                                                
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useMemo } from 'react'
import { Container, Icon, Layout, Text, PageError, NoDataCard } from '@wings-software/uicore'
import { useParams } from 'react-router-dom'
import cx from 'classnames'
import { Color } from '@harness/design-system'
import { clone } from 'lodash-es'
import * as Moment from 'moment'
import { extendMoment } from 'moment-range'
import type { PlotOptions } from 'highcharts'
import { Classes } from '@blueprintjs/core'
import { useStrings } from 'framework/strings'
import { VariationWithIcon } from '@cf/components/VariationWithIcon/VariationWithIcon'
import { CFVariationColors } from '@cf/constants'
import { Feature, FeatureEvaluation, GetFeatureEvaluationsQueryParams, useGetFeatureEvaluations } from 'services/cf'
import { formatDate, formatNumber, getErrorMessage } from '@cf/utils/CFUtils'
import useActiveEnvironment from '@cf/hooks/useActiveEnvironment'
import { EvaluationsChart } from './EvaluationsChart'
import css from './MetricsView.module.scss'
 
const moment = extendMoment(Moment)
 
export interface TabEvaluationsProps {
  flagData: Feature
  startDate: Date
  endDate: Date
}
 
// Remove year in chart for only current year dates
const _formatDateWithoutYear = (date: number): string => {
  const currentYear = new Date(date).getFullYear()
 
  return formatDate(date)
    .replace(new RegExp(currentYear + '$', 'i'), '')
    .trim()
    .replace(',', '')
}
 
export const TabEvaluations: React.FC<TabEvaluationsProps> = ({ flagData, startDate, endDate }) => {
  const { getString } = useStrings()
  const { accountId: accountIdentifier, orgIdentifier, projectIdentifier } = useParams<Record<string, string>>()
  const { activeEnvironment: environmentIdentifier } = useActiveEnvironment()
 
  const queryParams = useMemo<GetFeatureEvaluationsQueryParams>(
    () => ({
      accountIdentifier,
      org: orgIdentifier,
      orgIdentifier,
      project: projectIdentifier,
      projectIdentifier,
      environmentIdentifier,
      startTime: startDate.getTime(),
      endTime: endDate.getTime()
    }),
    [startDate, endDate, accountIdentifier, orgIdentifier, projectIdentifier, environmentIdentifier]
  )
  const { data, loading, error, refetch } = useGetFeatureEvaluations({
    identifier: flagData.identifier,
    queryParams
  })
  const _data = data as FeatureEvaluation[] // Note: Backend Swagger spec is wrong at the moment
  const total = _data?.reduce((_total, entry: FeatureEvaluation) => {
    const _entry = _total.find(({ variationIdentifier }) => variationIdentifier === entry.variationIdentifier)
 
    if (_entry) {
      _entry.count = (_entry.count || 0) + (entry.count || 0)
    } else {
      _total.push(clone(entry))
    }
 
    return _total
  }, [] as FeatureEvaluation[])
  const metricsGroupedByDay = _data?.reduce((_metricsGroupedByDay, entry) => {
    const _entry = _metricsGroupedByDay.find(
      ({ day, variationIdentifier }) =>
        variationIdentifier === entry.variationIdentifier && day === moment.utc(entry.date).format('MMM DD')
    )
 
    Iif (_entry) {
      _entry.count = (_entry.count || 0) + (entry.count || 0)
    } else {
      _metricsGroupedByDay.push({ ...entry, day: moment.utc(entry.date).format('MMM DD') })
    }
 
    return _metricsGroupedByDay
  }, [] as (FeatureEvaluation & { day: string })[])
 
  // categories is used to render x-axis
  const categories = Array.from(moment.range(new Date(startDate), new Date(endDate)).by('day', { step: 1 })).map(
    interval => _formatDateWithoutYear(interval.valueOf())
  )
 
  // series is used to render y-axis + tooltip
  const series = flagData.variations.map((variation, index) => {
    const seriesData = categories.map(
      dayFromRanges =>
        metricsGroupedByDay?.find(
          ({ variationIdentifier, day }) => variationIdentifier === variation.identifier && dayFromRanges === day
        )?.count || 0
    )
 
    // Tooltip with combination with audit logs are not implemented
    // @see https://harness.atlassian.net/browse/FFM-802
    const tooltips = categories.reduce((_tooltip, dayFromRanges) => {
      _tooltip[dayFromRanges] = `
        <p>${dayFromRanges}</p>
        <h3>
          ${getString('cf.featureFlags.metrics.flagEvaluations', { count: 100 })}
        </h3>
        <table className=${cx(Classes.HTML_TABLE, css.dataTable)}>
        <tbody>
          <tr>
            <td>True</td>
            <td>${formatNumber(1000 || 0, true)} evaluations</td>
          </tr>
        </tbody>
      </table>
      `
      return _tooltip
    }, {} as Record<string, string>)
 
    const variationSeriesItem = {
      name: variation.name,
      color: CFVariationColors[index % CFVariationColors.length],
      data: seriesData,
      tooltips
    }
 
    return variationSeriesItem
  })
  const sum = total?.reduce((_sum, entry) => _sum + (entry.count || 0), 0) || 0
 
  return (
    <Container className={css.contentBody}>
      {loading && <Icon name="spinner" size={16} color="blue500" />}
      {error && <PageError message={getErrorMessage(error)} onClick={() => refetch()} />}
      {!loading && !error && _data?.length === 0 && (
        <NoDataCard icon="cf-main" message={getString('cf.featureFlags.metrics.noData')} />
      )}
      {!loading && !error && !!_data?.length && (
        <Container margin={{ top: 'medium' }}>
          <EvaluationsChart
            series={series as PlotOptions}
            categories={categories}
            title={getString('cf.featureFlags.metrics.flagEvaluations', {
              count: formatNumber(sum)
            })}
          />
          <Container margin={{ top: 'xxlarge' }}>
            <table className={cx(Classes.HTML_TABLE, css.dataTable)}>
              <thead>
                <tr>
                  <th style={{ paddingLeft: 'var(--spacing-large)' }}>{getString('cf.shared.variation')}</th>
                  <th>{getString('cf.featureFlags.metrics.totalEvaluations')}</th>
                </tr>
              </thead>
              <tbody>
                {total?.map(entry => {
                  const index = flagData.variations.findIndex(
                    variation => variation.identifier === entry.variationIdentifier
                  )
 
                  return (
                    <tr key={entry.variationIdentifier}>
                      <td style={{ paddingLeft: 'var(--spacing-large)' }}>
                        <Layout.Horizontal spacing="xsmall" style={{ alignItems: 'center' }}>
                          <VariationWithIcon variation={flagData.variations[index]} index={index} />
                        </Layout.Horizontal>
                      </td>
                      <td>
                        {formatNumber(entry.count || 0, true)}{' '}
                        <Text inline color={Color.GREY_400}>
                          ({(((entry.count || 0) / sum) * 100).toFixed(2).replace(/0+$/, '')}%)
                        </Text>
                      </td>
                    </tr>
                  )
                })}
              </tbody>
            </table>
          </Container>
        </Container>
      )}
    </Container>
  )
}