Skip to content

Alterations after database naming changes #132

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 12 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
version: "3.8"

services:
mysql:
image: mysql:8.0
statistics_mysql:
image: mysql:8.4
container_name: statistics_mysql
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
MYSQL_DATABASE: wca_development
Expand All @@ -11,14 +12,17 @@ services:
volumes:
- mysql-data:/var/lib/mysql

db_restore:
image: ubuntu:20.04
statistics_db_restore:
image: ubuntu:24.04
container_name: statistics_db_restore
depends_on:
- mysql
- statistics_mysql
entrypoint: >
sh -c "
apt update && apt install curl unzip mysql-client wget -y &&
result=$(mysql -h mysql -sN -u root -e 'use wca_development; select datediff(now(), max(results_posted_at)) from Competitions;' || echo 999) &&
apt-get update && \
apt-get install curl unzip mysql-client wget -y && \
mysql -h statistics_mysql -sN -u root -e 'create database if not exists wca_development;' && \
result=$(mysql -h statistics_mysql -sN -u root -e 'use wca_development; select datediff(now(), max(results_posted_at)) from competitions;' || echo 999) &&
echo 'Days since last update: ' $result &&
if [ -n \"$result\" ] && [ $result -lt 7 ]; then
echo 'Database is already up to date, skipping restoration.'
Expand All @@ -31,7 +35,7 @@ services:
rm -rf wca-developer-database-dump.sql &&
unzip wca-developer-database-dump.zip &&
echo 'Restoring WCA database. This also may take a while...' &&
mysql -h mysql -u root -e 'drop database if exists wca_development; create database wca_development; use wca_development; source wca-developer-database-dump.sql;' &&
mysql -h statistics_mysql -u root -e 'drop database if exists wca_development; create database wca_development; use wca_development; source wca-developer-database-dump.sql;' &&
echo 'Restoration complete!'
"
volumes:
Expand Down
2 changes: 1 addition & 1 deletion iac/rds.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "aws_db_instance" "dumped_db" {
engine = "mysql"
engine_version = "8.0.33"
storage_type = "gp2"
instance_class = "db.t3.medium"
instance_class = "db.t4g.large"
db_name = "dumped_db"
username = data.aws_ssm_parameter.dumped_db_write_user.value
password = data.aws_ssm_parameter.dumped_db_write_password.value
Expand Down
8 changes: 8 additions & 0 deletions iac/subnet.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ resource "aws_default_subnet" "default_az1" {
availability_zone = "${var.aws_region}a"

provider = aws.no_tags

lifecycle {
ignore_changes = [tags, tags_all]
}
}

resource "aws_default_subnet" "default_az2" {
availability_zone = "${var.aws_region}b"

provider = aws.no_tags

lifecycle {
ignore_changes = [tags, tags_all]
}
}
4 changes: 4 additions & 0 deletions iac/vpc.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
resource "aws_default_vpc" "default" {
provider = aws.no_tags

lifecycle {
ignore_changes = [tags, tags_all]
}
}
19 changes: 16 additions & 3 deletions misc/python/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
## Instalation

You need to be in the root folder

python3 -m virtualenv venv
source venv/bin/activate
pip3 install -r misc/python/requirements.txt
```bash
python3 -m virtualenv venv
source venv/bin/activate
pip3 install -r misc/python/requirements.txt
```

## Execution

Virtual environment should be active (check the Installation step)

```bash
python -m misc.python.statistics.5bld_before_4bld
```

or any other filename. You can also open the file and check how to execute it in the first line.
32 changes: 16 additions & 16 deletions misc/python/statistics/5bld_before_4bld.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@
title = "Competitors who got 5BLD before 4BLD"

query = """select
personId,
personName,
r.countryId,
eventId,
person_id,
person_name,
r.country_id,
event_id,
best,
c.id,
c.name,
c.start_date
from
Results r
inner join Competitions c on
r.competitionId = c.id
results r
inner join competitions c on
r.competition_id = c.id
where
eventId in ('%s', '%s')
event_id in ('%s', '%s')
and best > 0
order by
c.start_date"""
Expand All @@ -51,22 +51,22 @@
select
min(start_date)
from
Results r
inner join Competitions c on r.competitionId = c.id
results r
inner join competitions c on r.competition_id = c.id
where
r.personId = p.id
and eventId = ':SECOND_EVENT_ID'
r.person_id = p.id
and event_id = ':SECOND_EVENT_ID'
and best > 0
),
(
select
min(start_date)
from
Results r
inner join Competitions c on r.competitionId = c.id
results r
inner join competitions c on r.competition_id = c.id
where
r.personId = p.id
and eventId = ':FIRST_EVENT_ID'
r.person_id = p.id
and event_id = ':FIRST_EVENT_ID'
and best > 0
)
) days
Expand Down
10 changes: 5 additions & 5 deletions misc/python/statistics/avg_competition_by_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
select
count(*) / 5
from
Competitions c2
competitions c2
where
countryId = c.id
country_id = c.id
and year(c2.start_date) >= %(min_year)s
and year(c2.start_date) <= %(max_year)s
and results_posted_at is not null
) average,%(sub_queries)s
from
Countries c
countries c
) result
where
average > 0
Expand All @@ -55,9 +55,9 @@
select
count(*)
from
Competitions c2
competitions c2
where
c2.countryId = c.id
c2.country_id = c.id
and results_posted_at is not null
and year(c2.start_date) = year(current_date())-%(diff)s) m%(diff)s"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
ct.name,
count(*)/ count(distinct competition_id) average
from
Competitions c
competitions c
inner join competition_events e on
c.id = e.competition_id
inner join Countries ct on
c.countryId = ct.id
inner join countries ct on
c.country_id = ct.id
where
year(c.start_date) <= %(max_year)s
and year(c.start_date) >= %(min_year)s
and results_posted_at is not null
group by
countryId) result
country_id) result
order by
average desc,
name
Expand Down
32 changes: 16 additions & 16 deletions misc/python/statistics/best_podiums.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,36 @@

query = """
select comp_id, c.name,
case when eventId = '333mbf' then (99*3 - substring(first_result, 1, 2) - substring(second_result, 1, 2) - substring(third_result, 1, 2)) else first_result + second_result + third_result end avg_sum,
case when event_id = '333mbf' then (99*3 - substring(first_result, 1, 2) - substring(second_result, 1, 2) - substring(third_result, 1, 2)) else first_result + second_result + third_result end avg_sum,
first_id, first_name,
first_result,
second_id, second_name,
second_result,
third_id, third_name,
third_result
from (
select competitionId comp_id,
max(case when row_num=1 then personId end) first_id,
max(case when row_num=1 then personName end) first_name,
select competition_id comp_id,
max(case when row_num=1 then person_id end) first_id,
max(case when row_num=1 then person_name end) first_name,
max(case when row_num=1 then best_results end) first_result,
max(case when row_num=2 then personId end) second_id,
max(case when row_num=2 then personName end) second_name,
max(case when row_num=2 then person_id end) second_id,
max(case when row_num=2 then person_name end) second_name,
max(case when row_num=2 then best_results end) second_result,
max(case when row_num=3 then personId end) third_id,
max(case when row_num=3 then personName end) third_name,
max(case when row_num=3 then person_id end) third_id,
max(case when row_num=3 then person_name end) third_name,
max(case when row_num=3 then best_results end) third_result,
eventId
event_id
from (
select competitionId, personId, personName, case when eventId in ('333bf', '444bf', '555bf', '333mbf') then best else average end best_results, eventId,
row_number() over (partition by competitionId order by case when eventId in ('333bf', '444bf', '555bf', '333mbf') then best else average end) row_num
from Results
where eventId = '%s' and roundTypeId in ('c', 'f') and (case when eventId in ('333bf', '444bf', '555bf', '333mbf') then best else average end) > 0
select competition_id, person_id, person_name, case when event_id in ('333bf', '444bf', '555bf', '333mbf') then best else average end best_results, event_id,
row_number() over (partition by competition_id order by case when event_id in ('333bf', '444bf', '555bf', '333mbf') then best else average end) row_num
from results
where event_id = '%s' and round_type_id in ('c', 'f') and (case when event_id in ('333bf', '444bf', '555bf', '333mbf') then best else average end) > 0
) final_podiums_without_ties
group by competitionId
group by competition_id
) podiums_pivotted
inner join Competitions as c on podiums_pivotted.comp_id = c.id
inner join competitions as c on podiums_pivotted.comp_id = c.id
where third_result is not null
order by case when eventId = '333mbf' then avg_sum end desc, case when eventId != '333mbf' then convert(avg_sum, float) end
order by case when event_id = '333mbf' then avg_sum end desc, case when event_id != '333mbf' then convert(avg_sum, float) end
limit 10
"""

Expand Down
19 changes: 9 additions & 10 deletions misc/python/statistics/days_5bld_become_faster_than_4bld.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ def __init__(self, wca_id, country_id, name):


query = """select
personId,
r.countryId,
personName,
person_id,
r.country_id,
person_name,
best,
start_date,
competitionId,
competition_id,
c.name,
eventId
event_id
from
Results r
inner join Competitions c on r.competitionId = c.id
inner join RoundTypes rt on r.roundTypeId = rt.id
results r
inner join competitions c on r.competition_id = c.id
where
eventId in ('%s', '%s')
event_id in ('%s', '%s')
and best > 0
order by
c.start_date,
rt.`rank`"""
round_type_id"""


def compare_results(ev1, ev2):
Expand Down
22 changes: 10 additions & 12 deletions misc/python/statistics/longest_success_streak.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,25 @@ def __init__(self, wca_id, name, country):


query = """select
personId,
personName,
r.countryId,
competitionId,
person_id,
person_name,
r.country_id,
competition_id,
value1,
value2,
value3,
value4,
value5
from
Results r
inner join Competitions c on
r.competitionId = c.id
inner join RoundTypes rt on
r.roundTypeId = rt.id
results r
inner join competitions c on
r.competition_id = c.id
where
eventId = %(event_id)s
event_id = %(event_id)s
order by
start_date,
competitionId,
rt.`rank`
competition_id,
round_type_id
"""


Expand Down
10 changes: 5 additions & 5 deletions misc/python/statistics/ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ def __init__(self, wca_id, name, country) -> None:
title = "Ranges"

query = """select
personId,
personName,
countryId,
person_id,
person_name,
country_id,
value1,
value2,
value3,
value4,
value5
from
Results
results
where
eventId = '%s'
event_id = '%s'
"""


Expand Down
Loading
Loading