Skip to content

Commit

Permalink
Add support for serving observation properties. (#1426)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyurva authored Oct 4, 2024
1 parent 2c94f88 commit 6b01171
Show file tree
Hide file tree
Showing 21 changed files with 670 additions and 265 deletions.
5 changes: 5 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ func main() {
if err := sqldb.CreateTables(sqlClient); err != nil {
log.Fatalf("Can not create tables in database: %v", err)
}

err = sqldb.CheckSchema(sqlClient)
if err != nil {
log.Fatalf("SQL schema check failed: %v", err)
}
}

// Store
Expand Down
3 changes: 2 additions & 1 deletion internal/server/v2/observation/contained_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/datacommonsorg/mixer/internal/server/resource"
"github.com/datacommonsorg/mixer/internal/server/stat"
"github.com/datacommonsorg/mixer/internal/server/v2/shared"
"github.com/datacommonsorg/mixer/internal/sqldb/sqlquery"
"github.com/datacommonsorg/mixer/internal/store/bigtable"
"github.com/datacommonsorg/mixer/internal/util"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -220,7 +221,7 @@ func FetchContainedIn(
return nil, err
}
}
directResp, err := FetchDirectSQL(
directResp, err := sqlquery.GetObservations(
ctx,
store.SQLClient,
sqlProvenances,
Expand Down
49 changes: 2 additions & 47 deletions internal/server/v2/observation/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package observation

import (
"context"
"database/sql"
"fmt"
"net/url"
"sort"
"strings"
Expand All @@ -29,6 +27,7 @@ import (
"github.com/datacommonsorg/mixer/internal/server/ranking"
"github.com/datacommonsorg/mixer/internal/server/stat"
"github.com/datacommonsorg/mixer/internal/server/v2/shared"
"github.com/datacommonsorg/mixer/internal/sqldb/sqlquery"
"github.com/datacommonsorg/mixer/internal/store"
"github.com/datacommonsorg/mixer/internal/store/bigtable"
"github.com/datacommonsorg/mixer/internal/util"
Expand Down Expand Up @@ -88,7 +87,7 @@ func FetchDirect(
if err != nil {
return nil, err
}
sqlObservation, err := FetchDirectSQL(
sqlObservation, err := sqlquery.GetObservations(
ctx,
store.SQLClient,
sqlProvenances,
Expand Down Expand Up @@ -191,47 +190,3 @@ func FetchDirectBT(
}
return result, nil
}

// FetchDirectSQL fetches data from SQLite database.
func FetchDirectSQL(
ctx context.Context,
sqlClient *sql.DB,
sqlProvenances map[string]*pb.Facet,
variables []string,
entities []string,
queryDate string,
filter *pbv2.FacetFilter,
) (*pbv2.ObservationResponse, error) {
result := initObservationResult(variables)
if sqlClient == nil {
return result, nil
}
// Construct query
entitiesStr := "'" + strings.Join(entities, "', '") + "'"
variablesStr := "'" + strings.Join(variables, "', '") + "'"
query := fmt.Sprintf(
`
SELECT entity, variable, date, value, provenance FROM observations
WHERE entity IN (%s)
AND variable IN (%s)
AND value != ''
`,
entitiesStr,
variablesStr,
)
if queryDate != "" && queryDate != shared.LATEST {
query += fmt.Sprintf("AND date = (%s) ", queryDate)
}
query += "ORDER BY date ASC;"
// Execute query
rows, err := sqlClient.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
tmp, err := handleSQLRows(rows, variables)
if err != nil {
return nil, err
}
return processSqlData(result, tmp, queryDate, sqlProvenances), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"geoId/06": {
"ordered_facets": [
{
"facet_id": "custom",
"facet_id": "2921750529",
"observations": [
{
"date": "2015",
Expand All @@ -22,7 +22,7 @@
}
},
"facets": {
"custom": {
"2921750529": {
"import_name": "Custom Prov",
"provenance_url": "custom.datacommons.org"
}
Expand Down
Loading

0 comments on commit 6b01171

Please sign in to comment.