Skip to content

Commit 8310c44

Browse files
authored
Merge pull request #105 from JoelEager/log-all-the-things
And there was logging
2 parents a4d4bbd + 89740a5 commit 8310c44

File tree

10 files changed

+84
-51
lines changed

10 files changed

+84
-51
lines changed

config.env.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT)
88
SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME")
99

10+
LOG_LEVEL = environ.get("PACKET_LOG_LEVEL", "INFO")
11+
1012
# OpenID Connect SSO config
1113
REALM = environ.get("PACKET_REALM", "csh")
1214

packet/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import os
6+
import logging
67

78
import csh_ldap
89
from flask import Flask
@@ -23,6 +24,9 @@
2324

2425
app.config["VERSION"] = __version__
2526

27+
# Logger configuration
28+
logging.getLogger().setLevel(app.config["LOG_LEVEL"])
29+
2630
# Initialize the extensions
2731
db = SQLAlchemy(app)
2832
migrate = Migrate(app, db)
@@ -36,6 +40,8 @@
3640
# LDAP
3741
_ldap = csh_ldap.CSHLDAP(app.config["LDAP_BIND_DN"], app.config["LDAP_BIND_PASS"])
3842

43+
app.logger.info("DB and LDAP configured")
44+
3945
# pylint: disable=wrong-import-position
4046
from . import models
4147
from . import context_processors
@@ -46,3 +52,5 @@
4652
from .routes import upperclassmen
4753
else:
4854
from .routes import freshmen
55+
56+
app.logger.info("Routes registered")

packet/debug_utils.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

packet/log_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
General utilities for logging metadata
3+
"""
4+
5+
from functools import wraps
6+
from datetime import datetime
7+
8+
from packet import context_processors, app
9+
10+
11+
def log_time(func):
12+
"""
13+
Decorator for logging the execution time of a function
14+
"""
15+
@wraps(func)
16+
def wrapped_function(*args, **kwargs):
17+
start = datetime.now()
18+
19+
result = func(*args, **kwargs)
20+
21+
seconds = (datetime.now() - start).total_seconds()
22+
app.logger.info("{}.{}() returned after {} seconds".format(func.__module__, func.__name__, seconds))
23+
24+
return result
25+
26+
return wrapped_function
27+
28+
29+
def log_cache(func):
30+
"""
31+
Decorator for logging cache info
32+
"""
33+
@wraps(func)
34+
def wrapped_function(*args, **kwargs):
35+
result = func(*args, **kwargs)
36+
37+
app.logger.info("get_csh_name(): {}, get_rit_name(): {}".format(context_processors.get_csh_name.cache_info(),
38+
context_processors.get_rit_name.cache_info()))
39+
40+
return result
41+
42+
return wrapped_function

packet/models.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,12 @@ def did_sign(self, username, is_csh):
103103
# The user must be a misc CSHer that hasn't signed this packet or an off-floor freshmen
104104
return False
105105

106-
107106
def is_100(self):
108107
"""
109108
Checks if this packet has reached 100%
110109
"""
111110
return self.signatures_required().total == self.signatures_recieved().total
112111

113-
114112
@classmethod
115113
def open_packets(cls):
116114
"""

packet/routes/api.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,26 @@ def sign(packet_id, info):
1515
# Check if the CSHer is an upperclassman and if so, sign that row
1616
for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures):
1717
sig.signed = True
18-
db.session.commit()
19-
if not was_100 and packet.is_100():
20-
notify_slack(packet.freshman.name)
21-
return "Success: Signed Packet: " + packet.freshman_username
18+
app.logger.info("Member {} signed packet {} as an upperclassman".format(info["uid"], packet_id))
19+
return commit_sig(packet, was_100)
2220

2321
# The CSHer is a misc so add a new row
2422
db.session.add(MiscSignature(packet=packet, member=info["uid"]))
25-
db.session.commit()
26-
if not was_100 and packet.is_100():
27-
notify_slack(packet.freshman.name)
28-
return "Success: Signed Packet: " + packet.freshman_username
23+
app.logger.info("Member {} signed packet {} as a misc".format(info["uid"], packet_id))
24+
return commit_sig(packet, was_100)
2925
else:
3026
# Check if the freshman is onfloor and if so, sign that row
3127
for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures):
3228
sig.signed = True
33-
db.session.commit()
34-
if not was_100 and packet.is_100():
35-
notify_slack(packet.freshman.name)
36-
return "Success: Signed Packet: " + packet.freshman_username
29+
app.logger.info("Freshman {} signed packet {}".format(info["uid"], packet_id))
30+
return commit_sig(packet, was_100)
3731

32+
app.logger.warn("Failed to add {}'s signature to packet {}".format(info["uid"], packet_id))
3833
return "Error: Signature not valid. Reason: Unknown"
34+
35+
def commit_sig(packet, was_100):
36+
db.session.commit()
37+
if not was_100 and packet.is_100():
38+
notify_slack(packet.freshman.name)
39+
40+
return "Success: Signed Packet: " + packet.freshman_username

packet/routes/freshmen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def submit_essays(packet_id, info=None):
4545
packet.info_achieve = request.form.get("info_achieve", None)
4646

4747
db.session.commit()
48+
app.logger.info("Updated essays for {}".format(info["uid"]))
4849
return redirect(url_for("essays", packet_id=packet_id), 302)
4950
else:
5051
return redirect(url_for("index"), 302)

packet/routes/shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from packet import auth, app
88
from packet.utils import before_request, packet_auth
99
from packet.models import Packet
10+
from packet.log_utils import log_cache, log_time
1011

1112

1213
@app.route("/logout/")
@@ -16,8 +17,10 @@ def logout():
1617

1718

1819
@app.route("/packet/<packet_id>/")
20+
@log_cache
1921
@packet_auth
2022
@before_request
23+
@log_time
2124
def freshman_packet(packet_id, info=None):
2225
packet = Packet.by_id(packet_id)
2326

@@ -43,8 +46,10 @@ def freshman_packet(packet_id, info=None):
4346

4447

4548
@app.route("/packets/")
49+
@log_cache
4650
@packet_auth
4751
@before_request
52+
@log_time
4853
def packets(info=None):
4954
open_packets = Packet.open_packets()
5055

packet/routes/upperclassmen.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from packet import app
1010
from packet.models import Packet, MiscSignature
1111
from packet.utils import before_request, packet_auth
12+
from packet.log_utils import log_cache, log_time
1213

1314

1415
@app.route("/")
@@ -18,8 +19,10 @@ def index():
1819

1920

2021
@app.route("/member/<uid>/")
22+
@log_cache
2123
@packet_auth
2224
@before_request
25+
@log_time
2326
def upperclassman(uid, info=None):
2427
open_packets = Packet.open_packets()
2528

@@ -37,8 +40,10 @@ def upperclassman(uid, info=None):
3740

3841

3942
@app.route("/upperclassmen/")
43+
@log_cache
4044
@packet_auth
4145
@before_request
46+
@log_time
4247
def upperclassmen_total(info=None):
4348
open_packets = Packet.open_packets()
4449

packet/utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def packet_auth(func):
7979
@wraps(func)
8080
def wrapped_function(*args, **kwargs):
8181
if app.config["REALM"] == "csh":
82-
if ldap_is_intromember(ldap_get_member(str(session["userinfo"].get("preferred_username", "")))):
82+
username = str(session["userinfo"].get("preferred_username", ""))
83+
if ldap_is_intromember(ldap_get_member(username)):
84+
app.logger.warn("Stopped intro member {} from accessing upperclassmen packet".format(username))
8385
return "Sorry, upperclassmen packet is not available to intro members.", 401
8486

8587
return func(*args, **kwargs)
@@ -88,10 +90,12 @@ def wrapped_function(*args, **kwargs):
8890

8991
def notify_slack(name: str):
9092
"""
91-
Sends a congratulate on sight decree to Slack.
93+
Sends a congratulate on sight decree to Slack
9294
"""
9395
if app.config["SLACK_WEBHOOK_URL"] is None:
94-
print("SLACK_WEBHOOK_URL not configured, not sending message to slack.")
96+
app.logger.warn("SLACK_WEBHOOK_URL not configured, not sending message to slack.")
9597
return
98+
9699
msg = f'{name} got :100: on packet. Shower on sight.'
97100
requests.put(app.config["SLACK_WEBHOOK_URL"], json={'text':msg})
101+
app.logger.info("Posted 100% notification to slack for " + name)

0 commit comments

Comments
 (0)