Skip to content

Commit

Permalink
Merge branch 'master' into rookie-subletting
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-Jess committed Oct 27, 2023
2 parents 1afb007 + 21f58a9 commit 08963d2
Show file tree
Hide file tree
Showing 11 changed files with 988 additions and 681 deletions.
2 changes: 1 addition & 1 deletion .github/cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"upgrade-cdk": "yarn upgrade cdkactions@latest cdkactions-cli@latest"
},
"dependencies": {
"@pennlabs/kraken": "^0.8.6",
"@pennlabs/kraken": "^0.8.12",
"cdkactions": "^0.2.3",
"constructs": "^3.2.109"
},
Expand Down
8 changes: 4 additions & 4 deletions .github/cdk/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@pennlabs/kraken@^0.8.6":
version "0.8.6"
resolved "https://registry.yarnpkg.com/@pennlabs/kraken/-/kraken-0.8.6.tgz#79a9d10bed36b699c526556cd69b6d81341847d1"
integrity sha512-aBblQa/661DJ2GP3Dq1KEzCZ72ZV/Jw7z4HNZoWPxGWn+tSPwvaPkSNDpK7tT+nJmu427giGU8DLyciU79hKbA==
"@pennlabs/kraken@^0.8.12":
version "0.8.12"
resolved "https://registry.yarnpkg.com/@pennlabs/kraken/-/kraken-0.8.12.tgz#63aab218236f33af0ec0b90344926e7cbf167d60"
integrity sha512-+iYz/Xv674c0VujbYc65sFYU182Su3cmcByUndwKdnj75KDQVcHKOKrcfAcf5Z7eerGAYC7xfNftaZydjV0alw==
dependencies:
cdkactions "^0.2.3"
constructs "^3.2.80"
Expand Down
25 changes: 15 additions & 10 deletions .github/workflows/cdkactions_build-and-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ jobs:
run: |-
cd backend
pipenv run black --check .
# - name: Test (run in parallel)
# run: |-
# cd backend
# pipenv run coverage run --concurrency=multiprocessing manage.py test --settings=pennmobile.settings.ci --parallel
# pipenv run coverage combine
# - name: Upload Code Coverage
# run: |-
# ROOT=$(pwd)
# cd backend
# pipenv run codecov --root $ROOT --flags backend
- name: Test (run in parallel)
run: |-
cd backend
pipenv run coverage run --concurrency=multiprocessing manage.py test --settings=pennmobile.settings.ci --parallel
pipenv run coverage combine
pipenv run coverage xml
- name: Upload Code Coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./backend/
fail_ci_if_error: true
files: coverage.xml
name: codecov-umbrella
verbose: true
container:
image: python:3.9.14-buster
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cdkactions_validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
yarn install
yarn build
git --no-pager diff ../workflows
# git diff-index --quiet HEAD -- ../workflows
git diff-index --quiet HEAD -- ../workflows
- name: Push updated manifests
if: "false"
run: |-
Expand Down
1 change: 1 addition & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ flake8-absolute-import = "*"
rope = "*"
pytest = "*"
tblib = "*"
coverage = "*"

[packages]
beautifulsoup4 = "*"
Expand Down
1,445 changes: 785 additions & 660 deletions backend/Pipfile.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions backend/tests/penndata/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def setUp(self):
)

def test_response(self):

response = self.client.get(reverse("events", args=["type"]))
res_json = json.loads(response.content)
self.assertEquals(2, len(res_json))
Expand Down Expand Up @@ -161,6 +160,7 @@ def test_get_recent(self):
{
"id": room.id,
"name": room.name,
"image_url": room.image_url,
"last_updated": None,
"count": None,
"capacity": None,
Expand All @@ -174,6 +174,7 @@ def test_get_recent(self):
"name": self.fitness_room.name,
# this format: 2023-03-12T16:56:51-04:00
"last_updated": self.new_time.strftime("%Y-%m-%dT%H:%M:%S%z")[:-2] + ":00",
"image_url": self.fitness_room.image_url,
"count": self.new_count,
"capacity": None,
}
Expand All @@ -192,7 +193,6 @@ def setUp(self):
call_command("load_fitness_rooms")

def test_get_fitness_snapshot(self):

self.assertEqual(FitnessSnapshot.objects.all().count(), 0)

call_command("get_fitness_snapshot")
Expand All @@ -215,7 +215,7 @@ class TestFitnessUsage(TestCase):
def load_snapshots_1(self, date):
# 6:00, 0
FitnessSnapshot.objects.create(
room=self.room, date=date + datetime.timedelta(hours=6), count=0, capacity=0.0
room=self.room, date=date + datetime.timedelta(hours=6), count=0, capacity=0.0,
)
# 7:30, 10
FitnessSnapshot.objects.create(
Expand All @@ -226,7 +226,7 @@ def load_snapshots_1(self, date):
)
# 8:00, 65
FitnessSnapshot.objects.create(
room=self.room, date=date + datetime.timedelta(hours=8), count=65, capacity=65.0
room=self.room, date=date + datetime.timedelta(hours=8), count=65, capacity=65.0,
)
# 8:30, 0
FitnessSnapshot.objects.create(
Expand All @@ -237,7 +237,7 @@ def load_snapshots_1(self, date):
)
# 10:00, 60
FitnessSnapshot.objects.create(
room=self.room, date=date + datetime.timedelta(hours=10), count=60, capacity=60.0
room=self.room, date=date + datetime.timedelta(hours=10), count=60, capacity=60.0,
)
# 20:00, 0
FitnessSnapshot.objects.create(
Expand Down
Empty file added backend/tests/utils/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions backend/tests/utils/test_r_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from json.decoder import JSONDecodeError
from unittest.mock import patch

from django.test import TestCase

from utils.r_request import RRequest


def raise_decode_error():
raise JSONDecodeError("Invalid JSON data", "invalid_json", 0)


class RRequestTestCase(TestCase):
def setUp(self):
self.url = "https://pennlabs.org"
self.json = {"data": "data"}
self.rrequest = RRequest()

@patch("requests.request")
def test_successful_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.request("get", self.url)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_unsuccessful_request(self, mock_response):
mock_response.return_value.status_code = 400
mock_response.return_value.content = "Bad Error"
response = self.rrequest.request("post", self.url, json=self.json)
self.assertEqual(400, response.status_code)
self.assertEqual("Bad Error", response.content)

@patch("requests.request")
def test_bad_json(self, mock_response):
mock_response.return_value.status_code = 200
mock_response.return_value.json = raise_decode_error
response = self.rrequest.delete(self.url, json=self.json)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_get_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.get(self.url)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_post_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.post(self.url, json=self.json)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_patch_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.patch(self.url)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_put_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.put(self.url, json=self.json)
self.assertEqual(200, response.status_code)

@patch("requests.request")
def test_delete_request(self, mock_response):
mock_response.return_value.status_code = 200
response = self.rrequest.delete(self.url, json=self.json)
self.assertEqual(200, response.status_code)
101 changes: 101 additions & 0 deletions backend/utils/r_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from enum import Enum
from json.decoder import JSONDecodeError

import requests


class Method(str, Enum):
POST = "post"
GET = "get"
PATCH = "patch"
PUT = "put"
DELETE = "delete"


class RRequest:
"""
Robust wrapper around Python requests library
Primary use case is to interact with unstable APIs where responses
return one-off malformed data or failures
"""

NUM_RETRIES = 2

def __init__(self, num_retries=NUM_RETRIES):
self.num_retries = num_retries

def get(self, *args, **kwargs):
return self.request(Method.GET, *args, **kwargs)

def post(self, *args, **kwargs):
return self.request(Method.POST, *args, **kwargs)

def patch(self, *args, **kwargs):
return self.request(Method.PATCH, *args, **kwargs)

def put(self, *args, **kwargs):
return self.request(Method.PUT, *args, **kwargs)

def delete(self, *args, **kwargs):
return self.request(Method.DELETE, *args, **kwargs)

def request(
self,
method,
url,
params=None,
data=None,
headers=None,
cookies=None,
files=None,
auth=None,
timeout=None,
allow_redirects=True,
proxies=None,
hooks=None,
stream=None,
verify=None,
cert=None,
json=None,
):
response = self.__default_response()

for _ in range(self.num_retries):
response = requests.request(
method,
url,
params=params,
data=data,
headers=headers,
cookies=cookies,
files=files,
auth=auth,
timeout=timeout,
allow_redirects=allow_redirects,
proxies=proxies,
hooks=hooks,
stream=stream,
verify=verify,
cert=cert,
json=json,
)

if response.status_code != 200:
continue

try:
response.json()
except (TypeError, JSONDecodeError):
continue
return response

if not response.content:
response.content = "RRequest: Default Error"

return response

def __default_response(self):
response = requests.models.Response
response.status_code = 400
return response
7 changes: 7 additions & 0 deletions k8s/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export class MyChart extends PennLabsChart {
env: [{ name: "DJANGO_SETTINGS_MODULE", value: "pennmobile.settings.production" }]
});

new CronJob(this, 'load-target-populations', {
schedule: cronTime.everyYearIn(8),
image: backendImage,
secret,
cmd: ["python", "manage.py", "load_target_populations"],
env: [{ name: "DJANGO_SETTINGS_MODULE", value: "pennmobile.settings.production" }]
});
}
}

Expand Down

0 comments on commit 08963d2

Please sign in to comment.