Calendar Schedule Computation Documentation
This code provides functions to generate and manage calendar schedules for users within a company. It includes functions to generate schedules for a date range, handle imports, and manage cache.
Constants
var (
USE_GLOBAL_CACHE = false
)
USE_GLOBAL_CACHE
: A flag to determine whether to use global cache or not.
Functions
FindEffectiveScheduleIDForUserByDate
func FindEffectiveScheduleIDForUserByDate(curUser *entity.UserProfileTable, calcDate string) *entity.ScheduleTable
Finds the effective schedule ID for a user on a given date.
curUser *entity.UserProfileTable
: The user profile.calcDate string
: The calculation date inyyyy-mm-dd
format.
Returns the effective schedule table.
GenerateCalendarScheduleOneCompanyDateRange
func GenerateCalendarScheduleOneCompanyDateRange(companyID, start, end string, includeOverwrite, force bool) ([]*entity.CalendarScheduleTable, error)
Generates calendar schedules for all users in a company within a date range.
companyID string
: The company ID.start string
: The start date inyyyy-mm-dd
format.end string
: The end date inyyyy-mm-dd
format.includeOverwrite bool
: Whether to include overwrites.force bool
: Whether to force regeneration.
Returns a slice of calendar schedule tables and an error.
GenerateCalendarScheduleOnePersonDateRange
func GenerateCalendarScheduleOnePersonDateRange(user *entity.UserProfileTable, start, end string, includeOverwrite, force, cacheFollowForce bool, cache *caching.AllCacheStruct) ([]*entity.CalendarScheduleTable, error)
Generates calendar schedules for a user within a date range.
user *entity.UserProfileTable
: The user profile.start string
: The start date inyyyy-mm-dd
format.end string
: The end date inyyyy-mm-dd
format.includeOverwrite bool
: Whether to include overwrites.force bool
: Whether to force regeneration.cacheFollowForce bool
: Whether the cache should follow the force parameter.cache *caching.AllCacheStruct
: The cache structure.
Returns a slice of calendar schedule tables and an error.
prepareCalendarScheduleCache
func prepareCalendarScheduleCache(companyID, start, end string, force, cacheFollowForce bool) *caching.AllCacheStruct
Prepares the cache for calendar schedule generation.
companyID string
: The company ID.start string
: The start date inyyyy-mm-dd
format.end string
: The end date inyyyy-mm-dd
format.force bool
: Whether to force regeneration.cacheFollowForce bool
: Whether the cache should follow the force parameter.
Returns the cache structure.
GenerateCalendarScheduleOnePersonOneDate
func GenerateCalendarScheduleOnePersonOneDate(user *entity.UserProfileTable, calcDate time.Time, includeOverwrite, force, cacheFollowForce bool, cache *caching.AllCacheStruct) *entity.CalendarScheduleTable
Generates a calendar schedule for a user on a specific date.
user *entity.UserProfileTable
: The user profile.calcDate time.Time
: The calculation date.includeOverwrite bool
: Whether to include overwrites.force bool
: Whether to force regeneration.cacheFollowForce bool
: Whether the cache should follow the force parameter.cache *caching.AllCacheStruct
: The cache structure.
Returns the calendar schedule table.
IsDateInSchedule
func IsDateInSchedule(calcDate string, schedule *entity.ScheduleTable) bool
Checks if a date is within a schedule.
calcDate string
: The calculation date inyyyy-mm-dd
format.schedule *entity.ScheduleTable
: The schedule table.
Returns true
if the date is within the schedule, otherwise false
.
BuildSampleDataImportCalender
func BuildSampleDataImportCalender() *bytes.Buffer
Builds a sample data import calendar.
Returns a buffer containing the sample data.
WriteShift
func WriteShift(f *excelize.File, row int, col int, shift entity.ShiftStruct)
Writes shift data into Excel cells.
f *excelize.File
: The Excel file.row int
: The row number.col int
: The column number.shift entity.ShiftStruct
: The shift structure.
XYToExcelColumnName
func XYToExcelColumnName(col, row int) string
Converts XY coordinates to Excel column format.
col int
: The column number.row int
: The row number.
Returns the Excel column name.
ToExcelColumnName
func ToExcelColumnName(col int) string
Converts a column number to Excel-style column name.
col int
: The column number.
Returns the Excel column name.
ReadImportData
func ReadImportData(buffer *bytes.Buffer) (entity.ImportDataStruct, error)
Reads import data from a buffer.
buffer *bytes.Buffer
: The buffer containing the import data.
Returns the import data structure and an error.
DaysInMonth
func DaysInMonth(year, month int) int
Returns the number of days in a given month and year.
year int
: The year.month int
: The month.
Returns the number of days.
ProcessImport
func ProcessImport(companyID string, importData entity.ImportDataStruct) error
Processes the import data.
companyID string
: The company ID.importData entity.ImportDataStruct
: The import data structure.
Returns an error if any.
BuildSampleDataImportwithIds
func BuildSampleDataImportwithIds(companyID string, userIds []string, month, year int) (*bytes.Buffer, error)
Builds sample data for import calendar with user IDs.
companyID string
: The company ID.userIds []string
: The list of user IDs.month int
: The month.year int
: The year.
Returns a buffer containing the sample data and an error.
importProcessOneDateOnePerson
func importProcessOneDateOnePerson(importdata entity.ImportDataStruct, cache *caching.AllCacheStruct, idx int, errs []string, d entity.ImportDataUser, jadwal entity.ShiftStruct, UserProfile *entity.UserProfileTable, listCalendarSchedule *[]*entity.CalendarScheduleTable) error
Processes import data for one date and one person.
importdata entity.ImportDataStruct
: The import data structure.cache *caching.AllCacheStruct
: The cache structure.idx int
: The index.errs []string
: The list of errors.d entity.ImportDataUser
: The import data user.jadwal entity.ShiftStruct
: The shift structure.UserProfile *entity.UserProfileTable
: The user profile.listCalendarSchedule *[]*entity.CalendarScheduleTable
: The list of calendar schedules.
Returns an error if any.
ParallelGenerateCalendarScheduleOneCompanyDateRange
func ParallelGenerateCalendarScheduleOneCompanyDateRange(companyID, start, end string, includeOverwrite, force bool) ([]*entity.CalendarScheduleTable, error)
Generates calendar schedules for all users in a company within a date range in parallel.
companyID string
: The company ID.start string
: The start date inyyyy-mm-dd
format.end string
: The end date inyyyy-mm-dd
format.includeOverwrite bool
: Whether to include overwrites.force bool
: Whether to force regeneration.
Returns a slice of calendar schedule tables and an error.
Example Usage
package main
import (
"fio-backend/entity"
"fio-backend/utils/caching"
"fio-backend/utils/compute"
"fmt"
"time"
)
func main() {
companyID := "your_company_id"
startDate := "2024-05-01"
endDate := "2024-05-31"
// Generate calendar schedules for all users in a company within a date range
schedules, err := compute.GenerateCalendarScheduleOneCompanyDateRange(companyID, startDate, endDate, true, false)
if err != nil {
fmt.Println("Error generating calendar schedules:", err)
return
}
// Print the generated schedules
for _, schedule := range schedules {
fmt.Printf("Schedule: %+v\n", schedule)
}
// Generate calendar schedules for a specific user within a date range
user := &entity.UserProfileTable{
Id: "user_id_123",
FullName: "John Doe",
CompanyID: companyID,
}
userSchedules, err := compute.GenerateCalendarScheduleOnePersonDateRange(user, startDate, endDate, true, false, false, nil)
if err != nil {
fmt.Println("Error generating user calendar schedules:", err)
return
}
// Print the generated user schedules
for _, schedule := range userSchedules {
fmt.Printf("User Schedule: %+v\n", schedule)
}
// Build sample data import calendar
buffer := compute.BuildSampleDataImportCalender()
fmt.Println("Sample data import calendar created")
// Read import data from buffer
importData, err := compute.ReadImportData(buffer)
if err != nil {
fmt.Println("Error reading import data:", err)
return
}
fmt.Printf("Import Data: %+v\n", importData)
// Process import data
err = compute.ProcessImport(companyID, importData)
if err != nil {
fmt.Println("Error processing import data:", err)
return
}
fmt.Println("Import data processed successfully")
}
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 schedules and shifts.
- Modularization: Break down the functions into smaller, more modular functions to improve readability and maintainability.
This comprehensive documentation provides a detailed understanding of the calendar schedule computation functions and their functionalities. By addressing the suggested improvements, you can create a more robust and maintainable solution for generating and managing calendar schedules.