-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
182 lines (153 loc) · 4.6 KB
/
config.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package config
import (
"context"
"fmt"
"time"
bq "cloud.google.com/go/bigquery"
"github.com/rs/zerolog/log"
"golang.org/x/xerrors"
"google.golang.org/api/option"
)
type Config struct {
Project []Project
}
type Project struct {
ID string
Dataset []Dataset
}
type Dataset struct {
ID string
TableConfig []TableConfig
}
type TableConfig struct {
Table string
DateForShards string
TimeThreshold *TimeThreshold
DurationThreshold *DurationThreshold
}
type TimeThreshold struct {
time.Time
}
type DurationThreshold struct {
time.Duration
}
const timefmt = "15:04:05"
func (t *TimeThreshold) UnmarshalText(text []byte) error {
tmp, err := time.Parse(timefmt, string(text))
current := time.Now().In(time.Local)
t.Time = getTodaysClockObject(tmp, current)
return err
}
func (t TimeThreshold) MarshalText() (text []byte, err error) {
text = []byte(t.Format(timefmt))
return text, nil
}
func (d *DurationThreshold) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (d DurationThreshold) MarshalText() (text []byte, err error) {
return []byte(d.Duration.String()), nil
}
func getTodaysClockObject(clock, current time.Time) time.Time {
y, m, d := current.Date()
hh, mm, ss := clock.Clock()
return time.Date(y, m, d, hh, mm, ss, 0, time.Local)
}
type FreshnessResult struct {
Table string
Reason []string
}
// CheckFreshness returns old tables whose last modified time is oldeer than time threshold on the config file.
func CheckFreshness(config Config, current time.Time, opts ...option.ClientOption) (oldTables []FreshnessResult, err error) {
ctx := context.Background()
for _, pj := range config.Project {
client, err := bq.NewClient(ctx, pj.ID, opts...)
if err != nil {
return nil, xerrors.Errorf("failed to create client: %w", err)
}
for _, ds := range pj.Dataset {
for _, tc := range ds.TableConfig {
tableID := getSuitableTableID(tc)
md, err := client.Dataset(ds.ID).Table(tableID).Metadata(ctx)
if err != nil { // table is not created
log.Warn().Msgf("failed to fetch metadata: table: %s.%s", ds.ID, tableID)
// Before time threshold, table may not exist.
if tc.TimeThreshold == nil || current.After(tc.TimeThreshold.Time) {
oldTables = append(oldTables, FreshnessResult{
Table: fmt.Sprintf("%s.%s.%s", pj.ID, ds.ID, tableID),
Reason: []string{"Table doesn't exist"},
})
}
continue
}
if old, reason := tc.isOld(current, md.LastModifiedTime); old {
oldTables = append(oldTables, FreshnessResult{
Table: md.FullID,
Reason: reason,
})
}
}
}
}
return oldTables, nil
}
func getSuitableTableID(tc TableConfig) string {
datefmt := "20060102"
tableIDPrefix := tc.Table
switch tc.DateForShards {
case "TODAY":
{
return tableIDPrefix + time.Now().In(time.Local).Format(datefmt)
}
case "ONE_DAY_AGO":
{
return tableIDPrefix + time.Now().In(time.Local).AddDate(0, 0, -1).Format(datefmt)
}
case "FIRST_DAY_OF_THE_MONTH":
{
now := time.Now().In(time.Local)
firstDayOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local).Format(datefmt)
return tableIDPrefix + firstDayOfMonth
}
case "": // non-sharded table
{
return tableIDPrefix
}
default: // TODO: handle error
{
log.Info().Msgf("invalid format DateForShards: %s", tc.DateForShards)
return tableIDPrefix
}
}
}
func (t *TableConfig) isOld(current, lastModified time.Time) (isOld bool, reason []string) {
isOld, timeReason := t.isOldForTimeThreshold(lastModified)
if isOld {
reason = append(reason, timeReason)
}
isOld, durationReason := t.isOldForDurationThreshold(current, lastModified)
if isOld {
reason = append(reason, durationReason)
}
return len(reason) > 0, reason
}
func (t *TableConfig) isOldForTimeThreshold(lastModified time.Time) (isOld bool, reason string) {
if t.TimeThreshold == nil {
return false, ""
}
if !lastModified.After(t.TimeThreshold.Time) {
return false, ""
}
return true, fmt.Sprintf("The table should be created by %s, but last modified time is %s", t.TimeThreshold.Time.Format("15:04"), lastModified.Format("15:04"))
}
func (t *TableConfig) isOldForDurationThreshold(current, lastModified time.Time) (isOld bool, reason string) {
if t.DurationThreshold == nil {
return false, ""
}
if current.In(time.Local).Sub(lastModified.In(time.Local)) < t.DurationThreshold.Duration {
return false, ""
}
return true, fmt.Sprintf("The table should be modified in %s, but not modified in %s", t.DurationThreshold.Duration, current.In(time.Local).Sub(lastModified.In(time.Local)))
}