| 87 | } |
| 88 | |
| 89 | func ConnectionLogs(ctx context.Context, db database.Store, query string, apiKey database.APIKey) (database.GetConnectionLogsOffsetParams, database.CountConnectionLogsParams, []codersdk.ValidationError) { |
| 90 | // Always lowercase for all searches. |
| 91 | query = strings.ToLower(query) |
| 92 | values, errors := searchTerms(query, func(term string, values url.Values) error { |
| 93 | values.Add("search", term) |
| 94 | return nil |
| 95 | }) |
| 96 | if len(errors) > 0 { |
| 97 | // nolint:exhaustruct // We don't need to initialize these structs because we return an error. |
| 98 | return database.GetConnectionLogsOffsetParams{}, database.CountConnectionLogsParams{}, errors |
| 99 | } |
| 100 | |
| 101 | parser := httpapi.NewQueryParamParser() |
| 102 | filter := database.GetConnectionLogsOffsetParams{ |
| 103 | OrganizationID: parseOrganization(ctx, db, parser, values, "organization"), |
| 104 | WorkspaceOwner: parser.String(values, "", "workspace_owner"), |
| 105 | WorkspaceOwnerEmail: parser.String(values, "", "workspace_owner_email"), |
| 106 | Type: string(httpapi.ParseCustom(parser, values, "", "type", httpapi.ParseEnum[database.ConnectionType])), |
| 107 | Username: parser.String(values, "", "username"), |
| 108 | UserEmail: parser.String(values, "", "user_email"), |
| 109 | ConnectedAfter: parser.Time3339Nano(values, time.Time{}, "connected_after"), |
| 110 | ConnectedBefore: parser.Time3339Nano(values, time.Time{}, "connected_before"), |
| 111 | WorkspaceID: parser.UUID(values, uuid.Nil, "workspace_id"), |
| 112 | ConnectionID: parser.UUID(values, uuid.Nil, "connection_id"), |
| 113 | Status: string(httpapi.ParseCustom(parser, values, "", "status", httpapi.ParseEnum[codersdk.ConnectionLogStatus])), |
| 114 | } |
| 115 | |
| 116 | if filter.Username == "me" { |
| 117 | filter.UserID = apiKey.UserID |
| 118 | filter.Username = "" |
| 119 | } |
| 120 | |
| 121 | if filter.WorkspaceOwner == "me" { |
| 122 | filter.WorkspaceOwnerID = apiKey.UserID |
| 123 | filter.WorkspaceOwner = "" |
| 124 | } |
| 125 | |
| 126 | // This MUST be kept in sync with the above |
| 127 | // nolint:exhaustruct // CountCap is not obtained from the query parameters. |
| 128 | countFilter := database.CountConnectionLogsParams{ |
| 129 | OrganizationID: filter.OrganizationID, |
| 130 | WorkspaceOwner: filter.WorkspaceOwner, |
| 131 | WorkspaceOwnerID: filter.WorkspaceOwnerID, |
| 132 | WorkspaceOwnerEmail: filter.WorkspaceOwnerEmail, |
| 133 | Type: filter.Type, |
| 134 | UserID: filter.UserID, |
| 135 | Username: filter.Username, |
| 136 | UserEmail: filter.UserEmail, |
| 137 | ConnectedAfter: filter.ConnectedAfter, |
| 138 | ConnectedBefore: filter.ConnectedBefore, |
| 139 | WorkspaceID: filter.WorkspaceID, |
| 140 | ConnectionID: filter.ConnectionID, |
| 141 | Status: filter.Status, |
| 142 | } |
| 143 | parser.ErrorExcessParams(values) |
| 144 | return filter, countFilter, parser.Errors |
| 145 | } |
| 146 | |