Skip to content

Commit 428ffe5

Browse files
authored
Merge pull request #377 from fabric-testbed/375-slice-metrics-for-portal-to-display
375 slice metrics for portal to display
2 parents 13037b5 + e8bb52e commit 428ffe5

28 files changed

+833
-211
lines changed

fabric_cf/actor/core/apis/abc_actor_management_object.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,48 @@ def get_slices(self, *, slice_id: ID, caller: AuthToken, slice_name: str = None,
276276
@return returns list of slices
277277
"""
278278

279+
@abstractmethod
280+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
281+
"""
282+
Add or update metrics
283+
284+
@param project_id project id
285+
@param oidc_sub oidc sub
286+
@param slice_count slice_count
287+
288+
@return true or false
289+
290+
@throws Exception in case of error
291+
"""
292+
293+
@abstractmethod
294+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
295+
"""
296+
Get metrics
297+
298+
@param project_id project id
299+
@param oidc_sub oidc sub
300+
@param excluded_projects excluded_projects
301+
302+
@return list of metric information
303+
304+
@throws Exception in case of error
305+
"""
306+
307+
def get_slice_count(self, *, caller: AuthToken, email: str = None, states: List[int] = None,
308+
project: str = None, user_id: str = None, excluded_projects: List[str] = None) -> int:
309+
"""
310+
Obtains Slice count matching the filter criteria.
311+
312+
@param email email
313+
@param project project id
314+
@param states slice states
315+
@param caller caller
316+
@param user_id user_id
317+
@param excluded_projects excluded_projects
318+
@return returns number of slices
319+
"""
320+
279321
def remove_slice(self, *, slice_id: ID, caller: AuthToken) -> ResultAvro:
280322
"""
281323
Removes the specified slice

fabric_cf/actor/core/apis/abc_database.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,50 @@ def get_slices(self, *, slice_id: ID = None, slice_name: str = None, project_id:
217217
@throws Exception in case of error
218218
"""
219219

220+
@abstractmethod
221+
def get_slice_count(self, *, project_id: str = None, email: str = None, states: list[int] = None,
222+
oidc_sub: str = None, slc_type: List[SliceTypes] = None,
223+
excluded_projects: List[str] = None) -> int:
224+
"""
225+
Retrieves the slices count.
226+
227+
@param project_id project id
228+
@param email email
229+
@param states states
230+
@param oidc_sub oidc sub
231+
@param slc_type slice type
232+
@param excluded_projects excluded_projects
233+
234+
@return number of slices matching the filter criteria
235+
236+
@throws Exception in case of error
237+
"""
238+
239+
@abstractmethod
240+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
241+
"""
242+
Add or Update Metrics
243+
244+
@param project_id project id
245+
@param oidc_sub oidc sub
246+
@param slice_count slice_count
247+
248+
@return true or false
249+
250+
@throws Exception in case of error
251+
"""
252+
253+
@abstractmethod
254+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
255+
"""
256+
Get Metrics
257+
@param project_id: project id
258+
@param oidc_sub: user id
259+
@param excluded_projects: list of project ids to exclude
260+
261+
@return list of metrics
262+
"""
263+
220264
@abstractmethod
221265
def initialize(self):
222266
"""

fabric_cf/actor/core/apis/abc_mgmt_actor.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,47 @@ def get_slices(self, *, slice_id: ID = None, slice_name: str = None, email: str
6363
@return returns list of slices
6464
"""
6565

66+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
67+
"""
68+
Add or update metrics
69+
70+
@param project_id project id
71+
@param oidc_sub oidc sub
72+
@param slice_count slice_count
73+
74+
@return true or false
75+
76+
@throws Exception in case of error
77+
"""
78+
raise NotImplementedError
79+
80+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
81+
"""
82+
Get metrics
83+
84+
@param project_id project id
85+
@param oidc_sub oidc sub
86+
@param excluded_projects excluded_projects
87+
88+
@return list of metric information
89+
90+
@throws Exception in case of error
91+
"""
92+
raise NotImplementedError
93+
94+
def get_slice_count(self, *, email: str = None, project: str = None, states: List[int] = None,
95+
user_id: str = None, excluded_projects: List[str] = None) -> int:
96+
"""
97+
Obtains slice count.
98+
@param email email
99+
@param project project id
100+
@param states slice states
101+
@param user_id user_id
102+
@param excluded_projects excluded_projects
103+
@return returns list of slices
104+
"""
105+
raise NotImplementedError
106+
66107
@abstractmethod
67108
def add_slice(self, *, slice_obj: SliceAvro) -> ID:
68109
"""

fabric_cf/actor/core/common/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class Constants:
199199
STATE_FILE_LOCATION = '/tmp/fabric_actor.tmp'
200200
MAINT_PROJECT_ID = 'maint.project.id'
201201
INFRASTRUCTURE_PROJECT_ID = "infrastructure.project.id"
202+
TOTAL_SLICE_COUNT_SEED = "total_slice_count_seed"
202203

203204
ELASTIC_TIME = "request.elasticTime"
204205
ELASTIC_SIZE = "request.elasticSize"

fabric_cf/actor/core/manage/actor_management_object.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ def get_slices(self, *, slice_id: ID, caller: AuthToken, slice_name: str = None,
170170
result.status = ManagementObject.set_exception_details(result=result.status, e=e)
171171
return result
172172

173+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
174+
try:
175+
return self.db.get_metrics(project_id=project_id, oidc_sub=oidc_sub, excluded_projects=excluded_projects)
176+
except Exception as e:
177+
self.logger.error("get_metrics {}".format(e))
178+
179+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
180+
try:
181+
return self.db.increment_metrics(project_id=project_id, oidc_sub=oidc_sub, slice_count=slice_count)
182+
except Exception as e:
183+
self.logger.error("add_or_update_metrics {}".format(e))
184+
185+
def get_slice_count(self, *, caller: AuthToken, email: str = None, states: List[int] = None,
186+
project: str = None, user_id: str = None, excluded_projects: List[str] = None) -> int:
187+
try:
188+
return self.db.get_slice_count(email=email, states=states, project_id=project, oidc_sub=user_id)
189+
except Exception as e:
190+
self.logger.error("get_slice_count {}".format(e))
191+
return -1
192+
173193
def add_slice(self, *, slice_obj: SliceAvro, caller: AuthToken) -> ResultStringAvro:
174194
result = ResultStringAvro()
175195
result.status = ResultAvro()

fabric_cf/actor/core/manage/local/local_actor.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,30 @@ def get_slices(self, *, slice_id: ID = None, slice_name: str = None, email: str
7272

7373
return None
7474

75+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
76+
try:
77+
return self.manager.increment_metrics(project_id=project_id, oidc_sub=oidc_sub, slice_count=slice_count)
78+
except Exception as e:
79+
self.on_exception(e=e, traceback_str=traceback.format_exc())
80+
return False
81+
82+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
83+
try:
84+
return self.manager.get_metrics(project_id=project_id, oidc_sub=oidc_sub,
85+
excluded_projects=excluded_projects)
86+
except Exception as e:
87+
self.on_exception(e=e, traceback_str=traceback.format_exc())
88+
89+
def get_slice_count(self, *, email: str = None, project: str = None, states: List[int] = None,
90+
user_id: str = None, excluded_projects: List[str] = None) -> int:
91+
try:
92+
return self.manager.get_slice_count(caller=self.auth, states=states, email=email, project=project,
93+
user_id=user_id, excluded_projects=excluded_projects)
94+
except Exception as e:
95+
self.on_exception(e=e, traceback_str=traceback.format_exc())
96+
97+
return -1
98+
7599
def remove_slice(self, *, slice_id: ID) -> bool:
76100
self.clear_last()
77101
try:
@@ -307,4 +331,4 @@ def get_poas(self, *, states: List[int] = None, slice_id: ID = None, rid: ID = N
307331
return result.poas
308332

309333
except Exception as e:
310-
self.on_exception(e=e, traceback_str=traceback.format_exc())
334+
self.on_exception(e=e, traceback_str=traceback.format_exc())

fabric_cf/actor/core/plugins/db/actor_database.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,46 @@ def get_slices(self, *, slice_id: ID = None, slice_name: str = None, project_id:
230230
self.lock.release()
231231
return result
232232

233+
def increment_metrics(self, *, project_id: str, oidc_sub: str, slice_count: int = 1) -> bool:
234+
try:
235+
self.lock.acquire()
236+
self.db.increment_metrics(project_id=project_id, user_id=oidc_sub, slice_count=slice_count)
237+
return True
238+
except Exception as e:
239+
self.logger.error(e)
240+
self.logger.error(traceback.format_exc())
241+
finally:
242+
if self.lock.locked():
243+
self.lock.release()
244+
return False
245+
246+
def get_metrics(self, *, project_id: str, oidc_sub: str, excluded_projects: List[str] = None) -> list:
247+
try:
248+
return self.db.get_metrics(project_id=project_id, user_id=oidc_sub, excluded_projects=excluded_projects)
249+
except Exception as e:
250+
self.logger.error(e)
251+
self.logger.error(traceback.format_exc())
252+
finally:
253+
if self.lock.locked():
254+
self.lock.release()
255+
256+
def get_slice_count(self, *, project_id: str = None, email: str = None, states: list[int] = None,
257+
oidc_sub: str = None, slc_type: List[SliceTypes] = None,
258+
excluded_projects: List[str] = None) -> int:
259+
try:
260+
slice_type = [SliceTypes.ClientSlice.value]
261+
if slc_type is not None:
262+
slice_type = [x.value for x in slc_type]
263+
return self.db.get_slice_count(project_id=project_id, email=email, states=states, oidc_sub=oidc_sub,
264+
slc_type=slice_type, excluded_projects=excluded_projects)
265+
except Exception as e:
266+
self.logger.error(e)
267+
self.logger.error(traceback.format_exc())
268+
finally:
269+
if self.lock.locked():
270+
self.lock.release()
271+
return -1
272+
233273
def add_reservation(self, *, reservation: ABCReservationMixin):
234274
try:
235275
#self.lock.acquire()

fabric_cf/actor/db/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# Author: Komal Thareja ([email protected])
2626

2727
from sqlalchemy import JSON, ForeignKey, LargeBinary, Index, TIMESTAMP
28+
from sqlalchemy.dialects.postgresql import JSONB
2829
from sqlalchemy.orm import declarative_base
2930
from sqlalchemy import Column, String, Integer, Sequence
3031
from sqlalchemy.orm import relationship
@@ -92,6 +93,17 @@ class Miscellaneous(Base):
9293
properties = Column(JSON)
9394

9495

96+
class Metrics(Base):
97+
"""
98+
Represents Metrics Database Table
99+
"""
100+
__tablename__ = 'Metrics'
101+
m_id = Column(Integer, Sequence('m_id', start=1, increment=1), autoincrement=True, primary_key=True)
102+
user_id = Column(String, nullable=False, index=True)
103+
project_id = Column(String, nullable=False, index=True)
104+
slice_count = Column(Integer, nullable=False)
105+
106+
95107
class Proxies(Base):
96108
"""
97109
Represents Proxies Database Table

0 commit comments

Comments
 (0)