Skip to content

Commit

Permalink
handled excluded projects
Browse files Browse the repository at this point in the history
  • Loading branch information
kthare10 committed May 10, 2024
1 parent 6f04dd2 commit 1312eb8
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 27 deletions.
6 changes: 4 additions & 2 deletions fabric_cf/actor/core/apis/abc_actor_management_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,20 +291,21 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
"""

@abstractmethod
def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
"""
Get metrics
@param project_id project id
@param oidc_sub oidc sub
@param excluded_projects excluded_projects
@return list of metric information
@throws Exception in case of error
"""

def get_slice_count(self, *, caller: AuthToken, email: str = None, states: List[int] = None,
project: str = None, user_id: str = None) -> int:
project: str = None, user_id: str = None, excluded_projects: List[str] = None) -> int:
"""
Obtains Slice count matching the filter criteria.
Expand All @@ -313,6 +314,7 @@ def get_slice_count(self, *, caller: AuthToken, email: str = None, states: List[
@param states slice states
@param caller caller
@param user_id user_id
@param excluded_projects excluded_projects
@return returns number of slices
"""

Expand Down
15 changes: 14 additions & 1 deletion fabric_cf/actor/core/apis/abc_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def get_slices(self, *, slice_id: ID = None, slice_name: str = None, project_id:

@abstractmethod
def get_slice_count(self, *, project_id: str = None, email: str = None, states: list[int] = None,
oidc_sub: str = None, slc_type: List[SliceTypes] = None) -> List[ABCSlice] or None:
oidc_sub: str = None, slc_type: List[SliceTypes] = None,
excluded_projects: List[str] = None) -> int:
"""
Retrieves the slices count.
Expand All @@ -228,6 +229,7 @@ def get_slice_count(self, *, project_id: str = None, email: str = None, states:
@param states states
@param oidc_sub oidc sub
@param slc_type slice type
@param excluded_projects excluded_projects
@return number of slices matching the filter criteria
Expand All @@ -248,6 +250,17 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
@throws Exception in case of error
"""

@abstractmethod
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
"""
Get Metrics
@param project_id: project id
@param oidc_sub: user id
@param excluded_projects: list of project ids to exclude
@return list of metrics
"""

@abstractmethod
def initialize(self):
"""
Expand Down
6 changes: 4 additions & 2 deletions fabric_cf/actor/core/apis/abc_mgmt_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
"""
raise NotImplementedError

def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
"""
Get metrics
@param project_id project id
@param oidc_sub oidc sub
@param excluded_projects excluded_projects
@return list of metric information
Expand All @@ -91,13 +92,14 @@ def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
raise NotImplementedError

def get_slice_count(self, *, email: str = None, project: str = None, states: List[int] = None,
user_id: str = None) -> int:
user_id: str = None, excluded_projects: List[str] = None) -> int:
"""
Obtains slice count.
@param email email
@param project project id
@param states slice states
@param user_id user_id
@param excluded_projects excluded_projects
@return returns list of slices
"""
raise NotImplementedError
Expand Down
6 changes: 3 additions & 3 deletions fabric_cf/actor/core/manage/actor_management_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ def get_slices(self, *, slice_id: ID, caller: AuthToken, slice_name: str = None,
result.status = ManagementObject.set_exception_details(result=result.status, e=e)
return result

def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
try:
return self.db.get_metrics(project_id=project_id, oidc_sub=oidc_sub)
return self.db.get_metrics(project_id=project_id, oidc_sub=oidc_sub, excluded_projects=excluded_projects)
except Exception as e:
self.logger.error("get_metrics {}".format(e))

Expand All @@ -183,7 +183,7 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
self.logger.error("add_or_update_metrics {}".format(e))

def get_slice_count(self, *, caller: AuthToken, email: str = None, states: List[int] = None,
project: str = None, user_id: str = None) -> int:
project: str = None, user_id: str = None, excluded_projects: List[str] = None) -> int:
try:
return self.db.get_slice_count(email=email, states=states, project_id=project, oidc_sub=user_id)
except Exception as e:
Expand Down
9 changes: 5 additions & 4 deletions fabric_cf/actor/core/manage/local/local_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
self.on_exception(e=e, traceback_str=traceback.format_exc())
return False

def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
try:
return self.manager.get_metrics(project_id=project_id, oidc_sub=oidc_sub)
return self.manager.get_metrics(project_id=project_id, oidc_sub=oidc_sub,
excluded_projects=excluded_projects)
except Exception as e:
self.on_exception(e=e, traceback_str=traceback.format_exc())

def get_slice_count(self, *, email: str = None, project: str = None, states: List[int] = None,
user_id: str = None) -> int:
user_id: str = None, excluded_projects: List[str] = None) -> int:
try:
return self.manager.get_slice_count(caller=self.auth, states=states, email=email, project=project,
user_id=user_id)
user_id=user_id, excluded_projects=excluded_projects)
except Exception as e:
self.on_exception(e=e, traceback_str=traceback.format_exc())

Expand Down
11 changes: 6 additions & 5 deletions fabric_cf/actor/core/plugins/db/actor_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int
self.lock.release()
return False

def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
try:
return self.db.get_metrics(project_id=project_id, user_id=oidc_sub)
return self.db.get_metrics(project_id=project_id, user_id=oidc_sub, excluded_projects=excluded_projects)
except Exception as e:
self.logger.error(e)
self.logger.error(traceback.format_exc())
Expand All @@ -254,13 +254,14 @@ def get_metrics(self, *, project_id: str, oidc_sub: str) -> list:
self.lock.release()

def get_slice_count(self, *, project_id: str = None, email: str = None, states: list[int] = None,
oidc_sub: str = None, slc_type: List[SliceTypes] = None) -> List[ABCSlice] or None:
oidc_sub: str = None, slc_type: List[SliceTypes] = None,
excluded_projects: List[str] = None) -> int:
try:
slice_type = None
if slc_type is not None:
slice_type = [x.value for x in slc_type]
return self.db.get_slice_count(project_id=project_id, email=email,
states=states, oidc_sub=oidc_sub, slc_type=slice_type)
return self.db.get_slice_count(project_id=project_id, email=email, states=states, oidc_sub=oidc_sub,
slc_type=slice_type, excluded_projects=excluded_projects)
except Exception as e:
self.logger.error(e)
self.logger.error(traceback.format_exc())
Expand Down
17 changes: 13 additions & 4 deletions fabric_cf/actor/db/psql_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,16 @@ def create_slices_filter(*, slice_id: str = None, slice_name: str = None, projec
filter_dict['oidc_claim_sub'] = oidc_sub
return filter_dict

def get_slice_count(self, *, project_id: str = None, email: str = None, states: list[int] = None,
oidc_sub: str = None, slc_type: list[int] = None) -> int:
def get_slice_count(self, *, project_id: str = None, email: str = None, states: List[int] = None,
oidc_sub: str = None, slc_type: List[int] = None, excluded_projects: List[str]) -> int:
"""
Get slices count for an actor
@param project_id project id
@param email email
@param states states
@param oidc_sub oidc claim sub
@param slc_type slice type
@param excluded_projects excluded_projects
@return list of slices
"""
session = self.get_session()
Expand All @@ -564,6 +565,9 @@ def get_slice_count(self, *, project_id: str = None, email: str = None, states:
if slc_type is not None:
rows = rows.filter(Slices.slc_type.in_(slc_type))

if excluded_projects is not None:
rows = rows.filter(Slices.project_id.notin_(excluded_projects))

return rows.count()
except Exception as e:
self.logger.error(Constants.EXCEPTION_OCCURRED.format(e))
Expand Down Expand Up @@ -1626,12 +1630,13 @@ def increment_metrics(self, *, project_id: str, user_id: str, slice_count: int =
self.logger.error(Constants.EXCEPTION_OCCURRED.format(e))
raise e

def get_metrics(self, *, project_id: str = None, user_id: str = None) -> list:
def get_metrics(self, *, project_id: str = None, user_id: str = None, excluded_projects: List[str] = None) -> list:
"""
Get Metric count
@param project_id: project_id
@param user_id: user_id
@return entry identified by name
@param excluded_projects: excluded_projects
@return list of metrics
"""
result = []
session = self.get_session()
Expand All @@ -1644,6 +1649,10 @@ def get_metrics(self, *, project_id: str = None, user_id: str = None) -> list:
filter_criteria = and_(Metrics.project_id == project_id)
elif user_id is not None:
filter_criteria = and_(Metrics.user_id == user_id)

if excluded_projects:
filter_criteria = and_(Metrics.project_id.notin_(excluded_projects))

rows = session.query(Metrics).filter(filter_criteria).all()

for r in rows:
Expand Down
8 changes: 5 additions & 3 deletions fabric_cf/orchestrator/core/orchestrator_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ def get_poas(self, *, token: str, sliver_id: str = None, poa_id: str = None, sta
self.logger.error(f"Exception occurred processing poa e: {e}")
raise e

def get_metrics_overview(self, *, token: str):
def get_metrics_overview(self, *, token: str = None, excluded_projects: List[str] = None):
"""
Get metrics overview
"""
Expand All @@ -978,8 +978,10 @@ def get_metrics_overview(self, *, token: str):
user_id = fabric_token.uuid

active_states = SliceState.list_values_ex_closing_dead()
active_slice_count = controller.get_slice_count(states=active_states, user_id=user_id, project=project)
non_active_metrics = controller.get_metrics(user_id=user_id, project_id=project)
active_slice_count = controller.get_slice_count(states=active_states, user_id=user_id, project=project,
excluded_projects=excluded_projects)
non_active_metrics = controller.get_metrics(user_id=user_id, project_id=project,
excluded_projects=excluded_projects)
total_slices = 0
for m in non_active_metrics:
total_slices += m.get("slice_count", 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def metrics_overview_get(excluded_projects=None): # noqa: E501
:rtype: Metrics
"""
return rc.metrics_overview_get()
return rc.metrics_overview_get(excluded_projects=excluded_projects)
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from typing import List

from fabric_cf.orchestrator.swagger_server.response.utils import get_token

from fabric_cf.orchestrator.swagger_server.response.constants import GET_METHOD, METRICS_GET_PATH

from fabric_cf.orchestrator.swagger_server import received_counter, success_counter, failure_counter
Expand All @@ -9,7 +13,7 @@
from fabric_cf.orchestrator.swagger_server.models import Metrics


def metrics_overview_get() -> Metrics: # noqa: E501
def metrics_overview_get(excluded_projects: List[str] = None) -> Metrics: # noqa: E501
"""Control Framework metrics overview
{
"results": [
Expand All @@ -32,7 +36,8 @@ def metrics_overview_get() -> Metrics: # noqa: E501
logger = handler.get_logger()
received_counter.labels(GET_METHOD, METRICS_GET_PATH).inc()
try:
metrics = handler.get_metrics_overview()
token = get_token()
metrics = handler.get_metrics_overview(token=token, excluded_projects=excluded_projects)
response = Metrics()
if metrics:
if isinstance(metrics.json_data, list):
Expand Down

0 comments on commit 1312eb8

Please sign in to comment.