yats.git

ref: 50c609d05a1149eef5535c33306198163e2bb041

server/db/queries.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
/**
 * 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 db

import (
	"fmt"
	"github.com/gocql/gocql"
	"time"
	"yats-server/model"
)

func GetExternalSources(session *gocql.Session, idClient string) []model.ExternalSource {
	var sources []model.ExternalSource
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT app,type,name from sources where id_client='%s'", idClient)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		sources = append(sources, model.ExternalSource{
			Name: m["name"].(string),
			App:  m["app"].(string),
			Type: m["type"].(string),
		})

		m = map[string]interface{}{}
	}
	return sources
}

func GetAvailableMetrics(session *gocql.Session, idClient string) []model.AvailableMetric {
	var metrics []model.AvailableMetric
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT name,description from metric_info where id_client='%s'", idClient)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		metrics = append(metrics, model.AvailableMetric{
			Name:        m["name"].(string),
			Description: m["description"].(string),
		})

		m = map[string]interface{}{}
	}
	return metrics
}

func MetricsFrom(session *gocql.Session, idClient string, metricName string, fromTime string, limit int32) []model.MetricModel {
	var metrics []model.MetricModel
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT id_client, mtime, name, value FROM metric where id_client='%s' and name ='%s' and mtime > '%s' order by mtime limit %d", idClient, metricName, fromTime, limit)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		metrics = append(metrics, model.MetricModel{
			IdClient: m["id_client"].(string),
			Mtime:    m["mtime"].(time.Time).UnixMilli(),
			Name:     m["name"].(string),
			Value:    m["value"].(string),
		})

		m = map[string]interface{}{}
	}
	return metrics
}

func MetricsBetween(session *gocql.Session, idClient string, metricName string, fromTime string, toTime string, limit int32) []model.MetricModel {
	var metrics []model.MetricModel
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT id_client, mtime, name, value FROM metric where id_client='%s' and name ='%s' and mtime > '%s' and mtime <= '%s' order by mtime limit %d", idClient, metricName, fromTime, toTime, limit)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		metrics = append(metrics, model.MetricModel{
			IdClient: m["id_client"].(string),
			Mtime:    m["mtime"].(time.Time).UnixMilli(),
			Name:     m["name"].(string),
			Value:    m["value"].(string),
		})

		m = map[string]interface{}{}
	}
	return metrics
}

func EventsFrom(session *gocql.Session, idClient string, fromTime string, limit int32) []model.EventModel {
	var events []model.EventModel
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT id_client, etime, name FROM event where id_client='%s' and etime > '%s' order by etime limit %d", idClient, fromTime, limit)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		events = append(events, model.EventModel{
			IdClient: m["id_client"].(string),
			Etime:    m["etime"].(time.Time).UnixMilli(),
			Name:     m["name"].(string),
		})

		m = map[string]interface{}{}
	}
	return events
}

func EventsBetween(session *gocql.Session, idClient string, fromTime string, totime string, limit int32) []model.EventModel {
	var events []model.EventModel
	m := map[string]interface{}{}

	q := fmt.Sprintf("SELECT id_client, etime, name FROM event where id_client='%s' and etime > '%s' and etime <= '%s' order by etime limit %d", idClient, fromTime, totime, limit)
	fmt.Println(q)
	iter := session.Query(q).Iter()
	for iter.MapScan(m) {
		events = append(events, model.EventModel{
			IdClient: m["id_client"].(string),
			Etime:    m["etime"].(time.Time).UnixMilli(),
			Name:     m["name"].(string),
		})

		m = map[string]interface{}{}
	}
	return events
}