Brijesh's Git Server — watchman @ main

observability tool, needs to be rewritten once identity is stable

database/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
package database

import (
	"fmt"
	"time"
	"watchman/schema"
)

func (s *service) BatchInsertLogs(logs []schema.Log) error {
	stmt, err := s.db.Prepare("INSERT INTO Logs (Time, Level, Message, Subject, UserID, ProjectID) VALUES (?, ?, ?, ?, ?, ?)")
	if err != nil {
		return fmt.Errorf("error preparing statement: %v", 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)
		if err != nil {
			return fmt.Errorf("error inserting into database: %v", err)
		}
	}
	return nil
}

func (s *service) GetLogs(projectID string, userID string, startTime string, endTime string, level string) ([]schema.Log, error) {
	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 := s.db.Query(query)
	if err != nil {
		return nil, fmt.Errorf("error querying database: %v", 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)
		if err != nil {
			return nil, fmt.Errorf("error scanning row: %v", err)
		}
		logs = append(logs, log)
	}
	return logs, nil
}

func (s *service) DeleteLogs(projectID string, userID string, startTime string, endTime string, level string) error {
	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 := s.db.Prepare(query)
	if err != nil {
		return fmt.Errorf("error preparing statement: %v", err)
	}
	defer stmt.Close()
	_, err = stmt.Exec()
	if err != nil {
		return fmt.Errorf("error executing statement: %v", err)
	}
	return nil
}