builds_cache.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from typing import Any
  10. from materialize.buildkite_insights.buildkite_api import builds_api
  11. from materialize.buildkite_insights.cache import generic_cache
  12. from materialize.buildkite_insights.cache.cache_constants import FetchMode
  13. from materialize.buildkite_insights.cache.generic_cache import CacheFilePath
  14. from materialize.util import sha256_of_utf8_string
  15. def get_or_query_single_build(
  16. pipeline_slug: str,
  17. fetch_mode: FetchMode,
  18. build_number: int,
  19. ) -> list[Any]:
  20. cache_file_path = _get_file_path_for_single_build(
  21. pipeline_slug=pipeline_slug,
  22. build_number=build_number,
  23. )
  24. fetch_action = lambda: builds_api.get_single_build(
  25. pipeline_slug=pipeline_slug, build_number=build_number
  26. )
  27. return generic_cache.get_or_query_data(cache_file_path, fetch_action, fetch_mode)
  28. def get_or_query_builds(
  29. pipeline_slug: str,
  30. fetch_mode: FetchMode,
  31. max_fetches: int,
  32. branch: str | None,
  33. build_states: list[str] | None,
  34. items_per_page: int = 50,
  35. first_page: int = 1,
  36. ) -> list[Any]:
  37. meta_data = f"{branch}-{build_states}"
  38. cache_file_path = _get_file_path_for_builds(
  39. pipeline_slug=pipeline_slug,
  40. meta_data=meta_data,
  41. max_fetches=max_fetches,
  42. items_per_page=items_per_page,
  43. first_page=first_page,
  44. )
  45. fetch_action = lambda: builds_api.get_builds(
  46. pipeline_slug=pipeline_slug,
  47. max_fetches=max_fetches,
  48. branch=branch,
  49. build_states=build_states,
  50. items_per_page=items_per_page,
  51. )
  52. return generic_cache.get_or_query_data(cache_file_path, fetch_action, fetch_mode)
  53. def get_or_query_builds_for_all_pipelines(
  54. fetch_mode: FetchMode,
  55. max_fetches: int,
  56. branch: str | None,
  57. build_states: list[str] | None,
  58. items_per_page: int = 50,
  59. first_page: int = 1,
  60. ) -> list[Any]:
  61. meta_data = f"{branch}-{build_states}"
  62. cache_file_path = _get_file_path_for_builds(
  63. pipeline_slug="all",
  64. meta_data=meta_data,
  65. max_fetches=max_fetches,
  66. items_per_page=items_per_page,
  67. first_page=first_page,
  68. )
  69. fetch_action = lambda: builds_api.get_builds_of_all_pipelines(
  70. max_fetches=max_fetches,
  71. branch=branch,
  72. items_per_page=items_per_page,
  73. build_states=build_states,
  74. )
  75. return generic_cache.get_or_query_data(cache_file_path, fetch_action, fetch_mode)
  76. def _get_file_path_for_builds(
  77. pipeline_slug: str,
  78. meta_data: str,
  79. max_fetches: int,
  80. items_per_page: int,
  81. first_page: int,
  82. ) -> CacheFilePath:
  83. max_entries = max_fetches * items_per_page
  84. meta_data = f"{meta_data}-{max_entries}-{first_page}"
  85. hash_value = sha256_of_utf8_string(meta_data)[:8]
  86. return CacheFilePath(
  87. cache_item_type="builds", pipeline_slug=pipeline_slug, params_hash=hash_value
  88. )
  89. def _get_file_path_for_single_build(
  90. pipeline_slug: str,
  91. build_number: int,
  92. ) -> CacheFilePath:
  93. meta_data = f"{build_number}"
  94. hash_value = sha256_of_utf8_string(meta_data)[:8]
  95. return CacheFilePath(
  96. cache_item_type="build", pipeline_slug=pipeline_slug, params_hash=hash_value
  97. )