Skip to main content

get-pairs-sequencially


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

ParameterTypeDescription
scanLogs[]*entity.AttendanceLogTableList of attendance log entries, each containing a timestamp.
durationfloat64Minimum allowed interval duration (in minutes) between a check-in and check-out.
tresholdfloat64Minimum gap duration (in minutes) required between intervals to consider them as separate.

Return Value


  • Type: IntervalPairsA 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 and lastOne).

2. Pairing Logic

  • Iterate through scanLogs:

    1. Parse timestamps of the current and previous logs (scan.Timestamp and lastOne.Timestamp).
    2. Calculate the time gap (gapDuration) between them.
    3. Add to rawPair if:
      • The gapDuration meets the duration criteria for appending to an existing pair.
      • The gapDuration meets the treshold criteria for starting a new pair.
    4. Append completed rawPair to rawPairs when conditions are met, and reset rawPair.

3. Processing Intervals

  • For each raw pair:

    1. Validate that it contains exactly two entries (check-in and check-out).
    2. Calculate the interval duration between check-in and check-out timestamps.
    3. Create IntervalDuration objects for check-in (ci) and check-out (co).
    4. 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.
    5. Apply additional validation:
      • Mark intervals as invalid if the duration is below duration or the gap is below treshold.

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

FieldTypeDescription
IndexintIndex of the interval in the sequence.
Ci*IntervalDurationMetadata for the check-in log.
Co*IntervalDurationMetadata for the check-out log.
CiTimestringTimestamp of the check-in log.
CoTimestringTimestamp of the check-out log.
Durationfloat64Interval duration (in minutes).
IsCalculatedboolWhether the interval meets calculation criteria.
CalculatedReasonstringReason why the interval was invalid, if applicable.

IntervalDuration

FieldTypeDescription
Log*entity.AttendanceLogTableReference to the log entry.
CandidatestringType of log ("ci" for check-in, "co" for check-out).
DurationToNextfloat64Duration to the next interval, for check-ins.
PrevDurationfloat64Duration to the previous interval, for check-outs.
IntervalNumintInterval 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.
  • Performance:
    • Efficiently manages memory by modifying scanLogs in place.
    • Uses sequential processing to maintain order.

Feel free to extend this documentation or refine the formatting based on your team’s preferences.