ref: 5afe7695f8c71bda069c2d8fd0e8cffc270d43b7
server/db/metric.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 |
/** * 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/dates" "yats-server/model" ) type MetricModel struct { IdClient string `json:"id_client"` Mtime time.Time `json:"mtime"` Name string `json:"name"` Value string `json:"value"` } func PrintMetrics(metrics []model.MetricModel) { for _, metric := range metrics { fmt.Printf("%s\n", metric.Name) } } func LoadMetrics(session *gocql.Session, idClient string, daysRange dates.DaysRange) []model.MetricModel { var metrics []model.MetricModel m := map[string]interface{}{} metricInfos := GetClientMetrics(session, idClient) for mx := range metricInfos { metricName := metricInfos[mx].Name fmt.Printf("Querying from: %s to: %s\n", daysRange.From, daysRange.To) q := fmt.Sprintf("SELECT id_client, mtime, name, value FROM metric where id_client='%s' and name ='%s' and mtime > '%s' and mtime < '%s'", idClient, metricName, daysRange.From, daysRange.To) 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 DeleteMetrics(session *gocql.Session, idClient string, daysRange dates.DaysRange) { metricInfos := GetClientMetrics(session, idClient) for mx := range metricInfos { metricName := metricInfos[mx].Name fmt.Printf("Querying from: %s to: %s\n", daysRange.From, daysRange.To) q := fmt.Sprintf("DELETE FROM metric where id_client='%s' and name ='%s' and mtime > '%s' and mtime < '%s'", idClient, metricName, daysRange.From, daysRange.To) fmt.Println(q) session.Query(q).Exec() } } func SaveMetric(idClient string, metricName string, metricValue string) { q := fmt.Sprintf("insert into metric ( id_client, mtime, name, value) values ('%s',toTimestamp(now()),'%s','%s');", idClient, metricName, metricValue) fmt.Println(q) Session.Query(q).Exec() } func SaveMetricAt(idClient string, tstamp time.Time, metricName string, metricValue string) { timeAsBytes, _ := tstamp.UTC().MarshalText() q := fmt.Sprintf("insert into metric ( id_client, mtime, name, value) values ('%s','%s','%s','%s');", idClient, string(timeAsBytes), metricName, metricValue) Session.Query(q).Exec() } func CanReadSourceMetric(idClient string, sourceApp string, metricName string) bool { m := map[string]interface{}{} q := fmt.Sprintf(" SELECT name,app,type from sources where id_client='%s' and app='%s' and type='metric' and name='%s';", idClient, sourceApp, metricName) fmt.Println(q) iter := Session.Query(q).Iter() for iter.MapScan(m) { return true } return false } |