less ip info

This commit is contained in:
ScrawnyRonnie 2024-03-24 22:31:04 -04:00
parent 763afed32f
commit d7e45e2e87
3 changed files with 24 additions and 5 deletions

View file

@ -262,7 +262,7 @@ export async function get_accounts_login_info(pagination, sort, filter) {
// this was a really hard query to get right... // this was a really hard query to get right...
// https://www.gab.lc/articles/better_faster_subqueries_postgresql/ // https://www.gab.lc/articles/better_faster_subqueries_postgresql/
const accounts = await pool.query( const accounts = await pool.query(
'SELECT account.*, COALESCE(l.lastLogin, TIMESTAMP \'epoch\') as last_login, l2.ip_address, l2.canonical_hostname FROM account' + 'SELECT account.*, COALESCE(l.lastLogin, TIMESTAMP \'epoch\') as last_login, l2.ip_address FROM account' +
' LEFT OUTER JOIN (' + ' LEFT OUTER JOIN (' +
' SELECT MAX(id) as loginId, account_id, MAX(login_time) as lastLogin' + ' SELECT MAX(id) as loginId, account_id, MAX(login_time) as lastLogin' +
' FROM login' + ' FROM login' +
@ -282,10 +282,11 @@ export async function get_accounts_login_info(pagination, sort, filter) {
r.admin = r.gm; r.admin = r.gm;
if (r.ip_address !== null) { if (r.ip_address !== null) {
const splitOctet = r.ip_address.split('.'); // Split IP address by '.'
const lastOctets = splitOctet.slice(-2).join('.');
r.last_login = { r.last_login = {
time: r.last_login, time: r.last_login,
hostname: r.canonical_hostname, ip: lastOctets,
ip: r.ip_address,
} }
} else { } else {
r.last_login = {} r.last_login = {}
@ -527,7 +528,25 @@ export async function get_account_logins(account_id, pagination) {
pagination.item_count = login_count; pagination.item_count = login_count;
pagination.page_count = Math.ceil(pagination.item_count / pagination.items_per_page); pagination.page_count = Math.ceil(pagination.item_count / pagination.items_per_page);
logins.rows.forEach((r) => {
if (r.ip_address !== null) {
const splitOctet = r.ip_address.split('.'); // Split IP address by '.'
const lastOctets = splitOctet.slice(-2).join('.');
r.ipAddress = {
ip: lastOctets,
}
} else {
r.last_login = {}
}
delete r.canonical_hostname;
delete r.hostname;
delete r.ip_address;
});
return logins.rows; return logins.rows;
} catch (e) { } catch (e) {
if (e.code) if (e.code)
e.code = pg_error_inv[e.code] e.code = pg_error_inv[e.code]

View file

@ -36,7 +36,7 @@
{#each logins as login, i} {#each logins as login, i}
<tr> <tr>
<td> <td>
<code>{login.hostname} - {login.ip_address}</code> <code>{login.ipAddress.ip}</code>
</td> </td>
<td>{moment(login.login_time).format('MMMM Do YYYY, h:mm:ss a')} ({moment(login.login_time).fromNow()})</td> <td>{moment(login.login_time).format('MMMM Do YYYY, h:mm:ss a')} ({moment(login.login_time).fromNow()})</td>
</tr> </tr>

View file

@ -140,7 +140,7 @@
<td>{moment(user.created).fromNow()}</td> <td>{moment(user.created).fromNow()}</td>
<td>{#if user.last_login.time} <td>{#if user.last_login.time}
{moment(user.last_login.time).fromNow()}<br/> {moment(user.last_login.time).fromNow()}<br/>
<code>{user.last_login.hostname} - {user.last_login.ip}</code> <code>{user.last_login.ip}</code>
{:else} {:else}
Never logged in Never logged in
{/if} {/if}