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)
buscribe_transcriptions.end_time, FROM (
names, SELECT buscribe_transcriptions.start_time,
buscribe_transcriptions.transcription_line buscribe_transcriptions.end_time,
FROM timespans JOIN ARRAY(
buscribe_transcriptions ON ( SELECT speaker_name
buscribe_transcriptions.start_time >= timespans.start_time AND buscribe_transcriptions.start_time <= timespans.end_time AND FROM buscribe_line_inferred_speakers
buscribe_transcriptions.end_time >= timespans.start_time AND buscribe_transcriptions.end_time <= timespans.end_time WHERE buscribe_line_inferred_speakers.line = buscribe_transcriptions.id
) ) as names,
LEFT OUTER JOIN ( buscribe_transcriptions.transcription_line
SELECT line, FROM buscribe_transcriptions
ARRAY( WHERE buscribe_transcriptions.start_time >= timespans.start_time
SELECT speaker_name AND buscribe_transcriptions.start_time <= timespans.end_time
FROM buscribe_line_inferred_speakers AS inner_speakers AND buscribe_transcriptions.end_time >= timespans.start_time
WHERE inner_speakers.line = buscribe_line_inferred_speakers.line AND buscribe_transcriptions.end_time <= timespans.end_time
) AS names ) AS transcriptions
FROM buscribe_line_inferred_speakers ) AS transcriptions
) AS inferred_speakers ON buscribe_transcriptions.id = inferred_speakers.line; 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