Skip to content

Commit

Permalink
Fully functional torrent search (Laggy window issue on high results c…
Browse files Browse the repository at this point in the history
…ount remains)
  • Loading branch information
techtacoriginal committed Apr 12, 2018
1 parent b8e737f commit c0a77ff
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 23 deletions.
6 changes: 3 additions & 3 deletions app/WndMain.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

<div class="grid-body" style="margin-top: 10px">
<div class="top-search txtSearch">
<input type="text" class="top-search__input" placeholder="Search torrents" autocomplete="off">
<input id="txtSearch" type="text" class="top-search__input" placeholder="Search torrents" autocomplete="off" value="game of thrones">
<i class="zmdi zmdi-search top-search__reset"></i>
</div> <!-- txtSearch -->
<div class="btnSearch" style="margin: 2px 0">
Expand All @@ -103,7 +103,7 @@
<span>Max Results:</span>
</div> <!-- lblResCount -->
<div>
<input type="text" class="txtbox" style="height: 32px" placeholder="Enter amount" autocomplete="off">
<input id="txtResCount" type="text" class="txtbox" style="height: 32px" placeholder="Enter amount" autocomplete="off" value="50">
</div> <!-- txtResCount -->
<div class="item-v-center">
<span style="margin-left: 20px">Filter:</span>
Expand All @@ -123,7 +123,7 @@
<th data-sort-method='filesize'>Torrent Size</th>
</tr>
</thead>
<tbody>
<tbody id="tblMainBody">
<tr>
<td>2017-Dec-05 15:12:49</td>
<td style="display: none">2017-Dec-05 15:12:49</td>
Expand Down
3 changes: 2 additions & 1 deletion app/css/WndMain.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ body {
/* Table */
table {
cursor: default;
transform: rotateX(0deg);
}
.table-responsive {
overflow-x: hidden;
Expand Down Expand Up @@ -450,7 +451,7 @@ thead > tr > th:hover,
.top-search__reset:after, .top-search__reset:before {
position: absolute;
left: 20px;
top: 14px
top: 15px
}

.top-search__reset:after {
Expand Down
10 changes: 5 additions & 5 deletions app/js/WndMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ $(document).ready(function () {

/* Data table functions
----------------------------*/
$("#tblMain tbody tr").on('click', function () {
$("#tblMainBody").on('click', 'tr', function () {
if (!$(this).hasClass('active')) {
$('#tblMain .active').removeClass('active');
$(this).addClass('active');
}
});
let $table = $('#tblMain');
$table.floatThead({
zIndex: 1
});
// let $table = $('#tblMain');
// $table.floatThead({
// zIndex: 1
// });

/* Filtering
----------------------------*/
Expand Down
136 changes: 136 additions & 0 deletions app/main-functions/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const fs = require('fs');
const path = require('path');
const papa = require('papaparse');

process.on('uncaughtException', function (error) {
console.log(error);
process.send(['search-failed', 'general']); //mainWindow.webContents.send('search-failed', 'general');
});

let args = process.argv.slice(2);
let query = args[0];
let count = parseInt(args[1]);
let smart = (args[2] === 'true');
count = count > 0 ? count : 100;

console.log(count);
console.log(query);
console.log(smart);

let i = 1;
let reg;
let stream;
let result = '';

let procData = smart ? smartSearch : regularSearch;

function regularSearch(results, parser) {
let stop = false;
results.data.forEach(function (record) {
// console.log(record['NAME']);
if (record['NAME'].toUpperCase().indexOf(query.toUpperCase()) > -1) {
if (i > count) {
if (!stop) {
parser.abort();
stream.close();
}
stop = true;
} else {
result += '<tr><td>' + record['#ADDED'] +
'</td><td class="d-none">' + record['HASH(B64)'] +
'</td><td>' + record['NAME'] +
'</td><td>' + record['SIZE(BYTES)'] + '</td></tr>';
// console.log(i + ' ' + record['NAME']);
i++;
}
}
});
}

function smartSearch(results, parser) {
let stop = false;
results.data.forEach(function (record) {
// console.log(record['NAME']);
if (record['NAME'].match(reg)) {
if (i > count) {
if (!stop) {
parser.abort();
stream.close();
}
stop = true;
} else {
result += '<tr><td>' + record['#ADDED'] +
'</td><td class="d-none">' + record['HASH(B64)'] +
'</td><td>' + record['NAME'] +
'</td><td>' + formatBytes(record['SIZE(BYTES)'], 1) + '</td></tr>';
// console.log(i + ' ' + formatBytes(record['SIZE(BYTES)']));
i++;
}
}
});
}

let finSearch = function () {
process.send(['search-success', {
resCount: --i,
results: result
}]); //mainWindow.webContents.send('search-failed', 'process');
console.log(process.uptime());
process.kill(process.pid);
};


function search() {

stream = fs.createReadStream(path.join(process.cwd(), 'data', 'processed.csv'))
.once('open', function () {
papa.parse(stream, {
// fastMode: true,
delimiter: ';',
escapeChar: '\\',
header: true,
chunk: procData,
complete: finSearch,
error: function (error, file) {
process.send(['search-failed', 'process']); //mainWindow.webContents.send('search-failed', 'process');
console.log(error);
}
});
})
.on('error', function (err) {
process.send(['search-failed', 'read']); //mainWindow.webContents.send('search-failed', 'read');
console.log(err);
});

}


function startSearch(text) {
reg = new RegExp(regexify(text), 'i');
search();
}

function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, '\\$&');
}

function regexify(text) {
text = text.trim().replace(/(\s+)/g, ' ');
let words = text.split(' ');
let final = '';
words.forEach(function (item) {
final += '(?=.*' + escapeRegExp(item) + ')';
});
return final;
}

function formatBytes(bytes,decimals) {
if(bytes === 0) return '0 Bytes';
let k = 1024,
dm = decimals || 2,
sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}

startSearch(query);
55 changes: 46 additions & 9 deletions app/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ const {app, BrowserWindow, ipcMain, dialog} = electron;
app.commandLine.appendSwitch('remote-debugging-port', '9222');

let procImport;
let procSearch;
let awaitingQuit = false;
let mainWindow;

/* Process handles
--------------------*/
process.on('cont-quit', function () {
app.quit();
if (!procSearch && !procImport) {
app.quit();
}
}); // Emitted if the window is waiting for child processes to exit

// process.on('uncaughtException', function (error) {
Expand Down Expand Up @@ -46,6 +49,11 @@ ipcMain.on('pop-import', function (event) {
initImport(event);
}); // Import dump file open dialog

/* Search */
ipcMain.on('search-start', function (event, data) {
initSearch(data[0], data[1], data[2]);
}); // Handle search event

/* Notification senders
------------------------*/
function popMsg(msg) {
Expand All @@ -57,9 +65,18 @@ function popSuccess(msg) {
function popErr(msg) {
mainWindow.webContents.send('notify', [msg, 'danger']);
} // Show red background notification
function popWarn(msg) {
mainWindow.webContents.send('notify', [msg, 'warning']);
} //

/* Misc Functions
------------------*/
function waitProcess(event, _process, name) {
event.preventDefault();
_process.kill('SIGINT');
popWarn('Wait for background process ' + name + ' to finish');
awaitingQuit = true;
}
function startOB() {
mainWindow = new BrowserWindow({
width: 1200,
Expand All @@ -83,12 +100,11 @@ function startOB() {

mainWindow.on('close', function (event) {
if (procImport) {
event.preventDefault();
console.log('Killing child processes');
procImport.kill('SIGINT');
popErr('Wait for background process \'IMPORT\' to finish');
awaitingQuit = true;
} // Validation of any running child processes before closing
waitProcess(event, procImport, '\'IMPORT\'');
} // Validation of any running child processes before closing (Import)
if (procSearch) {
waitProcess(event, procSearch, '\'SEARCH\'');
} // Validation of any running child processes before closing (Search)
});
mainWindow.on('closed', function () {
mainWindow = null;
Expand Down Expand Up @@ -130,8 +146,29 @@ function initImport(event) {
mainWindow.webContents.send(m[0], m[1]);
});
} else {
popErr('One Import process is already running');
popWarn('One Import process is already running');
}

}
} // Show open dialog and initiate import child process
} // Show open dialog and initiate import child process

function initSearch(query, count, smart) {
if (!procSearch) {
procSearch = cp.fork(path.join(__dirname, 'main-functions', 'search.js'), [query, count, smart], {
cwd: __dirname
});
procSearch.on('exit', function () {
console.log('Search process ended');
procSearch = null;
if (awaitingQuit){
process.emit('cont-quit');
}
});
procSearch.on('message', function (m) {
mainWindow.webContents.send(m[0], m[1]);
});
} else {
popWarn('One Search process is already running');
mainWindow.webContents.send('hide-ol');
}
} // Initiate search child process
47 changes: 42 additions & 5 deletions app/renderers/RndMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,50 @@ $('#mnuUpdTrcks').on('click', function () {
/* Table */
let tbl = new Tablesort(document.getElementById('tblMain'));

/* Search */
function startSearch(){
$("#olAnim").attr("src", "img/load.svg");
showOL('Searching..');
let query = $('#txtSearch').val();
let count = parseInt($('#txtResCount').val());
let smart = $('#chkSmartSearch').prop('checked');
ipcRenderer.send('search-start', [query, count, smart]);
}
$('#btnSearch').on('click', function () {
startSearch();
});
$('#txtSearch').keypress(function(e) {
if(e.which === 13) {
startSearch();
}
});
ipcRenderer.on('search-failed', function (event, data) {
hideOL();
switch (data) {
case 'read':
popMsg('Failed to read the dump file. Possible corruption or file doesn\'t exist', 'danger')();
break;
case 'process':
popMsg('Search error. Mismatching data in dump file', 'danger')();
break;
default:
popMsg('Search failed. Unspecified error.', 'danger')();
}
});
ipcRenderer.on('search-success', function (event, data) {
hideOL();
$('#txtStat').text(data.resCount + ' Results found');
$('#tblMainBody').html(data.results);
tbl.refresh();
$('#txtFilter').val('');
});

/* Overlay
-------------*/
ipcRenderer.on('hide-ol', function () {
hideOL();
});

function showOL(text) {
$('#olText').text(text);
$('#overlay').css({
Expand All @@ -98,11 +140,6 @@ function hideOL() {
});
}

$('#btnSearch').on('click', function () {
$("#olAnim").attr("src", "img/load.svg");
showOL('Searching..');
});

$('#overlay').on('click', function () {
hideOL();
});
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"electron": "^1.8.4",
"jquery": "^3.3.1",
"material-design-iconic-font": "^2.2.0",
"papaparse": "^4.3.7",
"popper.js": "^1.14.1"
}
}

0 comments on commit c0a77ff

Please sign in to comment.