Github Actions with D3

My prediction is that Automation and DevOps will get used more and more by non-software companies. You can already see this with data analytics pipelines where your SQL models are checked into your repository. If you run an e-commerce company, why not manage parts of your operations and customer support in Github and benefit from version control, issue tracking, team collaboration and workflow automation?

The use cases are endless, and while there are many workflow automation tools available, I suspect that you'll want to manage your workflows where you store your source code.

If you add a new product take screenshots and run Lighthouse of your published site and make sure it looks good on various devices.

count_targeted count_injected count_rejected count_sent to from
178 178 0 174 2020-12-22T10:00 2020-12-22T16:00
294 294 0 290 2020-12-22T12:00 2020-12-22T18:00
447 447 0 442 2020-12-22T18:00 2020-12-23T00:00
373 373 0 368 2020-12-23T00:00 2020-12-23T06:00

This workflow can be described in a simple yaml file:

name: Run D3 stats manually

on:
  workflow_dispatch: # Launch manually and provide default values
    inputs:
      csv-file:
        description: 'csv file to analyze'
        default: 'billybob.csv'
        required: true
      output-file:
        description: 'SVG'
        default: 'output.svg'
        required: true
  schedule: # Also run once every day at 8am
   - cron:  '0 8 * * *'

jobs:
  action:
    runs-on: ubuntu-latest

    steps:
      - name: checkout # Check out the files in the repo
      - uses: actions/checkout@v2

      - name: Get Sparkpost
        run: |
            TSFROM="$(date -u -d '6 hour ago' '+%Y-%m-%dT%H:00')"
            TSTO="$(date -u '+%Y-%m-%dT%H:00')"
            echo $TSFROM
            echo $TSTO
            curl \
            -H "Accept: application/json" \
            -H "Authorization: $" \
            "https://api.sparkpost.com/api/v1/metrics/deliverability?from=${TSFROM}&to=${TSTO}&metrics=count_targeted,count_injected,count_rejected,count_sent" | jq -r --arg from "${TSFROM}" --arg to "$TSTO" '.results[] + {from: $from, to: $to} | [.[]] | @csv' >> billybob.csv

      - name: Analyze csv
        env:
          DEFAULT_FILE: billybob.csv
        id: analyze-csv
        uses: mwhitaker/d3-line-chart-action@main
        with:
          csv-file: $NaN
          date-column: from # Specify date column in csv
          value-column: count_sent # Specify value column in csv
      - name: Get the summary stats
        if: steps.analyze-csv.outputs.summary-alert == 'false' # only run this step if there are outliers and create a new issue.
        id: summary-stats
        uses: actions/github-script@v3
        with:
          github-token: $
          script: |
            const result = $NaN
            const comment = [
                `⚡️ Summary Sparkpost Stats ⚡️`,
                '| Category | Value |',
                '| --- | --- |',
                `| Q1 | ${result.quartiles[0]} |`,
                `| Q2 | ${result.quartiles[1]} |`,
                `| Q3 | ${result.quartiles[2]} |`,
                `| Range lower | ${result.range[0]} |`,
                `| Range upper | ${result.range[1]} |`,
                `| Outliers | ${result.outliers.join(" ")} |`,
                ' ',
                `See the [Sparkpost graph](../blob/main/screenshot-output/sparkpost_emails_docker.png)`
            ].join('\n')
            github.issues.create({
              owner: context.repo.owner,
              repo: context.repo.repo,
              title: `Sparkpost alert`,
              body: comment
            })
      - name: Commit and push if it changed # commit new line graph if it has changed
        run: |-
          git config user.name "Automated"
          git config user.email "actions@users.noreply.github.com"
          git add -A
          timestamp=$(date -u)
          git commit -m "Latest data: ${timestamp}" || exit 0
          git push