Skip to content

Commit

Permalink
Issue #3161 User stats report - support new template, add user name t…
Browse files Browse the repository at this point in the history
…o report
  • Loading branch information
mzueva committed Aug 1, 2023
1 parent 6e7b268 commit 60f8941
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
<br />
<span style="font-size:smaller">Usage statistics for</span>
<span style="font-size:smaller">{PERIOD}</span>
<br />
<span style="font-size:smaller">{USER_NAME}</span>
</td>
</tr>
<tr>
Expand Down
45 changes: 31 additions & 14 deletions workflows/pipe-common/scripts/send_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
TABLE_TEMPLATE = "group-3-table.html"
TABLE_CENTER_TEMPLATE = "group-3-table-center.html"

ICON_URL_TEMPLATE = os.getenv("CP_ICON_URL", "https://raw.githubusercontent.com/epam/cloud-pipeline/develop/deploy/contents/install/email-templates/user-statistics/")
USER_FIRST_NAME_ATTR = os.getenv("CP_USER_FIRST_NAME", "FirstName")
USER_LAST_NAME_ATTR = os.getenv("CP_USER_LAST_NAME", "LastName")

TYPE = 'Type'
TYPE_GPU = 'GPU'
TYPE_CPU = 'CPU'
Expand All @@ -47,11 +51,11 @@


class Top3(Enum):
INSTANCES = ("Top-3 instances", "instance")
PIPELINES = ("Top-3 pipelines", "pipeline")
TOOLS = ("Top-3 tools", "tool")
BUCKETS = ("Top-3 used buckets", "bucket")
CAPABILITIES = ("Top-3 run capabilities", "capability")
INSTANCES = ("Top-3 instances", "share", "instance")
PIPELINES = ("Top-3 pipelines", "fork", "pipeline")
TOOLS = ("Top-3 tools", "tool", "tool")
BUCKETS = ("Top-3 used buckets", "hdd", "bucket")
CAPABILITIES = ("Top-3 run capabilities", "play", "capability")


class Stat(object):
Expand Down Expand Up @@ -134,8 +138,20 @@ def format_tables(tables, table_center_templ, table_templ):
def format_column(table_center_templ, tables):
column = tables.popitem()
return table_center_templ.format(**{"TABLE_TITLE": column[0][0],
"TABLE_ICON": column[0][1],
"ITEMS": column[1]})
"ITEMS": column[1],
"TABLE_ICON": column[0][2],
"TABLE_ICON_URL": Stat.get_icon_url(column[0][1])})
@staticmethod
def get_icon_url(icon):
return ICON_URL_TEMPLATE + icon + ".svg"

@staticmethod
def format_user(user):
attributes = user.get('attributes', {})
if USER_LAST_NAME_ATTR and USER_FIRST_NAME_ATTR in attributes:
return attributes[USER_FIRST_NAME_ATTR] + ' ' + attributes[USER_LAST_NAME_ATTR]
else:
return user.get('userName')

def get_tables(self):
tables = dict()
Expand All @@ -151,9 +167,10 @@ def get_tables(self):
tables[Top3.CAPABILITIES.value] = Stat.format_items_seconds(self.top3_run_capabilities)
return tables

def get_object_str(self, start, end, deploy_name, template, table_templ, table_center_templ):
def get_object_str(self, start, end, deploy_name, template, table_templ, table_center_templ, user):
return template.format(**{'PLATFORM_NAME': deploy_name,
'PERIOD': "{} - {}".format(start, end),
'USER_NAME': Stat.format_user(user),
'COMPUTE_HOURS': Stat.format_hours(self.compute_hours),
'RUNS_COUNT': Stat.format_count(self.runs_count),
'LOGIN_TIME': Stat.format_hours(self.login_time),
Expand All @@ -163,7 +180,7 @@ def get_object_str(self, start, end, deploy_name, template, table_templ, table_c
'CPU': Stat.format_hours(self.cpu_hours),
'GPU': Stat.format_hours(self.gpu_hours),
'COMPUTE': Stat.format_seconds(self.clusters_compute_secs),
'WORKER_NODES': self.worker_nodes_count,
'WORKER_NODES': Stat.format_seconds(self.worker_nodes_count),
'TABLE': Stat.format_tables(self.get_tables(), table_center_templ, table_templ)})


Expand All @@ -178,10 +195,10 @@ def __init__(self, api, start, end, stat, deploy_name, to_user, logger, cc_users
self.logger = logger
self.cc_users = cc_users

def send_notifications(self, template, table_templ, table_center_templ):
def send_notifications(self, template, table_templ, table_center_templ, user):
if not self.to_user:
return
text = self.build_text(template, table_templ, table_center_templ)
text = self.build_text(template, table_templ, table_center_templ, user)
self.logger.debug("Email Text:")
self.logger.debug(text)
self.logger.info('Sending notification to user: %s' % self.to_user)
Expand All @@ -190,9 +207,9 @@ def send_notifications(self, template, table_templ, table_center_templ):
self.to_user,
copy_users=self.cc_users)

def build_text(self, template, table_templ, table_center_templ):
def build_text(self, template, table_templ, table_center_templ, user):
stat_str = self.stat.get_object_str(self.start, self.end, self.deploy_name,
template, table_templ, table_center_templ)
template, table_templ, table_center_templ, user)
return EMAIL_TEMPLATE.format(**{'text': stat_str,
'from': self.start,
'to': self.end,
Expand Down Expand Up @@ -299,7 +316,7 @@ def _send_users_stat(api, logger, from_date, to_date, users, template_path, send
receiver = send_to if send_to else _user_name
notifier = Notifier(api, from_date, to_date, stat, os.getenv('CP_DEPLOY_NAME', 'Cloud Pipeline'),
receiver, logger, cc_users=cc_users)
notifier.send_notifications(template, table_templ, table_center_templ)
notifier.send_notifications(template, table_templ, table_center_templ, user)


def expand_commas(data):
Expand Down

0 comments on commit 60f8941

Please sign in to comment.