-
Notifications
You must be signed in to change notification settings - Fork 2
/
helper.go
151 lines (134 loc) · 3.78 KB
/
helper.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
package bqwt
import (
"cloud.google.com/go/bigquery"
"context"
"encoding/json"
"fmt"
"github.com/viant/toolbox/cred"
"github.com/viant/toolbox/secret"
"net/http"
"os"
"strings"
"time"
)
func loadCredentials(location string) (*cred.Config, error) {
if location == "" {
location = os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
}
home := os.Getenv("HOME")
secretService := secret.New(home, false)
return secretService.CredentialsFromLocation(location)
}
func getProjectID(datasetID string) (string, error) {
datasetFragments := strings.SplitN(datasetID, ":", 2)
if len(datasetFragments) == 2 {
return datasetFragments[0], nil
}
credConfig, err := loadCredentials("")
if err != nil {
return "", err
}
return credConfig.ProjectID, nil
}
func getSchemaName(datasetID string) string {
datasetFragments := strings.SplitN(datasetID, ":", 2)
if len(datasetFragments) == 2 {
return datasetFragments[1]
}
return ""
}
// GetTablesInfo returns table info for supplied dataset
func GetTablesInfo(ctx context.Context, request *Request, lastModified bool) ([]*TableInfo, error) {
projectID, err := getProjectID(request.DatasetID)
if err != nil {
return nil, err
}
//schema is same as dataset
schema := getSchemaName(request.DatasetID)
if schema == "" {
return nil, fmt.Errorf("dataset schema field is empty")
}
SQL := ""
useLegacy := true
if !lastModified {
loopBackTime := time.Now().Add(-time.Second * time.Duration(request.LoopbackWindowInSec))
SQL = fmt.Sprintf(TableInfoLegacySQL, request.DatasetID, loopBackTime.Unix()*1000)
if request.StorageRegion != "" {
useLegacy = false
SQL = fmt.Sprintf(TableInfoStandardSQL, request.StorageRegion, loopBackTime.Unix()*1000, schema)
}
} else {
SQL = fmt.Sprintf(LastModifiedTableLegacySQL, request.DatasetID)
if request.StorageRegion != "" {
useLegacy = false
SQL = fmt.Sprintf(LastModifiedTableStandardSQL, request.StorageRegion, schema)
}
}
return GetTablesInfoFromSQL(ctx, projectID, request.Location, SQL, useLegacy)
}
func GetTablesInfoFromSQL(ctx context.Context, projectID, datasetLocation, SQL string, useLegacy bool) ([]*TableInfo, error) {
var err error
var result = make([]*TableInfo, 0)
if err = RunBQQuery(ctx, projectID, datasetLocation, SQL, []interface{}{}, useLegacy, func(row []bigquery.Value) (b bool, e error) {
tableName := AsString(row[1])
info := &TableInfo{
ProjectID: projectID,
DatasetID: AsString(row[0]),
TableID: tableName,
}
if info.Created, err = AsTime(row[2]); err != nil {
return false, err
}
if info.LastModified, err = AsTime(row[3]); err != nil {
return false, err
}
result = append(result, info)
return true, nil
}); err != nil {
return nil, err
}
return result, err
}
func getTempURL(URL string) string {
return URL + "-tmp"
}
func buildRequest(httpRequest *http.Request) (*Request, error) {
request := &Request{}
if httpRequest.ContentLength > 0 {
return request, json.NewDecoder(httpRequest.Body).Decode(request)
}
formFields, err := NewFormFields(httpRequest)
if err == nil {
err = formFields.Validate()
}
if err != nil {
return nil, err
}
return formFields.AsRequest()
}
func HandleRequest(w http.ResponseWriter, r *http.Request) {
request, err := buildRequest(r)
if err != nil {
handleError(err, w)
return
}
srv := New()
response := srv.Handle(request)
if response.Error != "" {
handleError(response.Error, w)
return
}
if request.AbsoluteExpression {
_, err = fmt.Fprint(w, response.Meta.AbsoluteExpression)
} else if request.Expression {
_, err = fmt.Fprint(w, response.Meta.Expression)
} else {
err = json.NewEncoder(w).Encode(response)
}
if err != nil {
handleError(err, w)
}
}
func handleError(err interface{}, w http.ResponseWriter) {
http.Error(w, fmt.Sprintf("Error %v", err), http.StatusInternalServerError)
}