Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
update usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
akelch11 committed Aug 21, 2022
1 parent 5ea94f1 commit dd41ea2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 13 deletions.
70 changes: 60 additions & 10 deletions database.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, date
from psycopg2 import connect
from os import environ
from big_lists import dhall_list
Expand Down Expand Up @@ -78,20 +79,69 @@ def create_requests_table():
close_connection(cur, conn)


if __name__ == "__main__":
clear_query = "DROP TABLE %s;"
def create_metrics_table():
create_table_query = ''' CREATE table metrics
(DAY TEXT PRIMARY KEY NOT NULL, REQUESTS INT NOT NULL, MATCHES INT NOT NULL) '''

cur, conn = new_connection()
cur.execute(create_table_query)
close_connection(cur, conn)


def create_new_day_usage_metrics():
day_string = date.today().isoformat()
init_metrics_query = ''' INSERT INTO metrics (day, requests, matches)
VALUES (\'{}\', 0, 0) '''.format(day_string)

print('day string: ' + day_string)
print('init metrics query ' + init_metrics_query)

cur, conn = new_connection()
for table in [
# 'matches',
# 'requests',
# 'users',
]:
cur.execute(clear_query, [table])
cur.execute(init_metrics_query)
close_connection(cur, conn)
print('database deleted')


def update_request_usage_metric():
try:
day_string = date.today().isoformat()
increment_requests = ''' UPDATE metrics SET REQUESTS = REQUESTS + 1 WHERE day = \'{}\''''.format(
day_string)
cur, conn = new_connection()
cur.execute(increment_requests)
close_connection(cur, conn)
except Exception as ex:
print('EXCEPTION IN UPDATING MATCHES OCCURRED')
print(ex)


def update_matches_usage_metric():
try:
day_string = date.today().isoformat()
increment_requests = ''' UPDATE metrics SET MATCHES = MATCHES + 1 WHERE day = \'{}\''''.format(
day_string)
cur, conn = new_connection()
cur.execute(increment_requests)
close_connection(cur, conn)
except Exception as ex:
print('EXCEPTION IN UPDATING MATCHES OCCURRED')
print(ex)


if __name__ == "__main__":
clear_query = "DROP TABLE %s;"

# cur, conn = new_connection()
# for table in [
# # 'matches',
# # 'requests',
# # 'users',
# ]:
# cur.execute(clear_query, [table])
# close_connection(cur, conn)
# print('database deleted')

# create_matches_table()
# create_requests_table()
# create_user_table()
print('database created')
# create_metrics_table()
print('database script ran')
5 changes: 5 additions & 0 deletions matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import string
import notifications
import database
from datetime import datetime
from big_lists import dhall_list

Expand Down Expand Up @@ -313,6 +314,8 @@ def accept_match(netid, matchid, phonenum):
# If the other person has not accepted, notify the other person that match is confirmed
message = "{} also accepted the match! Have fun eating!".format(
match_name)
# match has been finalized, update matches count
database.update_matches_usage_metric()

elif row[2] == netid:
# We know that the user is the second_netid
Expand All @@ -325,6 +328,8 @@ def accept_match(netid, matchid, phonenum):
# If the other person has not accepted, notify the other person that match is confirmed
message = "{} also accepted the match! Have fun eating!".format(
match_name)
# match has been finalized, update matches count
database.update_matches_usage_metric()
notifications.send_message(message, phonenum)

query = """UPDATE matches SET {} = TRUE WHERE MATCH_ID = %s""".format(
Expand Down
7 changes: 4 additions & 3 deletions meal_requests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ast import Is
from requests import get
from database import new_connection, close_connection
from database import new_connection, close_connection, update_request_usage_metric
from matcher import match_requests
from big_lists import dhall_list
from datetime import datetime, date
Expand Down Expand Up @@ -40,6 +40,9 @@ def add_request(netid, meal_type, start_time, end_time, dhall_arr, atdhall):

match_requests()

# increment request counter
update_request_usage_metric()

return True

# Remove request from request table
Expand Down Expand Up @@ -348,5 +351,3 @@ def recur_request_to_string(recur_req_dict):
+ ', on ' + day_string

return ret_string


3 changes: 3 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import auth
import req_validation
import notifications
import database
from big_lists import majors, dhall_list

app = Flask(__name__)
Expand Down Expand Up @@ -528,6 +529,8 @@ def add_imports():
job = scheduler.add_job(
meal_requests.clean_requests, 'interval', hours=5)

initialize_usage_metrics = scheduler.add_job(database.create_new_day_usage_metrics, 'cron',
hour=12, minute=7, timezone='America/New_York')
# schedule lunch job to start at 10:00AM ET
recur_lunch_text_job = scheduler.add_job(notifications.send_recurring_request_notifications_lunch,
'cron', hour=9, minute=45, timezone='America/New_York')
Expand Down

0 comments on commit dd41ea2

Please sign in to comment.