Skip to content

Commit

Permalink
fix: Current minute in the timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
antonko committed Mar 31, 2024
1 parent a590c2d commit b12fb1b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions backend/src/services/job_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ async def get_all_jobs(self, max_jobs: int = 50000) -> list[Job]:
jobs_task = await asyncio.gather(*tasks)
jobs: list[Job] = [job for job in jobs_task if job is not None]

twenty_four_hours_ago = datetime.now(UTC) - timedelta(hours=1)
one_hour_ago = datetime.now(UTC) - timedelta(hours=1)
return [
job
for job in jobs
if job.enqueue_time >= twenty_four_hours_ago
or (job.start_time and job.start_time >= twenty_four_hours_ago)
if job.enqueue_time >= one_hour_ago
or (job.start_time and job.start_time >= one_hour_ago)
]

async def get_job_by_id(self, job_id: str) -> Job | None:
Expand Down Expand Up @@ -160,27 +160,30 @@ def generate_statistics(self, jobs_list: list[Job]) -> list[JobsTimeStatistics]:
"""Generate statistics for jobs."""
# TODO: This function is too complex. It should be refactored.
max_time_diff = 60
now_fixed = datetime.now(UTC).replace(second=0, microsecond=0)

one_hour_ago = datetime.now(UTC).replace(second=0, microsecond=0) - timedelta(hours=1)
current_minute = (datetime.now(UTC) - one_hour_ago).total_seconds() // 60
one_hour_ago = now_fixed - timedelta(hours=1) + timedelta(minutes=1)
current_minute = (now_fixed - one_hour_ago).total_seconds() // max_time_diff

statistics = [
JobsTimeStatistics(date=(one_hour_ago + timedelta(minutes=i)))
for i in range(max_time_diff)
]
for job in jobs_list:
created_diff = (job.enqueue_time - one_hour_ago).total_seconds() // 60
created_diff = (job.enqueue_time - one_hour_ago).total_seconds() // max_time_diff
if 0 <= created_diff < max_time_diff:
statistics[int(created_diff)].total_created += 1

if job.status == JobStatus.in_progress and job.start_time:
start_diff = int((job.start_time - one_hour_ago).total_seconds() // 60)
start_diff = int((job.start_time - one_hour_ago).total_seconds() // max_time_diff)
for i in range(start_diff, min(int(current_minute) + 1, max_time_diff)):
statistics[i].total_in_progress += 1

if job.status == JobStatus.complete and job.start_time and job.finish_time:
start_diff = int((job.start_time - one_hour_ago).total_seconds() // 60)
finish_diff = int((job.finish_time - one_hour_ago).total_seconds() // 60)
start_diff = int((job.start_time - one_hour_ago).total_seconds() // max_time_diff)
finish_diff = int(
(job.finish_time - one_hour_ago).total_seconds() // max_time_diff,
)
for i in range(start_diff, min(finish_diff + 1, max_time_diff)):
statistics[i].total_in_progress += 1
if finish_diff < max_time_diff:
Expand Down

0 comments on commit b12fb1b

Please sign in to comment.