Skip to content
Closed
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
118 changes: 57 additions & 61 deletions README.md

Large diffs are not rendered by default.

112 changes: 112 additions & 0 deletions backend/src/scripts/wine-stats.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Prints a summary of what is in the catalog. Handy for checking an import.
// Usage: tsx src/scripts/wine-stats.ts
import { pool } from '../db';

interface Totals {
wines: number;
ratings: number;
countries: number;
wineries: number;
grapes: number;
regions: number;
average: number;
database_size: string;
}

const TOTALS = `
SELECT
(SELECT count(*) FROM wine) AS wines,
(SELECT count(*) FROM community_rating) AS ratings,
(SELECT count(DISTINCT country) FROM wine) AS countries,
(SELECT count(DISTINCT winery_name) FROM wine) AS wineries,
(SELECT count(DISTINCT region_name) FROM wine) AS regions,
(SELECT count(DISTINCT grape) FROM wine, unnest(grapes) AS grape) AS grapes,
(SELECT round(avg(rating), 2) FROM community_rating) AS average,
pg_size_pretty(pg_database_size(current_database())) AS database_size
`;

const BY_TYPE = `
SELECT type, count(*) AS wines, round(avg(community_avg), 2) AS average
FROM wine
WHERE community_avg IS NOT NULL
GROUP BY type
ORDER BY wines DESC
`;

const TOP_COUNTRIES = `
SELECT country, count(*) AS wines, round(avg(community_avg), 2) AS average
FROM wine
WHERE country IS NOT NULL AND community_avg IS NOT NULL
GROUP BY country
ORDER BY wines DESC
LIMIT 10
`;

const TOP_GRAPES = `
SELECT grape, count(*) AS wines
FROM wine, unnest(grapes) AS grape
GROUP BY grape
ORDER BY wines DESC
LIMIT 10
`;

/** The best rated wines that enough people have rated for the average to mean something. */
const BEST_RATED = `
SELECT name, winery_name, country, community_avg AS average, community_count AS ratings
FROM wine
WHERE community_count >= 100
ORDER BY community_avg DESC, community_count DESC
LIMIT 10
`;

const RATINGS_PER_YEAR = `
SELECT extract(year FROM rated_at)::int AS year, count(*) AS ratings
FROM community_rating
GROUP BY year
ORDER BY year
`;

function heading(text: string): void {
console.log(`\n${text}`);
console.log('-'.repeat(text.length));
}

async function rows<T extends object>(sql: string): Promise<T[]> {
return (await pool.query<T>(sql)).rows;
}

async function main(): Promise<void> {
const [totals] = await rows<Totals>(TOTALS);

heading('Catalog');
console.log(`Wines: ${Number(totals.wines).toLocaleString('en')}`);
console.log(`Ratings: ${Number(totals.ratings).toLocaleString('en')}`);
console.log(`Countries: ${totals.countries}`);
console.log(`Regions: ${Number(totals.regions).toLocaleString('en')}`);
console.log(`Wineries: ${Number(totals.wineries).toLocaleString('en')}`);
console.log(`Grapes: ${totals.grapes}`);
console.log(`Average: ${totals.average} of 5`);
console.log(`Database: ${totals.database_size}`);

heading('Wines by type');
console.table(await rows(BY_TYPE));

heading('Countries with most wines');
console.table(await rows(TOP_COUNTRIES));

heading('Most common grapes');
console.table(await rows(TOP_GRAPES));

heading('Best rated wines (at least 100 ratings)');
console.table(await rows(BEST_RATED));

heading('Ratings per year');
console.table(await rows(RATINGS_PER_YEAR));
}

main()
.catch((error: unknown) => {
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
})
.finally(() => pool.end());
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ During development, Vite forwards `/graphql` to the backend. On the VM, Apache f
## Student reviews

A review is 0.5 to 5 in half stars and an optional comment (max 500 characters).
There is no login yet, so the frontend creates a random UUID once (`crypto.randomUUID()`, or random bytes on plain HTTP, where `randomUUID` does not exist),
The frontend has no login yet (the backend part is described under «Users and login» below),
so it creates a random UUID once (`crypto.randomUUID()`, or random bytes on plain HTTP, where `randomUUID` does not exist),
stores it in localStorage and sends it as `userId`. Treat it like a password: the API never
returns it, reviews are shown with a display name such as "Student #12". Every student gets their own number the first time they save a review, so two students never share a name.

Expand Down
2 changes: 1 addition & 1 deletion docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Open the frontend at [localhost:5173](http://localhost:5173/).
Open [localhost:3001](http://localhost:3001/) to check the backend's database connection.
Open [localhost:3001/graphql](http://localhost:3001/graphql) to try GraphQL queries.
It shows the database name, user, and server time. A failed query returns HTTP 503.
The frontend requests `/api/hello`. Vite forwards that request to the backend's `/` endpoint during local development.
The frontend requests `/graphql`. Vite forwards that request to the backend during local development.

To update an existing checkout, stop the development servers and run these commands from the project folder:

Expand Down
3 changes: 1 addition & 2 deletions docs/tasks.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Tasks

Kork & Krone lets students search wines, compare their own die rolls with what the wine community thinks,
and keep a list of wines they want to try.
Kork & Krone lets students search wines, compare their own star ratings with what the wine community thinks,and keep a list of wines they want to try.
The catalog comes from the [X-Wines](https://github.com/rogerioxavier/X-Wines) dataset, imported into our own PostgreSQL database.
The frontend reads it through our GraphQL backend.

Expand Down
34 changes: 22 additions & 12 deletions docs/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,41 +175,51 @@ For a foreground backend started with `npm start`, press **Ctrl+C** in its termi
`nohup` does not restart the backend after a crash or VM reboot.
Start it again with the commands above. Use systemd if automatic restarts are needed.

## Apache backend proxy
## Apache configuration

Apache serves the built frontend and forwards GraphQL requests to the backend.
The frontend asks for `/project2/graphql`, and Apache passes that on to the backend on port 3001.

`FallbackResource` makes deep links such as `/project2/sok` work on reload: the files do not
exist on disk, so Apache serves `index.html` and React Router takes over.

For the database greeting backend, the frontend requests `/api/hello`.
Apache must forward that request to the backend's `/` endpoint.
Vite's development proxy does not run in a production build.
Configure this once, after starting the backend:

```bash
sudo a2enmod proxy proxy_http
sudo nano /etc/apache2/conf-available/project2.conf
```

Add these directives to the file:
The file should contain:

```apache
ProxyPass "/api/hello" "http://127.0.0.1:3001/"
ProxyPassReverse "/api/hello" "http://127.0.0.1:3001/"
ProxyPass "/project2/graphql" "http://127.0.0.1:3001/graphql"
ProxyPassReverse "/project2/graphql" "http://127.0.0.1:3001/graphql"

<Directory /var/www/html/project2>
FallbackResource /project2/index.html
</Directory>
```

Enable the configuration and check its syntax:

```bash
sudo a2enmod proxy proxy_http
sudo a2enconf project2
sudo apache2ctl configtest
```

If the check reports `Syntax OK`, reload Apache:
If the check reports `Syntax OK`, reload Apache and try a query:

```bash
sudo systemctl reload apache2
curl -i http://localhost/api/hello
curl -s -X POST http://localhost/project2/graphql -H 'Content-Type: application/json' -d '{"query":"{ wines(first: 1) { totalCount } }"}'
```

Expect the same database response as the direct backend request.
If the backend later uses GraphQL, update this mapping to match its endpoint and frontend requests.
Expect a number of wines back. A `503` means Apache is running but cannot reach the backend,
so check that the backend process is still up.

The file on the VM also has an older `ProxyPass "/api/hello"` from the first submission.
It does nothing now and can be removed the next time the configuration is touched.
See Apache's [reverse proxy documentation](https://httpd.apache.org/docs/2.4/mod/mod_proxy.html#examples).
ProxyPass "/project2/graphql" "http://127.0.0.1:3001/graphql"
ProxyPassReverse "/project2/graphql" "http://127.0.0.1:3001/graphql"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
.message {
font-weight: 600;
order: 1;
}
}