Skip to content

fix(antennas-postgres) to make it more suitable for quickstarts #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/tests/antennas-postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ curl -fsSL http://stedolan.github.io/jq/download/linux64/jq > /usr/local/bin/jq
chmod +x /usr/local/bin/jq

# Turn on the demo and give it a few (good) seconds to spin up.
docker-compose up -d
AUTOSETUP=1 docker-compose up -d
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool! I think this approach solves @ruf-io's #28.

sleep 20

# Run a SQL using the sql endpoint
Expand Down
9 changes: 5 additions & 4 deletions antennas-postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ https://user-images.githubusercontent.com/11491779/166932582-e5a9fd47-e397-4419-

If you want to try it right now, clone the project and run:

```
docker-compose up
```bash
# Run `AUTOSETUP=1 docker-compose up` to run steps 2-3 automatically
docker-compose up
```

After a successful build:
Expand Down Expand Up @@ -100,14 +101,14 @@ GRANT SELECT ON antennas, antennas_performance TO materialize;


-- Create the Postgres Source
CREATE MATERIALIZED SOURCE IF NOT EXISTS antennas_publication_source
CREATE SOURCE IF NOT EXISTS antennas_publication_source
FROM POSTGRES
CONNECTION 'host=postgres port=5432 user=materialize password=materialize dbname=postgres'
PUBLICATION 'antennas_publication_source';


-- Turn the Postgres tables into Materialized Views
CREATE MATERIALIZED VIEWS FROM SOURCE antennas_publication_source;
CREATE VIEWS FROM SOURCE antennas_publication_source;


-- Filter last half minute updates and aggregate by anntena ID and GeoJSON to obtain the average performance in the last half minute.
Expand Down
1 change: 1 addition & 0 deletions antennas-postgres/backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async function* antennasUpdates(_, ctxVars) {
.finally(() => {
console.log('Finished tail.');
done = true;
resolve([]);
});

connectionEventEmitter.on('disconnect', (unsubscriptionId) => {
Expand Down
2 changes: 2 additions & 0 deletions antennas-postgres/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ services:
container_name: helper
build:
context: ./helper
environment:
- AUTOSETUP=${AUTOSETUP}
init: true
depends_on:
- materialized
Expand Down
30 changes: 20 additions & 10 deletions antennas-postgres/helper/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function setUpMaterialize() {
const poolClient = await pool.connect();

await poolClient.query(`
CREATE MATERIALIZED SOURCE IF NOT EXISTS antennas_publication_source
CREATE SOURCE IF NOT EXISTS antennas_publication_source
FROM POSTGRES
CONNECTION 'host=postgres port=5432 user=materialize password=materialize dbname=postgres'
PUBLICATION 'antennas_publication_source';
Expand All @@ -27,7 +27,7 @@ async function setUpMaterialize() {

if (!rowCount) {
await poolClient.query(`
CREATE MATERIALIZED VIEWS FROM SOURCE antennas_publication_source;
CREATE VIEWS FROM SOURCE antennas_publication_source;
`);

await poolClient.query(`
Expand Down Expand Up @@ -76,11 +76,21 @@ async function dataGenerator() {
}, 1000);
}

setUpMaterialize()
.then(() => {
console.log('Generating data.');
dataGenerator();
})
.catch((err) => {
console.error(err);
});
const {AUTOSETUP} = process.env;

/**
* If AUTOSETUP = true then run automatically the source creation, etc..
*/
if (AUTOSETUP) {
setUpMaterialize()
.then(() => {
console.log('Generating data.');
dataGenerator();
})
.catch((err) => {
console.error(err);
});
} else {
console.log('Generating data.');
dataGenerator();
}
32 changes: 22 additions & 10 deletions antennas-postgres/microservice/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,31 @@ const antennasPerformanceListener = (data) => {

const onError = (err) => {
console.error('Ouch. Some error: ', err);

setTimeout(() => {
subscribe();
}, 10000);
};

const onComplete = () => {
console.log('Finished.');

setTimeout(() => {
subscribe();
}, 10000);
};

graphqlClient.subscribe(
{
query: 'subscription { antennasUpdates { antenna_id, geojson, performance } }',
},
{
next: antennasPerformanceListener,
error: onError,
complete: onComplete,
}
);
const subscribe = () => {
graphqlClient.subscribe(
{
query: 'subscription { antennasUpdates { antenna_id, geojson, performance } }',
},
{
next: antennasPerformanceListener,
error: onError,
complete: onComplete,
}
);
};

subscribe();