diff --git a/packages/console/src/components/AuditLogTable/components/EventName/index.tsx b/packages/console/src/components/AuditLogTable/components/EventName/index.tsx index c7284ccba..896690c74 100644 --- a/packages/console/src/components/AuditLogTable/components/EventName/index.tsx +++ b/packages/console/src/components/AuditLogTable/components/EventName/index.tsx @@ -8,13 +8,13 @@ import { logEventTitle } from '@/consts/logs'; import * as styles from './index.module.scss'; type Props = { - type: string; + eventKey: string; isSuccess: boolean; to?: string; }; -const EventName = ({ type, isSuccess, to }: Props) => { - const title = logEventTitle[type] ?? type; +const EventName = ({ eventKey, isSuccess, to }: Props) => { + const title = logEventTitle[eventKey] ?? eventKey; return (
diff --git a/packages/console/src/components/AuditLogTable/index.tsx b/packages/console/src/components/AuditLogTable/index.tsx index ffe94e2a4..87852e6a4 100644 --- a/packages/console/src/components/AuditLogTable/index.tsx +++ b/packages/console/src/components/AuditLogTable/index.tsx @@ -115,7 +115,7 @@ const AuditLogTable = ({ userId }: Props) => { )} {isLoading && } {logs?.length === 0 && } - {logs?.map(({ type, payload, createdAt, id }) => ( + {logs?.map(({ key, payload, createdAt, id }) => ( { }} > - + {showUserColumn && ( {payload.userId ? : '-'} diff --git a/packages/console/src/consts/logs.ts b/packages/console/src/consts/logs.ts index 83a7d3bb8..c9598dc6c 100644 --- a/packages/console/src/consts/logs.ts +++ b/packages/console/src/consts/logs.ts @@ -1,6 +1,9 @@ +import type { LogKey } from '@logto/schemas'; + type LogEventTitle = Record; -export const logEventTitle: LogEventTitle = Object.freeze({ +/** @deprecated Don't use or update. */ +const logEventTitleLegacy: LogEventTitle = Object.freeze({ RegisterUsernamePassword: 'Register with username and password', RegisterEmailSendPasscode: 'Register with email (send passcode)', RegisterEmail: 'Register with email', @@ -19,3 +22,12 @@ export const logEventTitle: LogEventTitle = Object.freeze({ RefreshTokenExchangeToken: 'Exchange token by refresh token', RevokeToken: 'Revoke token', }); + +export const logEventTitle: Record & Partial> = + Object.freeze({ + ...logEventTitleLegacy, + 'ExchangeTokenBy.AuthorizationCode': 'Exchange token by auth code', + 'ExchangeTokenBy.RefreshToken': 'Exchange token by refresh token', + 'Interaction.Create': 'Interaction started', + 'Interaction.End': 'Interaction ended', + }); diff --git a/packages/console/src/pages/AuditLogDetails/index.tsx b/packages/console/src/pages/AuditLogDetails/index.tsx index de82e18d2..b00a3f805 100644 --- a/packages/console/src/pages/AuditLogDetails/index.tsx +++ b/packages/console/src/pages/AuditLogDetails/index.tsx @@ -57,11 +57,11 @@ const AuditLogDetails = () => {
-
{logEventTitle[data.type]}
+
{logEventTitle[data.key]}
-
{t('log_details.event_type')}
-
{data.type}
+
{t('log_details.event_key')}
+
{data.key}
{t('log_details.application')}
diff --git a/packages/core/src/middleware/koa-audit-log-legacy.ts b/packages/core/src/middleware/koa-audit-log-legacy.ts index a1469caba..553cc86ef 100644 --- a/packages/core/src/middleware/koa-audit-log-legacy.ts +++ b/packages/core/src/middleware/koa-audit-log-legacy.ts @@ -71,7 +71,7 @@ const initLogger = (basePayload?: Readonly) => { await insertLog({ id: nanoid(), - type: logger.type, + key: logger.type, payload: { ...logger.basePayload, ...logger.payload, diff --git a/packages/core/src/middleware/koa-audit-log.ts b/packages/core/src/middleware/koa-audit-log.ts index 50916395a..1fe378be6 100644 --- a/packages/core/src/middleware/koa-audit-log.ts +++ b/packages/core/src/middleware/koa-audit-log.ts @@ -139,7 +139,7 @@ export default function koaAuditLog< entries.map(async ({ payload }) => { return insertLog({ id: nanoid(), - type: payload.key, + key: payload.key, payload: { ip, userAgent, ...payload }, }); }) diff --git a/packages/core/src/queries/log.ts b/packages/core/src/queries/log.ts index 2b3acffac..feb9de78c 100644 --- a/packages/core/src/queries/log.ts +++ b/packages/core/src/queries/log.ts @@ -20,7 +20,7 @@ export type LogCondition = { const buildLogConditionSql = (logCondition: LogCondition) => conditionalSql(logCondition, ({ logType, applicationId, userId }) => { const subConditions = [ - conditionalSql(logType, (logType) => sql`${fields.type}=${logType}`), + conditionalSql(logType, (logType) => sql`${fields.key}=${logType}`), conditionalSql(userId, (userId) => sql`${fields.payload}->>'userId'=${userId}`), conditionalSql( applicationId, @@ -59,7 +59,7 @@ export const getDailyActiveUserCountsByTimeInterval = async ( from ${table} where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000) and ${fields.createdAt} <= to_timestamp(${endTimeInclusive}::double precision / 1000) - and ${fields.type} like ${`${token.Flow.ExchangeTokenBy}.%`} + and ${fields.key} like ${`${token.Flow.ExchangeTokenBy}.%`} and ${fields.payload}->>'result' = 'Success' group by date(${fields.createdAt}) `); @@ -73,6 +73,6 @@ export const countActiveUsersByTimeInterval = async ( from ${table} where ${fields.createdAt} > to_timestamp(${startTimeExclusive}::double precision / 1000) and ${fields.createdAt} <= to_timestamp(${endTimeInclusive}::double precision / 1000) - and ${fields.type} like ${`${token.Flow.ExchangeTokenBy}.%`} + and ${fields.key} like ${`${token.Flow.ExchangeTokenBy}.%`} and ${fields.payload}->>'result' = 'Success' `); diff --git a/packages/integration-tests/src/tests/api/logs-legacy.test.ts b/packages/integration-tests/src/tests/api/logs-legacy.test.ts index c970ad27b..1ba6f4e23 100644 --- a/packages/integration-tests/src/tests/api/logs-legacy.test.ts +++ b/packages/integration-tests/src/tests/api/logs-legacy.test.ts @@ -20,7 +20,7 @@ describe('admin console logs (legacy)', () => { const logs = await getLogs(); const registerLog = logs.filter( - ({ type, payload }) => type === 'RegisterUsernamePassword' && payload.username === username + ({ key, payload }) => key === 'RegisterUsernamePassword' && payload.username === username ); expect(registerLog.length).toBeGreaterThan(0); diff --git a/packages/phrases/src/locales/de/translation/admin-console/log-details.ts b/packages/phrases/src/locales/de/translation/admin-console/log-details.ts index 056530189..8e5a314e7 100644 --- a/packages/phrases/src/locales/de/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/de/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: 'Zurück zu {{name}}', success: 'Erfolgreich', failed: 'Fehlgeschlagen', - event_type: 'Event Typ', + event_key: 'Event Key', // UNTRANSLATED application: 'Anwendung', ip_address: 'IP Adresse', user: 'Benutzer', diff --git a/packages/phrases/src/locales/en/translation/admin-console/log-details.ts b/packages/phrases/src/locales/en/translation/admin-console/log-details.ts index f864c564d..24e490a16 100644 --- a/packages/phrases/src/locales/en/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/en/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: 'Back to {{name}}', success: 'Success', failed: 'Failed', - event_type: 'Event type', + event_key: 'Event Key', application: 'Application', ip_address: 'IP address', user: 'User', diff --git a/packages/phrases/src/locales/fr/translation/admin-console/log-details.ts b/packages/phrases/src/locales/fr/translation/admin-console/log-details.ts index 3627ecc13..91e300da4 100644 --- a/packages/phrases/src/locales/fr/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/fr/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: 'Retour à {{name}}', success: 'Succès', failed: 'Échoué', - event_type: "Type d'événement", + event_key: 'Event Key', // UNTRANSLATED application: 'Application', ip_address: 'Addresse IP', user: 'Utilisateur', diff --git a/packages/phrases/src/locales/ko/translation/admin-console/log-details.ts b/packages/phrases/src/locales/ko/translation/admin-console/log-details.ts index b03e6e8ad..3f99109b9 100644 --- a/packages/phrases/src/locales/ko/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/ko/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: '{{name}}으로 돌아가기', success: '성공', failed: '실패', - event_type: '활동 종류', + event_key: 'Event Key', // UNTRANSLATED application: '어플리케이션', ip_address: 'IP 주소', user: '사용자', diff --git a/packages/phrases/src/locales/pt-br/translation/admin-console/log-details.ts b/packages/phrases/src/locales/pt-br/translation/admin-console/log-details.ts index a5f250808..83e903f5b 100644 --- a/packages/phrases/src/locales/pt-br/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/pt-br/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: 'Voltar para {{name}}', success: 'Sucesso', failed: 'Falhou', - event_type: 'Tipo de evento', + event_key: 'Event Key', // UNTRANSLATED application: 'Aplicativo', ip_address: 'Endereço de IP', user: 'Usuário', diff --git a/packages/phrases/src/locales/pt-pt/translation/admin-console/log-details.ts b/packages/phrases/src/locales/pt-pt/translation/admin-console/log-details.ts index 71c197a1c..1f6293804 100644 --- a/packages/phrases/src/locales/pt-pt/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/pt-pt/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: 'De volta a {{name}}', success: 'Sucesso', failed: 'Falha', - event_type: 'Tipo de evento', + event_key: 'Event Key', // UNTRANSLATED application: 'Aplicação', ip_address: 'Endereço IP', user: 'Utilizador', diff --git a/packages/phrases/src/locales/tr-tr/translation/admin-console/log-details.ts b/packages/phrases/src/locales/tr-tr/translation/admin-console/log-details.ts index 3a45a6bbd..ecd278594 100644 --- a/packages/phrases/src/locales/tr-tr/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/tr-tr/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: '{{name}}e geri dön', success: 'Başarılı', failed: 'Başarısız', - event_type: 'Etkinlik tipi', + event_key: 'Event Key', // UNTRANSLATED application: 'Uygulama', ip_address: 'IP adresi', user: 'Kullanıcı', diff --git a/packages/phrases/src/locales/zh-cn/translation/admin-console/log-details.ts b/packages/phrases/src/locales/zh-cn/translation/admin-console/log-details.ts index 0df24289d..f0a78276e 100644 --- a/packages/phrases/src/locales/zh-cn/translation/admin-console/log-details.ts +++ b/packages/phrases/src/locales/zh-cn/translation/admin-console/log-details.ts @@ -3,7 +3,7 @@ const log_details = { back_to_user: '返回 {{name}}', success: '成功', failed: '失败', - event_type: '事件类型', + event_key: 'Event Key', // UNTRANSLATED application: '应用', ip_address: 'IP 地址', user: '用户', diff --git a/packages/schemas/alterations/next-1671336831-refactor-log-types.ts b/packages/schemas/alterations/next-1671336831-refactor-log-types.ts new file mode 100644 index 000000000..7e689fb06 --- /dev/null +++ b/packages/schemas/alterations/next-1671336831-refactor-log-types.ts @@ -0,0 +1,32 @@ +import { sql } from 'slonik'; + +import type { AlterationScript } from '../lib/types/alteration.js'; + +const alteration: AlterationScript = { + up: async (pool) => { + await pool.query(sql` + -- Update metadata + alter table logs rename column type to key; + alter table logs alter column key type varchar(128); + alter index logs__type rename to logs__key; + + -- Update token exchange keys + update logs set "key" = 'ExchangeTokenBy.AuthorizationCode' where "key" = 'CodeExchangeToken'; + update logs set "key" = 'ExchangeTokenBy.RefreshToken' where "key" = 'RefreshTokenExchangeToken'; + `); + }, + down: async (pool) => { + await pool.query(sql` + -- Update token exchange keys + update logs set "key" = 'CodeExchangeToken' where "key" = 'ExchangeTokenBy.AuthorizationCode'; + update logs set "key" = 'RefreshTokenExchangeToken' where "key" = 'ExchangeTokenBy.RefreshToken'; + + -- Update metadata + alter table logs alter column key type varchar(64); + alter table logs rename column key to type; + alter index logs__key rename to logs__type; + `); + }, +}; + +export default alteration; diff --git a/packages/schemas/tables/logs.sql b/packages/schemas/tables/logs.sql index e01313261..7c5344995 100644 --- a/packages/schemas/tables/logs.sql +++ b/packages/schemas/tables/logs.sql @@ -1,13 +1,13 @@ create table logs ( id varchar(21) not null, - type varchar(64) not null, + key varchar(128) not null, payload jsonb /* @use ArbitraryObject */ not null default '{}'::jsonb, created_at timestamptz not null default (now()), primary key (id) ); -create index logs__type on logs (type); +create index logs__key on logs (key); create index logs__created_at on logs (created_at); create index logs__user_id on logs ((payload->>'user_id') nulls last); create index logs__application_id on logs ((payload->>'application_id') nulls last);