Brijesh's Git Server — watchman @ fa969251d72ae021889eb25e4a6b4fa69c468b21

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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
package internal

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"net/http"
	"time"
	"watchman/schema"
	"watchman/utils"
)

func BatchInsertLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	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)

	fmt.Println("Logs: ", logs)

	stmt, err := db.Prepare("INSERT INTO Logs (Time, Level, Message, Subject, UserID, ProjectID) VALUES (?, ?, ?, ?, ?, ?)")
	utils.HandleError(w, r, http.StatusInternalServerError, "Error preparing statement: ", err)
	defer stmt.Close()

	for _, log := range logs {
		log.Time = int32(time.Now().Unix())
		_, err = stmt.Exec(log.Time, log.Level, log.Message, log.Subject, log.UserID, log.ProjectID)
		utils.HandleError(w, r, http.StatusInternalServerError, "Error inserting into database: ", err)
	}

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

func GetLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	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")

	query := "SELECT * FROM Logs WHERE "

	if projectID != "" {
		query += "ProjectID = '" + projectID + "' AND "
	}
	if userID != "" {
		query += "UserID = '" + userID + "' AND "
	}
	if startTime != "" {
		query += "Time > '" + startTime + "' AND "
	}
	if endTime != "" {
		query += "Time < '" + endTime + "' AND "
	}
	if level != "" {
		query += "Level = '" + level + "' AND "
	}

	if projectID != "" || userID != "" || startTime != "" || endTime != "" || level != "" {
		query = query[:len(query)-5]
	} else {
		query = query[:len(query)-7]
	}

	rows, err := db.Query(query)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error querying database: ", err)
	defer rows.Close()

	var logs []schema.Log
	for rows.Next() {
		var log schema.Log
		err = rows.Scan(&log.Time, &log.Level, &log.Message, &log.Subject, &log.UserID, &log.ProjectID)
		utils.HandleError(w, r, http.StatusInternalServerError, "Error scanning row: ", err)
		logs = append(logs, log)
	}

	w.Header().Set("Content-Type", "application/json")
	response := schema.Response_Type{
		Status:    "OK",
		Message:   "Logs retrieved successfully",
		RequestID: r.Context().Value(schema.RequestIDKey{}).(string),
		Data:      logs,
	}
	err = json.NewEncoder(w).Encode(response)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
	}
}

func DeleteLogs(w http.ResponseWriter, r *http.Request, db *sql.DB) {
	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")
	query := "DELETE FROM Logs WHERE "

	if projectID != "" {
		query += "ProjectID = '" + projectID + "' AND "
	}
	if userID != "" {
		query += "UserID = '" + userID + "' AND "
	}
	if startTime != "" {
		query += "Time > '" + startTime + "' AND "
	}
	if endTime != "" {
		query += "Time < '" + endTime + "' AND "
	}
	if level != "" {
		query += "Level = '" + level + "' AND "
	}

	if projectID != "" || userID != "" || startTime != "" || endTime != "" || level != "" {
		query = query[:len(query)-5]
	} else {
		query = query[:len(query)-7]
	}

	stmt, err := db.Prepare(query)
	utils.HandleError(w, r, http.StatusInternalServerError, "Error preparing statement: ", err)
	defer stmt.Close()

	_, err = stmt.Exec()
	utils.HandleError(w, r, http.StatusInternalServerError, "Error executing statement: ", err)

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