2017-12-09 09:54:16 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2017-12-12 06:09:29 +00:00
|
|
|
"encoding/json"
|
2023-07-10 10:10:03 +00:00
|
|
|
"io"
|
2017-12-12 06:09:29 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2017-12-12 07:26:01 +00:00
|
|
|
"sync"
|
2021-08-30 16:31:32 +00:00
|
|
|
"time"
|
2017-12-12 06:09:29 +00:00
|
|
|
|
2023-11-22 11:16:53 +00:00
|
|
|
"go.uber.org/multierr"
|
|
|
|
|
2023-07-10 10:10:03 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/generation"
|
2017-12-09 09:54:16 +00:00
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
)
|
|
|
|
|
2018-06-23 13:45:32 +00:00
|
|
|
// DeepReportPath stores the path to the report generated during full recursive import.
|
|
|
|
var DeepReportPath = "full_import_report.json"
|
|
|
|
|
2018-05-11 23:08:37 +00:00
|
|
|
// oneDeepSync ensure there is no more than one running deep sync.
|
2017-12-12 07:26:01 +00:00
|
|
|
var oneDeepSync sync.Mutex
|
2020-01-20 07:41:36 +00:00
|
|
|
var oneThemeDeepSync sync.Mutex
|
2017-12-12 07:26:01 +00:00
|
|
|
|
2019-01-18 14:39:58 +00:00
|
|
|
// DeepSyncProgress expose the progression of the depp synchronization (0 = 0%, 255 = 100%).
|
|
|
|
var DeepSyncProgress uint8
|
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
type SyncReport struct {
|
2022-07-12 10:35:49 +00:00
|
|
|
DateStart time.Time `json:"_started"`
|
|
|
|
DateEnd time.Time `json:"_ended"`
|
|
|
|
DateUpdated []time.Time `json:"_updated"`
|
|
|
|
Regeneration []string `json:"_regeneration"`
|
|
|
|
SyncId string `json:"_id,omitempty"`
|
|
|
|
ThemesSync []string `json:"_themes,omitempty"`
|
|
|
|
Themes map[string][]string `json:"themes"`
|
2024-03-15 16:46:50 +00:00
|
|
|
Exercices []string `json:"exercices,omitempty"`
|
2022-07-01 22:01:05 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
// SpeedySyncDeep performs a recursive synchronisation without importing files.
|
2022-07-01 22:01:05 +00:00
|
|
|
func SpeedySyncDeep(i Importer) (errs SyncReport) {
|
2017-12-12 07:26:01 +00:00
|
|
|
oneDeepSync.Lock()
|
2021-08-30 16:31:32 +00:00
|
|
|
defer func() {
|
2019-11-25 14:08:50 +00:00
|
|
|
oneDeepSync.Unlock()
|
|
|
|
if DeepSyncProgress != 255 {
|
2020-01-20 07:41:36 +00:00
|
|
|
log.Printf("Speedy synchronization terminated at step %d/255", DeepSyncProgress)
|
2019-11-25 14:08:50 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-01-18 14:39:58 +00:00
|
|
|
DeepSyncProgress = 1
|
2017-12-12 07:26:01 +00:00
|
|
|
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes = map[string][]string{}
|
2017-12-09 09:54:16 +00:00
|
|
|
|
2019-11-25 14:08:50 +00:00
|
|
|
startTime := time.Now()
|
2021-10-26 17:56:40 +00:00
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
errs.DateStart = startTime
|
2022-10-29 15:03:57 +00:00
|
|
|
exceptions, sterrs := SyncThemes(i)
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, sterr := range multierr.Errors(sterrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.ThemesSync = append(errs.ThemesSync, sterr.Error())
|
|
|
|
}
|
2017-12-09 09:54:16 +00:00
|
|
|
|
|
|
|
if themes, err := fic.GetThemes(); err == nil {
|
2019-01-18 14:39:58 +00:00
|
|
|
DeepSyncProgress = 2
|
2024-03-15 16:46:50 +00:00
|
|
|
|
|
|
|
if i.Exists(StandaloneExercicesDirectory) {
|
2024-03-24 19:01:09 +00:00
|
|
|
themes = append(themes, &fic.Theme{Path: StandaloneExercicesDirectory})
|
2024-03-15 16:46:50 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:39:58 +00:00
|
|
|
var themeStep uint8 = uint8(250) / uint8(len(themes))
|
|
|
|
|
|
|
|
for tid, theme := range themes {
|
2021-08-30 16:31:32 +00:00
|
|
|
DeepSyncProgress = 3 + uint8(tid)*themeStep
|
2023-07-09 17:05:58 +00:00
|
|
|
ex_exceptions, seerrs := SyncExercices(i, theme, exceptions[theme.Path])
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, seerr := range multierr.Errors(seerrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], seerr.Error())
|
|
|
|
}
|
2017-12-09 09:54:16 +00:00
|
|
|
|
|
|
|
if exercices, err := theme.GetExercices(); err == nil {
|
2021-08-30 16:31:32 +00:00
|
|
|
if len(exercices) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-01-18 14:39:58 +00:00
|
|
|
var exerciceStep uint8 = themeStep / uint8(len(exercices))
|
|
|
|
for eid, exercice := range exercices {
|
2021-08-30 16:31:32 +00:00
|
|
|
log.Printf("Speedy synchronization in progress: %d/255 - doing Theme %q, Exercice %q: %q\n", DeepSyncProgress, theme.Name, exercice.Title, exercice.Path)
|
2019-11-25 14:08:50 +00:00
|
|
|
|
2021-08-30 16:31:32 +00:00
|
|
|
DeepSyncProgress = 3 + uint8(tid)*themeStep + uint8(eid)*exerciceStep
|
2023-07-09 17:05:58 +00:00
|
|
|
flagsBindings, ferrs := SyncExerciceFlags(i, exercice, ex_exceptions[eid])
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, ferr := range multierr.Errors(ferrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], ferr.Error())
|
|
|
|
}
|
2019-11-25 13:57:21 +00:00
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
DeepSyncProgress += exerciceStep / 2
|
2023-07-09 17:05:58 +00:00
|
|
|
_, herrs := SyncExerciceHints(i, exercice, flagsBindings, ex_exceptions[eid])
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, herr := range multierr.Errors(herrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], herr.Error())
|
|
|
|
}
|
2017-12-09 09:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:39:58 +00:00
|
|
|
DeepSyncProgress = 254
|
2022-07-01 22:01:05 +00:00
|
|
|
errs.DateEnd = time.Now()
|
2018-01-06 16:38:24 +00:00
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
DeepSyncProgress = 255
|
|
|
|
log.Println("Speedy synchronization done in", time.Since(startTime))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncDeep performs a recursive synchronisation: from themes to challenge items.
|
2022-07-01 22:01:05 +00:00
|
|
|
func SyncDeep(i Importer) (errs SyncReport) {
|
2020-01-20 07:41:36 +00:00
|
|
|
oneDeepSync.Lock()
|
2021-08-30 16:31:32 +00:00
|
|
|
defer func() {
|
2020-01-20 07:41:36 +00:00
|
|
|
oneDeepSync.Unlock()
|
|
|
|
if DeepSyncProgress != 255 {
|
|
|
|
log.Printf("Full synchronization terminated at step %d/255", DeepSyncProgress)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
DeepSyncProgress = 1
|
|
|
|
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes = map[string][]string{}
|
2020-01-20 07:41:36 +00:00
|
|
|
|
|
|
|
startTime := time.Now()
|
2021-10-26 17:56:40 +00:00
|
|
|
|
2024-03-15 16:46:50 +00:00
|
|
|
// Import all themes
|
2022-07-01 22:01:05 +00:00
|
|
|
errs.DateStart = startTime
|
2022-10-29 15:03:57 +00:00
|
|
|
exceptions, sterrs := SyncThemes(i)
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, sterr := range multierr.Errors(sterrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.ThemesSync = append(errs.ThemesSync, sterr.Error())
|
|
|
|
}
|
2020-01-20 07:41:36 +00:00
|
|
|
|
2024-03-15 16:46:50 +00:00
|
|
|
// Synchronize themes
|
|
|
|
if themes, err := fic.GetThemes(); err == nil {
|
2020-01-20 07:41:36 +00:00
|
|
|
DeepSyncProgress = 2
|
2024-03-15 16:46:50 +00:00
|
|
|
|
|
|
|
// Also synchronize standalone exercices
|
|
|
|
if i.Exists(StandaloneExercicesDirectory) {
|
|
|
|
themes = append(themes, &fic.Theme{Path: StandaloneExercicesDirectory})
|
|
|
|
}
|
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
var themeStep uint8 = uint8(250) / uint8(len(themes))
|
|
|
|
|
|
|
|
for tid, theme := range themes {
|
2022-10-29 15:03:57 +00:00
|
|
|
stderrs := SyncThemeDeep(i, theme, tid, themeStep, exceptions[theme.Path])
|
2023-11-22 11:16:53 +00:00
|
|
|
for _, stderr := range multierr.Errors(stderrs) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Themes[theme.Name] = append(errs.Themes[theme.Name], stderr.Error())
|
|
|
|
}
|
2020-01-20 07:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeepSyncProgress = 254
|
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
EditDeepReport(&errs, true)
|
2020-01-20 07:41:36 +00:00
|
|
|
|
2023-07-10 10:10:03 +00:00
|
|
|
resp, err := generation.FullGeneration()
|
|
|
|
if err != nil {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Regeneration = append(errs.Regeneration, err.Error())
|
2023-07-10 10:10:03 +00:00
|
|
|
} else {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
v, _ := io.ReadAll(resp.Body)
|
|
|
|
errs.Regeneration = append(errs.Regeneration, string(v))
|
2020-01-20 07:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DeepSyncProgress = 255
|
|
|
|
log.Println("Full synchronization done in", time.Since(startTime))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
func readDeepReport() (ret *SyncReport, err error) {
|
2020-01-20 07:41:36 +00:00
|
|
|
if fdfrom, err := os.Open(DeepReportPath); err == nil {
|
|
|
|
defer fdfrom.Close()
|
|
|
|
|
|
|
|
jdec := json.NewDecoder(fdfrom)
|
|
|
|
|
|
|
|
if err := jdec.Decode(&ret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
func EditDeepReport(errs *SyncReport, erase bool) {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Regeneration = []string{}
|
2018-01-08 00:34:22 +00:00
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
if !erase {
|
|
|
|
if in, err := readDeepReport(); err != nil {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Regeneration = append(errs.Regeneration, err.Error())
|
2020-01-20 07:41:36 +00:00
|
|
|
log.Println(err)
|
|
|
|
} else {
|
2022-07-01 22:01:05 +00:00
|
|
|
for k, v := range errs.Themes {
|
|
|
|
in.Themes[k] = v
|
2020-01-20 07:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errs = in
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 22:01:05 +00:00
|
|
|
errs.DateUpdated = append(errs.DateUpdated, time.Now())
|
2020-01-20 07:41:36 +00:00
|
|
|
|
2018-06-23 13:45:32 +00:00
|
|
|
if fdto, err := os.Create(DeepReportPath); err == nil {
|
2017-12-12 06:09:29 +00:00
|
|
|
defer fdto.Close()
|
|
|
|
|
|
|
|
if out, err := json.Marshal(errs); err == nil {
|
|
|
|
fdto.Write(out)
|
|
|
|
} else {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Regeneration = append(errs.Regeneration, err.Error())
|
2017-12-12 06:09:29 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-12 10:35:49 +00:00
|
|
|
errs.Regeneration = append(errs.Regeneration, err.Error())
|
2017-12-12 06:09:29 +00:00
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SyncThemeDeep performs a recursive synchronisation: from challenges to challenge items.
|
2023-11-22 11:16:53 +00:00
|
|
|
func SyncThemeDeep(i Importer, theme *fic.Theme, tid int, themeStep uint8, exceptions *CheckExceptions) (errs error) {
|
2023-07-09 17:05:58 +00:00
|
|
|
var ex_exceptions map[int]*CheckExceptions
|
|
|
|
|
2020-01-20 07:41:36 +00:00
|
|
|
oneThemeDeepSync.Lock()
|
|
|
|
defer oneThemeDeepSync.Unlock()
|
|
|
|
|
2021-08-30 16:31:32 +00:00
|
|
|
DeepSyncProgress = 3 + uint8(tid)*themeStep
|
2023-07-09 17:05:58 +00:00
|
|
|
ex_exceptions, errs = SyncExercices(i, theme, exceptions)
|
2020-01-20 07:41:36 +00:00
|
|
|
|
|
|
|
if exercices, err := theme.GetExercices(); err == nil && len(exercices) > 0 {
|
|
|
|
var exerciceStep uint8 = themeStep / uint8(len(exercices))
|
|
|
|
for eid, exercice := range exercices {
|
|
|
|
log.Printf("Deep synchronization in progress: %d/255 - doing Theme %q, Exercice %q: %q\n", DeepSyncProgress, theme.Name, exercice.Title, exercice.Path)
|
|
|
|
|
2021-08-30 16:31:32 +00:00
|
|
|
DeepSyncProgress = 3 + uint8(tid)*themeStep + uint8(eid)*exerciceStep
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, SyncExerciceFiles(i, exercice, ex_exceptions[eid]))
|
2020-01-20 07:41:36 +00:00
|
|
|
|
|
|
|
DeepSyncProgress += exerciceStep / 3
|
2023-07-09 17:05:58 +00:00
|
|
|
flagsBindings, ferrs := SyncExerciceFlags(i, exercice, ex_exceptions[eid])
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, ferrs)
|
2020-01-20 07:41:36 +00:00
|
|
|
|
|
|
|
DeepSyncProgress += exerciceStep / 3
|
2023-07-09 17:05:58 +00:00
|
|
|
_, herrs := SyncExerciceHints(i, exercice, flagsBindings, ex_exceptions[eid])
|
2023-11-22 11:16:53 +00:00
|
|
|
errs = multierr.Append(errs, herrs)
|
2020-01-20 07:41:36 +00:00
|
|
|
}
|
2017-12-12 06:51:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 00:34:22 +00:00
|
|
|
return
|
2017-12-09 09:54:16 +00:00
|
|
|
}
|