convereted all raw SQL to ORM, and tightened up str/int use in JobExtra-> value field - sqlalchemy 2 is more strict here

This commit is contained in:
2023-09-26 12:35:23 +10:00
parent 3adca0b73d
commit a8a4a1e5fe

39
ai.py
View File

@@ -2,6 +2,7 @@
# pylint: disable=singleton-comparison
from sqlalchemy import func, desc, asc, distinct
from flask import request, render_template, redirect, make_response
from flask_login import login_required
from PIL import Image
@@ -12,6 +13,7 @@ from path import PathType
from files import Entry, File
from job import JobExtra, NewJob
from face import Face, FaceFileLink, FaceRefimgLink, FaceNoMatchOverride, FaceForceMatchOverride
from person import Refimg, Person, PersonRefimgLink
################################################################################
@app.route("/ai_stats", methods=["GET"])
@@ -24,34 +26,19 @@ def ai_stats():
description: renders ai_stats.html to display counts of how many matches for each person we have
"""
stats = db.session.execute( {
"select p.tag, count(f.id) "
"from person p, face f, face_file_link ffl, face_refimg_link frl, person_refimg_link prl "
"where p.id = prl.person_id and prl.refimg_id = frl.refimg_id and frl.face_id = ffl.face_id "
" and ffl.face_id = f.id group by p.tag order by 2 desc" } )
cnt_res = db.session.execute( {
"select count(1) from "
" ( select p.tag from person p, face f, face_file_link ffl, face_refimg_link frl, person_refimg_link prl "
" where p.id = prl.person_id and prl.refimg_id = frl.refimg_id and frl.face_id = ffl.face_id "
" and ffl.face_id = f.id group by p.tag ) as foo" } )
num_stats=cnt_res.first()[0]
stats=Person.query.join(PersonRefimgLink).join(Refimg).join(FaceRefimgLink).with_entities( func.count( Person.tag ).label('count'), Person.tag ).group_by( Person.tag ).order_by( desc(text('count')),asc(Person.tag)).all()
fstats={}
fstats["files_with_a_face"] = db.session.execute( "select count(distinct file_eid) as count from face_file_link" ).first()[0]
sql="select count(distinct ffl.file_eid) as count from face_file_link ffl, face_refimg_link frl where frl.face_id = ffl.face_id"
fstats["files_with_a_match"] = db.session.execute( sql ).first()[0]
sql={"select count(distinct ffl.file_eid) from face f left join "
" face_refimg_link frl on f.id = frl.face_id join face_file_link ffl on f.id = ffl.face_id where frl.refimg_id is null" }
fstats["files_with_missing_matches"] = db.session.execute( sql ).first()[0]
fstats["files_with_a_face"] = FaceFileLink.query.with_entities( func.count(distinct(FaceFileLink.file_eid))).first()[0]
fstats["files_with_a_match"] = FaceFileLink.query.join(Face).join(FaceRefimgLink).with_entities( func.count(distinct(FaceFileLink.file_eid)) ).first()[0]
fstats["files_with_missing_matches"] = Face.query.join(FaceRefimgLink, isouter=True).join(FaceFileLink).filter(FaceRefimgLink.refimg_id==None).with_entities( func.count(distinct(FaceFileLink.file_eid)) ).first()[0]
fstats["all_faces"] = FaceFileLink.query.with_entities( func.count(distinct(FaceFileLink.face_id)) ).first()[0]
fstats["all_matched_faces"] = FaceRefimgLink.query.with_entities( func.count(distinct(FaceRefimgLink.face_id)) ).first()[0]
sql="select count(f.id) from face f left join face_refimg_link frl on f.id = frl.face_id where frl.refimg_id is null"
fstats["all_unmatched_faces"] = Face.query.join(FaceRefimgLink, isouter=True).filter(FaceRefimgLink.refimg_id==None).with_entities( func.count(distinct(Face.id)) ).first()[0]
# files_with_no_matches?
fstats["all_faces"] = db.session.execute( "select count(distinct face_id) as count from face_file_link" ).first()[0]
fstats["all_matched_faces"] = db.session.execute( "select count(distinct face_id) as count from face_refimg_link" ).first()[0]
sql="select count(f.id) from face f left join face_refimg_link frl on f.id = frl.face_id where frl.refimg_id is null"
fstats["all_unmatched_faces"] = db.session.execute( sql).first()[0]
return render_template("ai_stats.html", page_title="AI Statistics", stats=stats, num_stats=num_stats, fstats=fstats )
return render_template("ai_stats.html", page_title="AI Statistics", stats=stats, num_stats=len(stats), fstats=fstats )
################################################################################
@@ -94,7 +81,7 @@ def run_ai_on_import():
ptype=PathType.query.filter(PathType.name=="Import").first()
jex.append( JobExtra( name="person", value="all" ) )
jex.append( JobExtra( name="path_type", value=ptype.id ) )
jex.append( JobExtra( name="path_type", value=str(ptype.id) ) )
NewJob( "run_ai_on_path", num_files=0, wait_for=None, jex=jex, desc="Look for face(s) in import path(s)")
return redirect("/jobs")
@@ -114,7 +101,7 @@ def run_ai_on_storage():
jex=[]
ptype=PathType.query.filter(PathType.name=="Storage").first()
jex.append( JobExtra( name="person", value="all" ) )
jex.append( JobExtra( name="path_type", value=ptype.id ) )
jex.append( JobExtra( name="path_type", value=str(ptype.id) ) )
NewJob( "run_ai_on_path", num_files=0, wait_for=None, jex=jex, desc="Look for face(s) in storage path(s)")
return redirect("/jobs")