Download task_047/solution/code/log_analyzer.go from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/solution/code/log_analyzer.go
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_047/solution/code/log_analyzer.go
-
curl -L -o log_analyzer.go https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_047/solution/code/log_analyzer.go
4.32 kB
| package loganalyzer | |
| import ( | |
| "bufio" | |
| "encoding/json" | |
| "errors" | |
| "fmt" | |
| "io" | |
| "net/http" | |
| "os" | |
| "strings" | |
| "sync" | |
| "time" | |
| ) | |
| type Log struct { | |
| ID string `json:"id"` | |
| Message string `json:"message"` | |
| Level string `json:"level"` | |
| Timestamp string `json:"timestamp"` | |
| Tag string `json:"tag"` | |
| } | |
| type Repository interface { | |
| Save(log Log) error | |
| Get(id string) (Log, bool) | |
| } | |
| func GenerateTag(log Log) string { | |
| text := strings.ToLower(log.Level + " " + log.Message) | |
| switch { | |
| case strings.Contains(text, "error"): | |
| return "error" | |
| case strings.Contains(text, "warn"): | |
| return "warn" | |
| case strings.Contains(text, "login"): | |
| return "auth" | |
| default: | |
| return strings.ToLower(log.Level) | |
| } | |
| } | |
| type memoryRepository struct { | |
| mu sync.RWMutex | |
| data map[string]Log | |
| } | |
| func NewMemoryRepository() Repository { return &memoryRepository{data: map[string]Log{}} } | |
| func (r *memoryRepository) Save(log Log) error { | |
| r.mu.Lock() | |
| defer r.mu.Unlock() | |
| r.data[log.ID] = log | |
| return nil | |
| } | |
| func (r *memoryRepository) Get(id string) (Log, bool) { | |
| r.mu.RLock() | |
| defer r.mu.RUnlock() | |
| log, ok := r.data[id] | |
| return log, ok | |
| } | |
| type fileRepository struct { | |
| mu sync.Mutex | |
| path string | |
| mem Repository | |
| } | |
| func NewFileRepository(path string) Repository { | |
| r := &fileRepository{path: path, mem: NewMemoryRepository()} | |
| file, err := os.Open(path) | |
| if err == nil { | |
| defer file.Close() | |
| scanner := bufio.NewScanner(file) | |
| scanner.Buffer(make([]byte, 4096), 2*1024*1024) | |
| for scanner.Scan() { | |
| var item Log | |
| if json.Unmarshal(scanner.Bytes(), &item) == nil { | |
| _ = r.mem.Save(item) | |
| } | |
| } | |
| } | |
| return r | |
| } | |
| func (r *fileRepository) Save(log Log) error { | |
| r.mu.Lock() | |
| defer r.mu.Unlock() | |
| file, err := os.OpenFile(r.path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) | |
| if err != nil { | |
| return err | |
| } | |
| defer file.Close() | |
| raw, err := json.Marshal(log) | |
| if err != nil { | |
| return err | |
| } | |
| _, err = file.Write(append(raw, '\n')) | |
| if err != nil { | |
| return err | |
| } | |
| return r.mem.Save(log) | |
| } | |
| func (r *fileRepository) Get(id string) (Log, bool) { return r.mem.Get(id) } | |
| func NewRepositoryFromConfig(kind string, path string) Repository { | |
| if strings.ToLower(kind) == "file" { | |
| return NewFileRepository(path) | |
| } | |
| return NewMemoryRepository() | |
| } | |
| type Service struct{ Repo Repository } | |
| var ErrInvalidLog = errors.New("invalid log") | |
| func NewService(repo Repository) *Service { return &Service{Repo: repo} } | |
| func (s *Service) Process(log Log) (Log, error) { | |
| if strings.TrimSpace(log.ID) == "" || strings.TrimSpace(log.Message) == "" || strings.TrimSpace(log.Level) == "" || strings.TrimSpace(log.Timestamp) == "" { | |
| return Log{}, fmt.Errorf("%w: id, message, level and timestamp are required", ErrInvalidLog) | |
| } | |
| if _, err := time.Parse(time.RFC3339, log.Timestamp); err != nil { | |
| return Log{}, fmt.Errorf("%w: timestamp must be RFC3339", ErrInvalidLog) | |
| } | |
| if log.Tag == "" { | |
| log.Tag = GenerateTag(log) | |
| } | |
| if err := s.Repo.Save(log); err != nil { | |
| return Log{}, err | |
| } | |
| return log, nil | |
| } | |
| func NewHandler(repo Repository) http.Handler { | |
| svc := NewService(repo) | |
| mux := http.NewServeMux() | |
| mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { | |
| if r.Method != http.MethodPost { | |
| http.Error(w, "method not allowed", http.StatusMethodNotAllowed) | |
| return | |
| } | |
| var log Log | |
| decoder := json.NewDecoder(r.Body) | |
| if err := decoder.Decode(&log); err != nil { | |
| http.Error(w, err.Error(), http.StatusBadRequest) | |
| return | |
| } | |
| if decoder.Decode(new(any)) != io.EOF { | |
| http.Error(w, "request must contain one JSON log", http.StatusBadRequest) | |
| return | |
| } | |
| saved, err := svc.Process(log) | |
| if err != nil { | |
| code := http.StatusInternalServerError | |
| if errors.Is(err, ErrInvalidLog) { | |
| code = http.StatusBadRequest | |
| } | |
| http.Error(w, err.Error(), code) | |
| return | |
| } | |
| w.Header().Set("Content-Type", "application/json") | |
| _ = json.NewEncoder(w).Encode(saved) | |
| }) | |
| mux.HandleFunc("/log/", func(w http.ResponseWriter, r *http.Request) { | |
| if r.Method != http.MethodGet { | |
| http.Error(w, "method not allowed", http.StatusMethodNotAllowed) | |
| return | |
| } | |
| id := strings.TrimPrefix(r.URL.Path, "/log/") | |
| log, ok := repo.Get(id) | |
| if !ok { | |
| http.NotFound(w, r) | |
| return | |
| } | |
| w.Header().Set("Content-Type", "application/json") | |
| _ = json.NewEncoder(w).Encode(log) | |
| }) | |
| return mux | |
| } | |