Skip to content

Commit

Permalink
Update report script and add user populate in Activity
Browse files Browse the repository at this point in the history
  • Loading branch information
mexeniz committed Oct 20, 2019
1 parent adbe821 commit 5a33237
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 46 deletions.
21 changes: 20 additions & 1 deletion db.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class Activity(Base):
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.utc_timestamp())
deleted_at = Column(DateTime)
user_id = Column(BigInteger, ForeignKey("users.id"))
strava_id = Column(String, unique=True)
name = Column(String)
start_date = Column(DateTime)
Expand All @@ -103,6 +102,9 @@ class Activity(Base):
manual = Column(Boolean)
promo_comment = Column(String)
promo_multiplier = Column(Float)

user_id = Column(BigInteger, ForeignKey("users.id"))
user = relationship(User)

# SQL Database Connector
# ICMM Intania Challenge & F5 Challenge 2020
Expand Down Expand Up @@ -178,6 +180,12 @@ def get_run_by_strava_id(cls, strava_id):
row = sess.query(Activity).filter(Activity.strava_id == strava_id).one()
return row

@classmethod
def get_all_runs(cls):
sess = cls.SESSION()
rows = sess.query(Activity).all()
return rows

@staticmethod
def __run_to_activity(run):
return Activity(
Expand All @@ -203,6 +211,17 @@ def insert_run(cls, run):
sess.commit()


###########
# Summary
###########
@classmethod
def get_summary_user(cls):
pass

@classmethod
def get_summary_intania_distance(cls, intania_range):
pass

# MongoDB Connector
class ChallengeDB():
MONGO_CLIENT = None
Expand Down
2 changes: 1 addition & 1 deletion model2.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, id, reg_info_id, strava_id, first_name, last_name, strava_co
self.created_at = created_at

# From joining table
self.intania = foundation
self.intania = intania
self.foundation = foundation

def to_row(self):
Expand Down
157 changes: 115 additions & 42 deletions report.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#!/usr/bin/env python
import os
import argparse
import csv
import pytz
import datetime
import argparse
from stravalib.client import Client
from stravalib.model import Activity
from db import ChallengeDB
from model import Run, Runner
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import os
import pytz

import gspread
from oauth2client.service_account import ServiceAccountCredentials
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from stravalib.client import Client
from stravalib.model import Activity

from db import ChallengeSqlDB
from model2 import Run, User

DEFAULT_SCOPE = ["https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/drive"]
"https://www.googleapis.com/auth/drive"]


class ChallengeSpread():

def __init__(self, credentials_path):
self.credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_path, DEFAULT_SCOPE)
self.credentials = ServiceAccountCredentials.from_json_keyfile_name(
credentials_path, DEFAULT_SCOPE)
self.gc = gspread.authorize(self.credentials)

def __get_sheet(self, spread_key, sheet_name):
Expand All @@ -39,7 +41,7 @@ def update_summary_run(self, spread_key, sheet_name, run_data):
j = idx % 2
element = run_data[i][j]
cell.value = element

# Update in batch
worksheet.update_cells(cell_list, "USER_ENTERED")

Expand All @@ -54,86 +56,153 @@ def update_runner(self, spread_key, sheet_name, runner_data):
j = idx % 3
element = runner_data[i][j]
cell.value = element

# Update in batch
worksheet.update_cells(cell_list, "USER_ENTERED")


# Reuired environment
MONGODB_URI = os.environ["MONGODB_URI"]
DATABASE_NAME = os.environ["DATABASE_NAME"]
TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S"

DEFAULT_OUTPUT_DIR = "./"

STRING_TIME_FORMAT = "%Y-%b-%d %H:%M:%S"
MYSQL_HOST = os.environ["MYSQL_HOST"]
MYSQL_USERNAME = os.environ["MYSQL_USERNAME"]
MYSQL_PASSWORD = os.environ["MYSQL_PASSWORD"]
MYSQL_DB_NAME = os.environ["MYSQL_DB_NAME"]

ChallengeSqlDB.init(MYSQL_HOST, MYSQL_USERNAME,
MYSQL_PASSWORD, MYSQL_DB_NAME)

ChallengeDB.init(MONGODB_URI, DATABASE_NAME)

def update_runner_spread(challenge_spread, spread_key, sheet_name):
runner_data = ChallengeDB.find_summary_runner()
runner_data = ChallengeSqlDB.get_summary_user()
challenge_spread.update_runner(spread_key, sheet_name, runner_data)

def update_run_spread(challenge_spread, spread_key, sheet_name, intania_range=range(50,99)):
run_data = ChallengeDB.find_summary_intania_distance(intania_range)

def update_run_spread(challenge_spread, spread_key, sheet_name, intania_range=range(50, 99)):
run_data = ChallengeSqlDB.get_summary_intania_distance(intania_range)
challenge_spread.update_summary_run(spread_key, sheet_name, run_data)


def upload_reports(drive_cleint_config, token_path, folder_id, report_paths):
g_auth = GoogleAuth()
g_auth.LoadClientConfigFile(drive_cleint_config)
g_auth.LoadCredentialsFile(token_path)
drive = GoogleDrive(g_auth)

for report_path in report_paths:
with open(report_path,"r") as file:
with open(report_path, "r") as file:
title = os.path.basename(file.name)
file_drive = drive.CreateFile({
"title": title,
"title": title,
"parents": [{"kind": "drive#fileLink", "id": folder_id}]
})
file_drive.SetContentString(file.read())
file_drive.SetContentString(file.read())
file_drive.Upload()
print("Upload file: %s" % (title))


def gen_run_report(timestamp, report_path):
runs = ChallengeDB.find_run()
runs = ChallengeSqlDB.get_all_runs()
with open(report_path, "w", newline="") as csvfile:
fieldnames = ["timestamp"] + list(runs[0].to_doc().keys())
fieldnames = ["timestamp",
"start_date",
"start_date_local",
"strava_id",
"name",
"distance",
"moving_time",
"elapsed_time",
"elev_high",
"elev_low",
"total_elevation_gain",
"manual",
"promo_comment",
"promo_multiplier",
"user_strava_id",
"user_intania",
"user_ranger",
"created_at"
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

writer.writeheader()
n_run = 0
for run in runs:
row = run.to_doc()
row = vars(run)
row["timestamp"] = timestamp
row["startDate"] = row["startDate"].strftime(STRING_TIME_FORMAT)
row["startDateLocal"] = row["startDateLocal"].strftime(STRING_TIME_FORMAT)
row["createdAt"] = row["createdAt"].strftime(STRING_TIME_FORMAT)
row["start_date"] = row["start_date"].strftime(TIME_STRING_FORMAT)
row["start_date_local"] = row["start_date_local"].strftime(
TIME_STRING_FORMAT)
row["created_at"] = row["created_at"].strftime(TIME_STRING_FORMAT)

# Customise user info
user = run.user
row["user_strava_id"] = user.strava_id
if user.clubs:
row["user_intania"] = user.clubs[0].intania
else:
row["user_intania"] = ""

if user.registration and user.registration.foundation:
row["user_ranger"] = user.registration.foundation.name
else:
row["user_ranger"] = ""

# Filter only wanted fields
row = {key: row[key] for key in fieldnames if key in row}
writer.writerow(row)
print("Total Runs:", len(runs))

n_run += 1

print("Total Runs:", n_run)
print("Generated report to", report_path)


def gen_runner_report(timestamp, report_path):
runners = ChallengeDB.find_runner()
users = ChallengeSqlDB.get_all_users()
with open(report_path, "w", newline="") as csvfile:
fieldnames = ["timestamp","_id", "displayname", "intania", "createdAt"]
fieldnames = ["timestamp", "id", "strava_id", "first_name",
"last_name", "intania", "ranger", "created_at"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

writer.writeheader()
for runner in runners:
row = runner.to_doc()
n_user = 0
for user in users:
row = vars(user)
row["timestamp"] = timestamp
row["createdAt"] = row["createdAt"].strftime(STRING_TIME_FORMAT)
row = {key:row[key] for key in fieldnames}
row["created_at"] = row["created_at"].strftime(TIME_STRING_FORMAT)

# Customise intania and ranger fields
if user.clubs:
row["intania"] = user.clubs[0].intania
else:
row["intania"] = ""

if user.registration and user.registration.foundation:
row["ranger"] = user.registration.foundation.name
else:
row["ranger"] = ""

# Filter only wanted fields
row = {key: row[key] for key in fieldnames if key in row}
writer.writerow(row)
print("Total Runners:", len(runners))

n_user += 1

print("Total Runners:", n_user)
print("Generated report to", report_path)


def main():
if args.time_zone:
tz = pytz.timezone(args.time_zone)
now = datetime.datetime.now(tz)
else:
now = datetime.datetime.now()

timestamp = now.strftime(STRING_TIME_FORMAT)
timestamp = now.strftime(TIME_STRING_FORMAT)
report_prefix = now.strftime("%Y%m%d_%H%M%S")
print("Report timestamp:", timestamp)

Expand All @@ -149,17 +218,21 @@ def main():

if args.drive_cleint_config and args.drive_token and args.drive_folder_id:
print("GDrive config is set, uploading reports to Gdrive.")
upload_reports(args.drive_cleint_config, args.drive_token, args.drive_folder_id, [runner_report_path, run_report_path])
upload_reports(args.drive_cleint_config, args.drive_token,
args.drive_folder_id, [runner_report_path, run_report_path])

if args.credentials:
print("GSpread credentials is set, uploading summary to spreadsheet.")
challenge_spread = ChallengeSpread(args.credentials)

if args.run_spread_key and args.run_sheet_name:
update_run_spread(challenge_spread, args.run_spread_key, args.run_sheet_name)
update_run_spread(challenge_spread,
args.run_spread_key, args.run_sheet_name)

if args.runner_spread_key and args.runner_sheet_name:
update_runner_spread(challenge_spread, args.runner_spread_key, args.runner_sheet_name)
update_runner_spread(
challenge_spread, args.runner_spread_key, args.runner_sheet_name)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand All @@ -185,4 +258,4 @@ def main():
parser.add_argument("--runner-sheet-name", help="Worksheet name for runner summary", action="store",
dest="runner_sheet_name", type=str)
args = parser.parse_args()
main()
main()
9 changes: 7 additions & 2 deletions update_run.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ def main():
else:
intania = 0

if user.registration and user.registration.foundation:
ranger_team = user.registration.foundation.name
else:
ranger_team = None

time.sleep(0.2)
activities = client.get_activities(
after=CHALLENGE_START_DATE, before=CHALLENGE_END_DATE)
print("Get activities: idx=%d/%d id=%s displayname='%s %s' intania:'%d'" %
(idx + 1, n_user, user.strava_id, user.first_name, user.last_name, intania))
print("Get activities: idx=%d/%d id=%s displayname='%s %s' intania='%s' ranger='%s'" %
(idx + 1, n_user, user.strava_id, user.first_name, user.last_name, intania, ranger_team))
n_run = 0
for act in activities:
if act.type != Activity.RUN:
Expand Down

0 comments on commit 5a33237

Please sign in to comment.