yats.git

ref: 5c9a5a1c5e7481ebba418da8a8f17fa14ed85d30

server/rest/rest-event.go


  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
/**
 * Yats - yats
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Paolo Lulli <kevwe.com>
 * @copyright Paolo Lulli 2024
 */

package rest

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
	"yats-server/config"
	"yats-server/db"
	"yats-server/model"
)

// WriteEvent godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary write event to db
// @Schemes
// @Description store events
// @Tags Events
// @Accept json
// @Param event1 body model.EventModel true "Event body data"
// @Produce json
// @Success 202 {string} WriteEvent
// @Router /event [post]
func WriteEvent(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {
		var event model.EventModel

		if err := c.BindJSON(&event); err != nil {
			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
			return
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("%s / %s ", clientCN, event.Name)

		if event.Etime == 0 {
			db.SaveEvent(clientCN, event.Name)
		} else {
			unixTimeUTC := time.Unix(event.Etime, 0)
			db.SaveEventAt(clientCN, unixTimeUTC, event.Name)
		}
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
	}
}

// SearchEvents  godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary Get the events From ... and To when present
// @Schemes
// @Description retrieve events in the specified time window
// @Tags Events
// @Accept json
// @Param event2 body model.EventSearchRequest true "Event query filters"
// @Produce json
// @Success 200 {string} SearchEvents
// @Router /event/search [post]
func SearchEvents(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {
		var event model.EventSearchRequest

		if err := c.BindJSON(&event); err != nil {
			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
			return
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("Client ID: %s\n ", clientCN)

		if event.SourceApplication != "" {
			if !db.CanReadSourceEvent(clientCN, event.SourceApplication) {
				c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
			}
			clientCN = event.SourceApplication
		}

		var eventsPack []model.EventModel
		if event.To == 0 {
			unixTimeUTC := time.Unix(event.From, 0)
			timeAsBytes, _ := unixTimeUTC.UTC().MarshalText()
			eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100)
		} else if event.From != 0 {
			fromUnixTimeUTC := time.Unix(event.From, 0)
			toUnixTimeUTC := time.Unix(event.To, 0)
			fromInBytes, _ := fromUnixTimeUTC.UTC().MarshalText()
			toInBytes, _ := toUnixTimeUTC.UTC().MarshalText()
			eventsPack = db.EventsBetween(db.Session, clientCN, string(fromInBytes), string(toInBytes), 100)
		}
		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": eventsPack})
	}
}

// GetEventsFrom godoc
// @Param X-SSL-Client-CN header string true "clientCN"
// @Summary Get the events from timestamp {from}
// @Schemes
// @Description retrieve events  from timestamp {from}
// @Tags Events
// @Accept json
// @Param event2 body model.EventSearchRequest true "Event query filters"
// @Produce json
// @Success 200 {string} SearchEvents
// @Router /event/{from} [get]
// @Param from path string true "Starting from timestamp :from"
func GetEventsFrom(cfg config.Configuration) gin.HandlerFunc {
	return func(c *gin.Context) {

		fromParam := c.Param("from")
		fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64)
		if err != nil {
			c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
		}

		clientCN := GetClientCN(c, cfg)
		fmt.Printf("Client ID: %s\n ", clientCN)

		var eventsPack []model.EventModel

		unixTimeUTC := time.Unix(fromParamInt64, 0)
		timeAsBytes, _ := unixTimeUTC.UTC().MarshalText()
		eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100)

		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": eventsPack})
	}
}