Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#86 Decoupling the scraper from the backend and editing the scraper to make it more versatile with different quarters #92

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
84b7ca3
Making new scraper folder and editing docker file to accommodate
CTrando Mar 17, 2019
59321d7
#86: decouple scraper from backend
snowme34 Apr 1, 2019
0d19ed8
#86: Allowing the scraper to work with multiple quarters - backend is…
CTrando Apr 3, 2019
279b401
Fixing comment and pruning quarter insert script
CTrando Apr 4, 2019
c9afcc0
Making webreg upload script create database and quarters table for fu…
CTrando Apr 5, 2019
4e9ebc5
Adding necessary config file
CTrando Apr 5, 2019
db0f27e
add quarter parameter to application
snowme34 Apr 8, 2019
922b633
bug fix, add quarter variable, note frontend still not sending the ne…
snowme34 Apr 8, 2019
4cc3bb1
#86 trying to add default quarter support for backend
snowme34 Apr 9, 2019
490ca06
#86 fix import path
snowme34 Apr 9, 2019
e3f76a9
Fixing chrome version
CTrando Apr 15, 2019
47faaba
Merge branch 'scratch/issue86' of github.com:ucsdscheduleplanner/UCSD…
CTrando Apr 15, 2019
682a549
#86 trying to prune Dockerfile
snowme34 Apr 19, 2019
4452996
#86 working on pruning requirements.txt
snowme34 Apr 19, 2019
3c6a080
#86 add note for possible caching bug
snowme34 Apr 19, 2019
9ad923d
#86 disable WI19, no one cares
snowme34 Apr 19, 2019
498ce1e
#86 add mysql non-root user, based on config.ini
snowme34 Apr 19, 2019
29e49a2
Adding golang routes and adding back department functionality and cou…
CTrando Apr 26, 2019
6862d9a
Merge branch 'scratch/issue86' of github.com:ucsdscheduleplanner/UCSD…
CTrando Apr 26, 2019
2872b6a
Adding other github repos, they will not be submodules so will not ac…
CTrando Apr 26, 2019
03b4504
Finished moving backend to Golang
CTrando May 3, 2019
72ca1a6
Adding more tests and working out compatability issues with frontend
CTrando May 3, 2019
db27989
Creating module for Go backend and restructure the code
snowme34 Jun 2, 2019
b771ba5
Add 2 functions in RoutesCommon to remove repeats; Add systematical w…
snowme34 Jun 3, 2019
1531f72
Add development config
snowme34 Jun 3, 2019
0eb55b0
Update RoutesCommon functions and error processing
snowme34 Jun 5, 2019
a7bde26
Cleanup Routes Code and update comments
snowme34 Jun 5, 2019
d8a97fa
Make a straightforward constructor for db struct and update the exist…
snowme34 Jun 6, 2019
fafe144
Add miltiple constructors for db since lower-case struct fields are n…
snowme34 Jun 6, 2019
513216b
Test using mock db
snowme34 Jun 6, 2019
98c989a
refactor, fix error handling
snowme34 Aug 3, 2019
49ddc45
refacotr more
snowme34 Aug 4, 2019
f6af1f6
refactor, restructure, add a ctx package with env
snowme34 Aug 5, 2019
9eb7de2
change embarrassing package name
snowme34 Aug 6, 2019
06feb39
refactor, add handler factoary
snowme34 Aug 11, 2019
3dd064a
remove python code
snowme34 Aug 11, 2019
ddf6944
add Docker support for go backend
snowme34 Aug 19, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions backend-py/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM python:3

ENV DEBIAN_FRONTEND noninteractive

RUN apt-get update && apt-get -y install locales && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && locale-gen

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

ENV PYTHONPATH /app

EXPOSE 5000

CMD ["bash", "./docker-run.sh"]
File renamed without changes.
15 changes: 13 additions & 2 deletions backend/application.py → backend-py/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from flask_caching import Cache
from backend import generate_class_json, get_all_classes_in, get_departments

import configparser, os

config = configparser.ConfigParser()
config.read(os.path.join(os.path.abspath(os.path.dirname(__file__)), "config", "config.example.ini"))
DEFAULT_QUARTER = config["DB"]["DEFAULT_QUARTER"]

application = Flask(__name__)
CORS(application)
Compress(application)
Expand All @@ -25,10 +31,12 @@ def return_db_data():
classes = request_json['classes']
ret_classes = {}

quarter = request_json['quarter'] if 'quarter' in request_json else DEFAULT_QUARTER

for Class in classes:
department, course_num = Class['department'], Class['courseNum']
full_name = "{} {}".format(department, course_num)
ret_classes[full_name] = generate_class_json(department, course_num)
ret_classes[full_name] = generate_class_json(department, course_num, quarter)

return jsonify(ret_classes)

Expand All @@ -44,7 +52,10 @@ def return_department_list():
@cache.cached(timeout=3600, key_prefix="class_summaries", query_string=True)
def return_classes():
department = request.args.get('department')
classes = get_all_classes_in(department)

# quarter = request.args.get('quarter') if 'quarter' in request.args else DEFAULT_QUARTER
quarter = request.args.get('quarter', DEFAULT_QUARTER, type=str)
classes = get_all_classes_in(department, quarter)
return jsonify(classes)


Expand Down
13 changes: 7 additions & 6 deletions backend/backend.py → backend-py/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@
pool_recycle=3600)

# Caching departments and class types
# TODO: might have to cache different data for different quarters
departments = []
class_types = []


def get_all_classes_in(department):
def get_all_classes_in(department, quarter):
# Must order this one separately because doing it lexically won't work
ret_dict = {}
sql = text(
"SELECT DISTINCT COURSE_NUM, DEPARTMENT, "
"INSTRUCTOR, TYPE, DESCRIPTION FROM CLASS_DATA WHERE DEPARTMENT = :department")
result = cursor.execute(sql, department=department).fetchall()
"INSTRUCTOR, TYPE, DESCRIPTION FROM :quarter WHERE DEPARTMENT = :department")
result = cursor.execute(sql, quarter=quarter,department=department).fetchall()
# use dict here for fast lookup
ret_dict["CLASS_SUMMARY"] = {}
for row in result:
Expand All @@ -52,16 +53,16 @@ def get_departments():
return departments


def generate_class_json(department, course_num):
def generate_class_json(department, course_num, quarter):
"""
Generates a set of classes of the same version and ID.
For example returns all the CSE 20 classes given that ID.
:param course_num: the course number
:param department: the department
:return: returns all the classes with the same ID in a list
"""
sql = text("SELECT * FROM CLASS_DATA WHERE DEPARTMENT = :department AND COURSE_NUM = :course_num")
result = cursor.execute(sql, department=department, course_num=course_num).fetchall()
sql = text("SELECT * FROM :quarter WHERE DEPARTMENT = :department AND COURSE_NUM = :course_num")
result = cursor.execute(sql, quarter=quarter, department=department, course_num=course_num).fetchall()
class_versions = [dict(row) for row in result]
# The different sections of the given class
return class_versions
12 changes: 12 additions & 0 deletions backend-py/config/config.example.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[DB]
USERNAME=dev
snowme34 marked this conversation as resolved.
Show resolved Hide resolved
PASSWORD=password
DB_NAME=classes
ENDPOINT=
DEFAULT_QUARTER=SP19


[VARS]
QUARTERS=["SP19"] ; Example: QUARTERS=["SP19", "WI19"]

#sdschedule-database
5 changes: 0 additions & 5 deletions backend/docker-run.sh → backend-py/docker-run.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/bin/bash

# download data or not
if [[ $SDSCHEDULE_SCRAPE -eq 1 ]]; then
python3 -u datautil/webreg_scrape_upload.py
fi

# uwsgi or flask
if [[ $ENV == "PROD" ]]; then
useradd app
Expand Down
7 changes: 7 additions & 0 deletions backend-py/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
uwsgi>=2.0.17.1
SQLAlchemy>=1.2.15
mysqlclient>=1.3.14
Flask>=1.0.2
Flask-Compress>=1.4.0
Flask-Cors>=3.0.7
Flask-Caching>=1.4.0
24 changes: 0 additions & 24 deletions backend/Dockerfile

This file was deleted.

66 changes: 66 additions & 0 deletions backend/backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"log"
"net/http"
"path/filepath"
"strconv"

"gopkg.in/ini.v1"

"github.com/ucsdscheduleplanner/UCSD-Schedule-Planner/backend/environ"
"github.com/ucsdscheduleplanner/UCSD-Schedule-Planner/backend/route"
"github.com/ucsdscheduleplanner/UCSD-Schedule-Planner/backend/store"
)

func readConfig(configFile string) (config *ini.File, err error) {
config, err = ini.Load(configFile)

if err != nil {
return nil, fmt.Errorf("Error reading config file: %v", err)
}

if config == nil {
return nil, fmt.Errorf("Could not find config file: %v", configFile)
}

return
}

func main() {

// config, err := readConfig(filepath.Join(".", "config", "config.example.ini"))
snowme34 marked this conversation as resolved.
Show resolved Hide resolved
config, err := readConfig(filepath.Join(".", "config", "config.dev.ini"))

if err != nil {
panic("Failed to load config: " + err.Error())
}

db, err := store.NewDBConfig(config)
if err != nil {
panic("Failed to init db: " + err.Error())
}
defer db.Close()

env, err := environ.NewEnvConfig(db, config)
if err != nil {
panic("Failed to init env: " + err.Error())
}

log.Printf("Start server on port: %v\n", env.Port)

// TODO: use env in handlers
// TODO: make a handler factory
http.HandleFunc("/api_course_nums", route.MakeHandler(route.GetCourseNums, db, route.LogPrefixCourseNums))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this LogPrefix?

http.HandleFunc("/api_departments", route.MakeHandler(route.GetDepartments, db, route.LogPrefixDepartment))
http.HandleFunc("/api_instructors", route.MakeHandler(route.GetInstructors, db, route.LogPrefixInstructors))
http.HandleFunc("/api_types", route.MakeHandler(route.GetTypes, db, route.LogPrefixTypes))
http.HandleFunc("/api_class_data", route.MakeHandler(route.GetClassData, db, route.LogPrefixClassData))

err = http.ListenAndServe(":"+strconv.Itoa(env.Port), nil)

if err != nil {
log.Panic(err) // non-fatal to run deferred calls
}
}
14 changes: 14 additions & 0 deletions backend/config/config.dev.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[BACKEND]
PORT=8080

[DB]
USERNAME=dev
PASSWORD=password
DB_NAME=classes
ENDPOINT=localhost:22180
DEFAULT_QUARTER=SP19

[VARS]
QUARTERS=["SP19"] ; Example: QUARTERS=["SP19", "WI19"]

#sdschedule-database
14 changes: 12 additions & 2 deletions backend/config/config.example.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[BACKEND]
PORT=8080

[DB]
USERNAME=root
USERNAME=splanner
PASSWORD=password
ENDPOINT=sdschedule-database
DB_NAME=classes
ENDPOINT=
DEFAULT_QUARTER=SP19

[VARS]
QUARTERS=["SP19"] ; Example: QUARTERS=["SP19", "WI19"]

#sdschedule-database
Empty file removed backend/datautil/__init__.py
Empty file.
35 changes: 0 additions & 35 deletions backend/datautil/sql_to_postgres.py

This file was deleted.

Loading