Skip to content

Commit

Permalink
clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ppigazzini committed Apr 27, 2024
1 parent 7bac210 commit 2f9066f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion server/fishtest/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def download_run_pgns(self):
return Response("Invalid filename format", status=400)
run_id = match.group(1)
pgns_reader, total_size = self.request.rundb.get_run_pgns(run_id)
if pgns_reader is None or total_size == 0:
if pgns_reader is None:
return Response("No data found", status=404)
response = Response(content_type="application/gzip")
response.app_iter = FileIter(pgns_reader)
Expand Down
34 changes: 16 additions & 18 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,25 @@ def upload_pgn(self, run_id, pgn_zip):

def get_pgn(self, run_id):
pgn = self.pgndb.find_one({"run_id": run_id})
if pgn:
return pgn["pgn_zip"]
return None
return pgn["pgn_zip"] if pgn else None

def get_run_pgns(self, run_id):
pgns = self.pgndb.find({"run_id": {"$regex": f"^{run_id}-\\d+"}})
if pgns is not None:
# Create a generator that yields each pgn.gz file
pgn_generator = (pgn["pgn_zip"] for pgn in pgns)
# Compute the total size using MongoDB's aggregation framework
total_size_agg = self.pgndb.aggregate(
[
{"$match": {"run_id": {"$regex": f"^{run_id}-\\d+"}}},
{"$group": {"_id": None, "totalSize": {"$sum": "$size"}}},
]
)
total_size = (
total_size_agg.next()["totalSize"] if total_size_agg.alive else 0
)
return GeneratorAsFileReader(pgn_generator), total_size
return None, 0
# Compute the total size using MongoDB's aggregation framework
total_size_agg = self.pgndb.aggregate(
[
{"$match": {"run_id": {"$regex": f"^{run_id}-\\d+"}}},
{"$group": {"_id": None, "totalSize": {"$sum": "$size"}}},
]
)
total_size = total_size_agg.next()["totalSize"] if total_size_agg.alive else 0
# Create a generator that yields each pgn.gz file
pgn_generator = (
GeneratorAsFileReader(pgn["pgn_zip"] for pgn in pgns)
if total_size
else None
)
return pgn_generator, total_size

def write_nn(self, net):
validate(nn_schema, net, "net")
Expand Down

0 comments on commit 2f9066f

Please sign in to comment.