@@ -20,9 +20,18 @@ import (
2020 "time"
2121)
2222
23- func ImportLenexFile (file string , meeting string , exclude []int , include []int , features []string , stg importModel.ImportSetting ) (* importModel.ImportFileStats , error ) {
23+ type ProgressCallback func (progress float64 , message string )
24+
25+ func ImportLenexFile (file string , meeting string , exclude []int , include []int , features []string , stg importModel.ImportSetting , progressCallback ProgressCallback ) (* importModel.ImportFileStats , error ) {
2426 var stats importModel.ImportFileStats
2527
28+ // Helper function to handle nil callback
29+ progress := func (pct float64 , msg string ) {
30+ if progressCallback != nil {
31+ progressCallback (pct , msg )
32+ }
33+ }
34+
2635 // Read content via getFileReader for both local and remote sources
2736 buf , err1 := getFileReader (file )
2837 if err1 != nil {
@@ -90,8 +99,39 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
9099 return nil , errors .New ("timezone " + stg .TimeZone + " is not a valid timezone" )
91100 }
92101
102+ // CALCULATE TOTAL ITEMS FOR PROGRESS TRACKING
103+ totalTeams := len (meet .Clubs )
104+ totalAthletes := 0
105+ totalEntries := 0
106+ totalResults := 0
107+ totalHeats := 0
108+ totalEvents := 0
109+ totalAgeGroups := 0
110+
111+ for _ , session := range meet .Sessions {
112+ for _ , event := range session .Events {
113+ totalEvents ++
114+ totalAgeGroups += len (event .AgeGroups )
115+ totalHeats += len (event .Heats )
116+ }
117+ }
118+
119+ for _ , team := range meet .Clubs {
120+ totalAthletes += len (team .Athletes )
121+ for _ , athlete := range team .Athletes {
122+ totalEntries += len (athlete .Entries )
123+ totalResults += len (athlete .Results )
124+ }
125+ }
126+
127+ totalItems := totalEvents + totalAgeGroups + totalHeats + totalTeams + totalAthletes + totalEntries + totalResults
128+ processedItems := 0
129+
130+ progress (20 , fmt .Sprintf ("Starting import with %d total items to process" , totalItems ))
131+
93132 for _ , session := range meet .Sessions {
94133 for _ , event := range session .Events {
134+ processedItems ++
95135
96136 // EVENT IMPORT
97137 fmt .Printf ("%d" , event .Number )
@@ -143,6 +183,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
143183
144184 // AGE GROUP IMPORT
145185 for _ , ageGroup := range event .AgeGroups {
186+ processedItems ++
187+
146188 minAge := meetingYear - ageGroup .AgeMin
147189 maxAge := meetingYear - ageGroup .AgeMax
148190
@@ -206,6 +248,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
206248
207249 // HEATS
208250 for _ , heat := range event .Heats {
251+ processedItems ++
252+
209253 startTime := heat .Daytime .Time
210254
211255 // if Daytime does not include but date only hours, add date of the session
@@ -255,6 +299,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
255299
256300 // TEAMS
257301 for _ , team := range meet .Clubs {
302+ processedItems ++
303+
258304 stateId , err := strconv .Atoi (team .Region )
259305 if err != nil {
260306 stateId = 0
@@ -299,6 +345,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
299345
300346 // ATHLETES
301347 for _ , athlete := range team .Athletes {
348+ processedItems ++
349+
302350 dsvAthlete , err := strconv .Atoi (athlete .License )
303351 if err != nil {
304352 dsvAthlete = 0
@@ -341,6 +389,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
341389 // STARTS + RESULTS + DISQUALIFICATIONS
342390 // TODO: support entry lists (no heat) -> entries already can have lanes and heats
343391 for _ , entry := range athlete .Entries {
392+ processedItems ++
393+
344394 heat := heats [entry .HeatId ]
345395
346396 if heat .Number == 0 {
@@ -391,6 +441,8 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
391441 }
392442
393443 for _ , result := range athlete .Results {
444+ processedItems ++
445+
394446 heat := heats [result .HeatId ]
395447 rank := ranks [result .ResultId ]
396448
@@ -524,106 +576,16 @@ func ImportLenexFile(file string, meeting string, exclude []int, include []int,
524576 }
525577 // only heats with heat number != 0 are imported until here!
526578 }
579+
580+ // Update progress after each athlete
581+ progressPct := 20 + (float64 (processedItems )/ float64 (totalItems ))* 80
582+ progress (progressPct , fmt .Sprintf ("Processing athletes: %d / %d" , processedItems , totalItems ))
527583 }
528584 }
529585
530586 fmt .Printf (" +==============================+ \n " )
531587
532- //var starts []startModel.Start
533- //
534- //for _, dsvResult := range erg.PNErgebnisse {
535- //
536- // if dsvResult.GrundDerNichtwertung == "AB" {
537- // continue
538- // }
539- //
540- // if unusedEvents[dsvResult.Wettkampfnummer] {
541- // continue
542- // }
543- //
544- // if !IsEventImportable(dsvResult.Wettkampfnummer, exclude, include) {
545- // continue
546- // }
547- //
548- // // RESULT
549- // result := startModel.Result{
550- // Time: dsvResult.Endzeit.Duration(),
551- // ResultType: "result_list",
552- // }
553- //
554- // starts = append(starts, *newStart)
555- //
556- // if dsvResult.GrundDerNichtwertung != "" {
557- // disqType := "disqualified"
558- // switch dsvResult.GrundDerNichtwertung {
559- // case "NA":
560- // disqType = "dns"
561- // break
562- // case "AU":
563- // disqType = "dnf"
564- // break
565- // case "ZU":
566- // disqType = "time"
567- // break
568- // }
569- // disqualification, created, err4 := dc.ImportDisqualification(start, dsvResult.Disqualifikationsbemerkung, disqType, time.UnixMicro(0))
570- // if err4 != nil {
571- // return &stats, err4
572- // }
573- // cs := 'o'
574- // if created {
575- // cs = '+'
576- // stats.Created.Disqualifications++
577- // }
578- // stats.Imported.Disqualifications++
579- // fmt.Printf("[ %c ] > id: %s, type: %s, reason: %s\n", cs, disqualification.Identifier, disqualification.Type, disqualification.Reason)
580- // } else {
581- // _, _, err3 := sc.ImportResult(start, result)
582- // if err3 != nil {
583- // return &stats, err3
584- // }
585- // stats.Created.Results++
586- // stats.Imported.Results++
587- // }
588- //
589- //}
590- //
591- //for _, dsvLap := range erg.PNZwischenzeiten {
592- // if unusedEvents[dsvLap.Wettkampfnummer] {
593- // continue
594- // }
595- //
596- // if !IsEventImportable(dsvLap.Wettkampfnummer, exclude, include) {
597- // continue
598- // }
599- //
600- // // LAP Result
601- // lapResult := startModel.Result{
602- // Time: dsvLap.Zwischenzeit.Duration(),
603- // ResultType: "lap",
604- // LapMeters: dsvLap.Distanz,
605- // }
606- //
607- // var lapStart startModel.Start
608- //
609- // found := false
610- // for _, start := range starts {
611- // if start.AthleteMeetingId == dsvLap.VeranstaltungsIdSchwimmer && start.Event == dsvLap.Wettkampfnummer {
612- // lapStart = start
613- // found = true
614- // break
615- // }
616- // }
617- //
618- // if found {
619- // _, _, err3 := sc.ImportResult(lapStart, lapResult)
620- // if err3 != nil {
621- // return &stats, err3
622- // }
623- // stats.Created.Results++
624- // stats.Imported.Results++
625- // }
626- //}
588+ progress (95 , "Finalizing import..." )
627589
628590 return & stats , nil
629591}
0 commit comments