yats.git

ref: 5646a8896664ecc31a858a514925b94c50848a76

server/db/dates_test.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
/**
 * 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"
	"os"
	"testing"
	"time"
	"yats-server/dates"
	"yats-server/model"
)

func TestTodayString(t *testing.T) {

	daysRange := CalculateDaysRange()
	fmt.Printf("from: %s to: %s\n", daysRange.From, daysRange.To)
}

func TestManyDaysAgo(t *testing.T) {

	daysRange := DaysAgo(10)
	fmt.Printf("from: %s to: %s\n", daysRange.From, daysRange.To)
}

func CalculateDaysRange() dates.DaysRange {
	currentTime := time.Now()
	yesterday := currentTime.AddDate(0, 0, -1)
	fmt.Println("today YYYY-MM-DD : ", currentTime.Format("2006-02-01"))
	fmt.Println("yesterday YYYY-MM-DD : ", yesterday.Format("2006-02-01"))
	return dates.DaysRange{
		From: yesterday.Format("2006-02-01"),
		To:   currentTime.Format("2006-02-01"),
	}
}

func DaysAgo(n int) dates.DaysRange {
	currentTime := time.Now().AddDate(0, 0, n*(-1))
	yesterday := currentTime.AddDate(0, 0, -1)
	fmt.Println("today YYYY-MM-DD : ", currentTime.Format("2006-02-01"))
	fmt.Println("yesterday YYYY-MM-DD : ", yesterday.Format("2006-02-01"))
	return dates.DaysRange{
		From: yesterday.Format("2006-02-01"),
		To:   currentTime.Format("2006-02-01"),
	}
}

func TestParquet(t *testing.T) {

	event := model.EventModel{
		IdClient: "abc",
		Etime:    int64(time.Now().UnixMilli()),
		Name:     "SomeTest",
	}

	tmpFile, _ := os.CreateTemp("/tmp/", "tempEvent-XXXX.parquet")
	EventToParquet([]model.EventModel{event}, tmpFile.Name())

	metric := model.MetricModel{
		IdClient: "mabc",
		Mtime:    int64(time.Now().UnixMilli()),
		Name:     "someName",
		Value:    "SomeValue",
	}

	tmpMetricFile, _ := os.CreateTemp("/tmp/", "tempMetric-XXXX.parquet")

	MetricToParquet([]model.MetricModel{metric}, tmpMetricFile.Name())
}

func TestDates(t *testing.T) {

	now := time.Now()

	yyyymmdd := dates.YYYYMMDD(now)
	fmt.Println("--->" + yyyymmdd)

	r := dates.CalculatePreviousMonthRange(now)

	l := dates.CalculatePreviousMonth(now)
	fmt.Printf("Range from %s to: %s\n", r.From, r.To)
	fmt.Printf("Last month: %s\n", l)

	fmt.Println(now.Format("2006-01-31"))
}