applyStaticWeightsToTimeseries.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233
  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. timeseriesData[key].some(series => series.value > 0)
  19. ) as WebVitals[]
  20. )
  21. : PERFORMANCE_SCORE_WEIGHTS;
  22. return {
  23. ...Object.keys(weights).reduce((acc, webVital) => {
  24. acc[webVital] = timeseriesData[webVital].map(({name, value}) => ({
  25. name,
  26. value: value * weights[webVital] * 0.01,
  27. }));
  28. return acc;
  29. }, {}),
  30. total: timeseriesData.total,
  31. } as WebVitalsScoreBreakdown;
  32. }