Skip to content

Commit

Permalink
Updates queries;
Browse files Browse the repository at this point in the history
Updates UI;
Updates API calls
  • Loading branch information
msimmer committed Nov 1, 2019
1 parent 2f2fd60 commit ca27228
Show file tree
Hide file tree
Showing 15 changed files with 515 additions and 214 deletions.
2 changes: 2 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class API {
let stdout = ''
let stderr = ''

console.log(`Running %s with arguments %s`, script, args.join(' '))

proc.stdout.on('data', data => (stdout += String(data)))
proc.stderr.on('data', data => (stderr += String(data)))

Expand Down
13 changes: 13 additions & 0 deletions lib/create-table-client-torrent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports =
'DROP TABLE IF EXISTS `client_torrents`; \
CREATE TABLE `client_torrents` ( \
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, \
`client_id` bigint(20) unsigned NOT NULL, \
`torrent_id` bigint(20) unsigned NOT NULL, \
PRIMARY KEY (`id`), \
KEY `client_torrents_client_id_index` (`client_id`), \
KEY `client_torrents_torrent_id_index` (`torrent_id`), \
UNIQUE KEY (`client_id`, `torrent_id`), \
CONSTRAINT `client_torrents_ibfk_1` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE, \
CONSTRAINT `client_torrents_ibfk_2` FOREIGN KEY (`torrent_id`) REFERENCES `torrents` (`id`) ON DELETE CASCADE \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;'
2 changes: 1 addition & 1 deletion lib/create-table-clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ module.exports =
PRIMARY KEY (`id`), \
UNIQUE KEY `rcp_port` (`rpc_port`), \
UNIQUE KEY `transmission_port` (`transmission_port`) \
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
2 changes: 1 addition & 1 deletion lib/create-table-ports.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ module.exports =
PRIMARY KEY (`id`), \
UNIQUE KEY `rcp_port` (`rpc_port`), \
UNIQUE KEY `transmission_port` (`transmission_port`) \
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
5 changes: 2 additions & 3 deletions lib/create-table-torrents.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module.exports =
"DROP TABLE IF EXISTS `torrents`; \
'DROP TABLE IF EXISTS `torrents`; \
CREATE TABLE `torrents` ( \
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, \
`name` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL, \
`hash` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL, \
`active` tinyint(1) DEFAULT '0', \
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, \
PRIMARY KEY (`id`) \
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;'
138 changes: 123 additions & 15 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mysql = require('mysql')
const createClientsQuery = require('./create-table-clients')
const createPortsQuery = require('./create-table-ports')
const createTorrentsQuery = require('./create-table-torrents')
const createClientTorrentQuery = require('./create-table-client-torrent')

const connection = mysql.createConnection({
host: process.env.DATABASE_HOST,
Expand All @@ -21,15 +22,33 @@ class Database {
this.connection.end()
}

// Create tables
drop(callback) {
return this.connection.query(
`
DROP TABLE IF EXISTS client_torrents;
DROP TABLE IF EXISTS clients;
DROP TABLE IF EXISTS torrents;
DROP TABLE IF EXISTS ports;
`,
error => {
if (error) callback(error)
return callback(null)
}
)
}

// Drop and re-create tables
create(callback) {
return this.connection.query(createClientsQuery, error1 => {
if (error1) callback(error1)
return this.connection.query(createPortsQuery, error2 => {
if (error2) callback(error2)
return this.connection.query(createTorrentsQuery, error3 => {
if (error3) callback(error3)
return callback(null)
return this.connection.query(createClientTorrentQuery, error4 => {
if (error4) callback(error4)
return callback(null)
})
})
})
})
Expand All @@ -42,15 +61,23 @@ class Database {

const clients = [[]]
const ports = [[]]
const torrents = [[]]

// Setup client and port data
for (let i = 0; i <= n; i++) {
for (let i = 0; i < n; i++) {
const name = `transmission-${i}`
const rpcPort = rpcMin + i
const transmissionPort = transmissionMin + i
const available = false

clients[0].push([name, rpcPort, transmissionPort])
ports[0].push([rpcPort, transmissionPort])
ports[0].push([rpcPort, transmissionPort, available])
}

for (let i = 0; i < 5; i++) {
const name = `Torrent-${i}.torrent`
const hash = `hash-${i}-${String(Math.random()).slice(2)}`
torrents[0].push([name, hash])
}

return this.connection.query(
Expand All @@ -64,7 +91,15 @@ class Database {
clients,
error2 => {
if (error2) return callback(error2)
return callback(null)

return this.connection.query(
`INSERT INTO torrents (name, hash) VALUES ?`,
torrents,
error3 => {
if (error3) return callback(error3)
return callback(null)
}
)
}
)
}
Expand All @@ -78,11 +113,67 @@ class Database {
})
}

findTorrents(torrentIds, callback) {
return this.connection.query(
`SELECT * FROM torrents WHERE id IN (${torrentIds.join()})`,
(error, results) => {
if (error) return callback(error)
return callback(null, results)
}
)
}

// Mutates
getClientsTorrents(clients, callback) {
// Check if the clients are an array and if the first one has been processed
if (!clients || !clients.length || clients[0].torrents) {
return callback(null, clients)
}

const client = clients.shift() // Remove from bottom

return this.connection.query(
`SELECT t.*
FROM torrents AS t
INNER JOIN client_torrents AS ct ON t.id = ct.torrent_id
WHERE ct.client_id = ${client.id}
`,
(error, torrents) => {
if (error) return callback(error)
client.torrents = torrents
clients.push(client) // Add to top

return this.getClientsTorrents(clients, callback)
}
)
}

getClients(callback) {
return this.connection.query('SELECT * FROM clients', (error, results) => {
if (error) return callback(error)
return callback(null, results)
})
return this.connection.query(
'SELECT * FROM clients',
(error1, results1) => {
if (error1) return callback(error1)

// Add torrent data. Mutates
return this.getClientsTorrents(results1, (error2, results2) => {
if (error2) return callback(error2)
return callback(null, results2)
})
}
)
}

findClients(clientIds, callback) {
return this.connection.query(
`SELECT * FROM clients WHERE id IN (${clientIds.join()})`,
(error1, results1) => {
// Add torrent data. Mutates
return this.getClientsTorrents(results1, (error2, results2) => {
if (error2) return callback(error2)
return callback(null, results2)
})
}
)
}

addClients(n, callback) {
Expand Down Expand Up @@ -150,7 +241,9 @@ class Database {

// Update client status
return this.connection.query(
`DELETE FROM clients WHERE rpc_port IN (${ports_})`,
`DELETE FROM client_torrents WHERE torrent_id IN (
(SELECT id FROM clients WHERE rpc_port IN (${ports_})));
DELETE FROM clients WHERE rpc_port IN (${ports_})`,
(error2, results2) => {
if (error2) callback(error2)
return callback(null, results2)
Expand Down Expand Up @@ -214,22 +307,37 @@ class Database {
}

// Adds a torrent to all active clients
addTorrents(torrentIds, callback) {
addTorrents(torrentIds, clientIds, callback) {
const torrentIds_ = torrentIds.join()
const clientTorrentIds = clientIds
.reduce(
(acc, cId) => acc.concat(torrentIds.map(tId => `(${cId}, ${tId})`)),
[]
)
.join()

return this.connection.query(
`UPDATE torrents SET active = 1 WHERE id IN (${torrentIds_})`,
`INSERT INTO client_torrents (client_id, torrent_id)
VALUES ${clientTorrentIds}
ON DUPLICATE KEY UPDATE
client_id = VALUES(client_id),
torrent_id = VALUES(torrent_id)`,
(error, results) => {
if (error) return callback(error)
return callback(null, results)
}
)
}

// Removes a torrent from all active clients
removeTorrents(torrentIds, callback) {
// Removes torrents from selected clients
removeTorrents(clientIds, torrentIds, callback) {
const clientIds_ = clientIds.join()
const torrentIds_ = torrentIds.join()

return this.connection.query(
`UPDATE torrents SET active = 0 WHERE id IN (${torrentIds_})`,
`DELETE FROM client_torrents
WHERE torrent_id IN (${torrentIds_})
AND client_id IN (${clientIds_})`,
(error, results) => {
if (error) return callback(error)
return callback(null, results)
Expand Down
4 changes: 3 additions & 1 deletion lib/test.sh → lib/mock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function response_ok {

# echo "$@"

# sleep 3
# sleep 1
# response_error "there was an error"

cmd="ls . > /dev/null 2>&1"
if eval "$cmd"; then
response_ok
Expand Down
Loading

0 comments on commit ca27228

Please sign in to comment.