Skip to content

Commit

Permalink
Merge pull request #197 from metal-stack/improved-audit-tableprinter
Browse files Browse the repository at this point in the history
Revisit audit table printers #192
  • Loading branch information
vknabel authored Jun 29, 2023
2 parents 7f2491f + 0d2a75b commit 218edcf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
37 changes: 18 additions & 19 deletions cmd/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,25 @@ func Test_AuditCmd_MultiResult(t *testing.T) {
auditTrace1,
},
wantTable: pointer.Pointer(`
TIME REQUEST ID DETAIL PATH CODE TENANT USER PHASE
May 19 01:03:03.000 b5817ef7-980a-41ef-9ed3-741a143870b0 POST /v1/audit 403 b-tenant b-user request
May 19 01:02:03.000 c40ad996-e1fd-4511-a7bf-418219cb8d91 GET /v1/audit 200 a-tenant a-user response
TIME REQUEST ID COMPONENT DETAIL PATH CODE USER
2022-05-19 01:03:03 b5817ef7-980a-41ef-9ed3-741a143870b0 test POST /v1/audit 403 b-user
2022-05-19 01:02:03 c40ad996-e1fd-4511-a7bf-418219cb8d91 example GET /v1/audit 200 a-user
`),
wantWideTable: pointer.Pointer(`
TIME REQUEST ID DETAIL PATH CODE TENANT USER PHASE BODY
May 19 01:03:03.000 b5817ef7-980a-41ef-9ed3-741a143870b0 POST /v1/audit 403 b-tenant b-user request {"c": "d"}
May 19 01:02:03.000 c40ad996-e1fd-4511-a7bf-418219cb8d91 GET /v1/audit 200 a-tenant a-user response {"a": "b"}
TIME REQUEST ID COMPONENT DETAIL PATH CODE USER TENANT BODY
2022-05-19 01:03:03 b5817ef7-980a-41ef-9ed3-741a143870b0 test POST /v1/audit 403 b-user b-tenant {"c": "d"}
2022-05-19 01:02:03 c40ad996-e1fd-4511-a7bf-418219cb8d91 example GET /v1/audit 200 a-user a-tenant {"a": "b"}
`),
template: pointer.Pointer(`{{ date "02/01/2006" .timestamp }} {{ .rqid }}`),
wantTemplate: pointer.Pointer(`
19/05/2022 b5817ef7-980a-41ef-9ed3-741a143870b0
19/05/2022 c40ad996-e1fd-4511-a7bf-418219cb8d91
`),
wantMarkdown: pointer.Pointer(`
| TIME | REQUEST ID | DETAIL | PATH | CODE | TENANT | USER | PHASE |
|---------------------|--------------------------------------|--------|-----------|------|----------|--------|----------|
| May 19 01:03:03.000 | b5817ef7-980a-41ef-9ed3-741a143870b0 | POST | /v1/audit | 403 | b-tenant | b-user | request |
| May 19 01:02:03.000 | c40ad996-e1fd-4511-a7bf-418219cb8d91 | GET | /v1/audit | 200 | a-tenant | a-user | response |
| TIME | REQUEST ID | COMPONENT | DETAIL | PATH | CODE | USER |
|---------------------|--------------------------------------|-----------|--------|-----------|------|--------|
| 2022-05-19 01:03:03 | b5817ef7-980a-41ef-9ed3-741a143870b0 | test | POST | /v1/audit | 403 | b-user |
| 2022-05-19 01:02:03 | c40ad996-e1fd-4511-a7bf-418219cb8d91 | example | GET | /v1/audit | 200 | a-user |
`),
},
{
Expand Down Expand Up @@ -155,22 +155,21 @@ May 19 01:02:03.000 c40ad996-e1fd-4511-a7bf-418219cb8d91 GET /v1/audit
auditTrace1,
},
wantTable: pointer.Pointer(`
TIME REQUEST ID DETAIL PATH CODE TENANT USER PHASE
May 19 01:02:03.000 c40ad996-e1fd-4511-a7bf-418219cb8d91 GET /v1/audit 200 a-tenant a-user response
TIME REQUEST ID COMPONENT DETAIL PATH CODE USER
2022-05-19 01:02:03 c40ad996-e1fd-4511-a7bf-418219cb8d91 example GET /v1/audit 200 a-user
`),
wantWideTable: pointer.Pointer(`
TIME REQUEST ID DETAIL PATH CODE TENANT USER PHASE BODY
May 19 01:02:03.000 c40ad996-e1fd-4511-a7bf-418219cb8d91 GET /v1/audit 200 a-tenant a-user response {"a": "b"}
`),
TIME REQUEST ID COMPONENT DETAIL PATH CODE USER TENANT BODY
2022-05-19 01:02:03 c40ad996-e1fd-4511-a7bf-418219cb8d91 example GET /v1/audit 200 a-user a-tenant {"a": "b"}
`),
template: pointer.Pointer(`{{ date "02/01/2006" .timestamp }} {{ .rqid }}`),
wantTemplate: pointer.Pointer(`
19/05/2022 c40ad996-e1fd-4511-a7bf-418219cb8d91
`),
wantMarkdown: pointer.Pointer(`
| TIME | REQUEST ID | DETAIL | PATH | CODE | TENANT | USER | PHASE |
|---------------------|--------------------------------------|--------|-----------|------|----------|--------|----------|
| May 19 01:02:03.000 | c40ad996-e1fd-4511-a7bf-418219cb8d91 | GET | /v1/audit | 200 | a-tenant | a-user | response |
`),
| TIME | REQUEST ID | COMPONENT | DETAIL | PATH | CODE | USER |
|---------------------|--------------------------------------|-----------|--------|-----------|------|--------|
| 2022-05-19 01:02:03 | c40ad996-e1fd-4511-a7bf-418219cb8d91 | example | GET | /v1/audit | 200 | a-user |`),
},
}
for _, tt := range tests {
Expand Down
17 changes: 11 additions & 6 deletions cmd/tableprinters/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
func (t *TablePrinter) AuditTable(data []*models.V1AuditResponse, wide bool) ([]string, [][]string, error) {
var (
rows [][]string
header = []string{"Time", "Request ID", "Detail", "Path", "Code", "Tenant", "User", "Phase"}
header = []string{"Time", "Request ID", "Component", "Detail", "Path", "Code", "User"}
)

if wide {
header = []string{"Time", "Request ID", "Detail", "Path", "Code", "Tenant", "User", "Phase", "Body"}
header = []string{"Time", "Request ID", "Component", "Detail", "Path", "Code", "User", "Tenant", "Body"}
}

for _, trace := range data {
Expand All @@ -23,18 +23,23 @@ func (t *TablePrinter) AuditTable(data []*models.V1AuditResponse, wide bool) ([]
statusCode = fmt.Sprintf("%d", trace.StatusCode)
}
row := []string{
time.Time(trace.Timestamp).Format(time.StampMilli),
time.Time(trace.Timestamp).Format(time.DateTime),
trace.Rqid,
trace.Component,
trace.Detail,
trace.Path,
statusCode,
trace.Tenant,
trace.User,
trace.Phase,
}

if wide {
row = append(row, trace.Body)
row = append(row, trace.Tenant)

body := trace.Body
if len(body) > 40 {
body = body[:37] + "..."
}
row = append(row, body)
}

rows = append(rows, row)
Expand Down

0 comments on commit 218edcf

Please sign in to comment.