yats.git

ref: 083a6fff33f5c3864d60e688dccccabb23d3f30d

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
/**
 * 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 MetricsFrom(session *gocql.Session, idClient string, metricName string, fromTime string, limit int32) []model.MetricRequest {
	var metrics []model.MetricRequest
	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.MetricRequest{
			ID_client: m["id_client"].(string),
			Mtime:     m["mtime"].(time.Time).UnixMilli(),
			Name:      m["name"].(string),
			Value:     m["value"].(string),
		})

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