-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from madflojo/parsecsv
Adds Database SQL load
- Loading branch information
Showing
12 changed files
with
219 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
version: '3.8' | ||
services: | ||
airport-lookup-example: | ||
image: tarmac:local | ||
image: madflojo/tarmac:unstable | ||
ports: | ||
- 80:8080 | ||
environment: | ||
- "APP_ENABLE_TLS=false" | ||
- "APP_LISTEN_ADDR=0.0.0.0:8080" | ||
- "APP_DEBUG=true" | ||
- "APP_TRACE=true" | ||
- "APP_DEBUG=false" | ||
- "APP_TRACE=false" | ||
- "APP_WASM_FUNCTION_CONFIG=/config/tarmac.json" | ||
- "APP_ENABLE_SQL=true" | ||
- "APP_SQL_TYPE=mysql" | ||
- "APP_SQL_DSN=root:example@tcp(mysql:3306)/example" | ||
volumes: | ||
- "./config:/config" | ||
- "./functions/build:/functions" | ||
depends_on: | ||
- mysql | ||
mysql: | ||
image: bitnami/mysql:latest | ||
restart: always | ||
environment: | ||
MYSQL_ROOT_PASSWORD: example | ||
MYSQL_DATABASE: example | ||
ports: | ||
- 3306:3306 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module github.com/tarmac-project/example-airport-lookup-go/functions/src/data/load | ||
|
||
go 1.21.1 | ||
|
||
require ( | ||
github.com/tarmac-project/example-airport-lookup-go v0.0.0-20231023010400-e4b3efdea82f | ||
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 | ||
) | ||
|
||
require ( | ||
github.com/enescakir/emoji v1.0.0 // indirect | ||
github.com/valyala/fastjson v1.6.4 // indirect | ||
github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/enescakir/emoji v1.0.0 h1:W+HsNql8swfCQFtioDGDHCHri8nudlK1n5p2rHCJoog= | ||
github.com/enescakir/emoji v1.0.0/go.mod h1:Bt1EKuLnKDTYpLALApstIkAjdDrS/8IAgTkKp+WKFD0= | ||
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7 h1:xoIK0ctDddBMnc74udxJYBqlo9Ylnsp1waqjLsnef20= | ||
github.com/pquerna/ffjson v0.0.0-20190930134022-aa0246cd15f7/go.mod h1:YARuvh7BUWHNhzDq2OM5tzR2RiCcN2D7sapiKyCel/M= | ||
github.com/tarmac-project/example-airport-lookup-go v0.0.0-20231023010400-e4b3efdea82f h1:XhPXDWSza1ae0WBqq3iwRGR5N2/x5W0gFamNULathXM= | ||
github.com/tarmac-project/example-airport-lookup-go v0.0.0-20231023010400-e4b3efdea82f/go.mod h1:mpM+9904v8UU6HzL/giS7yLzuGvezbDQvdKw6byxO80= | ||
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 h1:QKsEf6SXTYrJM9/B4cNoM4RS3/rzuViJaiutEcdSRZQ= | ||
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0/go.mod h1:UTKYV0QFdkJDgV2sJcnuCujVy49MCd8bgi2JmwviJ6E= | ||
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ= | ||
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY= | ||
github.com/wapc/wapc-guest-tinygo v0.3.3 h1:jLebiwjVSHLGnS+BRabQ6+XOV7oihVWAc05Hf1SbeR0= | ||
github.com/wapc/wapc-guest-tinygo v0.3.3/go.mod h1:mzM3CnsdSYktfPkaBdZ8v88ZlfUDEy5Jh5XBOV3fYcw= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/tarmac-project/example-airport-lookup-go/pkg/airport/parsers/csv" | ||
"github.com/tarmac-project/tarmac/pkg/sdk" | ||
"html" | ||
) | ||
|
||
type Function struct { | ||
tarmac *sdk.Tarmac | ||
} | ||
|
||
func (f *Function) Handler(_ []byte) ([]byte, error) { | ||
f.tarmac.Logger.Info("Airport raw data download starting") | ||
|
||
// Fetch the airport data | ||
data, err := f.tarmac.Function.Call("fetch", []byte("")) | ||
if err != nil { | ||
f.tarmac.Logger.Error(fmt.Sprintf("Failed to fetch airport data - %s", err)) | ||
return []byte(""), fmt.Errorf("Failed to fetch airport data: %s", err) | ||
} | ||
|
||
f.tarmac.Logger.Info("Airport raw data download complete, parsing data") | ||
|
||
// Parse the data | ||
parser, err := csv.New(bytes.NewReader(data)) | ||
if err != nil { | ||
f.tarmac.Logger.Error(fmt.Sprintf("Failed to create CSV parser - %s", err)) | ||
return []byte(""), fmt.Errorf("Failed to create CSV parser: %s", err) | ||
} | ||
|
||
airports, err := parser.Parse() | ||
if err != nil { | ||
f.tarmac.Logger.Error(fmt.Sprintf("Failed to parse airport data - %s", err)) | ||
return []byte(""), fmt.Errorf("Failed to parse airport data: %s", err) | ||
} | ||
f.tarmac.Logger.Info(fmt.Sprintf("Fetched %d airports", len(airports))) | ||
|
||
// Update the database | ||
success := 0 | ||
failure := 0 | ||
for _, airport := range airports { | ||
query := fmt.Sprintf(`INSERT INTO airports ( | ||
local_code, | ||
name, | ||
type, | ||
type_emoji, | ||
continent, | ||
iso_country, | ||
iso_region, | ||
municipality, | ||
emoji, | ||
status | ||
) VALUES ( | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s', | ||
'%s') | ||
ON DUPLICATE KEY UPDATE | ||
name = '%s', | ||
type = '%s', | ||
type_emoji = '%s', | ||
continent = '%s', | ||
iso_country = '%s', | ||
iso_region = '%s', | ||
municipality = '%s', | ||
emoji = '%s', | ||
status = '%s';`, | ||
html.EscapeString(airport.LocalCode), | ||
html.EscapeString(airport.Name), | ||
html.EscapeString(airport.Type), | ||
html.EscapeString(airport.TypeEmoji), | ||
html.EscapeString(airport.Continent), | ||
html.EscapeString(airport.ISOCountry), | ||
html.EscapeString(airport.ISORegion), | ||
html.EscapeString(airport.Municipality), | ||
html.EscapeString(airport.Emoji), | ||
html.EscapeString(airport.Status), | ||
html.EscapeString(airport.Name), | ||
html.EscapeString(airport.Type), | ||
html.EscapeString(airport.TypeEmoji), | ||
html.EscapeString(airport.Continent), | ||
html.EscapeString(airport.ISOCountry), | ||
html.EscapeString(airport.ISORegion), | ||
html.EscapeString(airport.Municipality), | ||
html.EscapeString(airport.Emoji), | ||
html.EscapeString(airport.Status), | ||
) | ||
f.tarmac.Logger.Trace(fmt.Sprintf("Executing query: %s", query)) | ||
|
||
_, err := f.tarmac.SQL.Query(query) | ||
if err != nil { | ||
f.tarmac.Logger.Debug(fmt.Sprintf("Failed to execute query - %s", err)) | ||
failure++ | ||
continue | ||
} | ||
success++ | ||
} | ||
f.tarmac.Logger.Info(fmt.Sprintf("Executed %d queries successfully, %d failures", success, failure)) | ||
|
||
return []byte(""), nil | ||
} | ||
|
||
func main() { | ||
var err error | ||
|
||
// Initialize Function | ||
f := &Function{} | ||
|
||
// Initialize the Tarmac SDK | ||
f.tarmac, err = sdk.New(sdk.Config{ | ||
Namespace: "airport-lookup", | ||
Handler: f.Handler, | ||
}) | ||
if err != nil { | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
module github.com/tarmac-project/example-airport-lookup-go/functions/src/init | ||
|
||
go 1.21 | ||
go 1.21.1 | ||
|
||
require ( | ||
github.com/tarmac-project/example-airport-lookup-go v0.0.0-20231023001804-82de5425cb1e | ||
github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 | ||
) | ||
require github.com/tarmac-project/tarmac/pkg/sdk v0.5.0 | ||
|
||
require ( | ||
github.com/enescakir/emoji v1.0.0 // indirect | ||
github.com/valyala/fastjson v1.6.4 // indirect | ||
github.com/wapc/wapc-guest-tinygo v0.3.3 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters