Skip to content

Audit date filter/kira pilot #4845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
sql query
  • Loading branch information
Kira-Pilot committed Oct 28, 2022
commit 1f0cb46fa43ddf50cbf4d0a869f5ae5449cc0a87
10 changes: 10 additions & 0 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {

queryStr := r.URL.Query().Get("q")
filter, errs := auditSearchQuery(queryStr)
fmt.Println("BLOOP FILTER", filter)

if len(errs) > 0 {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Invalid audit search query.",
Expand All @@ -50,6 +52,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) {
Action: filter.Action,
Username: filter.Username,
Email: filter.Email,
TimeFrom: filter.TimeFrom,
})
if err != nil {
httpapi.InternalServerError(rw, err)
Expand Down Expand Up @@ -257,12 +260,17 @@ func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []coders
return database.GetAuditLogsOffsetParams{}, nil
}
query = strings.ToLower(query)
fmt.Println("KIRA query", query)
// Because we do this in 2 passes, we want to maintain quotes on the first
// pass.Further splitting occurs on the second pass and quotes will be
// dropped.
elements := splitQueryParameterByDelimiter(query, ' ', true)
fmt.Println("MARGE elements", elements)

for _, element := range elements {
parts := splitQueryParameterByDelimiter(element, ':', false)
fmt.Println("MARGOT parts", parts)

switch len(parts) {
case 1:
// No key:value pair.
Expand All @@ -285,6 +293,8 @@ func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []coders
Action: actionFromString(parser.String(searchParams, "", "action")),
Username: parser.String(searchParams, "", "username"),
Email: parser.String(searchParams, "", "email"),
TimeFrom: time.Date(
2022, 10, 27, 00, 00, 00, 000000000, time.UTC),
}

return filter, parser.Errors
Expand Down
10 changes: 9 additions & 1 deletion coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion coderd/database/queries/auditlogs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SELECT
users.avatar_url AS user_avatar_url
FROM
audit_logs
LEFT JOIN
LEFT JOIN
users ON audit_logs.user_id = users.id
WHERE
-- Filter resource_type
Expand Down Expand Up @@ -50,6 +50,12 @@ WHERE
users.email = @email
ELSE true
END
-- Filter by time_from
AND CASE
WHEN @time_from :: timestamp with time zone != '0001-01-01 00:00:00' THEN
"time" > @time_from
ELSE true
END
ORDER BY
"time" DESC
LIMIT
Expand Down
5 changes: 3 additions & 2 deletions enterprise/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"organization_id": ActionIgnore, // Never changes.
"avatar_url": ActionTrack,
},
// We don't show any diff for the WorkspaceBuild resource
// We don't show any diff for the WorkspaceBuild resource,
// save for the template_version_id
&database.WorkspaceBuild{}: {
"id": ActionIgnore,
"created_at": ActionIgnore,
"updated_at": ActionIgnore,
"workspace_id": ActionIgnore,
"template_version_id": ActionIgnore,
"template_version_id": ActionTrack,
"build_number": ActionIgnore,
"transition": ActionIgnore,
"initiator_id": ActionIgnore,
Expand Down
2 changes: 2 additions & 0 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,8 +563,10 @@ export const getAuditLogs = async (
searchParams.set("offset", options.offset.toString())
}
if (options.q) {
console.log('q', options.q)
searchParams.set("q", options.q)
}
console.log('searchParams', searchParams)

const response = await axios.get(`/api/v2/audit?${searchParams.toString()}`)
return response.data
Expand Down