Helper Functions for Compute Documentation
This code provides various helper functions and constants used in the computation of attendance summaries, including functions for copying fields, checking conditions, and debugging.
Constants
const (
WINNER_MAX string = "max"
WINNER_MIN string = "min"
WINNER_FIRST string = "first"
WINNER_FORMULA string = "formula"
CANNOT_COMPUTE_NUMBER float64 = -1
CANNOT_COMPUTE_FLOAT float64 = -1
NO_OVERWRITE_VALUE int = -1
SET_CI_CO_FROM_DURATION bool = false
SET_CI_CO_FROM_RULE bool = true
SET_DURATION_FROM_RULE bool = true
RULE_COUNT_EQUAL bool = true
RULE_PENALTY_CI_CO_STACKED bool = true
RULE_WINNER string = WINNER_FORMULA
PRINT_DEBUG bool = false
)
Variables
var (
BMinusA func(string, string) float64 = timedate.GetMinutesFromDateTimeStringBtoA
)
Functions
DateTimeStrBMinusA
func DateTimeStrBMinusA(a, b string) float64
Calculates the difference in minutes between two date-time strings. Returns CANNOT_COMPUTE_FLOAT
if either string is empty.
OnlyTheseStrings
func OnlyTheseStrings(strMaster string, strs []string) bool
Checks if strMaster
is in the slice strs
.
NotTheseStrings
func NotTheseStrings(strMaster string, strs []string) bool
Checks if strMaster
is not in the slice strs
.
CopyNonZeroFieldsExclude
func CopyNonZeroFieldsExclude(dst, src interface{}, numberZeroValue int, exclude []string)
Copies non-zero fields from src
to dst
, excluding fields listed in exclude
.
ResetSummaryHeaderOrDetailExclude
func ResetSummaryHeaderOrDetailExclude(dst, src interface{}, exclude []string)
Resets fields in dst
to the values in src
, excluding fields listed in exclude
.
AddFloat64Fields
func AddFloat64Fields[T any, U any](a *T, b *U)
Adds fields of type float64
from b
to a
where the field names match. (Currently unused.)
CopyNonZeroFieldsOnly
func CopyNonZeroFieldsOnly(dst, src interface{}, numberZeroValue int, only []string)
Copies non-zero fields from src
to dst
, including only fields listed in only
.
isZero
func isZero(v reflect.Value, numberZeroValue int) bool
Checks if a reflect.Value
is zero, based on its kind.
calshiftsDebug
func calshiftsDebug(calDets []*entity.CalendarScheduleDetailsTable, cache *caching.AllCacheStruct) string
Generates a debug string for calendar shifts.
calSchedDebug
func calSchedDebug(calSched *entity.CalendarScheduleTable, cache *caching.AllCacheStruct) string
Generates a debug string for calendar schedules.
sumshiftsDebug
func sumshiftsDebug(sum *entity.AttendanceSummariesTable, cache *caching.AllCacheStruct) string
Generates a debug string for summary shifts.
userScheduleDebug
func userScheduleDebug(sched *entity.UserSchedulesTable) string
Generates a debug string for user schedules.
printlnDebug
func printlnDebug(a ...any)
Prints debug information if PRINT_DEBUG
is true.
printDebug
func printDebug(a ...any)
Prints debug information without a newline if PRINT_DEBUG
is true.
Example Usage
package main
import (
"fio-backend/entity"
"fio-backend/utils/caching"
"fio-backend/utils/compute"
"fmt"
)
func main() {
// Example usage of CopyNonZeroFieldsExclude
src := &entity.AttendanceSummariesTable{
Id: "summary_id_123",
ShiftID: "shift_id_123",
Duration: 480,
Notes: "Sample notes",
}
dst := &entity.AttendanceSummariesTable{}
exclude := []string{"Notes"}
compute.CopyNonZeroFieldsExclude(dst, src, compute.NO_OVERWRITE_VALUE, exclude)
fmt.Printf("Copied Summary: %+v\n", dst)
// Example usage of calshiftsDebug
calDets := []*entity.CalendarScheduleDetailsTable{
{
OrderNumber: 1,
ShiftID: "shift_id_123",
},
}
cache := &caching.AllCacheStruct{
Shifts: &caching.LocalShifts{
IndexedShifts: map[string]*entity.ShiftTable{
"shift_id_123": {
ShortLabel: "Morning Shift",
},
},
},
}
debugStr := compute.calshiftsDebug(calDets, cache)
fmt.Printf("Calendar Shifts Debug: %s\n", debugStr)
}
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 data sets.
- Modularization: Break down the functions into smaller, more modular functions to improve readability and maintainability.
This comprehensive documentation provides a detailed understanding of the helper functions for compute and their functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for computing attendance summaries.