| 1 | import { formatDistanceToNow } from './format' |
| 2 | import type { ClientInfoData, ClientStatus } from '../types/client' |
| 3 | |
| 4 | export class Client { |
| 5 | key: string |
| 6 | user: string |
| 7 | clientID: string |
| 8 | runID: string |
| 9 | version: string |
| 10 | wireProtocol: string |
| 11 | hostname: string |
| 12 | ip: string |
| 13 | firstConnectedAt: Date |
| 14 | lastConnectedAt: Date |
| 15 | disconnectedAt?: Date |
| 16 | online: boolean |
| 17 | status: ClientStatus |
| 18 | |
| 19 | constructor(data: ClientInfoData) { |
| 20 | this.key = data.key |
| 21 | this.user = data.user |
| 22 | this.clientID = data.clientID |
| 23 | this.runID = data.runID |
| 24 | this.version = data.version || '' |
| 25 | this.wireProtocol = data.wireProtocol || '' |
| 26 | this.hostname = data.hostname |
| 27 | this.ip = data.clientIP || '' |
| 28 | this.firstConnectedAt = new Date(data.firstConnectedAt * 1000) |
| 29 | this.lastConnectedAt = new Date(data.lastConnectedAt * 1000) |
| 30 | if (data.disconnectedAt && data.disconnectedAt > 0) { |
| 31 | this.disconnectedAt = new Date(data.disconnectedAt * 1000) |
| 32 | } |
| 33 | this.online = data.online |
| 34 | this.status = data.status || { |
| 35 | phase: this.online ? 'online' : 'offline', |
| 36 | curConns: 0, |
| 37 | proxyCount: 0, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | get displayName(): string { |
| 42 | if (this.clientID) { |
| 43 | return this.user ? `${this.user}.${this.clientID}` : this.clientID |
| 44 | } |
| 45 | return this.runID |
| 46 | } |
| 47 | |
| 48 | get wireProtocolLabel(): string { |
| 49 | if (!this.wireProtocol) return '' |
| 50 | return `Protocol ${this.wireProtocol}` |
| 51 | } |
| 52 | |
| 53 | get firstConnectedAgo(): string { |
| 54 | return formatDistanceToNow(this.firstConnectedAt) |
| 55 | } |
| 56 | |
| 57 | get lastConnectedAgo(): string { |
| 58 | return formatDistanceToNow(this.lastConnectedAt) |
| 59 | } |
| 60 | |
| 61 | get disconnectedAgo(): string { |
| 62 | if (!this.disconnectedAt) return '' |
| 63 | return formatDistanceToNow(this.disconnectedAt) |
| 64 | } |
| 65 | } |