Load reads all YYYYMMDD.json files from /telemetry/ and rebuilds the in-memory index. Any legacy YYYYMMDD.yaml files found are migrated to JSON and then deleted. Safe to call when the directory does not exist yet.
(dataFilesPath string)
| 52 | // migrated to JSON and then deleted. Safe to call when the directory does |
| 53 | // not exist yet. |
| 54 | func Load(dataFilesPath string) { |
| 55 | mu.Lock() |
| 56 | defer mu.Unlock() |
| 57 | |
| 58 | dataDir = util.FilePath(dataFilesPath, `/`, `telemetry`) |
| 59 | |
| 60 | mudlog.Info("telemetry.Load", "dir", dataDir) |
| 61 | |
| 62 | records = []Record{} |
| 63 | index = map[string]int{} |
| 64 | dirty = map[string]bool{} |
| 65 | |
| 66 | entries, err := os.ReadDir(dataDir) |
| 67 | if err != nil { |
| 68 | if os.IsNotExist(err) { |
| 69 | mudlog.Info("telemetry.Load", "status", "directory does not exist yet, will be created on first save") |
| 70 | } else { |
| 71 | mudlog.Error("telemetry.Load", "dir", dataDir, "error", err) |
| 72 | } |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | fileCount := 0 |
| 77 | for _, e := range entries { |
| 78 | if e.IsDir() { |
| 79 | continue |
| 80 | } |
| 81 | |
| 82 | name := e.Name() |
| 83 | path := filepath.Join(dataDir, name) |
| 84 | |
| 85 | switch { |
| 86 | case strings.HasSuffix(name, ".json"): |
| 87 | data, err := util.ReadFile(path) |
| 88 | if err != nil { |
| 89 | mudlog.Error("telemetry.Load", "file", name, "error", err) |
| 90 | continue |
| 91 | } |
| 92 | var loaded []Record |
| 93 | if err := json.Unmarshal(data, &loaded); err != nil { |
| 94 | mudlog.Error("telemetry.Load", "file", name, "error", err) |
| 95 | continue |
| 96 | } |
| 97 | for _, r := range loaded { |
| 98 | records = append(records, r) |
| 99 | index[recordKey(r.Date, r.Category, r.Zone, r.ItemId, r.MobId, r.RoomId, r.RaceId, r.Topic)] = len(records) - 1 |
| 100 | } |
| 101 | fileCount++ |
| 102 | |
| 103 | case strings.HasSuffix(name, ".yaml"): |
| 104 | data, err := util.ReadFile(path) |
| 105 | if err != nil { |
| 106 | mudlog.Error("telemetry.Load", "file", name, "error", err) |
| 107 | continue |
| 108 | } |
| 109 | var loaded []Record |
| 110 | if err := yaml.Unmarshal(data, &loaded); err != nil { |
| 111 | mudlog.Error("telemetry.Load", "file", name, "error", err) |