Skip to main content

Calculate Breaks Documentation

This code provides a function to calculate breaks for attendance summaries. It processes scan logs and applies rules to determine break times and durations.

CalculateBreaks Function

Function Signature

func CalculateBreaks(summary *AttendanceSummariesCompute, scanLogs []*entity.AttendanceLogTable)

Parameters

  • summary *AttendanceSummariesCompute: A pointer to an AttendanceSummariesCompute struct, which contains the attendance summary and related details.
  • scanLogs []*entity.AttendanceLogTable: A slice of AttendanceLogTable structs, representing the scan logs for the attendance.

Description

The CalculateBreaks function calculates break times and durations for an attendance summary. It processes scan logs and applies rules to determine the actual and rule-based break times. The function updates the SummaryDetails field of the AttendanceSummariesCompute struct with the calculated break details.

Steps

  1. Check for Shift: If the summary does not have a shift, the function returns early.
  2. Iterate Over Shift Breaks: For each break in the shift, the function:
    • Matches the break order number with the scan points.
    • Checks if there is an existing summary detail for the break.
    • If not, creates a new summary detail.
  3. Process Scan Points: The function checks if there are scan points for the break-in and break-out times. If found, it updates the summary detail with the scan times.
  4. Apply Rules: If scan points are missing, the function applies rules to determine the break times. It updates the summary detail with the rule-based times.
  5. Calculate Durations: The function calculates the early, late, and overtime durations for the break.
  6. Handle Overwrites: If there are overwrites, the function updates the summary detail with the overwrite values.

Example Usage

package main

import (
"fio-backend/entity"
"fio-backend/utils/compute"
"fmt"
)

func main() {
// Create a sample AttendanceSummariesCompute struct
summary := &compute.AttendanceSummariesCompute{
Id: "summary_id_123",
Shift: &entity.ShiftTable{
ShortLabel: "Morning Shift",
ShiftBreaks: []entity.ShiftBreakTable{
{
OrderNum: 1,
BreakID: "break_id_1",
Break: entity.BreakTable{
DurationMinutes: 30,
},
},
},
},
SummaryDetails: []entity.AttendanceSummariesDetailTable{},
ScanPoints: map[compute.ScanPointEvent]*compute.ScanPoint{
"breakIn-1": {
WinnerFromLogs: true,
UseThisTime: "2024-05-01T09:00:00Z",
WinnerLogID: "log_id_1",
},
"breakOut-1": {
WinnerFromLogs: true,
UseThisTime: "2024-05-01T09:30:00Z",
WinnerLogID: "log_id_2",
},
},
}

// Create sample scan logs
scanLogs := []*entity.AttendanceLogTable{
{
Id: "log_id_1",
UserProfileID: "user_id_123",
Timestamp: "2024-05-01T09:00:00Z",
},
{
Id: "log_id_2",
UserProfileID: "user_id_123",
Timestamp: "2024-05-01T09:30:00Z",
},
}

// Calculate breaks
compute.CalculateBreaks(summary, scanLogs)

// Print the updated summary details
for _, detail := range summary.SummaryDetails {
fmt.Printf("Break Detail: %+v\n", detail)
}
}

Improvements and Considerations

  • Error Handling: Improve error handling by returning errors instead of just logging them.
  • Code Clarity and Comments: Improve code comments to explain complex logic more clearly.
  • Unit Tests: Add unit tests to ensure the function works correctly with various inputs and edge cases.
  • Performance Optimization: Optimize the function for performance, especially when processing large numbers of scan logs and breaks.
  • Modularization: Break down the function into smaller, more modular functions to improve readability and maintainability.

This comprehensive documentation provides a detailed understanding of the CalculateBreaks function and its functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for calculating breaks in attendance summaries.