HUB
Sign inSign up

hyperobjekt/seda-export

By hyperobjekt

•Updated 2 months ago

export generator

Image
0

2.9K

hyperobjekt/seda-export repository overview

⁠SEDA PDF Export

⁠Education Recovery Project

⁠Commands

For editing the pdf via the vite dev server

npm run dev-v2

To export all reports or images

# reports
npm run export-all

# images
npm run export-all-images
⁠Deploying
# generate reports
npm run export-all

# upload reports to s3
aws s3 cp ./src/v2/batch-output/ s3://seda-pdf/reports/recovery --recursive

⁠Educational Opportunity Project

Execute a node.js script to export a PDF using provided JSON and handlebars template.

⁠Getting Started

Note: Running brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman before npm install might be required to overcome node-pre-gyp issues.

⁠Commands

For editing the template, run the following command to start watching the SCSS and Handlebars template files and compiling on changes:

npm run watch

While this is running, you can start a local development server to view the changes with:

npm run start

This will show a preview of the template in the browser when you go to http://localhost/⁠

To generate a PDF with the server running, you can send a curl post request with some sample data:

curl -vX POST http://localhost:80/ -d @input/data-district.json \
--header "Content-Type: application/json" --output sample.pdf

To run the export script only:

npm run export

⁠Rough draft district PDF (new CSV data)

The rough draft flow is an opt-in sibling to the current v2 export. Existing / and /v2 behavior stays unchanged unless rough mode is requested.

Use:

curl -vX POST "http://localhost:8080/v2?mode=rough" \
  -d '{"districtId":"","downloadUrl":""}' \
  --header "Content-Type: application/json" \
  --output rough-draft.pdf
  • districtId: optional; when empty, the renderer picks the first valid district from data/districts_01.csv.
  • downloadUrl: optional; shown as a link on the cover page when provided.

CLI alternative (no running server required):

npm run v2:export-rough

With options:

npm run v2:export-rough -- --districtId=0601330 --out=./output/rough-0601330.pdf --downloadUrl=https://example.com/data.csv

For additional details on the file locations assumed for input and output for the node export command, see package.json.

⁠Testing different region types

There are 3 region-specific json data src files with values corresponding to their prospective ranges. Toggle region types by editing the input value in the export script in package.json:

--data=./input/data-district.json
⁠Deploying

The production PDF generator runs on ECS and pulls its image from Amazon ECR. Build and push a tagged image, then update the ECS service to deploy it:

export AWS_PROFILE=client-admin
export AWS_REGION=us-east-1
export ECR_IMAGE=318011162599.dkr.ecr.us-east-1.amazonaws.com/seda-export
export IMAGE_TAG=$(git rev-parse --short HEAD)

aws --profile "$AWS_PROFILE" --region "$AWS_REGION" ecr get-login-password \
  | docker login --username AWS --password-stdin "${ECR_IMAGE%%/*}"
docker build -t "$ECR_IMAGE:$IMAGE_TAG" .
docker push "$ECR_IMAGE:$IMAGE_TAG"

Register the new image and update the ECS service:

aws ecs describe-task-definition \
  --profile "$AWS_PROFILE" --region "$AWS_REGION" \
  --task-definition edop-pdf-task \
  --query taskDefinition > /tmp/edop-pdf-task.json
jq --arg image "$ECR_IMAGE:$IMAGE_TAG" \
  'del(.taskDefinitionArn,.revision,.status,.requiresAttributes,.compatibilities,.registeredAt,.registeredBy) | .containerDefinitions[0].image = $image' \
  /tmp/edop-pdf-task.json > /tmp/edop-pdf-task-ecr.json
TASK_DEFINITION=$(aws ecs register-task-definition \
  --profile "$AWS_PROFILE" --region "$AWS_REGION" \
  --cli-input-json file:///tmp/edop-pdf-task-ecr.json \
  --query taskDefinition.taskDefinitionArn --output text)
aws ecs update-service \
  --profile "$AWS_PROFILE" --region "$AWS_REGION" \
  --cluster edop-pdf-cluster \
  --service edop-pdf-container-service \
  --task-definition "$TASK_DEFINITION"

Pull and run the image locally with:

docker pull "$ECR_IMAGE:$IMAGE_TAG"
docker run -p 8080:80 -d "$ECR_IMAGE:$IMAGE_TAG"

Then send a request to the running docker image with curl:

curl -vX POST http://localhost:8080/ -d @input/data-county.json --header "Content-Type: application/json" --output sample.pdf

This should generate a sample.pdf file containing a sample PDF. If it is generated successfully, you can deploy to AWS.

NOTE: if you are running on an M1 (apple silicon) you will likely receive a Error: Protocol error (Target.setDiscoverTargets): Target closed. error. if you're able, test on non-apple silicon, or have another team member test for you.

Once you have confirmed the PDF generation works successfully, wait for the ECS service deployment to complete. The service will decommission the running tasks and start new tasks using the registered ECR image.

The ECS target group checks /health, which returns immediately without rendering a report.

⁠Data Preparation

To obtain the necessary input files for states and districts, follow these steps:

  1. Download Data: Manually download all data from the "download all data" link located at the bottom right of the footer on https://dev.edopportunity.org/recovery/explorer/⁠. This action will provide states.json and three separate district files.
  2. Placement: Place states.json and the three district files (districts_1.json, districts_2.json, districts_3.json) into the src/v2/ directory. The export pipeline will merge the districts files automatically.
  3. Fitted lines: Place state_fitted_lines.csv into the src/v2/ directory.
⁠Chart overlays

There are 16 chart images in this archive that represent the 4 key metrics ("avg", "grd", "coh", "imp") for the 4 available regions ("states", "counties", "districts", and "schools").

  • Counties / Districts use socioeconomic status ("ses") as the X axis
  • Schools use % Free or reduced-price lunch program ("frl")

Create a div overlay over the image and position any overlay dots within the overlay container. The overlay container should have the following position style to ensure it matches up with the data area of the chart.

.overlay {
  position: absolute;
  top: 12.820512820512821%;
  left: 2.359882005899705%;
  bottom: 14.102564102564103%;
  right: 14.159292%;
}

Note: I have provided chart_overlay.png that has a green overlay indicating the data area of the chart. Make sure the overlay div is the same size as the green highlighted area. The data area is consistent between all charts.

To place a dot, calculate the % position of its x / y value within the following ranges:

{
  "avg": {
    "counties": [-4.5, 2.5],
    "districts": [-4.5, 4.5],
    "schools": [-8, 7]
  },
  "grd": {
    "counties": [0.4, 1.6],
    "districts": [0.4, 1.6],
    "schools": [-0.2, 2.6]
  },
  "coh": {
    "counties": [-0.5, 0.5],
    "districts": [-0.5, 0.5],
    "schools": [-1, 1]
  },
  "ses": {
    "counties": [-4, 3],
    "districts": [-5, 4]
  },
  "frl": {
    "schools": [0, 1]
  }
}

Note: I will also provide the range in the JSON object that is sent to the function, but they are here for reference.

Tag summary

Content type

Image

Digest

sha256:22d6d120c…

Size

952.2 MB

Last updated

2 months ago

docker pull hyperobjekt/seda-export