# yamllint disable rule:line-length
---
name: Generate weekly stats graphs
on:
  schedule:
    - cron: "0 13 * * 5"
  workflow_dispatch:

jobs:
  stats_graphs:
    runs-on: ubuntu-20.04
    needs: stats_updater
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v3
      - name: Install apps
        run: |
          git pull --rebase origin master
          sudo apt-get install gawk -y
          sudo apt-get install gnuplot-nox -y
      - name: Create global stats
        run: |
          # Prepare data
          cp Stats Stats3
          # Inverse file
          gawk -i inplace '{for(i=NF;i>1;i--)printf "%s ",$i;printf "%s",$1;print ""}' Stats3
          # Only totals
          head -n 2 Stats3 > tmp_file && mv tmp_file Stats3
          # Transpose data
          awk '
          { 
            for (i=1; i<=NF; i++)  {
            a[NR,i] = $i
            }
          }
          NF>p { p = NF }
          END {    
            for(j=1; j<=p; j++) {
              str=a[1,j]
              for(i=2; i<=NR; i++){
                str=str" "a[i,j];
              }
            print str
            }
          }' Stats3 > StatsTmp && mv StatsTmp Stats3
          sed -i "1d" Stats3
          # Plot graph
          gnuplot-nox -persist <<-EOFMarker
            set title 'Total addons'
            set ylabel 'Number of installations'
            set xdata time
            set datafile missing "-"
            set timefmt "%Y-%m-%d"
            set format x "%y-%m-%d"
            set datafile sep ' '
            set key top left autotitle columnheader
            set autoscale
            set terminal png size 500,300
            set output 'stats.png'
            set term png tiny
            plot 'Stats3' using 1:2 lt rgb 'red' w l title 'Total addons' smooth csplines
          EOFMarker
          rm Stats3
      - name: Create addons stats
        run: |
          # Prepare data
          cp Stats Stats3
          # Remove Totals
          sed -i "2d" Stats3
          # Inverse file
          gawk -i inplace '{for(i=NF;i>1;i--)printf "%s ",$i;printf "%s",$1;print ""}' Stats3
          # Only top 10
          head -n 11 Stats3 > tmp_file && mv tmp_file Stats3
          # Transpose data
          awk '
          { 
            for (i=1; i<=NF; i++)  {
            a[NR,i] = $i
            }
          }
          NF>p { p = NF }
          END {    
            for(j=1; j<=p; j++) {
              str=a[1,j]
              for(i=2; i<=NR; i++){
                str=str" "a[i,j];
              }
            print str
            }
          }' Stats3 > StatsTmp && mv StatsTmp Stats3
          # Add download count to names
          build_list="$(head -n 1 Stats3)"
          # shellcheck disable=SC2206
          build_list=($build_list)
          # shellcheck disable=SC2013
          for var in "${build_list[@]:1}"; do
          i=0
          j=0
          k=0
            # shellcheck disable=SC2013
            for i in $(sed -n "/${var}/p" Stats); do
              k="$((k+1))"
              if [ "$k" -eq 3 ]; then break; fi
              if [ "$i" -eq "$i" ] && [ "$i" -gt "$j" ]; then j="$i"; fi
            done
          sed -i "s|${var}|${var}_(${j}x)|g" Stats3
          done
          echo "... done"
          # Remove series with less than 4 points
          sed -i "/^[0-9|-]* [0-9*|-]* [0-9*|-]* -/d" Stats3
          # Plot graph
          ( gnuplot-nox -persist <<-EOFMarker
            set title 'Top 10 addons'
            set ylabel 'Number of installations'
            set xdata time
            set datafile missing "-"
            set timefmt "%Y-%m-%d"
            set format x "%y-%m-%d"
            set datafile sep ' '
            set autoscale
            set terminal png size 500,300
            set output 'stats_addons.png'
            set term png tiny
            plot for [i=2:*] 'Stats3' using 1:i w l title columnhead(i) smooth csplines
          EOFMarker
          ) || ( gnuplot-nox -persist <<-EOFMarker
            set title 'Top 10 addons'
            set ylabel 'Number of installations'
            set xdata time
            set datafile missing "-"
            set timefmt "%Y-%m-%d"
            set format x "%y-%m-%d"
            set datafile sep ' '
            set autoscale
            set terminal png size 500,300
            set output 'stats_addons.png'
            set term png tiny
            plot for [i=2:*] 'Stats3' using 1:i w l title columnhead(i)
          EOFMarker
          )
          #plot for [i=1:*] 'Stats3' using 0:i
          rm Stats3
          
      - name: Create individual stats
        run: |
          # Prepare data
          cp Stats Stats3
          # Remove Totals
          sed -i "2d" Stats3
          # Transpose data
          awk '
          { 
            for (i=1; i<=NF; i++)  {
            a[NR,i] = $i
            }
          }
          NF>p { p = NF }
          END {    
            for(j=1; j<=p; j++) {
              str=a[1,j]
              for(i=2; i<=NR; i++){
                str=str" "a[i,j];
              }
            print str
            }
          }' Stats3 > StatsTmp && mv StatsTmp Stats3
          # For each addon, create graph
          input="Stats3"
          while IFS= read -r line
          do
            TITLE="${line%% *}"
            FOLDER="$(grep -rl "ghcr.io/alexbelgium/$TITLE-" */* | xargs dirname)"
            # if folder exists
            if [ -d "$FOLDER" ]; then
          # Plot graph
            ( gnuplot-nox -persist <<-EOFMarker
              set title "$TITLE"
              set ylabel 'Number of installations'
              set xdata time
              set datafile missing "-"
              set timefmt "%Y-%m-%d"
              set format x "%y-%m-%d"
              set datafile sep ' '
              set autoscale
              set terminal png size 500,300
              set output "$FOLDER/stats.png"
              set term png tiny
              plot for [i=2:*] 'Stats3' using 1:i w l title columnhead(i) smooth csplines
            EOFMarker
            ) || ( gnuplot-nox -persist <<-EOFMarker
              set title "$TITLE"
              set ylabel 'Number of installations'
              set xdata time
              set datafile missing "-"
              set timefmt "%Y-%m-%d"
              set format x "%y-%m-%d"
              set datafile sep ' '
              set autoscale
              set terminal png size 500,300
              set output "$FOLDER/stats.png"
              set term png tiny
              plot for [i=2:*] 'Stats3' using 1:i w l title columnhead(i)
            EOFMarker
            )
            fi
          done < "$input"

          # Plot graph
          ( 
          ) || ( gnuplot-nox -persist <<-EOFMarker
            set title 'Top 10 addons'
            set ylabel 'Number of installations'
            set xdata time
            set datafile missing "-"
            set timefmt "%Y-%m-%d"
            set format x "%y-%m-%d"
            set datafile sep ' '
            set autoscale
            set terminal png size 500,300
            set output 'stats_addons.png'
            set term png tiny
            plot for [i=2:*] 'Stats3' using 1:i w l title columnhead(i)
          EOFMarker
          )
          #plot for [i=1:*] 'Stats3' using 0:i
          rm Stats3
          
      - name: Commit if needed
        uses: EndBug/add-and-commit@v9
        with:
          message: "GitHub bot : README updated"
          default_author: github_actions
