browsertrix/backend/btrixcloud/migrations/migration_0023_available_extra_exec_mins.py
Tessa Walsh be41c48c27
Add extra and gifted execution minutes (#1361)
Fixes #1358 

- Adds `extraExecMinutes` and `giftedExecMinutes` org quotas, which are
not reset monthly but are updateable amounts that carry across months
- Adds `quotaUpdate` field to `Organization` to track when quotas were
updated with timestamp
- Adds `extraExecMinutesAvailable` and `giftedExecMinutesAvailable`
fields to `Organization` to help with tracking available time left
(includes tested migration to initialize these to 0)
- Modifies org backend to track time across multiple categories, using
monthlyExecSeconds, then giftedExecSeconds, then extraExecSeconds.
All time is also written into crawlExecSeconds, which is now the monthly
total and also contains any overage time above the quotas
- Updates Dashboard crawling meter to include all types of execution
time if `extraExecMinutes` and/or `giftedExecMinutes` are set above 0
- Updates Dashboard Usage History table to include all types of
execution time (only displaying columns that have data)
- Adds backend nightly test to check handling of quotas and execution
time
- Includes migration to add new fields and copy crawlExecSeconds to
monthlyExecSeconds for previous months

Co-authored-by: emma <hi@emma.cafe>
2023-12-07 14:34:37 -05:00

64 lines
2.0 KiB
Python

"""
Migration 0023 -- Available extra/gifted minutes
"""
from btrixcloud.migrations import BaseMigration
MIGRATION_VERSION = "0023"
class Migration(BaseMigration):
"""Migration class."""
def __init__(self, mdb, migration_version=MIGRATION_VERSION):
super().__init__(mdb, migration_version)
async def migrate_up(self):
"""Perform migration up.
Add extraExecSecondsAvailable and giftedExecSecondsAvailable to org.
Initialize at 0 to avoid them being None.
Also add monthlyExecSeconds and copy previous crawlExecSeconds values
to it.
"""
# pylint: disable=duplicate-code
mdb_orgs = self.mdb["organizations"]
query = {
"extraExecSecondsAvailable": None,
"giftedExecSecondsAvailable": None,
}
async for org in mdb_orgs.find(query):
oid = org["_id"]
try:
await mdb_orgs.find_one_and_update(
{"_id": oid},
{
"$set": {
"extraExecSecondsAvailable": 0,
"giftedExecSecondsAvailable": 0,
}
},
)
# pylint: disable=broad-exception-caught
except Exception as err:
print(
f"Error adding exec seconds available fields to org {oid}: {err}",
flush=True,
)
async for org in mdb_orgs.find({"monthlyExecSeconds": None}):
oid = org["_id"]
try:
await mdb_orgs.update_one(
{"_id": oid},
[{"$set": {"monthlyExecSeconds": "$crawlExecSeconds"}}],
)
# pylint: disable=broad-exception-caught
except Exception as err:
print(
f"Error copying crawlExecSeconds to monthlyExecSeconds for org {oid}: {err}",
flush=True,
)