Created
July 5, 2024 08:48
-
-
Save 094459/e68634386df4f5fb7f6775d339672771 to your computer and use it in GitHub Desktop.
This is a quick lambda function that uses environment variables to hold your GITHUB keys, to them scrape the usage statistics and store them in CloudWatch logs. You should configure this to run only once a day, and configure the Lambda execution configuration to run the appropriate amount of time. Once in CloudWatch logs, you can use CloudWatch …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # forked from https://github.com/AnthonyBloomer/github-traffic-insights | |
| # changed so that it outputs just to cloudwatch | |
| # use Cloudwatch Insights to track GitHub Traffic | |
| import json | |
| import os | |
| from urllib import request | |
| def record_custom_event(event): | |
| print(json.dumps(event).encode("utf-8")) | |
| def api_req(request_url): | |
| token = "token " + os.getenv("GITHUB_TOKEN") | |
| req = request.Request(request_url, headers={"Accept" :"application/vnd.github.v3+json" , "Authorization": token }) | |
| response = request.urlopen(req) | |
| return json.load(response) | |
| # the default only returns 30 repos so we need to set per_page=100 | |
| def lambda_handler(event, context): | |
| base_url = "https://api.github.com" | |
| user = os.getenv("GITHUB_USERNAME") | |
| print("Getting repositories for %s" % user) | |
| req = api_req("%s/users/%s/repos?per_page=100" % (base_url, user)) | |
| ur = [(r["name"]) for r in req] | |
| print("Found %s repositories" % len(ur)) | |
| print(" ") | |
| for repo_name in ur: | |
| print("Getting referral data for %s" % repo_name) | |
| referrals = api_req("%s/repos/%s/%s/traffic/popular/referrers" % (base_url, user, repo_name)) | |
| if len(referrals) > 0: | |
| for ref in referrals: | |
| referred = { | |
| "eventType": "Referral", | |
| "repo_name": repo_name, | |
| "referrer": ref["referrer"], | |
| "count": ref["count"], | |
| "uniques": ref["uniques"], | |
| } | |
| print(referred) | |
| record_custom_event(referred) | |
| print("Getting top referral path data for %s" % repo_name) | |
| paths = api_req("%s/repos/%s/%s/traffic/popular/paths" % (base_url, user, repo_name)) | |
| if len(paths) > 0: | |
| for ref in paths: | |
| paths = { | |
| "eventType": "ReferralPath", | |
| "repo_name": repo_name, | |
| "path": ref["path"], | |
| "title": ref["title"], | |
| "count": ref["count"], | |
| "uniques": ref["uniques"], | |
| } | |
| print(paths) | |
| record_custom_event(paths) | |
| print("Getting views for %s" % repo_name) | |
| req = api_req("%s/repos/%s/%s/traffic/views" % (base_url, user, repo_name)) | |
| if req["count"] > 0 or req["uniques"] > 0: | |
| viewed = { | |
| "eventType": "View", | |
| "repo_name": repo_name, | |
| "count": req["count"], | |
| "uniques": req["uniques"], | |
| } | |
| print(viewed) | |
| record_custom_event(viewed) | |
| print("Getting clones for %s" % repo_name) | |
| req = api_req("%s/repos/%s/%s/traffic/clones" % (base_url, user, repo_name)) | |
| if req["count"] > 0 or req["uniques"] > 0: | |
| cloned = { | |
| "eventType": "Clone", | |
| "repo_name": repo_name, | |
| "count": req["count"], | |
| "uniques": req["uniques"], | |
| } | |
| record_custom_event(cloned) | |
| print(" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment