result construction optimization

trunk
HeNine 2 years ago
parent ba988a14d5
commit e85a1cfbea

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

Loading…
Cancel
Save