Skip to content

Commit

Permalink
Merge pull request #542 from the-hideout/fix-preset-image-refresh
Browse files Browse the repository at this point in the history
fix preset image refresh
  • Loading branch information
Razzmatazzz authored Jul 18, 2024
2 parents 3b42d27 + 8ccbb17 commit be36296
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 35 deletions.
40 changes: 36 additions & 4 deletions src/tarkov-data-manager/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,10 @@ app.post('/items/refresh-images/:id', async (req, res) => {
}
let newImage;
if (item.types.includes('preset')) {
newImage = await webSocketServer.getJsonImage(item.properties.items);
newImage = await webSocketServer.getJsonImage({
id: item.id,
items: item.properties.items,
});
} else {
const results = await webSocketServer.getImages(item.id);
newImage = results[item.id];
Expand Down Expand Up @@ -1870,6 +1873,9 @@ app.get('/presets', async (req, res) => {
<th>
name
</th>
<th>
images
</th>
</tr>
</thead>
<tbody>
Expand Down Expand Up @@ -1913,9 +1919,10 @@ app.get('/presets', async (req, res) => {
});

app.get('/presets/get', async (req, res) => {
const [presets, en] = await Promise.all([
const [presets, en, items] = await Promise.all([
query('SELECT * FROM manual_preset'),
tarkovData.locale('en'),
remoteData.get(),
]);
for (const preset of presets) {
const baseItemId = preset.items[0]._tpl;
Expand All @@ -1929,15 +1936,40 @@ app.get('/presets/get', async (req, res) => {
shortName: en[`${item._tpl} ShortName`],
};
});
preset.image_8x_link = items.get(preset.id)?.image_8x_link;
preset.image_512_link = items.get(preset.id)?.image_512_link;
preset.image_link = items.get(preset.id)?.image_link;
preset.base_image_link = items.get(preset.id)?.base_image_link;
preset.grid_image_link = items.get(preset.id)?.grid_image_link;
preset.icon_link = items.get(preset.id)?.icon_link;
}
res.json(presets);
});

app.put('/presets/:id', async (req, res) => {
const response = {message: 'No changes made.', errors: []};
try {
await query('UPDATE manual_preset SET append_name = ? WHERE id = ?', [req.body.append_name, req.params.id]);
response.message = 'Preset updated';
const preset = await query('SELECT * FROM manual_preset WHERE id = ?', [req.params.id]).then(results => results[0]);
if (preset.append_name !== req.body.append_name) {
await query('UPDATE manual_preset SET append_name = ? WHERE id = ?', [req.body.append_name, req.params.id]);
const [en, items] = await Promise.all([tarkovData.locale('en'), remoteData.get()]);
const baseItem = items.get(preset.items[0]._tpl);
await remoteData.setProperties(req.params.id, {
name: `${baseItem.name} ${en[req.body.append_name]}`,
short_name: `${baseItem.short_name} ${en[req.body.append_name]}`,
});
response.message = 'Preset updated';
try {
await regenerateFromExisting(req.params.id);
} catch (error) {
console.log(error);
if (Array.isArray(error)) {
response.errors = error.map(err => err.message || err);
} else {
response.errors.push(error.message || error);
}
}
}
} catch (error) {
response.errors.push(error.message);
}
Expand Down
54 changes: 25 additions & 29 deletions src/tarkov-data-manager/jobs/archive-prices.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import DataJob from '../modules/data-job.mjs';

const max_days_per_run = 1;
const max_days_per_run = 2;

class ArchivePricesJob extends DataJob {
constructor(options) {
Expand All @@ -24,7 +24,6 @@ class ArchivePricesJob extends DataJob {
// archive max_days_per_run number of days
for (let i = 0; i < max_days_per_run; i++) {
for (const gameMode of this.gameModes) {
const gameModeName = gameMode.name;
// get the price with the oldest timestamp
const oldestPrice = await this.query(`
SELECT * FROM price_data
Expand All @@ -33,13 +32,16 @@ class ArchivePricesJob extends DataJob {
LIMIT 1
`, [cutoff, gameMode.value]);
if (oldestPrice.length === 0) {
this.logger.success(`No ${gameModeName} prices found before ${cutoff}`);
return;
// we don't have any prices before the cutoff
// this means archiving is current, so nothing to do
this.logger.success(`No ${gameMode.name} prices found before ${cutoff}`);
continue;
}
// convert oldest price date to YYYY-MM-dd
// removes hours, mins, etc. to break at the start of the day
const archiveDate = this.getMysqlDate(oldestPrice[0].timestamp);

this.logger.log(`Archiving ${gameModeName} prices for ${archiveDate}`);
this.logger.log(`Archiving ${gameMode.name} prices for ${archiveDate}`);

// get minimum and average prices per item during day
const itemPrices = await this.query(`
Expand All @@ -65,10 +67,25 @@ class ArchivePricesJob extends DataJob {
ON DUPLICATE KEY UPDATE
price_min=VALUES(price_min), price_avg=VALUES(price_avg)
`, insertValues);
this.logger.log(`Inserted ${Object.keys(itemPrices).length} ${gameModeName} archived prices in ${new Date() - insertStart}ms`);
this.logger.log(`Inserted ${Object.keys(itemPrices).length} ${gameMode.name} archived prices in ${new Date() - insertStart}ms`);

// delete the prices we just archived
await this.deletePricesThrough(archiveDate);
// delete the prices we just archived from main price table
// can only delete 100k at a time, so need to loop
const batchSize = this.maxQueryRows;
let deletedCount = 0;
const deleteStart = new Date();
while (true) {
const deleteResult = await this.query(`
DELETE FROM price_data
WHERE timestamp < ? + INTERVAL 1 DAY AND game_mode = ?
LIMIT ?
`, [archiveDate, gameMode.value, batchSize]);
deletedCount += deleteResult.affectedRows;
if (deleteResult.affectedRows < batchSize) {
break;
}
}
this.logger.log(`Deleted ${deletedCount} individual ${gameMode.name} prices in ${new Date() - deleteStart}ms`);
}
}
}
Expand All @@ -77,27 +94,6 @@ class ArchivePricesJob extends DataJob {
getMysqlDate = (jsDate) => {
return jsDate.toISOString().slice(0, 10);
}

// deletes all prices through the given YYY-MM-dd date
deletePricesThrough = async (mysqlDateCutoff, gameMode) => {
// delete archived prices from main price table
// can only delete 100k at a time, so need to loop
const batchSize = this.maxQueryRows;
let deletedCount = 0;
const deleteStart = new Date();
while (true) {
const deleteResult = await this.query(`
DELETE FROM price_data
WHERE timestamp < ? + INTERVAL 1 DAY AND game_mode = ?
LIMIT ?
`, [mysqlDateCutoff, gameMode, batchSize]);
deletedCount += deleteResult.affectedRows;
if (deleteResult.affectedRows < batchSize) {
break;
}
}
this.logger.log(`Deleted ${deletedCount} individual prices in ${new Date() - deleteStart}ms`);
}
}

export default ArchivePricesJob;
4 changes: 4 additions & 0 deletions src/tarkov-data-manager/modules/remote-data.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import midmean from 'compute-midmean';

import normalizeName from './normalize-name.js';
import timer from './console-timer.js';
import { query, maxQueryRows } from './db-connection.mjs';
import gameModes from './game-modes.mjs';
Expand Down Expand Up @@ -330,6 +331,9 @@ const methods = {
}
if (currentValue !== value) {
changeValues[property] = value;
if (property === 'name' && !properties.normalized_name) {
changeValues.normalized_name = normalizeName(value);
}
}
}
if (Object.keys(changeValues).length === 0) {
Expand Down
77 changes: 75 additions & 2 deletions src/tarkov-data-manager/public/presets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
let table = false;

const existingImageElement = (itemId, imageType, url) => {
const tooltipId = `${itemId}-${imageType}-tooltip-content`;
return `
<a href="${url}" class="tooltipped" data-html="true" data-tooltip-id="${tooltipId}">✔️</a>
<div id="${tooltipId}" style="display: none;">
<div>${imageType} image</div>
<img src="${url}" style="max-height: 300px" loading="lazy" />
</div>
`;
};

$(document).ready( function () {
//$('.tooltipped').tooltip();
//$('.modal').modal();
Expand All @@ -26,7 +37,38 @@ $(document).ready( function () {
render: (data, type, wipe) => {
return data;
}
}
},
{
data: 'image_link',
render: (data, type, item) => {
if (type === 'display') {
let imageLink = item.image_512_link;
if (!imageLink) {
imageLink = item.base_image_link || item.grid_image_link || item.icon_link;
}
return `
<div class="row">
${imageLink ? `<div class="col s12"><img src="${imageLink}" loading="lazy" style="max-height: 200px" /></div>`: ''}
</div>
<div class="">
${item.image_8x_link ? existingImageElement(item.id, '8x', item.image_8x_link): missingImageElement('8x')}
${item.image_512_link ? existingImageElement(item.id, '512', item.image_512_link): missingImageElement('512')}
${data ? existingImageElement(item.id, 'inspect', data): missingImageElement('inspect')}
${item.base_image_link ? existingImageElement(item.id, 'base', item.base_image_link): missingImageElement('base')}
${item.grid_image_link ? existingImageElement(item.id, 'grid', item.grid_image_link): missingImageElement('grid')}
${item.icon_link ? existingImageElement(item.id, 'icon', item.icon_link) : missingImageElement('icon')}
</div>
<div class="row">
${item.image_8x_link || item.base_image_link ? `<a class="waves-effect waves-light regenerate btn-small tooltipped" data-id="${item.id}" data-tooltip="Regenerate images from source"><i class="material-icons">refresh</i></a>` : ''}
<a class="waves-effect waves-light refresh-images btn-small tooltipped" data-id="${item.id}" data-tooltip="Refresh images from game"><i class="material-icons">sync</i></a>
</div>
`;
}
return data;
},
className: 'image-column',
width: '10%',
},
];

table = $('table.main').DataTable({
Expand Down Expand Up @@ -91,6 +133,38 @@ $(document).ready( function () {
$('#modal-delete-confirm .delete-confirm').data('id', target.data('id'));
M.Modal.getInstance(document.getElementById('modal-delete-confirm')).open();
});

$('.btn-small.regenerate').off('click');
$('.btn-small.regenerate').click(event => {
let target = event.target;
if (target.nodeName !== 'A') {
target = target.parentElement;
}
$(target).addClass('disabled');
fetch(`/items/regenerate-images/${$(target).data('id')}`, {method: 'POST'}).then(response => response.json()).then(data => {
$(target).removeClass('disabled');
M.toast({text: data.message});
for (const error of data.errors) {
M.toast({text: error});
}
});
});

$('.btn-small.refresh-images').off('click');
$('.btn-small.refresh-images').click(event => {
let target = event.target;
if (target.nodeName !== 'A') {
target = target.parentElement;
}
$(target).addClass('disabled');
fetch(`/items/refresh-images/${$(target).data('id')}`, {method: 'POST'}).then(response => response.json()).then(data => {
$(target).removeClass('disabled');
M.toast({text: data.message});
for (const error of data.errors) {
M.toast({text: error});
}
});
});
}
});

Expand Down Expand Up @@ -129,7 +203,6 @@ $(document).ready( function () {
$('#modal-edit-preset .edit-preset-save').click(function(event) {
const form = $('#modal-edit-preset').find('form').first();
const formData = form.serialize();
console.log(formData)
$.ajax({
method: form.attr('method'),
url: form.attr('action'),
Expand Down

0 comments on commit be36296

Please sign in to comment.