from changes.config import db
from changes.constants import Result, Status
from changes.models import Project, Job, JobPlan
from changes.utils.locking import lock


@lock
def update_project_stats(project_id):
    last_5_builds = Job.query.filter_by(
        result=Result.passed,
        status=Status.finished,
        project_id=project_id,
    ).order_by(Job.date_finished.desc())[:5]
    if last_5_builds:
        avg_build_time = sum(
            b.duration for b in last_5_builds
            if b.duration
        ) / len(last_5_builds)
    else:
        avg_build_time = None

    db.session.query(Project).filter(
        Project.id == project_id
    ).update({
        Project.avg_build_time: avg_build_time,
    }, synchronize_session=False)


@lock
def update_project_plan_stats(project_id, plan_id):
    job_plan = JobPlan.query.filter(
        JobPlan.project_id == project_id,
        JobPlan.plan_id == plan_id,
    ).first()
    if not job_plan:
        return

    last_5_builds = Job.query.filter(
        Job.result == Result.passed,
        Job.status == Status.finished,
        Job.project_id == project_id,
    ).join(
        JobPlan, JobPlan.id == job_plan.id
    ).order_by(Job.date_finished.desc())[:5]
    if last_5_builds:
        avg_build_time = sum(
            b.duration for b in last_5_builds
            if b.duration
        ) / len(last_5_builds)
    else:
        avg_build_time = None

    db.session.query(JobPlan).filter(
        JobPlan.id == job_plan.id
    ).update({
        JobPlan.avg_build_time: avg_build_time,
    }, synchronize_session=False)

@@ -103,16 +103,29 @@
 Project,
+ ProjectPlan,
  Job, Jo
@@ -1453,19 +1453,23 @@
 n.query(
-Job
+Project
 Plan).fi
@@ -1482,24 +1482,88 @@
         
-JobPlan.
+ProjectPlan.project_id == job_plan.project_id,%0A        ProjectPlan.plan_
 id == jo
@@ -1565,26 +1565,32 @@
 == job_plan.
+plan_
 id
+,
 %0A    ).updat
@@ -1597,27 +1597,31 @@
 e(%7B%0A        
-Job
+Project
 Plan.avg_bui

Correct ProjectPlan update