-
Notifications
You must be signed in to change notification settings - Fork 0
Filter by request activity #19
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,11 @@ type rawClockifyEnvelope struct { | |
| Requests []json.RawMessage `json:"requests"` | ||
| } | ||
|
|
||
| type createdOnly struct { | ||
| type requestTimestamps struct { | ||
| CreatedAt string `json:"createdAt"` | ||
| Status struct { | ||
| ChangedAt string `json:"changedAt"` | ||
| } `json:"status"` | ||
|
Comment on lines
+16
to
+18
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there ever a case where we won't send this in a request? If so, change this to: Note the change to a pointer type and the If not, leave as-is.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the clockify responses I've seen, the |
||
| } | ||
|
dacut marked this conversation as resolved.
|
||
|
|
||
| func ParseRawClockifyEnvelope(respBytes []byte) (rawClockifyEnvelope, error) { | ||
|
|
@@ -23,65 +26,74 @@ func ParseRawClockifyEnvelope(respBytes []byte) (rawClockifyEnvelope, error) { | |
| return env, nil | ||
| } | ||
|
|
||
| // Filters raw request payloads by createdAt in [start, end]. | ||
| func FilterRawRequestsByCreatedAt( | ||
| rawRequests []json.RawMessage, | ||
| start, end time.Time, | ||
| ) []json.RawMessage { | ||
|
|
||
| filtered := make([]json.RawMessage, 0, len(rawRequests)) | ||
| // ParseClockifyRequests converts valid raw request payloads into ClockifyRequest structs and skips malformed JSON entries, logging each unmarshal error via log.Printf. | ||
| func ParseClockifyRequests(rawRequests []json.RawMessage) []ClockifyRequest { | ||
| requests := make([]ClockifyRequest, 0, len(rawRequests)) | ||
|
|
||
| for _, raw := range rawRequests { | ||
| var c createdOnly | ||
| if err := json.Unmarshal(raw, &c); err != nil { | ||
| continue // skip if no createdAt | ||
| } | ||
|
|
||
| ct, err := ParseFlexibleRFC3339(c.CreatedAt) | ||
| if err != nil { | ||
| var r ClockifyRequest | ||
| if err := json.Unmarshal(raw, &r); err != nil { | ||
| log.Printf("skipping bad request: %v", err) | ||
| continue | ||
| } | ||
| ct = ct.UTC() | ||
|
|
||
| if ct.Before(start) { | ||
| continue | ||
| } | ||
| if !ct.Before(end) { // exclusive | ||
| continue | ||
| } | ||
| requests = append(requests, r) | ||
| } | ||
|
|
||
| return requests | ||
| } | ||
|
|
||
| filtered = append(filtered, raw) | ||
| func isTimestampInWindow( | ||
| value string, | ||
| start, end time.Time, | ||
| ) bool { | ||
| timestamp, err := ParseFlexibleRFC3339(value) | ||
| if err != nil { | ||
| return false | ||
| } | ||
| return filtered | ||
|
|
||
| timestamp = timestamp.UTC() | ||
|
|
||
| return !timestamp.Before(start) && timestamp.Before(end) | ||
| } | ||
|
|
||
| // ParseClockifyRequests converts valid raw request payloads into ClockifyRequest structs and skips malformed JSON entries, logging each unmarshal error via log.Printf. | ||
| func ParseClockifyRequests(rawRequests []json.RawMessage) []ClockifyRequest { | ||
| requests := make([]ClockifyRequest, 0, len(rawRequests)) | ||
| // Filters raw requests that were created or had their status updated in [start, end). | ||
| func FilterRawRequestsByActivity( | ||
| rawRequests []json.RawMessage, | ||
| start, end time.Time, | ||
| ) []json.RawMessage { | ||
| filtered := make([]json.RawMessage, 0, len(rawRequests)) | ||
|
|
||
| for _, raw := range rawRequests { | ||
| var r ClockifyRequest | ||
| if err := json.Unmarshal(raw, &r); err != nil { | ||
| log.Printf("skipping bad request: %v", err) | ||
| var timestamps requestTimestamps | ||
| if err := json.Unmarshal(raw, ×tamps); err != nil { | ||
| continue | ||
| } | ||
|
|
||
| requests = append(requests, r) | ||
| createdInWindow := isTimestampInWindow(timestamps.CreatedAt, start, end) | ||
|
|
||
| statusUpdatedInWindow := isTimestampInWindow(timestamps.Status.ChangedAt, start, end) | ||
|
|
||
| if createdInWindow || statusUpdatedInWindow { | ||
| filtered = append(filtered, raw) | ||
| } | ||
| } | ||
|
|
||
| return requests | ||
| return filtered | ||
| } | ||
|
|
||
| // Filter a raw Clockify response for out-of-office requests that were created within the given time span. | ||
| func FilterByCreatedAt(respBytes []byte, createdStart, createdEnd time.Time) (ClockifyEnvelope, error) { | ||
| // FilterByActivity returns Clockify requests that were created or had their | ||
| // status updated within the given time span. | ||
| func FilterByActivity( | ||
| respBytes []byte, | ||
| start, end time.Time, | ||
| ) (ClockifyEnvelope, error) { | ||
| rawEnv, err := ParseRawClockifyEnvelope(respBytes) | ||
| if err != nil { | ||
| return ClockifyEnvelope{}, err | ||
| } | ||
|
|
||
| filtered := FilterRawRequestsByCreatedAt(rawEnv.Requests, createdStart, createdEnd) | ||
| filtered := FilterRawRequestsByActivity(rawEnv.Requests, start, end) | ||
|
|
||
| return ClockifyEnvelope{ | ||
| Requests: ParseClockifyRequests(filtered), | ||
| }, nil | ||
| return ClockifyEnvelope{Requests: ParseClockifyRequests(filtered)}, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.