Skip to content

Commit

Permalink
Merge pull request #40 from robusta-dev/loading_bar
Browse files Browse the repository at this point in the history
Loading bar for calculating strategy
  • Loading branch information
LeaveMyYard authored May 23, 2023
2 parents bdf27e1 + e3aafe5 commit ef49a5d
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
131 changes: 128 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pydantic = "1.10.7"
kubernetes = "^26.1.0"
prometheus-api-client = "^0.5.3"
numpy = "^1.24.2"
alive-progress = "^3.1.2"


[tool.poetry.group.dev.dependencies]
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
about-time==4.2.1 ; python_version >= "3.9" and python_version < "3.12"
alive-progress==3.1.2 ; python_version >= "3.9" and python_version < "3.12"
cachetools==5.3.0 ; python_version >= "3.9" and python_version < "3.12"
certifi==2022.12.7 ; python_version >= "3.9" and python_version < "3.12"
charset-normalizer==3.0.1 ; python_version >= "3.9" and python_version < "3.12"
Expand All @@ -9,8 +11,10 @@ cycler==0.11.0 ; python_version >= "3.9" and python_version < "3.12"
dateparser==1.1.7 ; python_version >= "3.9" and python_version < "3.12"
fonttools==4.39.0 ; python_version >= "3.9" and python_version < "3.12"
google-auth==2.16.2 ; python_version >= "3.9" and python_version < "3.12"
grapheme==0.6.0 ; python_version >= "3.9" and python_version < "3.12"
httmock==1.4.0 ; python_version >= "3.9" and python_version < "3.12"
idna==3.4 ; python_version >= "3.9" and python_version < "3.12"
importlib-resources==5.12.0 ; python_version >= "3.9" and python_version < "3.10"
kiwisolver==1.4.4 ; python_version >= "3.9" and python_version < "3.12"
kubernetes==26.1.0 ; python_version >= "3.9" and python_version < "3.12"
matplotlib==3.7.1 ; python_version >= "3.9" and python_version < "3.12"
Expand Down Expand Up @@ -43,3 +47,4 @@ tzdata==2022.7 ; python_version >= "3.9" and python_version < "3.12"
tzlocal==4.2 ; python_version >= "3.9" and python_version < "3.12"
urllib3==1.26.14 ; python_version >= "3.9" and python_version < "3.12"
websocket-client==1.5.1 ; python_version >= "3.9" and python_version < "3.12"
zipp==3.15.0 ; python_version >= "3.9" and python_version < "3.10"
7 changes: 5 additions & 2 deletions robusta_krr/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from robusta_krr.utils.configurable import Configurable
from robusta_krr.utils.logo import ASCII_LOGO
from robusta_krr.utils.version import get_version

from robusta_krr.utils.progress_bar import ProgressBar

class Runner(Configurable):
EXPECTED_EXCEPTIONS = (KeyboardInterrupt, PrometheusNotFound)
Expand Down Expand Up @@ -111,6 +111,8 @@ async def _calculate_object_recommendations(self, object: K8sObjectData) -> tupl
data = dict(zip(ResourceType, data_tuple))
queries = {resource: data[resource].query for resource in ResourceType}

self.__progressbar.progress()

# NOTE: We run this in a threadpool as the strategy calculation might be CPU intensive
# But keep in mind that numpy calcluations will not block the GIL
result = await asyncio.to_thread(self._strategy.run, data, object)
Expand Down Expand Up @@ -143,7 +145,8 @@ async def _collect_result(self) -> Result:
self.warning("Note that you are using the '*' namespace filter, which by default excludes kube-system.")
return Result(scans=[])

resource_recommendations = await self._gather_objects_recommendations(objects)
with ProgressBar(self.config, total=len(objects), title="Calculating Recommendation") as self.__progressbar:
resource_recommendations = await self._gather_objects_recommendations(objects)

return Result(
scans=[
Expand Down
23 changes: 23 additions & 0 deletions robusta_krr/utils/progress_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from robusta_krr.utils.configurable import Configurable
from alive_progress import alive_bar
from robusta_krr.core.models.config import Config

class ProgressBar(Configurable):
def __init__(self, config: Config, **kwargs) -> None:
super().__init__(config)
self.show_bar = self.echo_active
if self.show_bar:
self.alive_bar = alive_bar(**kwargs)

def __enter__(self):
if self.show_bar:
self.bar = self.alive_bar.__enter__()
return self

def progress(self):
if self.show_bar:
self.bar()

def __exit__(self, *args):
if self.show_bar:
self.alive_bar.__exit__(*args)

0 comments on commit ef49a5d

Please sign in to comment.