Skip to content

Commit b36d3ef

Browse files
committed
Don't request images from DB when not required
1 parent ee11616 commit b36d3ef

3 files changed

Lines changed: 73 additions & 25 deletions

File tree

backend/app.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ def coins(f, search=None, sort=None, reverse: bool = False, status_filter=None,
5050
cur = con.cursor()
5151

5252
sql = """
53-
SELECT coins.id, images.image, title, status, subjectshort, value, unit, year, mintmark, series, country
54-
FROM coins LEFT OUTER JOIN images ON images.id = coins.image
53+
SELECT coins.id, title, status, subjectshort, value, unit, year, mintmark, series, country
54+
FROM coins
5555
"""
5656

5757
params = []
@@ -91,6 +91,27 @@ def coins(f, search=None, sort=None, reverse: bool = False, status_filter=None,
9191
data = res.fetchall()
9292
con.close()
9393

94+
for i, record in enumerate(data):
95+
data[i] = list(record)
96+
97+
return data
98+
99+
100+
@app.get("/api/images")
101+
def coins(f):
102+
file = f
103+
con = sqlite_connect(file)
104+
cur = con.cursor()
105+
106+
sql = """
107+
SELECT coins.id, images.image
108+
FROM coins LEFT OUTER JOIN images ON images.id = coins.image
109+
"""
110+
111+
res = cur.execute(sql)
112+
data = res.fetchall()
113+
con.close()
114+
94115
for i, record in enumerate(data):
95116
data[i] = list(record)
96117
if data[i][1]:

frontend/src/components/CoinListView.vue

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ onMounted(async () => {
4141
watch(imagePresentation,async () => {
4242
images.value = {}
4343
if (imagePresentation.value === 'image' && props.settings.type) {
44-
const coins_list = await service.loadCoins()
45-
coins_list.forEach((coin) => {
46-
images.value[coin[0]] = coin[1];
44+
const coin_images = await service.loadImages()
45+
coin_images.forEach((coin_image) => {
46+
images.value[coin_image[0]] = coin_image[1];
4747
});
4848
}
4949
})
@@ -55,8 +55,9 @@ const onOpenFile = async () => {
5555
images.value = {};
5656
coinsList.value = await service.loadCoins()
5757
if (imagePresentation.value === 'image') {
58-
coinsList.value.forEach((coin) => {
59-
images.value[coin[0]] = coin[1];
58+
const coin_images = await service.loadImages()
59+
coin_images.forEach((coin_image) => {
60+
images.value[coin_image[0]] = coin_image[1];
6061
});
6162
}
6263
@@ -93,19 +94,19 @@ defineExpose({
9394
9495
function generateDescription( coin_data ) {
9596
let desc = [];
96-
if (coin_data[4])
97-
desc.push(coin_data[4]);
98-
if (coin_data[5] || coin_data[6])
99-
desc.push(convertFraction(props.settings.convert_fraction, coin_data[5]) + ' ' + coin_data[6]);
100-
if (coin_data[10])
101-
desc.push(coin_data[10]);
102-
if (coin_data[7]) {
103-
desc.push(formatYear(props.settings.enable_bc, coin_data[7]));
97+
if (coin_data[3])
98+
desc.push(coin_data[3]);
99+
if (coin_data[4] || coin_data[5])
100+
desc.push(convertFraction(props.settings.convert_fraction, coin_data[4]) + ' ' + coin_data[5]);
101+
if (coin_data[9])
102+
desc.push(coin_data[9]);
103+
if (coin_data[6]) {
104+
desc.push(formatYear(props.settings.enable_bc, coin_data[6]));
104105
}
106+
if (coin_data[7])
107+
desc.push(coin_data[7]);
105108
if (coin_data[8])
106109
desc.push(coin_data[8]);
107-
if (coin_data[9])
108-
desc.push(coin_data[9]);
109110
110111
return desc;
111112
}
@@ -166,7 +167,7 @@ const loadImage = async (coinId) => {
166167
v-for="(coin, index) in coinsList"
167168
:key="coin[0]"
168169
:subtitle="generateDescription(coin).join(', ')"
169-
:title="coin[2]"
170+
:title="coin[1]"
170171
@click="router.push('/coin/' + coin[0])"
171172
class="pa-1"
172173
>
@@ -189,7 +190,7 @@ const loadImage = async (coinId) => {
189190
<v-img :src="arrayBufferToBase64(images[coin[0]])" :width="100" max-height="56" />
190191
</template>
191192
<template v-slot:append>
192-
<StatusItem :status="coin[3]" :statuses="settings.statuses" :statusPresentation="statusPresentation" />
193+
<StatusItem :status="coin[2]" :statuses="settings.statuses" :statusPresentation="statusPresentation" />
193194
</template>
194195
</v-list-item>
195196
</v-list>

frontend/src/composables/useService.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,9 @@ export function useService(passwordDialogRef) {
295295
}
296296

297297
const loadCoinsLocal = async (search, sortBy, reverse, statusFilter, countryFilter, seriesFilter, typeFilter, periodFilter, mintFilter) => {
298-
let coinsList = [];
299-
300298
let sql = `
301-
SELECT coins.id, images.image, title, status, subjectshort, value, unit, year, mintmark, series, country
302-
FROM coins LEFT OUTER JOIN images ON images.id = coins.image
299+
SELECT coins.id, title, status, subjectshort, value, unit, year, mintmark, series, country
300+
FROM coins
303301
`
304302
let params = [];
305303
let sql_filters = [];
@@ -340,9 +338,36 @@ export function useService(passwordDialogRef) {
340338
if (reverse)
341339
sql += ' DESC';
342340

343-
coinsList = await executeQuery(sql, params)
341+
return await executeQuery(sql, params)
342+
}
344343

345-
return coinsList;
344+
const loadImages = async () => {
345+
if (connection_type === 'local')
346+
return loadImagesLocal();
347+
else if (connection_type === 'remote')
348+
return loadImagesRemote(connected_file);
349+
}
350+
351+
const loadImagesRemote = async (file) => {
352+
let images = [];
353+
354+
try {
355+
const response = await api.get('/api/images', {params: {f: file}})
356+
images = response.data
357+
}
358+
catch (err) {
359+
globalStatus.error.value = err
360+
}
361+
362+
return images;
363+
}
364+
365+
const loadImagesLocal = async () => {
366+
let sql = `
367+
SELECT coins.id, images.image
368+
FROM coins LEFT OUTER JOIN images ON images.id = coins.image
369+
`
370+
return await executeQuery(sql);
346371
}
347372

348373
const loadImage = async (coinId, type) => {
@@ -589,6 +614,7 @@ export function useService(passwordDialogRef) {
589614
openRemoteFile,
590615
openLocalFile,
591616
loadCoins,
617+
loadImages,
592618
loadImage,
593619
getDetails,
594620
getPhotos,

0 commit comments

Comments
 (0)