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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
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) if err != nil { w.WriteHeader(http.StatusBadRequest) fmt.Fprintf(w, "Error decoding log data: %v", err) return } stmt, err := db.Prepare("INSERT INTO Logs (Time, Level, Message, Subject, UserID, ProjectID) VALUES (?, ?, ?, ?, ?, ?)") if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error preparing SQL statement: %v", err) return } 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 { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error inserting log into database: %v", err) return } } response := schema.Response_Type{ Status: "OK", Message: "Logs created successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string), } w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(response) if err != nil { w.WriteHeader(http.StatusInternalServerError) } } 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) if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error querying database: %v", err) return } 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 { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error scanning row: %v", err) return } 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) } } // DeleteLogs function similar to above GetLogs that takes in the same parameters and deletes the logs from the database 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 " } query = query[:len(query)-5] stmt, err := db.Prepare(query) if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error preparing SQL statement: %v", err) return } defer stmt.Close() _, err = stmt.Exec() if err != nil { w.WriteHeader(http.StatusInternalServerError) fmt.Fprintf(w, "Error deleting logs from database: %v", err) return } response := schema.Response_Type{ Status: "OK", Message: "Logs deleted successfully", RequestID: r.Context().Value(schema.RequestIDKey{}).(string), } w.Header().Set("Content-Type", "application/json") err = json.NewEncoder(w).Encode(response) if err != nil { w.WriteHeader(http.StatusInternalServerError) } } |