applyStaticWeightsToTimeseries.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import type {Organization} from 'sentry/types/organization';
  2. import type {WebVitalsScoreBreakdown} from 'sentry/views/insights/browser/webVitals/queries/storedScoreQueries/useProjectWebVitalsScoresTimeseriesQuery';
  3. import type {WebVitals} from 'sentry/views/insights/browser/webVitals/types';
  4. import {getWeights} from 'sentry/views/insights/browser/webVitals/utils/getWeights';
  5. import {PERFORMANCE_SCORE_WEIGHTS} from 'sentry/views/insights/browser/webVitals/utils/scoreThresholds';
  6. // Returns a weighed score timeseries with each interval calculated from applying hardcoded weights to unweighted scores
  7. export function applyStaticWeightsToTimeseries(
  8. organization: Organization,
  9. timeseriesData: WebVitalsScoreBreakdown
  10. ) {
  11. const weights = organization.features.includes(
  12. 'performance-vitals-handle-missing-webvitals'
  13. )
  14. ? getWeights(
  15. Object.keys(timeseriesData)
  16. .filter(key => key !== 'total')
  17. .filter(key =>
  18. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  19. timeseriesData[key].some((series: any) => series.value > 0)
  20. ) as WebVitals[]
  21. )
  22. : PERFORMANCE_SCORE_WEIGHTS;
  23. return {
  24. ...Object.keys(weights).reduce((acc, webVital) => {
  25. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  26. acc[webVital] = timeseriesData[webVital].map(({name, value}: any) => ({
  27. name,
  28. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  29. value: value * weights[webVital] * 0.01,
  30. }));
  31. return acc;
  32. }, {}),
  33. total: timeseriesData.total,
  34. } as WebVitalsScoreBreakdown;
  35. }