refactor: ♻️ 批量查询ip归属地 (#164)

Co-authored-by: tk <fiyne1a@dingtalk.com>
This commit is contained in:
nsnail 2024-07-29 11:35:26 +08:00 committed by GitHub
parent 7c56c8d571
commit 2b4c25c07c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 131 additions and 73 deletions

View File

@ -38,7 +38,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
[Column]
[CsvIgnore]
[JsonIgnore]
public virtual int Duration { get; init; }
public virtual int Duration { get; protected init; }
/// <summary>
/// 程序响应码
@ -46,7 +46,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
[Column]
[CsvIgnore]
[JsonIgnore]
public virtual ErrorCodes ErrorCode { get; init; }
public virtual ErrorCodes ErrorCode { get; protected init; }
/// <summary>
/// HTTP状态码
@ -62,7 +62,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
[Column(Position = -1, DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_63)]
[CsvIgnore]
[JsonIgnore]
public virtual string LoginUserName { get; init; }
public virtual string LoginUserName { get; protected init; }
/// <summary>
/// 拥有者
@ -94,7 +94,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
#endif
[CsvIgnore]
[JsonIgnore]
public virtual string RequestBody { get; init; }
public virtual string RequestBody { get; protected init; }
/// <summary>
/// 请求头信息
@ -106,7 +106,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
#endif
[CsvIgnore]
[JsonIgnore]
public virtual string RequestHeaders { get; init; }
public virtual string RequestHeaders { get; protected init; }
/// <summary>
/// 请求地址
@ -114,7 +114,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
[Column(DbType = Chars.FLG_DB_FIELD_TYPE_VARCHAR_127)]
[CsvIgnore]
[JsonIgnore]
public virtual string RequestUrl { get; init; }
public virtual string RequestUrl { get; protected init; }
/// <summary>
/// 响应内容
@ -126,7 +126,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
#endif
[CsvIgnore]
[JsonIgnore]
public virtual string ResponseBody { get; init; }
public virtual string ResponseBody { get; protected init; }
/// <summary>
/// 响应头
@ -138,7 +138,7 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
#endif
[CsvIgnore]
[JsonIgnore]
public virtual string ResponseHeaders { get; init; }
public virtual string ResponseHeaders { get; protected init; }
/// <summary>
/// 服务器IP
@ -146,5 +146,5 @@ public record Sys_LoginLog : SimpleEntity, IFieldCreatedTime, IFieldOwner, IFiel
[Column]
[CsvIgnore]
[JsonIgnore]
public virtual int? ServerIp { get; init; }
public virtual int? ServerIp { get; protected init; }
}

View File

@ -42,6 +42,30 @@ public sealed record DynamicFilterInfo : DataAbstraction
return ret;
}
private static void ParseDateExp(FreeSql.Internal.Model.DynamicFilterInfo d)
{
var values = ((JsonElement)d.Value).Deserialize<string[]>();
if (!DateTime.TryParse(values[0], CultureInfo.InvariantCulture, out _)) {
var result = values[0]
.ExecuteCSharpCodeAsync<DateTime>([typeof(DateTime).Assembly], nameof(System))
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
values[0] = $"{result:yyyy-MM-dd HH:mm:ss}";
}
if (!DateTime.TryParse(values[1], CultureInfo.InvariantCulture, out _)) {
var result = values[1]
.ExecuteCSharpCodeAsync<DateTime>([typeof(DateTime).Assembly], nameof(System))
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
values[1] = $"{result:yyyy-MM-dd HH:mm:ss}";
}
d.Value = values;
}
private static void ProcessDynamicFilter(FreeSql.Internal.Model.DynamicFilterInfo d)
{
if (d?.Filters != null) {
@ -58,26 +82,7 @@ public sealed record DynamicFilterInfo : DataAbstraction
}
}
else if (d?.Operator == DynamicFilterOperator.DateRange) {
var values = ((JsonElement)d.Value).Deserialize<string[]>();
if (!DateTime.TryParse(values[0], CultureInfo.InvariantCulture, out _)) {
var result = values[0]
.ExecuteCSharpCodeAsync<DateTime>([typeof(DateTime).Assembly], nameof(System))
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
values[0] = $"{result:yyyy-MM-dd HH:mm:ss}";
}
if (!DateTime.TryParse(values[1], CultureInfo.InvariantCulture, out _)) {
var result = values[1]
.ExecuteCSharpCodeAsync<DateTime>([typeof(DateTime).Assembly], nameof(System))
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
values[1] = $"{result:yyyy-MM-dd HH:mm:ss}";
}
d.Value = values;
ParseDateExp(d);
}
}
}

View File

@ -47,7 +47,7 @@ public sealed record ExportLoginLogRsp : QueryLoginLogRsp
[CsvIgnore(false)]
[CsvIndex(2)]
[CsvName(nameof(Ln.登录名))]
public override string LoginUserName { get; init; }
public override string LoginUserName { get; protected init; }
/// <inheritdoc />
[CsvIgnore]

View File

@ -29,11 +29,11 @@ public record QueryLoginLogRsp : Sys_LoginLog
/// <inheritdoc cref="Sys_LoginLog.Duration" />
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public override int Duration { get; init; }
public override int Duration { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.ErrorCode" />
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public override ErrorCodes ErrorCode { get; init; }
public override ErrorCodes ErrorCode { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.HttpStatusCode" />
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
@ -45,7 +45,7 @@ public record QueryLoginLogRsp : Sys_LoginLog
/// <inheritdoc cref="Sys_LoginLog.LoginUserName" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string LoginUserName { get; init; }
public override string LoginUserName { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.Owner" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
@ -53,25 +53,25 @@ public record QueryLoginLogRsp : Sys_LoginLog
/// <inheritdoc cref="Sys_LoginLog.RequestBody" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string RequestBody { get; init; }
public override string RequestBody { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.RequestHeaders" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string RequestHeaders { get; init; }
public override string RequestHeaders { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.RequestUrl" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string RequestUrl { get; init; }
public override string RequestUrl { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.ResponseBody" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string ResponseBody { get; init; }
public override string ResponseBody { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.ResponseHeaders" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override string ResponseHeaders { get; init; }
public override string ResponseHeaders { get; protected init; }
/// <inheritdoc cref="Sys_LoginLog.ServerIp" />
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public override int? ServerIp { get; init; }
public override int? ServerIp { get; protected init; }
}

View File

@ -18,15 +18,14 @@ public sealed class ToolsService : ServiceBase<IToolsService>, IToolsService
/// <inheritdoc />
public Task<IEnumerable<GetModulesRsp>> GetModulesAsync()
{
return Task.FromResult<IEnumerable<GetModulesRsp>>(AppDomain.CurrentDomain.GetAssemblies()
.Select(x => {
var asm = x.GetName();
return new GetModulesRsp {
Name = asm.Name
, Version = asm.Version?.ToString()
};
})
.OrderBy(x => x.Name));
return Task.FromResult<IEnumerable<GetModulesRsp>>( //
AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.FullName?.Contains('#') != true && a.FullName?.Contains("DynamicMethods") != true)
.Select(x => {
var asm = x.GetName();
return new GetModulesRsp { Name = asm.Name, Version = asm.Version?.ToString() };
})
.OrderBy(x => x.Name));
}
/// <inheritdoc />

View File

@ -1,11 +1,32 @@
<template>
<el-drawer v-model="visible" :size="size" :title="title" @closed="$emit('closed')" destroy-on-close>
<el-main>
<el-descriptions :column="1" border class="font-monospace" size="small">
<el-main v-loading="!data" style="height: 100%">
<el-descriptions v-if="data" :column="1" border class="font-monospace" size="small">
<el-descriptions-item v-for="(item, i) in data" :key="i" :label="i" label-class-name="w15">
{{ item }}
</el-descriptions-item>
</el-descriptions>
<el-empty v-if="esData && !esData.hits" v-loading="true"></el-empty>
<el-descriptions
v-for="(desc, i) in esData.hits.hits.map((x) => x._source)"
v-if="esData?.hits?.hits?.length > 0"
:column="1"
:key="i"
class="trace-log mt-8"
size="small">
<el-descriptions-item
v-for="(item, j) in Object.entries(desc)
.filter((x) => ['@timestamp', 'log_level', 'log_thread', 'log_source', 'log_message'].includes(x[0]))
.sort()"
:key="j"
:label="item[0]"
label-class-name="w15">
<span v-if="item[0] === '@timestamp'">
{{ $TOOL.dateFormat(item[1]) }}
</span>
<span v-else v-html="$TOOL.highLightKeywords($TOOL.unicodeDecode(item[1].toString()))" />
</el-descriptions-item>
</el-descriptions>
</el-main>
</el-drawer>
</template>
@ -18,16 +39,27 @@ export default {
},
data() {
return {
title: '',
title: null,
visible: false,
data: {},
esData: null,
data: null,
}
},
methods: {
open(data, title) {
this.title = title
this.data = data
async open(title, query, queryEs) {
this.data = null
this.title = null
this.esData = null
this.visible = true
const res = await query()
this.title = title(res.data)
this.data = this.$TOOL.sortProperties(res.data)
if (queryEs) {
this.esData = {}
this.$API.adm_tools.queryEsLog.post(queryEs).then((res) => {
this.esData = res.data
})
}
return this
},
},

View File

@ -57,7 +57,6 @@ import naColUser from '@/components/naColUser/index.vue'
import naDept from '@/components/naDept/index.vue'
import naDicCatalog from '@/components/naDicCatalog/index.vue'
import naFormEmail from '@/components/naFormEmail/index.vue'
import naIp from '@/components/naIp/index.vue'
import naSearch from '@/components/naSearch'
import naUserSelect from '@/components/naUserSelect/index.vue'
@ -97,7 +96,6 @@ export default {
app.component('naDept', naDept)
app.component('naDicCatalog', naDicCatalog)
app.component('naFormEmail', naFormEmail)
app.component('naIp', naIp)
app.component('naSearch', naSearch)
app.component('naUserSelect', naUserSelect)

View File

@ -121,12 +121,12 @@ export default {
this.getData()
},
async rowClick(row) {
this.loading = true
const res = await this.$API.sys_cache.getEntry.post({ key: row.key })
this.dialog.info = true
await this.$nextTick()
this.$refs.info.open(this.$TOOL.sortProperties(res.data), this.$t('缓存详情'))
this.loading = false
this.$refs.info.open(
() => this.$t('缓存详情'),
() => this.$API.sys_cache.getEntry.post({ key: row.key }),
)
},
async getData() {
this.loading = true

View File

@ -48,6 +48,7 @@
:params="query"
:query-api="$API.sys_loginlog.pagedQuery"
:vue="this"
@data-change="dataChange"
ref="table"
remote-filter
remote-sort
@ -63,7 +64,8 @@
<el-table-column :label="$t('登录名')" prop="loginUserName" sortable="custom" width="150" />
<el-table-column :label="$t('客户端IP')" prop="createdClientIp" show-overflow-tooltip sortable="custom" width="200">
<template #default="{ row }">
<na-ip :ip="row.createdClientIp"></na-ip>
<p>{{ row.createdClientIp }}</p>
<p>{{ this.ips.filter((x) => x.ip === row.createdClientIp)[0]?.region ?? '...' }}</p>
</template>
</el-table-column>
<el-table-column :label="$t('操作系统')" align="center" prop="os" width="150" />
@ -86,6 +88,7 @@
<script>
import naInfo from '@/components/naInfo/index.vue'
import http from '@/utils/request'
export default {
components: {
@ -98,6 +101,7 @@ export default {
dialog: {
info: false,
},
ips: [],
loading: false,
query: {
dynamicFilter: {
@ -111,6 +115,14 @@ export default {
},
inject: ['reload'],
methods: {
async dataChange(data) {
this.apis = []
const ips = data.data.rows?.map((x) => x.createdClientIp)
const res = await Promise.all([
ips && ips.length > 0 ? http.get(`http://ip.line92.xyz/?ip=${ips.join('&ip=')}`) : new Promise((x) => x({ data: [] })),
])
this.ips = res[0]
},
filterChange(data) {
Object.entries(data).forEach(([key, value]) => {
this.$refs.search.form.dy[key] = value === 'true' ? true : value === 'false' ? false : value
@ -151,10 +163,13 @@ export default {
async rowClick(row) {
this.dialog.info = true
await this.$nextTick()
const res = await this.$API.sys_loginlog.get.post({
id: row.id,
})
this.$refs.info.open(this.$TOOL.sortProperties(res.data), this.$t('日志详情:{id}', { id: row.id }))
await this.$refs.info.open(
() => this.$t('日志详情:{id}', { id: row.id }),
() =>
this.$API.sys_loginlog.get.post({
id: row.id,
}),
)
},
},
mounted() {

View File

@ -134,7 +134,8 @@
</el-table-column>
<el-table-column :label="$t('客户端IP')" prop="createdClientIp" show-overflow-tooltip sortable="custom" width="200">
<template #default="{ row }">
<na-ip :ip="row.createdClientIp"></na-ip>
<p>{{ row.createdClientIp }}</p>
<p>{{ this.ips.filter((x) => x.ip === row.createdClientIp)[0]?.region ?? '...' }}</p>
</template>
</el-table-column>
<na-col-operation
@ -161,7 +162,7 @@
<script>
import { defineAsyncComponent } from 'vue'
import http from '@/utils/request'
const saveDialog = defineAsyncComponent(() => import('@/views/sys/user/save.vue'))
import naInfo from '@/components/naInfo/index.vue'
@ -183,6 +184,7 @@ export default {
},
owners: [],
apis: [],
ips: [],
loading: false,
query: {
dynamicFilter: {
@ -213,6 +215,7 @@ export default {
this.apis = []
const ownerIds = data.data.rows?.filter((x) => x.ownerId).map((x) => x.ownerId)
const apiCrcs = data.data.rows?.map((x) => x.apiPathCrc32)
const ips = data.data.rows?.map((x) => x.createdClientIp)
const res = await Promise.all([
ownerIds && ownerIds.length > 0
? this.$API.sys_user.query.post({
@ -233,9 +236,12 @@ export default {
},
})
: new Promise((x) => x({ data: [] })),
ips && ips.length > 0 ? http.get(`http://ip.line92.xyz/?ip=${ips.join('&ip=')}`) : new Promise((x) => x({ data: [] })),
])
this.owners = res[0].data
this.apis = res[1].data
this.ips = res[2]
},
filterChange(data) {
Object.entries(data).forEach(([key, value]) => {
@ -297,11 +303,14 @@ export default {
async rowClick(row) {
this.dialog.info = true
await this.$nextTick()
const res = await this.$API.sys_requestlog.get.post({
id: row.id,
createdTime: row.createdTime,
})
this.$refs.info.open(this.$TOOL.sortProperties(res.data), this.$t('日志详情:{id}', { id: row.id }))
await this.$refs.info.open(
() => this.$t('日志详情:{id}', { id: row.id }),
() =>
this.$API.sys_requestlog.get.post({
id: row.id,
createdTime: row.createdTime,
}),
)
},
},
mounted() {