Improving the function that fetches the device settings by device id to work using a single query. Removing unnecessary functions and sql files after performing this change.

pull/38/head
Matheus Lima 2019-02-05 12:42:01 -03:00
parent 0b01fdbe97
commit 8a5476f28b
9 changed files with 35 additions and 72 deletions

View File

@ -1,7 +1,7 @@
from os import path from os import path
from typing import List from typing import List
from selene.device.entity.device import Device, WakeWord, TextToSpeech from selene.device.entity.device import Device
from selene.util.db import DatabaseQuery, fetch from selene.util.db import DatabaseQuery, fetch
SQL_DIR = path.join(path.dirname(__file__), 'sql') SQL_DIR = path.join(path.dirname(__file__), 'sql')

View File

@ -5,36 +5,9 @@ from selene.util.db import DatabaseQuery, fetch
SQL_DIR = path.join(path.dirname(__file__), 'sql') SQL_DIR = path.join(path.dirname(__file__), 'sql')
def get_account_preferences_by_device_id(db, device_id): def get_device_settings_by_device_id(db, device_id):
query = DatabaseQuery( query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_account_preferences_by_device_id.sql'), file_path=path.join(SQL_DIR, 'get_device_settings_by_device_id.sql'),
args=dict(device_id=device_id),
singleton=True
)
return fetch(db, query)
def get_wake_word_settings_by_device_id(db, device_id):
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_wake_word_settings_by_device_id.sql'),
args=dict(device_id=device_id),
singleton=True
)
return fetch(db, query)
def get_text_to_speech_by_device_id(db, device_id):
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_text_to_speech_by_device_id.sql'),
args=dict(device_id=device_id),
singleton=True
)
return fetch(db, query)
def get_wake_word_by_device_id(db, device_id):
query = DatabaseQuery(
file_path=path.join(SQL_DIR, 'get_wake_word_by_device_id.sql'),
args=dict(device_id=device_id), args=dict(device_id=device_id),
singleton=True singleton=True
) )
@ -54,30 +27,9 @@ def convert_text_to_speech_setting(setting_name, engine) -> (str, str):
def get_device_settings(db, device_id): def get_device_settings(db, device_id):
response = {} response = get_device_settings_by_device_id(db, device_id)
sql_results = get_account_preferences_by_device_id(db, device_id) tts_setting = response['tts_settings']
response['uuid'] = sql_results['id'] tts_setting = convert_text_to_speech_setting(tts_setting['setting_name'], tts_setting['engine'])
response['systemUnit'] = sql_results['measurement_system'] tts_setting = {'@type': tts_setting[0], 'voice': tts_setting[1]}
response['dateFormat'] = sql_results['date_format'] response['tts_settings'] = tts_setting
response['timeFormat'] = sql_results['time_format']
sql_results = get_text_to_speech_by_device_id(db, device_id)
type, voice = convert_text_to_speech_setting(sql_results['setting_name'], sql_results['engine'])
response['ttsSettings'] = [{
'@type': type,
'voice': voice
}]
sql_results = get_wake_word_settings_by_device_id(db, device_id)
sql_results_wake_word = get_wake_word_by_device_id(db, device_id)
response['listenerSetting'] = {
'uuid': sql_results['id'],
'sampleRate': sql_results['sample_rate'],
'channels': sql_results['channels'],
'wakeWord': sql_results_wake_word['wake_word'],
'phonemes': sql_results['pronunciation'],
'threshold': sql_results['threshold'],
'multiplier': float(sql_results['threshold_multiplier']),
'energyRatio': float(sql_results['dynamic_energy_ratio'])
}
return response return response

View File

@ -1,4 +0,0 @@
SELECT acc_pref.* FROM device.account_preferences acc_pref
INNER JOIN account.account acc ON acc_pref.account_id = acc.id
INNER JOIN device.device dev ON acc.id = dev.account_id
WHERE dev.id = %(device_id)s

View File

@ -0,0 +1,27 @@
SELECT
acc.id as uuid,
acc.measurement_system as system_unit,
acc.date_format,
acc.time_format,
json_build_object('setting_name', tts.setting_name, 'engine', tts.engine) as tts_settings,
json_build_object(
'uuid', wk_word_st.id,
'sampleRate', wk_word_st.sample_rate,
'channels', wk_word_st.channels,
'wakeWord', wk_word.wake_word,
'phonemes', wk_word_st.pronunciation,
'threshold', wk_word_st.threshold,
'multiplier', wk_word_st.threshold_multiplier,
'energyRatio', wk_word_st.dynamic_energy_ratio) as listener_setting
FROM
device.device dev
INNER JOIN
device.account_preferences acc ON dev.account_id = acc.account_id
INNER JOIN
device.text_to_speech tts ON acc.text_to_speech_id = tts.id
INNER JOIN
device.wake_word wk_word ON acc.wake_word_id = wk_word.id
INNER JOIN
device.wake_word_settings wk_word_st ON wk_word.id = wk_word_st.wake_word_id
WHERE
dev.id = %(device_id)s

View File

@ -1,3 +0,0 @@
SELECT tts.* FROM device.text_to_speech tts
INNER JOIN device.device dev ON tts.id = dev.text_to_speech_id
WHERE dev.id = %(device_id)s

View File

@ -1 +0,0 @@
SELECT * FROM device.text_to_speech WHERE id = %(text_to_speech_id)s

View File

@ -1,3 +0,0 @@
SELECT wk_word.* FROM device.wake_word wk_word
INNER JOIN device.device dev ON wk_word.id = dev.wake_word_id
WHERE dev.id = %(device_id)s

View File

@ -1 +0,0 @@
SELECT * FROM device.wake_word WHERE id = %(wake_word_id)s

View File

@ -1,4 +0,0 @@
SELECT settings.* FROM device.wake_word_settings settings
INNER JOIN device.wake_word wk_word ON settings.wake_word_id = wk_word.id
INNER JOIN device.device dev ON wk_word.id = dev.wake_word_id
WHERE dev.id = %(device_id)s