Skip to main content

Attendance Summaries Computation Documentation

This code provides functions to compute attendance summaries for employees. It processes scan logs, applies rules, and generates summaries for attendance, including check-in, check-out, and breaks.

Constants

const (
SUMMARY_NO_SCHEDULE = "NoSchedule"
SUMMARY_NO_SHIFT = "NoShift"
SUMMARY_CANNOT_COMPUTE = "CannotCompute"
SUMMARY_INCOMPLETE = "Incomplete" // incomplete scans

SUMMARY_DAY_NO_WORK = "NoWork"
SUMMARY_DAY_WORK = "Work"

DEFAULT_DATE_RANGE = 30
DEFAULT_DAYS_IN_YEAR = 365

PRINT_DEBUG = true
)

Functions

CalculateOnePersonOneDateForHandler

func CalculateOnePersonOneDateForHandler(companyID, personID, calcDate string, force bool) ([]*entity.AttendanceSummariesTable, error)

Calculates attendance summaries for a single person on a specific date. This function is intended to be called by a handler and reads from the global cache.

  • companyID string: The company ID.
  • personID string: The person ID.
  • calcDate string: The calculation date in yyyy-mm-dd format.
  • force bool: If true, forces recalculation even if data is already cached.

Returns a slice of AttendanceSummariesTable and an error.

CalculateOneCompanyDateRange

func CalculateOneCompanyDateRange(companyID, start, end string, cache *caching.AllCacheStruct) ([]*entity.AttendanceSummariesTable, error)

Calculates attendance summaries for all employees in a company over a date range.

  • companyID string: The company ID.
  • start string: The start date in yyyy-mm-dd format.
  • end string: The end date in yyyy-mm-dd format.
  • cache *caching.AllCacheStruct: The cache structure.

Returns a slice of AttendanceSummariesTable and an error.

prepareSummaryCache

func prepareSummaryCache(companyID, start, end string, force, cacheFollowForce bool) *caching.AllCacheStruct

Prepares the cache needed for generating attendance summaries.

  • companyID string: The company ID.
  • start string: The start date in yyyy-mm-dd format.
  • end string: The end date in yyyy-mm-dd format.
  • force bool: If true, forces cache refresh.
  • cacheFollowForce bool: If true, forces cache refresh for dependent caches.

Returns a pointer to AllCacheStruct.

CalculateOnePersonDateRange

func CalculateOnePersonDateRange(companyID, personID, start, end string, force, cacheFollowForce bool, cache *caching.AllCacheStruct, wg *sync.WaitGroup) ([]*entity.AttendanceSummariesTable, error)

Calculates attendance summaries for a single person over a date range.

  • companyID string: The company ID.
  • personID string: The person ID.
  • start string: The start date in yyyy-mm-dd format.
  • end string: The end date in yyyy-mm-dd format.
  • force bool: If true, forces recalculation even if data is already cached.
  • cacheFollowForce bool: If true, forces cache refresh for dependent caches.
  • cache *caching.AllCacheStruct: The cache structure.
  • wg *sync.WaitGroup: A wait group for asynchronous operations.

Returns a slice of AttendanceSummariesTable and an error.

CalculateOnePersonOneDate

func CalculateOnePersonOneDate(curUser *entity.UserProfileTable, calcDate string, force, cacheFollowForce bool, cache *caching.AllCacheStruct, wg *sync.WaitGroup) ([]*entity.AttendanceSummariesTable, error)

Calculates attendance summaries for a single person on a specific date.

  • curUser *entity.UserProfileTable: The user profile.
  • calcDate string: The calculation date in yyyy-mm-dd format.
  • force bool: If true, forces recalculation even if data is already cached.
  • cacheFollowForce bool: If true, forces cache refresh for dependent caches.
  • cache *caching.AllCacheStruct: The cache structure.
  • wg *sync.WaitGroup: A wait group for asynchronous operations.

Returns a slice of AttendanceSummariesTable and an error.

CheckedAsWinner

func CheckedAsWinner(summary *entity.AttendanceSummariesTable, cache *caching.AllCacheStruct)

Marks the scan logs used in the summary as winners.

  • summary *entity.AttendanceSummariesTable: The attendance summary.
  • cache *caching.AllCacheStruct: The cache structure.

GetWinnerForSummary

func GetWinnerForSummary(summaries *[]AttendanceSummariesCompute, autoShiftRule string) int

Returns the index of the winning summary based on the auto shift rule.

  • summaries *[]AttendanceSummariesCompute: The attendance summaries.
  • autoShiftRule string: The auto shift rule.

Returns the index of the winning summary.

RoundBasedOnSetting

func RoundBasedOnSetting(summary *entity.AttendanceSummariesTable, cache *caching.AllCacheStruct)

Rounds the values in the summary based on the rounding settings.

  • summary *entity.AttendanceSummariesTable: The attendance summary.
  • cache *caching.AllCacheStruct: The cache structure.

Example Usage

package main

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

func main() {
// Example usage of CalculateOnePersonOneDateForHandler
companyID := "company_123"
personID := "person_123"
calcDate := "2024-05-01"
force := true

summaries, err := compute.CalculateOnePersonOneDateForHandler(companyID, personID, calcDate, force)
if err != nil {
fmt.Println("Error:", err)
} else {
for _, summary := range summaries {
fmt.Printf("Summary: %+v\n", summary)
}
}

// Example usage of CalculateOneCompanyDateRange
startDate := "2024-05-01"
endDate := "2024-05-31"
cache := &caching.AllCacheStruct{}

companySummaries, err := compute.CalculateOneCompanyDateRange(companyID, startDate, endDate, cache)
if err != nil {
fmt.Println("Error:", err)
} else {
for _, summary := range companySummaries {
fmt.Printf("Company Summary: %+v\n", summary)
}
}

// Example usage of CalculateOnePersonDateRange
var wg sync.WaitGroup
personSummaries, err := compute.CalculateOnePersonDateRange(companyID, personID, startDate, endDate, force, true, cache, &wg)
if err != nil {
fmt.Println("Error:", err)
} else {
for _, summary := range personSummaries {
fmt.Printf("Person Summary: %+v\n", summary)
}
}
}

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 functions work correctly with various inputs and edge cases.
  • Performance Optimization: Optimize the functions for performance, especially when processing large numbers of scan logs and breaks.
  • Modularization: Break down the functions into smaller, more modular functions to improve readability and maintainability.

This comprehensive documentation provides a detailed understanding of the attendance summaries computation functions and their functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for calculating attendance summaries.