-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathobservation.go
More file actions
239 lines (219 loc) · 6.45 KB
/
observation.go
File metadata and controls
239 lines (219 loc) · 6.45 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package observation is for V2 observation API
package observation
import (
"context"
"net/http"
"github.com/datacommonsorg/mixer/internal/merger"
pbv2 "github.com/datacommonsorg/mixer/internal/proto/v2"
"github.com/datacommonsorg/mixer/internal/server/cache"
"github.com/datacommonsorg/mixer/internal/server/resource"
v2 "github.com/datacommonsorg/mixer/internal/server/v2"
v2facet "github.com/datacommonsorg/mixer/internal/server/v2/facet"
"github.com/datacommonsorg/mixer/internal/server/v2/shared"
"github.com/datacommonsorg/mixer/internal/store"
"github.com/datacommonsorg/mixer/internal/util"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func ObservationCore(
ctx context.Context,
store *store.Store,
cachedata *cache.Cache,
metadata *resource.Metadata,
httpClient *http.Client,
in *pbv2.ObservationRequest,
) (*pbv2.ObservationResponse, shared.QueryType, error) {
// (TODO): The routing logic here is very rough. This needs more work.
var queryDate, queryValue, queryVariable, queryEntity, queryFacet bool
for _, item := range in.GetSelect() {
switch item {
case "date":
queryDate = true
case "value":
queryValue = true
case "variable":
queryVariable = true
case "entity":
queryEntity = true
case "facet":
queryFacet = true
}
}
if !queryVariable || !queryEntity {
return nil, "", status.Error(
codes.InvalidArgument, "Must select 'variable' and 'entity'")
}
variable := in.GetVariable()
entity := in.GetEntity()
// Observation date and value query.
if queryDate && queryValue {
// Series.
if len(variable.GetDcids()) > 0 && len(entity.GetDcids()) > 0 {
result, err := FetchDirect(
ctx,
store,
cachedata.SQLProvenances(ctx),
variable.GetDcids(),
entity.GetDcids(),
in.GetDate(),
in.GetFilter(),
)
return result, shared.QueryTypeValue, err
}
// Collection.
if len(variable.GetDcids()) > 0 && entity.GetExpression() != "" {
// Example of expression
// "geoId/06<-containedInPlace+{typeOf: City}"
expr := entity.GetExpression()
containedInPlace, err := v2.ParseContainedInPlace(expr)
if err != nil {
return nil, "", err
}
res, err := FetchContainedIn(
ctx,
store,
metadata,
cachedata.SQLProvenances(ctx),
httpClient,
metadata.RemoteMixerDomain,
variable.GetDcids(),
containedInPlace.Ancestor,
containedInPlace.ChildPlaceType,
in.GetDate(),
in.GetFilter(),
)
return res, shared.QueryTypeValue, err
}
// Derived series.
if variable.GetFormula() != "" && len(entity.GetDcids()) > 0 {
res, err := DerivedSeries(
ctx,
store,
variable.GetFormula(),
entity.GetDcids(),
)
return res, shared.QueryTypeDerived, err
}
}
// Get facet information for <variable, entity> pair.
if !queryDate && !queryValue && queryFacet {
// Series
if len(variable.GetDcids()) > 0 && len(entity.GetDcids()) > 0 {
res, err := v2facet.SeriesFacet(
ctx,
store,
cachedata,
variable.GetDcids(),
entity.GetDcids(),
)
return res, shared.QueryTypeFacet, err
}
// Collection
if len(variable.GetDcids()) > 0 && entity.GetExpression() != "" {
expr := entity.GetExpression()
containedInPlace, err := v2.ParseContainedInPlace(expr)
if err != nil {
return nil, "", err
}
res, err := v2facet.ContainedInFacet(
ctx,
store,
cachedata,
metadata,
cachedata.SQLProvenances(ctx),
httpClient,
metadata.RemoteMixerDomain,
variable.GetDcids(),
containedInPlace.Ancestor,
containedInPlace.ChildPlaceType,
in.GetDate(),
)
return res, shared.QueryTypeFacet, err
}
}
// Get existence of <variable, entity> pair.
if !queryDate && !queryValue {
if len(entity.GetDcids()) > 0 {
if len(variable.GetDcids()) > 0 {
// Have both entity.dcids and variable.dcids. Check existence cache.
res, err := Existence(
ctx, store, cachedata, variable.GetDcids(), entity.GetDcids())
return res, shared.QueryTypeExistenceByVar, err
}
// TODO: Support appending entities from entity.expression
// Only have entity.dcids, fetch variables for each entity.
res, err := Variable(ctx, store, entity.GetDcids())
return res, shared.QueryTypeExistenceByEntity, err
}
}
return &pbv2.ObservationResponse{}, "", nil
}
func ObservationInternal(
ctx context.Context,
store *store.Store,
cachedata *cache.Cache,
metadata *resource.Metadata,
httpClient *http.Client,
in *pbv2.ObservationRequest,
surface string,
) (*pbv2.ObservationResponse, shared.QueryType, error) {
errGroup, errCtx := errgroup.WithContext(ctx)
localRespChan := make(chan *pbv2.ObservationResponse, 1)
remoteRespChan := make(chan *pbv2.ObservationResponse, 1)
queryTypeChan := make(chan shared.QueryType, 1)
errGroup.Go(func() error {
localResp, queryType, err := ObservationCore(errCtx, store, cachedata, metadata, httpClient, in)
if err != nil {
return err
}
localRespChan <- localResp
// We only need to get the query type from the initial observation because
// it's based on the query parameters and will be the same in remote and local mixers.
queryTypeChan <- queryType
return nil
})
if metadata.RemoteMixerDomain != "" {
errGroup.Go(func() error {
remoteResp := &pbv2.ObservationResponse{}
err := util.FetchRemote(
metadata,
httpClient,
"/v2/observation",
in,
remoteResp,
surface,
)
if err != nil {
return err
}
remoteRespChan <- remoteResp
return nil
})
} else {
remoteRespChan <- nil
}
if err := errGroup.Wait(); err != nil {
return nil, "", err
}
close(localRespChan) //
close(remoteRespChan)
close(queryTypeChan)
localResp, remoteResp, queryType := <-localRespChan, <-remoteRespChan, <-queryTypeChan
// The order of argument matters, localResp is prefered and will be put first
// in the merged result.
return merger.MergeObservation(localResp, remoteResp), queryType, nil
}