|
| 1 | +"""A Python script to refresh the dataset for the GitHub Policy Dashboard.""" |
| 2 | + |
| 3 | +import botocore.config |
| 4 | +import boto3 |
| 5 | +import botocore |
| 6 | +from requests import Response |
| 7 | +import datetime |
| 8 | + |
| 9 | +import utilities as utils |
| 10 | + |
| 11 | +def refresh_data() -> dict: |
| 12 | + """A function to refresh the dataset for the GitHub Policy Dashboard. |
| 13 | +
|
| 14 | + Returns: |
| 15 | + dict: A dictionary containing the status of the data refresh operation and a message. |
| 16 | + """ |
| 17 | + |
| 18 | + # Check GitHub API rate limit |
| 19 | + # If not enough rate limit, show error message and say when to try again |
| 20 | + # If enough rate limit, proceed with data refresh |
| 21 | + |
| 22 | + env = utils.get_environment_variables() |
| 23 | + |
| 24 | + session = boto3.Session() |
| 25 | + secret_manager = session.client("secretsmanager", region_name=env["secret_region"]) |
| 26 | + |
| 27 | + rest = utils.get_rest_interface( |
| 28 | + secret_manager, |
| 29 | + env["secret_name"], |
| 30 | + env["org"], |
| 31 | + env["client_id"] |
| 32 | + ) |
| 33 | + |
| 34 | + response = rest.get("/rate_limit") |
| 35 | + |
| 36 | + if type(response) is not Response: |
| 37 | + return {"status": "error", "message": "Error fetching rate limit from GitHub API."} |
| 38 | + |
| 39 | + rate_limit = response.json() |
| 40 | + |
| 41 | + remaining = { |
| 42 | + "rest": { |
| 43 | + "remaining": rate_limit["rate"]["remaining"], |
| 44 | + "reset": rate_limit["rate"]["reset"] |
| 45 | + }, |
| 46 | + "graphql": { |
| 47 | + "remaining": rate_limit["resources"]["graphql"]["remaining"], |
| 48 | + "reset": rate_limit["resources"]["graphql"]["reset"] |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if remaining["rest"]["remaining"] < 5000: |
| 53 | + |
| 54 | + reset_time = datetime.datetime.fromtimestamp( |
| 55 | + remaining["rest"]["reset"] |
| 56 | + ).strftime("%H:%M") |
| 57 | + |
| 58 | + return {"status": "error", "message": f"GitHub API rate limit exceeded. Please try again after {reset_time}."} |
| 59 | + |
| 60 | + if remaining["graphql"]["remaining"] < 8000: |
| 61 | + |
| 62 | + reset_time = datetime.datetime.fromtimestamp( |
| 63 | + remaining["graphql"]["reset"] |
| 64 | + ).strftime("%H:%M") |
| 65 | + |
| 66 | + return {"status": "error", "message": f"GitHub GraphQL API rate limit exceeded. Please try again after {reset_time}."} |
| 67 | + |
| 68 | + # Proceed with data refresh |
| 69 | + |
| 70 | + lambda_config = botocore.config.Config( |
| 71 | + read_timeout=900, # 15 minutes (Maximum timeout for Lambda) |
| 72 | + retries={ |
| 73 | + "total_max_attempts": 1, |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + lambda_client = session.client("lambda", region_name=env["secret_region"], config=lambda_config) |
| 78 | + |
| 79 | + response = lambda_client.invoke( |
| 80 | + FunctionName="policy-dashboard-lambda", |
| 81 | + InvocationType="RequestResponse", |
| 82 | + ) |
| 83 | + |
| 84 | + if response["StatusCode"] != 200: |
| 85 | + return {"status": "error", "message": "Error invoking Lambda function to refresh dataset."} |
| 86 | + |
| 87 | + if "FunctionError" in response: |
| 88 | + return {"status": "error", "message": "Error in Lambda function execution."} |
| 89 | + |
| 90 | + return {"status": "success", "message": "Dataset refreshed successfully!"} |
0 commit comments