Skip to content
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

Add Plausible analytics to the public website #53

Merged
merged 10 commits into from
Mar 15, 2024
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: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
DATABASE_URL=postgres://postgres:postgres@localhost:5433/food-twin?sslmode=disable
NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN=
#NEXT_PUBLIC_PLAUSIBLE_DOMAIN=<Website domain for Plausible analytics>
1 change: 0 additions & 1 deletion .env.local.sample

This file was deleted.

10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ To set up the development environment for this website, you'll need to install t
- [Yarn](https://yarnpkg.com/) package manager
- [Docker](https://www.docker.com)

### Initialize `.env.local` File

The project uses environment variables, which are set by default in the [.env](.env) file. To customize these variables (e.g., to use a custom database), create a `.env.local` file at the root of the repository (`cp .env .env.local`) and modify as needed.

Note: `.env.local` is ignored by Git.

### Install Application Dependencies

If you use [`nvm`](https://github.com/creationix/nvm), activate the desired Node version:
Expand All @@ -38,7 +44,7 @@ Migrate the database:
yarn migrate
```

Download the contents of this [Google Drive folder](https://drive.google.com/drive/folders/1uDlDT7NNI7l5WC0A9WsAZTlFWpC0Uo1i?usp=sharing) and add it to a directory called `./app-data` in the cloned repository.
Download the contents of this [Google Drive folder](https://drive.google.com/drive/folders/1IZunCVMNnA3h6VRBD365DfeUWQt5smzG?usp=drive_link) and add it to a directory called `./app-data` in the cloned repository.

Ingest downloaded data with:

Expand All @@ -52,7 +58,7 @@ Start development server:
yarn dev
```

✨ You can now login to the app at [http://localhost:3000](http://localhost:3000)
✨ You can now access the app at [http://localhost:3000](http://localhost:3000)

### API Docs

Expand Down
60 changes: 33 additions & 27 deletions seeds/003-add-kcal-flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ const COUNTIES_FILE_PATH = path.join(
);

exports.seed = async function (knex) {
console.log("Starting database seeding process...");

/**
* CLEAR FLOWS, CROPS, AND COUNTIES TABLES
*/
console.log("Clearing tables: kcal_flows, crops, counties");
await knex.raw("TRUNCATE TABLE kcal_flows RESTART IDENTITY CASCADE");
await knex.raw("TRUNCATE TABLE crops RESTART IDENTITY CASCADE");
await knex.raw("TRUNCATE TABLE counties RESTART IDENTITY CASCADE");

/**
* INGEST COUNTIES
*/
console.log("Ingesting counties data...");
const { features: counties } = await fs.readJson(COUNTIES_FILE_PATH);
await knex.batchInsert(
"counties",
Expand All @@ -31,10 +35,12 @@ exports.seed = async function (knex) {
})),
500
);
console.log("Counties data ingestion completed.");

/**
* INGEST CROP CODES
*/
console.log("Ingesting crop codes...");
const cropCodes = await fs.readFile(CROPS_CSV_PATH, "utf-8");

// Split CSV into rows
Expand All @@ -56,10 +62,12 @@ exports.seed = async function (knex) {
})
.filter((row) => row.name !== "")
);
console.log("Crop codes ingestion completed.");

/**
* INGEST KCAL FLOWS
*/
console.log("Ingesting kcal flows...");
const files = (await fs.readdir(KCAL_FLOWS_DIR)).filter((file) =>
file.endsWith(".csv")
);
Expand Down Expand Up @@ -92,33 +100,31 @@ exports.seed = async function (knex) {
try {
await knex.batchInsert(
"kcal_flows",
rows
.map((line) => {
if (line === "") return;

const row = line.split(",");

const origin_id = row[0].padStart(5, "0");
const destination_id = row[1].padStart(5, "0");
const value = row[2];

if (!origin_id || !destination_id || !value) {
console.log("Skipping row", row);
return null;
}

return {
origin_id,
destination_id,
crop_id,
value,
impact_ratios: {
heat: parseFloat(row[3]),
drought: parseFloat(row[5]),
},
};
})
.filter((row) => row.origin_id !== "0Dade"), // Discard rows with invalid origin_id
rows.map((line) => {
if (line === "") return;

const row = line.split(",");

const origin_id = row[0].padStart(5, "0");
const destination_id = row[1].padStart(5, "0");
const value = row[2];

if (!origin_id || !destination_id || !value) {
console.log("Skipping row", row);
return null;
}

return {
origin_id,
destination_id,
crop_id,
value,
impact_ratios: {
heat: parseFloat(row[3]),
drought: parseFloat(row[5]),
},
};
}),
500
);
} catch (error) {
Expand Down
7 changes: 7 additions & 0 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ function Home({
href="/favicon-16x16.png"
/>
<link rel="manifest" href="/site.webmanifest" />
{process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN && (
<script
defer
data-domain={process.env.NEXT_PUBLIC_PLAUSIBLE_DOMAIN}
src="https://plausible.io/js/script.js"
></script>
)}
</Head>

<MapProvider>
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2957,9 +2957,9 @@ axe-core@^4.6.2:
integrity sha512-zIURGIS1E1Q4pcrMjp+nnEh+16G56eG/MUllJH8yEvw7asDo7Ac9uhC9KIH5jzpITueEZolfYglnCGIuSBz39g==

axios@^1.4.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.0.tgz#f02e4af823e2e46a9768cfc74691fdd0517ea267"
integrity sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==
version "1.6.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.1.tgz#76550d644bf0a2d469a01f9244db6753208397d7"
integrity sha512-vfBmhDpKafglh0EldBEbVuoe7DyAavGSLWhuSm5ZSEKQnHhBf0xAAwybbNH1IkrJNGnS/VG4I5yxig1pCEXE4g==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
Expand Down
Loading