get-pairs-sequencially
sidebar_position: 7
getPairsSequentially Function
Overview
The getPairsSequentially
function processes a list of attendance log entries, identifies valid intervals of check-ins and check-outs based on provided duration and threshold criteria, and returns a structured result containing these intervals.
Function Signature
func getPairsSequentially(scanLogs []*entity.AttendanceLogTable, duration, treshold float64) IntervalPairs
Parameters
Parameter | Type | Description |
---|---|---|
scanLogs | []*entity.AttendanceLogTable | List of attendance log entries, each containing a timestamp. |
duration | float64 | Minimum allowed interval duration (in minutes) between a check-in and check-out. |
treshold | float64 | Minimum gap duration (in minutes) required between intervals to consider them as separate. |
Return Value
Type:IntervalPairs
A collection of interval structures representing valid check-in and check-out pairs along with metadata.
Detailed Logic
1. Initialization
- Create an empty list of raw pairs (
rawPairs
) to hold potential interval pairs. - Start with the first log as the initial reference (
rawPair
andlastOne
).
2. Pairing Logic
-
Iterate through
scanLogs
:- Parse timestamps of the current and previous logs (
scan.Timestamp
andlastOne.Timestamp
). - Calculate the time gap (
gapDuration
) between them. - Add to
rawPair
if:- The
gapDuration
meets theduration
criteria for appending to an existing pair. - The
gapDuration
meets thetreshold
criteria for starting a new pair.
- The
- Append completed
rawPair
torawPairs
when conditions are met, and resetrawPair
.
- Parse timestamps of the current and previous logs (
3. Processing Intervals
-
For each raw pair:
- Validate that it contains exactly two entries (check-in and check-out).
- Calculate the interval duration between check-in and check-out timestamps.
- Create
IntervalDuration
objects for check-in (ci
) and check-out (co
). - Create an
Intervals
object containing:- Metadata such as index, timestamps, duration, and calculation status.
- Flags for whether the entry is the first or last in the sequence.
- Apply additional validation:
- Mark intervals as invalid if the duration is below
duration
or the gap is belowtreshold
.
- Mark intervals as invalid if the duration is below
4. Flags and Results
- Mark the first valid interval as the first check-in (
ci.First = true
). - Mark the last valid interval as the last check-out (
co.Last = true
). - Append valid intervals to
results
.
Example Use Case
Input
scanLogs := []*entity.AttendanceLogTable{
{Timestamp: "2024-12-10T08:00:00Z"},
{Timestamp: "2024-12-10T12:00:00Z"},
{Timestamp: "2024-12-10T18:00:00Z"},
}
duration := 180.0 // 3 hours
treshold := 30.0 // 30 minutes
Output
The function returns a structured IntervalPairs
object containing valid check-in and check-out intervals based on the provided criteria.
Key Structs and Enums
IntervalPairs
A collection of Intervals
objects.
Intervals
Field | Type | Description |
---|---|---|
Index | int | Index of the interval in the sequence. |
Ci | *IntervalDuration | Metadata for the check-in log. |
Co | *IntervalDuration | Metadata for the check-out log. |
CiTime | string | Timestamp of the check-in log. |
CoTime | string | Timestamp of the check-out log. |
Duration | float64 | Interval duration (in minutes). |
IsCalculated | bool | Whether the interval meets calculation criteria. |
CalculatedReason | string | Reason why the interval was invalid, if applicable. |
IntervalDuration
Field | Type | Description |
---|---|---|
Log | *entity.AttendanceLogTable | Reference to the log entry. |
Candidate | string | Type of log ("ci" for check-in, "co" for check-out). |
DurationToNext | float64 | Duration to the next interval, for check-ins. |
PrevDuration | float64 | Duration to the previous interval, for check-outs. |
IntervalNum | int | Interval index in the sequence. |
Notes
- Edge Cases:
- Ensure
scanLogs
is not empty to avoid processing errors. - Handle cases where intervals overlap or fail validation due to duration or gap criteria.
- Ensure
- Performance:
- Efficiently manages memory by modifying
scanLogs
in place. - Uses sequential processing to maintain order.
- Efficiently manages memory by modifying
Feel free to extend this documentation or refine the formatting based on your team’s preferences.