Skip to content

Commit f50098a

Browse files
authored
Merge branch 'main' into add-pf-error-handle
2 parents 9ae4398 + 07f6f0d commit f50098a

33 files changed

+920
-285
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
name: Report Broken/Down API
3+
about: Report a broken API or an issue with any API
4+
title: ''
5+
labels: help wanted
6+
assignees: marcodarko
7+
8+
---
9+
10+
## Report a broken API
11+
12+
Fill this out if you would like to report an issue with an API. Please include as much detail as possible to make sure we address your issue as soon as possible.
13+
14+
15+
16+
*Let's get some information first*
17+
18+
**I'm am reporting...**
19+
20+
- [ ] An API with broken source URL
21+
22+
- [ ] An API Down
23+
24+
- [ ] An API with FAIL uptime
25+
26+
- [ ] A Different issue. Please specify below.
27+
28+
29+
* Details *
30+
31+
32+
* What is the ***name*** and ***id*** of the API? (You can find the ID under the Details section of the API) *eg. TheAPI : 124094327094378*
33+
34+
35+
36+
37+
* How is this issue affecting your work? This can help us prioritize our issues.
38+
39+
40+
41+
42+
Thank you! We will review this request ASAP and will inform you of any progress.

.github/scripts/check_backup.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
This script checks if a backup file for the current date exists in a specified S3 bucket.
3+
If the backup file does not exist, a notification is sent to a Slack channel.
4+
5+
Expected file format in the S3 bucket:
6+
- The file should be in the folder 'db_backup/' with the following naming pattern:
7+
'smartapi_YYYYMMDD.zip', where YYYYMMDD corresponds to the current date.
8+
9+
Required Environment Variables:
10+
- AWS_ACCESS_KEY_ID: The AWS access key ID to read the AWS s3 bucket.
11+
- AWS_SECRET_ACCESS_KEY: The AWS secret access key to read the AWS s3 bucket.
12+
- BACKUP_BUCKET_NAME: The name of the AWS S3 bucket where backups are stored.
13+
- S3_FOLDER: The folder path within the S3 bucket where backups are stored (e.g., 'db_backup/').
14+
- AWS_REGION: The AWS region where the S3 bucket is located.
15+
- SLACK_CHANNEL: The Slack channel where notifications should be sent (e.g., '#observability-test').
16+
- SLACK_WEBHOOK_URL: The Slack Webhook URL used to send the notification.
17+
18+
Functionality:
19+
1. The script uses the AWS SDK (boto3) to check for the existence of the backup file in the specified S3 bucket.
20+
2. If the file is found, it logs that no action is needed.
21+
3. If the file is not found, it sends a notification to the configured Slack channel.
22+
23+
Dependencies:
24+
- boto3: For interacting with AWS S3.
25+
- requests: For sending HTTP POST requests to Slack.
26+
27+
"""
28+
29+
import boto3
30+
import botocore
31+
import os
32+
import requests
33+
34+
from datetime import datetime
35+
36+
37+
def send_slack_notification(message):
38+
39+
print(f" └─ {message}")
40+
41+
# Create the payload for Slack
42+
slack_data = {
43+
"channel": os.getenv("SLACK_CHANNEL"),
44+
"username": "SmartAPI",
45+
"icon_emoji": ":thumbsdown:",
46+
"text": message,
47+
}
48+
49+
try:
50+
print(" └─ Sending Slack notification.")
51+
response = requests.post(os.getenv("SLACK_WEBHOOK_URL"), json=slack_data, timeout=10)
52+
if response.status_code == 200:
53+
print(" └─ Slack notification sent successfully.")
54+
else:
55+
print(f" └─ Failed to send message to Slack: {response.status_code}, {response.text}")
56+
except requests.exceptions.Timeout as e:
57+
print(" └─ Request timed out to Slack WebHook URL.")
58+
raise e
59+
except requests.exceptions.RequestException as e:
60+
print(f" └─ Failed to send Slack notification. Error: {str(e)}")
61+
raise e
62+
63+
64+
def check_backup_file():
65+
66+
# Create the expected file name
67+
today_date = datetime.today().strftime("%Y%m%d")
68+
expected_file = f"{os.getenv('S3_FOLDER')}smartapi_{today_date}.zip"
69+
70+
# Create the S3 client
71+
s3_client = boto3.client("s3", region_name=os.getenv("AWS_REGION"))
72+
73+
# Try to fetch the file metadata
74+
try:
75+
response = s3_client.head_object(Bucket=os.getenv("BACKUP_BUCKET_NAME"), Key=expected_file)
76+
print(f" └─ Backup file {expected_file} exists!")
77+
78+
# Get the file size in bytes
79+
file_size = response['ContentLength']
80+
81+
# Check if the file is larger than 1MB
82+
if file_size > 1048576: # 1MB in bytes
83+
print(f" └─ Backup file is larger than 1MB! Size: {file_size} bytes.")
84+
print(" └─ Nothing to do!")
85+
else:
86+
message = f":alert: The backup file {expected_file} is smaller than 1MB!"
87+
send_slack_notification(message)
88+
89+
except botocore.exceptions.ClientError as e:
90+
print(e)
91+
message = f":alert: The backup file {expected_file} was NOT created today!"
92+
send_slack_notification(message)
93+
94+
95+
if __name__ == "__main__":
96+
check_backup_file()

.github/workflows/app_tests.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: App Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- main
10+
11+
jobs:
12+
run_app_tests:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
18+
steps:
19+
- name: Checkout source
20+
uses: actions/checkout@v4
21+
- name: Set up Python ${{ matrix.python-version }}
22+
uses: actions/setup-python@v5
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Update
26+
run: sudo apt-get update
27+
- name: Install apt-get packages
28+
run: sudo apt-get install libssl-dev libcurl4-openssl-dev
29+
- name: Upgrade pip
30+
run: pip install --upgrade pip
31+
- name: Install dependencies
32+
run: pip install -r requirements.txt
33+
- name: Install PyTest
34+
run: pip install pytest
35+
- name: Run App Tests
36+
run: pytest tests
37+
working-directory: src
38+
services:
39+
Elasticsearch:
40+
image: docker.elastic.co/elasticsearch/elasticsearch:8.14.3
41+
env:
42+
"discovery.type": single-node
43+
"xpack.security.enabled": false
44+
"xpack.security.http.ssl.enabled": false
45+
"xpack.security.transport.ssl.enabled": false
46+
ports:
47+
- 9200:9200

.github/workflows/check_backup.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Check S3 Backup and Notify Slack
2+
3+
on:
4+
workflow_dispatch: # Allows manual trigger from GitHub Actions UI
5+
schedule:
6+
- cron: '0 13 * * *' # 5:00 AM PST (UTC-8)
7+
8+
jobs:
9+
check-backup:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.x'
20+
21+
- name: Install boto3 (AWS SDK for Python)
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install boto3 requests
25+
26+
- name: Check if backup exists in S3
27+
run: python .github/scripts/check_backup.py
28+
env:
29+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
30+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
31+
AWS_REGION: ${{ secrets.AWS_REGION }}
32+
BACKUP_BUCKET_NAME: "${{ secrets.BACKUP_BUCKET_NAME }}"
33+
S3_FOLDER: "db_backup/"
34+
SLACK_CHANNEL: "#ncats-translator"
35+
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

.github/workflows/deploy-dev.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Deploy-to-EC2-Dev
22

33
on:
4+
workflow_dispatch:
45
push:
56
branches:
67
- main
@@ -42,6 +43,7 @@ jobs:
4243
cd /home/ubuntu/smartapi
4344
source .env/bin/activate
4445
echo "Installing backend requirements."
46+
pip install --upgrade pip
4547
pip install -Ur requirements.txt --no-cache-dir --ignore-installed --force-reinstall
4648
echo "Restarting smartapi backend services..."
4749
sudo systemctl restart smartapi@8000

.github/workflows/deploy-prod.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jobs:
3939
cd /home/ubuntu/smartapi
4040
source .env/bin/activate
4141
echo "Installing backend requirements."
42+
pip install --upgrade pip
4243
pip install -Ur requirements.txt --no-cache-dir --ignore-installed --force-reinstall
4344
echo "Restarting smartapi backend services..."
4445
sudo systemctl restart smartapi@8000

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.black]
22
line-length = 120
3-
target-version = ['py36', 'py37', 'py38', 'py39', 'py310']
3+
target-version = ["py38", "py39", "py310", "py311", "py312"]
44

55
[tool.isort]
66
profile = "black"

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
biothings[web_extra]==0.12.4
1+
biothings[web_extra]==0.12.5
22
# git+https://github.com/biothings/biothings.api.git@0.11.x#egg=biothings[web_extra]
33

44
# document validation
@@ -10,7 +10,7 @@ pycurl==7.45.2 # to use curl_httpclient in tornado
1010
aiocron==1.8
1111

1212
# gitdb version specified because gitdb.utils.compat not available in newest version
13-
gitdb==4.0.9
13+
gitdb==4.0.11
1414

1515
# local issuer certificate
1616
certifi
@@ -23,6 +23,6 @@ filelock
2323
boto3
2424

2525
# Biolink Model Toolkit, used in /api/metakg endpoint
26-
bmt-lite-v3.1.0==2.2.2
27-
26+
bmt-lite-v3.1.0==2.2.2; python_version >= "3.7.0" and python_version < "3.9.0"
27+
bmt-lite-v3.6.0==2.3.0; python_version >= "3.9.0"
2828
networkx==3.1.0

0 commit comments

Comments
 (0)