Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
kerrychu committed Jan 23, 2024
1 parent 318c3a5 commit a9c4866
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
12 changes: 2 additions & 10 deletions hooks/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
import json


def send_slack_message(data: dict[str, str], webhook: str) -> str:
text= ""
for key, value in data.items():
if isinstance(value, list):
for element in value:
for subkey, subvalue in element.items():
text += f"\t⦿ {subkey}: {subvalue}\n"
text += f"⦿ {key}: {value}\n"

payload = {"text": text}
def send_slack_message(message: str, webhook: str) -> str:
payload = {"text": message}
payload_json = json.dumps(payload)
headers = {"Content-type": "application/json"}
response = requests.request(
Expand Down
25 changes: 20 additions & 5 deletions jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
read_json_as_job_records,
write_job_records_to_json,
)
from utils.subprocess_operations import stdout_to_job_records, JOB_RECORDS, get_cmd_stdout
from utils.subprocess_operations import (
stdout_to_job_records,
JOB_RECORDS,
get_cmd_stdout,
)

load_dotenv()

Expand Down Expand Up @@ -37,6 +41,16 @@ def get_last_updated_job_records() -> Optional[JOB_RECORDS]:
return read_json_as_job_records(JOB_FILE_PATH)


def job_records_to_slack_message(job_records: JOB_RECORDS) -> str:
slack_message = ""
slack_message += "🔉 Update: New Jobs\n"
for job_record in job_records:
slack_message += "\n"
for key, value in job_record:
slack_message += f"⦿ {key}: {value}"
return slack_message


def monitor_my_jobs():
"""monitor all jobs"""

Expand All @@ -58,16 +72,17 @@ def monitor_my_jobs():
for record in current_jobs_records
if record["JOBID"] in new_job_ids
]
data = {"update": "NEW JOBS", "jobs": new_job_records}
send_slack_message(data=data, webhook=SLACK_WEBHOOK)

slack_message = job_records_to_slack_message(new_job_records)
send_slack_message(message=slack_message, webhook=SLACK_WEBHOOK)
if finished_job_ids != set():
finished_job_records = [
record
for record in last_updated_job_records
if record["JOBID"] in finished_job_ids
]
data = {"update": "Finished JOBS", "jobs": finished_job_records}
send_slack_message(data=data, webhook=SLACK_WEBHOOK)
slack_message = job_records_to_slack_message(finished_job_records)
send_slack_message(message=slack_message, webhook=SLACK_WEBHOOK)
write_job_records_to_json(current_jobs_records, JOB_FILE_PATH)
else:
last_updated_job_records = list_my_job_records()
Expand Down
10 changes: 7 additions & 3 deletions quota_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

load_dotenv()

PROJECT_IDs: list[str] = os.getenv("PROJECT_IDS").split(',')
PROJECT_IDs: list[str] = os.getenv("PROJECT_IDS").split(",")
SLACK_WEBHOOK: str = os.getenv("SLACK_QUOTA_WEBHOOK")


Expand All @@ -21,8 +21,12 @@ def monitor():
for project_id in PROJECT_IDs:
raw_quota = get_fileset_quota(project_id)
quota_record = stdout_to_quota_records(raw_quota)
send_slack_message(data=quota_record, webhook=SLACK_WEBHOOK)
slack_message = ""
for key, value in quota_record.items():
slack_message += f"⦿ {key}: {value}\n"

send_slack_message(message=slack_message, webhook=SLACK_WEBHOOK)


if __name__ == "__main__":
monitor()
monitor()
29 changes: 22 additions & 7 deletions utils/subprocess_operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import subprocess
from itertools import zip_longest
from typing import Optional

JOB_RECORD = dict[str, str]
Expand All @@ -20,17 +19,26 @@ def get_cmd_stdout(command: str) -> str:


def get_piped_stdout(main_command: str, piped_command: str) -> Optional[str]:
initial_command_result = subprocess.run(main_command.split(), stdout=subprocess.PIPE, text=True, check=False)
initial_command_result = subprocess.run(
main_command.split(), stdout=subprocess.PIPE, text=True, check=False
)

if initial_command_result.returncode == 0:
initial_stdout = initial_command_result.stdout
piped_result = subprocess.run(piped_command.split(" "), input=initial_stdout, stdout=subprocess.PIPE, text=True,
check=False)
piped_result = subprocess.run(
piped_command.split(" "),
input=initial_stdout,
stdout=subprocess.PIPE,
text=True,
check=False,
)

if piped_result.returncode == 0:
return piped_result.stdout
else:
print(f"Command: {main_command} | {piped_command} failed with error: {initial_command_result.stderr}")
print(
f"Command: {main_command} | {piped_command} failed with error: {initial_command_result.stderr}"
)


def strip_spaces(l: list[str]) -> list[str]:
Expand All @@ -55,10 +63,17 @@ def stdout_to_job_records(s: str) -> JOB_RECORDS:
def stdout_to_quota_records(s: str) -> QUOTA_RECORD:
s = s.strip()
s_list = s.split()
headers = ["FileSet", "Used Storage (GB)", "Storage Limit (GB)", "Current File Number", "File Number Limit"]
headers = [
"FileSet",
"Used Storage (GB)",
"Storage Limit (GB)",
"Current File Number",
"File Number Limit",
]
quota_record = {}
assert len(s_list) == len(
headers), f"unexpected error: quota size different from headers size. quota: {s_list}, headers: {headers}"
headers
), f"unexpected error: quota size different from headers size. quota: {s_list}, headers: {headers}"
for header, quota in zip(headers, s_list):
quota_record[header] = quota
return quota_record

0 comments on commit a9c4866

Please sign in to comment.