javascripts/dashboard/utils.js

  1. /**
  2. * @namespace javascripts.dashboard.utils
  3. */
  4. /**
  5. * Get the success and fail line values for the given context
  6. *
  7. * @function targetLineValues
  8. * @memberof javascripts.dashboard.utils
  9. */
  10. export function targetLineValues(context) {
  11. if (context === "production") return { successLineValue: 40, failLineValue: 50 }
  12. if (context === "deploy-preview") return { successLineValue: 45, failLineValue: 55 }
  13. if (context === "cms") return { successLineValue: 35, failLineValue: 45 }
  14. if (context === "lcp") return { successLineValue: 2500, failLineValue: 4000 }
  15. if (context === "fid") return { successLineValue: 100, failLineValue: 300 }
  16. if (context === "cls") return { successLineValue: 0.1, failLineValue: 0.25 }
  17. }
  18. /**
  19. * Get the minimum axis value for the given context
  20. *
  21. * @function minAxisValues
  22. * @memberof javascripts.dashboard.utils
  23. */
  24. export function minAxisValues(context) {
  25. if (context === "production") return 60
  26. if (context === "deploy-preview") return 60
  27. if (context === "cms") return 60
  28. if (context === "lcp") return 5000
  29. if (context === "fid") return 400
  30. if (context === "cls") return 0.4
  31. }
  32. /**
  33. * Get the x axis legend text for the given context
  34. *
  35. * @function axisTextValues
  36. * @memberof javascripts.dashboard.utils
  37. */
  38. export function axisTextValues(context) {
  39. if (context === "production") return "Build number"
  40. if (context === "deploy-preview") return "Build number"
  41. if (context === "cms") return "Build number"
  42. if (context === "lcp") return "Day"
  43. if (context === "fid") return "Day"
  44. if (context === "cls") return "Day"
  45. }
  46. /**
  47. * Get the y axis unit of measurment in the tooltip for the given context
  48. *
  49. * @function axisMeasurementValues
  50. * @memberof javascripts.dashboard.utils
  51. */
  52. export function axisMeasurementValues(context) {
  53. if (context === "production") return " s"
  54. if (context === "deploy-preview") return " s"
  55. if (context === "cms") return " s"
  56. if (context === "lcp") return " ms"
  57. if (context === "fid") return " ms"
  58. if (context === "cls") return ""
  59. }
  60. /**
  61. * Allow clicks on the given context
  62. *
  63. * @function allowClicks
  64. * @memberof javascripts.dashboard.utils
  65. */
  66. export function allowClicks(context) {
  67. if (context === "production") return true
  68. if (context === "deploy-preview") return true
  69. if (context === "cms") return true
  70. if (context === "lcp") return false
  71. if (context === "fid") return false
  72. if (context === "cls") return false
  73. }
  74. /**
  75. * Return all objects that have the key value within the given percentile
  76. *
  77. * @function metricsInPercentile
  78. * @memberof javascripts.dashboard.utils
  79. */
  80. export function metricsInPercentile(data, key, percentile) {
  81. const selectedValues = data.map((object) => object[key])
  82. const sortedValues = sortNumberArray(selectedValues)
  83. const percentileLimit = percentileValue(sortedValues, percentile)
  84. return data.filter((object) => object[key] <= percentileLimit )
  85. }
  86. /**
  87. * Sort the numbers from smallest to largest
  88. *
  89. * @function sortNumberArray
  90. * @memberof javascripts.dashboard.utils
  91. */
  92. function sortNumberArray(arr) {
  93. return arr.sort((a, b) => a - b)
  94. }
  95. /**
  96. * Get the value for the given percentile
  97. *
  98. * @function percentileValue
  99. * @memberof javascripts.dashboard.utils
  100. */
  101. function percentileValue(arr, p) {
  102. if (arr.length === 0) return 0;
  103. if (typeof p !== "number") throw new TypeError("p must be a number");
  104. if (p <= 0) return arr[0];
  105. if (p >= 1) return arr[arr.length - 1];
  106. let index = (arr.length - 1) * p,
  107. lower = Math.floor(index),
  108. upper = lower + 1,
  109. weight = index % 1
  110. if (upper >= arr.length) return arr[lower];
  111. return arr[lower] * (1 - weight) + arr[upper] * weight;
  112. }
  113. export function percentage(total, value) {
  114. return Math.round(
  115. (((value / total) * 100) + Number.EPSILON) * 10
  116. ) / 10
  117. }