feat: sort of log file list

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2022-08-16 22:55:25 +08:00
parent d420333af2
commit 12037aaa54
4 changed files with 67 additions and 37 deletions

View File

@@ -137,9 +137,12 @@ export class HealthController {
filename: string
type: string
index: number
created: number
}[]
for (const [i, file] of Object.entries(allFile)) {
const byteSize = fs.statSync(path.join(logDir, file)).size
const stat = await fs.stat(path.join(logDir, file))
const byteSize = stat.size
const size = formatByteSize(byteSize)
let index: number
let _type: string
@@ -154,10 +157,16 @@ export class HealthController {
index = +i
break
}
res.push({ size, filename: file, index, type: _type })
res.push({
size,
filename: file,
index,
type: _type,
created: stat.ctimeMs,
})
}
return res
return res.sort((a, b) => b.created - a.created)
}
@Get('/log/:type')
@@ -183,6 +192,7 @@ export class HealthController {
if (!fs.existsSync(logPath)) {
throw new BadRequestException('log file not exists')
}
stream = fs.createReadStream(logPath, {
encoding: 'utf8',
})

View File

@@ -6,9 +6,11 @@ import {
BadRequestException,
Injectable,
InternalServerErrorException,
Logger,
UnprocessableEntityException,
} from '@nestjs/common'
import { BizException } from '~/common/exceptions/biz.exception'
import { HttpService } from '~/processors/helper/helper.http.service'
import { ConfigsService } from '../configs/configs.service'
@@ -16,10 +18,13 @@ import { IP } from './tool.interface'
@Injectable()
export class ToolService {
private readonly logger: Logger
constructor(
private readonly httpService: HttpService,
private readonly configs: ConfigsService,
) {}
) {
this.logger = new Logger(ToolService.name)
}
async getIp(ip: string, timeout = 3000): Promise<IP> {
const isV4 = isIPv4(ip)
@@ -27,34 +32,37 @@ export class ToolService {
if (!isV4 && !isV6) {
throw new UnprocessableEntityException('Invalid IP')
}
try {
if (isV4) {
const { data } = await this.httpService.axiosRef.get(
`https://api.i-meto.com/ip/v1/qqwry/${ip}`,
{
timeout,
},
)
if (isV4) {
const { data } = await this.httpService.axiosRef.get(
`https://api.i-meto.com/ip/v1/qqwry/${ip}`,
{
timeout,
},
)
return camelcaseKeys(data, { deep: true }) as IP
} else {
const { data } = (await this.httpService.axiosRef.get(
`http://ip-api.com/json/${ip}`,
{
timeout,
},
)) as any
return camelcaseKeys(data, { deep: true }) as IP
} else {
const { data } = (await this.httpService.axiosRef.get(
`http://ip-api.com/json/${ip}`,
{
timeout,
},
)) as any
const res = {
cityName: data.city,
countryName: data.country,
ip: data.query,
ispDomain: data.as,
ownerDomain: data.org,
regionName: data.region_name,
} as const
const res = {
cityName: data.city,
countryName: data.country,
ip: data.query,
ispDomain: data.as,
ownerDomain: data.org,
regionName: data.region_name,
} as const
return res
return res
}
} catch (e) {
throw new BizException(`IP API 调用失败, ${e.message}`)
}
}