Brijesh's Git Server — watchman @ main

observability tool, needs to be rewritten once identity is stable

internal/logs.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
package internal

import (
	"encoding/json"
	"net/http"
	"watchman/schema"
	"watchman/utils"
)

func (s *Server) BatchInsertLogsHandler(w http.ResponseWriter, r *http.Request) {
	utils.HandleMethodNotAllowed(w, r, http.MethodPost)

	var logs []schema.Log
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&logs)
	utils.HandleError(w, r, http.StatusBadRequest, "Error decoding JSON: ", err)

	err = s.db.BatchInsertLogs(logs)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error inserting logs: ", err)

	response := schema.ResponseType{
		Status:    "OK",
		Message:   "Logs created successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
	}
	utils.SendResponse(w, r, response)
}

func (s *Server) GetLogsHandler(w http.ResponseWriter, r *http.Request) {
	utils.HandleMethodNotAllowed(w, r, http.MethodGet)

	projectID := r.URL.Query().Get("project_id")
	userID := r.URL.Query().Get("user_id")
	startTime := r.URL.Query().Get("start_time")
	endTime := r.URL.Query().Get("end_time")
	level := r.URL.Query().Get("level")

	logs, err := s.db.GetLogs(projectID, userID, startTime, endTime, level)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error getting logs: ", err)

	response := schema.ResponseType{
		Status:    "OK",
		Message:   "Logs retrieved successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
		Data:      logs,
	}
	utils.SendResponse(w, r, response)
}

func (s *Server) DeleteLogsHandler(w http.ResponseWriter, r *http.Request) {
	utils.HandleMethodNotAllowed(w, r, http.MethodDelete)

	projectID := r.URL.Query().Get("project_id")
	userID := r.URL.Query().Get("user_id")
	startTime := r.URL.Query().Get("start_time")
	endTime := r.URL.Query().Get("end_time")
	level := r.URL.Query().Get("level")

	err := s.db.DeleteLogs(projectID, userID, startTime, endTime, level)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error deleting logs: ", err)

	response := schema.ResponseType{
		Status:    "OK",
		Message:   "Logs deleted successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
	}
	utils.SendResponse(w, r, response)
}