<commit_before>"use strict";
var config = require('config');
var pg = require('pg');
var pgStore = require('connect-pg-simple');
var dbcache = require('./dbcache');
var fs = require('fs');

// Get the PostgreSQL login details from the config.
// Form: postgres://<PGUSER>:<PGPASS>@<URL>/<PGDATABASE> (for Heroku add ?ssl=true when accessing from a remote server)
// To set environment variable:
//  set C_DB_URL=user:pass@abc.com/table (Windows)
//  export C_DB_URL=user:pass@abc.com/table (*nix)
var connectionString = config.get('db.url');
var sessionStore;
var queryDef = [];

queryDef.getAllUsers = {'qstr': 'SELECT user_id, username, fullname, email, role, api_key FROM users', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.findUserById = {'qstr': 'SELECT * FROM users WHERE user_id = $1', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.findUserByUsername = {'qstr': 'SELECT * FROM users WHERE username = $1', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.changeDetailsById = {'qstr': 'UPDATE users SET username = $2, fullname = $3, email = $4, role = $5, api_key = $6 WHERE user_id = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.changePwdByUsername = {'qstr': 'UPDATE users SET password = $2 WHERE username = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.insertUser = {'qstr': 'INSERT INTO users(username, fullname, email, role, api_key) VALUES ($1, $2, $3, $4, $5)', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.deleteUser = {'qstr': 'DELETE FROM users WHERE user_id = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.getAllDevices = {'qstr': 'SELECT * FROM devices', 'readTables': ['devices'], 'writeTables': [], 'cached': true};
queryDef.getAllowedDevices = {'qstr': 'SELECT devices.* FROM devices INNER JOIN shared_devices ON devices.device_id = shared_devices.device_id WHERE shared_devices.user_id = $1 UNION SELECT devices.* FROM devices INNER JOIN users ON devices.api_key = users.api_key WHERE users.user_id = $1', 'readTables': ['devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};
queryDef.findDevicesByUser = {'qstr': 'SELECT devices.*, array_agg(shared_users.username) AS shared FROM devices JOIN users ON devices.api_key = users.api_key LEFT JOIN shared_devices ON shared_devices.device_id = devices.device_id JOIN users AS shared_users ON shared_users.user_id = shared_devices.user_id WHERE users.user_id = $1 GROUP BY devices.device_id ORDER BY devices.device_id', 'readTables': ['devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};
queryDef.changeDeviceById = {'qstr': 'UPDATE devices SET alias = $2, fixed_loc_lat = $3, fixed_loc_lon = $4 WHERE device_id = $1', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.insertDevice = {'qstr': 'INSERT INTO devices(api_key, identifier, alias) VALUES ($1, $2, $3) RETURNING *', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.deleteDevices = {'qstr': 'DELETE FROM devices WHERE device_id = ANY($1::int[])', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.addSharedUser = {'qstr': 'INSERT INTO shared_devices (device_id, user_id) SELECT d.device_id, u.user_id FROM devices d, users u WHERE u.username = $1 AND d.device_id = ANY($2::int[]) AND NOT EXISTS (SELECT * FROM shared_devices WHERE device_id = d.device_id AND user_id = u.user_id)', 'readTables': ['devices', 'users'], 'writeTables': ['shared_devices'], 'cached': false};
queryDef.deleteSharedUser = {'qstr': 'DELETE FROM shared_devices WHERE user_id = (SELECT user_id FROM users WHERE username = $1) AND device_id = ANY($2::int[])', 'readTables': ['users'], 'writeTables': ['shared_devices'], 'cached': false};
queryDef.getLastPositions = {'qstr': 'SELECT locations.*, devices.alias FROM locations INNER JOIN devices ON locations.device_id = devices.device_id WHERE location_id IN (SELECT max(location_id) FROM locations WHERE device_id IN (SELECT device_id FROM shared_devices WHERE user_id = $1 UNION SELECT devices.device_id FROM devices INNER JOIN users ON devices.api_key = users.api_key WHERE users.user_id = $1) GROUP BY device_id)', 'readTables': ['locations', 'devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};<commit_msg>Fix devices not showing up in the devices menu<commit_after>"use strict";
var config = require('config');
var pg = require('pg');
var pgStore = require('connect-pg-simple');
var dbcache = require('./dbcache');
var fs = require('fs');

// Get the PostgreSQL login details from the config.
// Form: postgres://<PGUSER>:<PGPASS>@<URL>/<PGDATABASE> (for Heroku add ?ssl=true when accessing from a remote server)
// To set environment variable:
//  set C_DB_URL=user:pass@abc.com/table (Windows)
//  export C_DB_URL=user:pass@abc.com/table (*nix)
var connectionString = config.get('db.url');
var sessionStore;
var queryDef = [];

queryDef.getAllUsers = {'qstr': 'SELECT user_id, username, fullname, email, role, api_key FROM users', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.findUserById = {'qstr': 'SELECT * FROM users WHERE user_id = $1', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.findUserByUsername = {'qstr': 'SELECT * FROM users WHERE username = $1', 'readTables': ['users'], 'writeTables': [], 'cached': true};
queryDef.changeDetailsById = {'qstr': 'UPDATE users SET username = $2, fullname = $3, email = $4, role = $5, api_key = $6 WHERE user_id = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.changePwdByUsername = {'qstr': 'UPDATE users SET password = $2 WHERE username = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.insertUser = {'qstr': 'INSERT INTO users(username, fullname, email, role, api_key) VALUES ($1, $2, $3, $4, $5)', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.deleteUser = {'qstr': 'DELETE FROM users WHERE user_id = $1', 'readTables': [], 'writeTables': ['users'], 'cached': false};
queryDef.getAllDevices = {'qstr': 'SELECT * FROM devices', 'readTables': ['devices'], 'writeTables': [], 'cached': true};
queryDef.getAllowedDevices = {'qstr': 'SELECT devices.* FROM devices INNER JOIN shared_devices ON devices.device_id = shared_devices.device_id WHERE shared_devices.user_id = $1 UNION SELECT devices.* FROM devices INNER JOIN users ON devices.api_key = users.api_key WHERE users.user_id = $1', 'readTables': ['devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};
queryDef.findDevicesByUser = {'qstr': 'SELECT devices.*, array_agg(shared_users.username) AS shared FROM devices JOIN users ON devices.api_key = users.api_key LEFT JOIN shared_devices ON shared_devices.device_id = devices.device_id LEFT JOIN users AS shared_users ON shared_users.user_id = shared_devices.user_id WHERE users.user_id = $1 GROUP BY devices.device_id ORDER BY devices.device_id', 'readTables': ['devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};
queryDef.changeDeviceById = {'qstr': 'UPDATE devices SET alias = $2, fixed_loc_lat = $3, fixed_loc_lon = $4 WHERE device_id = $1', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.insertDevice = {'qstr': 'INSERT INTO devices(api_key, identifier, alias) VALUES ($1, $2, $3) RETURNING *', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.deleteDevices = {'qstr': 'DELETE FROM devices WHERE device_id = ANY($1::int[])', 'readTables': [], 'writeTables': ['devices'], 'cached': false};
queryDef.addSharedUser = {'qstr': 'INSERT INTO shared_devices (device_id, user_id) SELECT d.device_id, u.user_id FROM devices d, users u WHERE u.username = $1 AND d.device_id = ANY($2::int[]) AND NOT EXISTS (SELECT * FROM shared_devices WHERE device_id = d.device_id AND user_id = u.user_id)', 'readTables': ['devices', 'users'], 'writeTables': ['shared_devices'], 'cached': false};
queryDef.deleteSharedUser = {'qstr': 'DELETE FROM shared_devices WHERE user_id = (SELECT user_id FROM users WHERE username = $1) AND device_id = ANY($2::int[])', 'readTables': ['users'], 'writeTables': ['shared_devices'], 'cached': false};
queryDef.getLastPositions = {'qstr': 'SELECT locations.*, devices.alias FROM locations INNER JOIN devices ON locations.device_id = devices.device_id WHERE location_id IN (SELECT max(location_id) FROM locations WHERE device_id IN (SELECT device_id FROM shared_devices WHERE user_id = $1 UNION SELECT devices.device_id FROM devices INNER JOIN users ON devices.api_key = users.api_key WHERE users.user_id = $1) GROUP BY device_id)', 'readTables': ['locations', 'devices', 'shared_devices', 'users'], 'writeTables': [], 'cached': true};
{'fraction_non_alphanumeric': 0.1711752671061605, 'fraction_numerical': 0.0075017049329393045, 'mean_word_length': 6.494889267461669, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2660601', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2643, 'n_words': 854}