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

Commit 69d2117

Browse files
committed
More management scripts
1 parent 9d6191e commit 69d2117

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

scripts/active_users.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import datetime
2+
import os
3+
4+
from sqlalchemy import (
5+
Column,
6+
Table,
7+
ForeignKey,
8+
Index,
9+
Integer,
10+
String,
11+
Date,
12+
DateTime,
13+
Text,
14+
create_engine,
15+
types,
16+
Float,
17+
inspect,
18+
MetaData,
19+
)
20+
from sqlalchemy.orm import declarative_base, sessionmaker
21+
22+
from sqlalchemy.ext.declarative import declared_attr
23+
from sqlalchemy.sql.schema import UniqueConstraint
24+
from sqlalchemy.orm import declarative_base
25+
26+
# Local application imports
27+
# -------------------------
28+
# This creates the base class we will use to create models
29+
30+
Base = declarative_base()
31+
32+
# authors will have a role defined for their user_id in auth_group and auth_membership
33+
engine = create_engine(os.environ["DBURL"])
34+
Session = sessionmaker(expire_on_commit=False)
35+
engine.connect()
36+
Session.configure(bind=engine)
37+
meta = MetaData()
38+
39+
40+
class UserActivity(Base):
41+
__tablename__ = "user_activity"
42+
timestamp = Column(DateTime, primary_key=True)
43+
num_users = Column(Integer)
44+
45+
46+
Base.metadata.create_all(bind=engine)
47+
48+
49+
def create_activity_entry(num_active: int) -> None:
50+
new_row = UserActivity(num_users=num_active, timestamp=datetime.datetime.utcnow())
51+
with Session.begin() as session:
52+
session.add(new_row)
53+
54+
55+
last_five = engine.execute(
56+
"""
57+
select count(distinct sid) from useinfo where timestamp > now() - interval '5 minutes'
58+
"""
59+
).first()
60+
61+
num_active = last_five[0]
62+
63+
create_activity_entry(num_active)

scripts/database_pruning.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# archive data from runestone database that is more than 2 years old
2+
import os
3+
from sqlalchemy import create_engine
4+
5+
dburl = os.environ["DBURL"]
6+
eng = create_engine(dburl)
7+
8+
for tbl in [
9+
"clickablearea_answers",
10+
"codelens_answers",
11+
"dragndrop_answers",
12+
"fitb_answers",
13+
"mchoice_answers",
14+
"parsons_answers",
15+
"shortanswer_answers",
16+
"unittest_answers",
17+
"code",
18+
"unittest",
19+
][:1]:
20+
res = eng.execute(
21+
f"""select id from {tbl} where timestamp = now()::date - interval '2 years' limit 1""",
22+
eng,
23+
).first()
24+
25+
max_id = res[0]
26+
print(f"copying rows from {tbl} before {max_id}")
27+
# this has to be run on the database server machine or it will fail. Other options including
28+
# using pandas may be better if we want to run remotely
29+
num = eng.execute(
30+
f"""copy (select * from {tbl} where id < {max_id}) to '{os.environ["HOME"]}/archive_{tbl}_{max_id}.csv' csv header""",
31+
eng,
32+
).first()
33+
if num and num[0] < 1:
34+
print("No records copied removing file")
35+
# remove the file
36+
37+
print(f"Copied {num}")
38+
39+
res = eng.execute(f"""delete from {tbl} where id < {max_id}""")
40+
print(f"deleted {res}")
41+
42+
43+
# To load the csv into the main table ignoring duplicates follow this pattern
44+
# CREATE TEMP TABLE tmp_table
45+
# ON COMMIT DROP -- omit if doing from psql
46+
# AS
47+
# SELECT *
48+
# FROM main_table
49+
# WITH NO DATA;
50+
#
51+
# COPY tmp_table FROM 'full/file/name/here';
52+
#
53+
# INSERT INTO main_table
54+
# SELECT DISTINCT ON (PK_field) *
55+
# FROM tmp_table
56+
# ORDER BY (some_fields)
57+
58+
# There may also be an option for an ON CONFLiCT DO NOTHING

scripts/populate_lib.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def update_library(book):
110110
return True
111111

112112

113+
print("Warning -- this script will leave __pycache__ in every book folder")
113114
os.chdir(cwd)
114115
for path in cwd.iterdir():
115116
if path.is_dir():

0 commit comments

Comments
 (0)