Skip to content

Commit

Permalink
refactor: Migrate Python code to TypeScript Serverless (#35)
Browse files Browse the repository at this point in the history
- add serverless (+deployment)
- add prettier
- migrate the following endpoints:
  - sync geozones
  - sync decrees
  - sync restrictions
  - retrieve area data
  - retrieve global statics
  • Loading branch information
deuzu authored Nov 5, 2023
1 parent f022380 commit 4de7e33
Show file tree
Hide file tree
Showing 44 changed files with 8,573 additions and 1,155 deletions.
11 changes: 11 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
end_of_line = LF
trim_trailing_whitespace = true
insert_final_newline = true

[*.{js,jsx,ts,tsx,json}]
indent_style = space
charset = utf-8
indent_size = 2
8 changes: 8 additions & 0 deletions src/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FETCH_DATA_LOCAL=true
PGDATABASE=ecowater
PGHOST=localhost
PGPORT=5432
PGUSER=ecowater
PGPASSWORD=ecowater
SECURITY_GROUP_ID=
SUBNET_ID=
13 changes: 13 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# package directories
node_modules
jspm_packages
.build

# Serverless directories
.serverless

# Dotenv
.env.*

# Local files
data/
1 change: 1 addition & 0 deletions src/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18
13 changes: 13 additions & 0 deletions src/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"useTabs": false,
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 120,
"semi": false,
"overrides": [
{
"files": "*.json.dist",
"options": { "parser": "json" }
}
]
}
53 changes: 48 additions & 5 deletions src/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
# Sync
# EcoWater's Source Code

## Getting Started
## Get Started

```console
Prerequisites:
- Node & Npm
- Docker & Docker Compose

```bash
<node-version-manager> use
npm install
docker compose up -d
docker compose run --rm app pip install -r requirements.txt
docker compose run --rm app python index.py
```

## Serverless

```bash
serverless invoke local --function synchronize-geozones
serverless invoke local --function synchronize-decrees
serverless invoke local --function synchronize-restrictions -p events/restriction-query.json
```

```bash
serverless deploy --stage staging
serverless deploy --stage prod
```

## SQL Commands

Get center point long lat of the first geozone:
```sql
SELECT gz.id, gz.name, ST_X(ST_Centroid(gz.geometry)) as longitude, ST_Y(ST_Centroid(gz.geometry)) as latitude, de.alert_level. re.user_individual
FROM geozone AS gz
INNER JOIN decree AS de ON de.geozone_id = gz.id
INNER JOIN restriction AS re ON re.decree_id = de.id
WHERE de.start_date <= NOW() AND de.end_date >= NOW()
LIMIT 1;
```

Find geozones by lat long:
```sql
SELECT gz.id, gz.external_id, gz.type, gz.name, de.alert_level
FROM geozone AS gz
INNER JOIN decree AS de ON de.geozone_id = gz.id
WHERE ST_Contains(
gz.geometry,
ST_SetSRID(
ST_MakePoint(4.356850234425376, 49.4610139711892),
4326
)
);
```
7 changes: 0 additions & 7 deletions src/compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
services:
app:
image: python
working_dir: /package
volumes:
- ./package:/package
- app_vendors:/usr/local/lib/python3.11/site-packages
pg:
image: postgis/postgis
environment:
Expand All @@ -18,5 +12,4 @@ services:
- pgdata:/var/lib/postgresql/data

volumes:
app_vendors: ~
pgdata: ~
55 changes: 55 additions & 0 deletions src/events/alert-numbers-query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"body": "{}",
"path": "/alert-numbers",
"headers": {
"Accept": "application/json",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "FR",
"Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
"X-Forwarded-For": "192.168.100.1, 192.168.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"pathParameters": {},
"multiValueHeaders": {},
"isBase64Encoded": false,
"multiValueQueryStringParameters": {},
"requestContext": {
"accountId": "123456789012",
"resourceId": "us4z18",
"stage": "test",
"requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
"identity": {
"accessKey": "",
"apiKeyId": "",
"cognitoIdentityPoolId": "",
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"apiKey": "",
"sourceIp": "192.168.100.1",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"user": ""
},
"path": "",
"requestTimeEpoch": 0,
"resourcePath": "/restrictions",
"httpMethod": "GET",
"apiId": "wt6mne2s9k"
},
"resource": "/alert-numbers",
"httpMethod": "GET",
"queryStringParameters": {},
"stageVariables": {}
}
59 changes: 59 additions & 0 deletions src/events/restriction-query.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"body": "{}",
"path": "/restrictions",
"headers": {
"Accept": "application/json",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "FR",
"Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
"X-Forwarded-For": "192.168.100.1, 192.168.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"pathParameters": {},
"multiValueHeaders": {},
"isBase64Encoded": false,
"multiValueQueryStringParameters": {},
"requestContext": {
"accountId": "123456789012",
"resourceId": "us4z18",
"stage": "test",
"requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
"identity": {
"accessKey": "",
"apiKeyId": "",
"cognitoIdentityPoolId": "",
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"apiKey": "",
"sourceIp": "192.168.100.1",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
"user": ""
},
"path": "",
"requestTimeEpoch": 0,
"resourcePath": "/restrictions",
"httpMethod": "GET",
"apiId": "wt6mne2s9k"
},
"resource": "/restrictions",
"httpMethod": "GET",
"queryStringParameters": {
"latitude": 4.3490927,
"longitude": 49.4547171,
"situation": "user_individual"
},
"stageVariables": {}
}
61 changes: 0 additions & 61 deletions src/features/display_data/data_provider.py

This file was deleted.

62 changes: 0 additions & 62 deletions src/features/display_data/getSummary.py

This file was deleted.

Loading

0 comments on commit 4de7e33

Please sign in to comment.