result construction optimization

trunk
HeNine 2 years ago
parent ba988a14d5
commit e85a1cfbea

@ -1,5 +1,6 @@
from collections import namedtuple
from unicodedata import name
import flask as flask
from datetime import datetime
from common import database
@ -362,7 +363,6 @@ def get_chat(db_conn, ts_query, start_time="-infinity", end_time="infinity"):
return results
def load_results_data(db_conn, results):
"""
Replace chat and transcript with all entries in result's timeframe.
@ -402,32 +402,35 @@ def load_results_data(db_conn, results):
"""
--sql
WITH timespans (id, start_time, end_time) AS (VALUES %s)
SELECT
timespans.id,
buscribe_transcriptions.start_time,
buscribe_transcriptions.end_time,
names,
buscribe_transcriptions.transcription_line
FROM timespans JOIN
buscribe_transcriptions ON (
buscribe_transcriptions.start_time >= timespans.start_time AND buscribe_transcriptions.start_time <= timespans.end_time AND
buscribe_transcriptions.end_time >= timespans.start_time AND buscribe_transcriptions.end_time <= timespans.end_time
)
LEFT OUTER JOIN (
SELECT line,
ARRAY(
SELECT speaker_name
FROM buscribe_line_inferred_speakers AS inner_speakers
WHERE inner_speakers.line = buscribe_line_inferred_speakers.line
) AS names
FROM buscribe_line_inferred_speakers
) AS inferred_speakers ON buscribe_transcriptions.id = inferred_speakers.line;
SELECT timespans.id,
ARRAY(
SELECT row_to_json(transcriptions)
FROM (
SELECT buscribe_transcriptions.start_time,
buscribe_transcriptions.end_time,
ARRAY(
SELECT speaker_name
FROM buscribe_line_inferred_speakers
WHERE buscribe_line_inferred_speakers.line = buscribe_transcriptions.id
) as names,
buscribe_transcriptions.transcription_line
FROM buscribe_transcriptions
WHERE buscribe_transcriptions.start_time >= timespans.start_time
AND buscribe_transcriptions.start_time <= timespans.end_time
AND buscribe_transcriptions.end_time >= timespans.start_time
AND buscribe_transcriptions.end_time <= timespans.end_time
) AS transcriptions
) AS transcriptions
FROM timespans;
""",
result_timespans
)
for transcript_line in cur:
results[transcript_line.id].transcript.append(transcript_line)
TranscriptRecord = namedtuple("TranscriptRecord", cur.fetchone().transcriptions[0].keys())
cur.scroll(-1)
for result in cur:
results[result.id].transcript.extend([TranscriptRecord(**transcription) for transcription in result.transcriptions])
return results

Loading…
Cancel
Save