Files
hassio-addons/.github/workflows/weekly_stats.yaml
2023-01-27 21:24:48 +01:00

241 lines
9.2 KiB
YAML

# yamllint disable rule:line-length
---
name: Generate weekly stats
on:
schedule:
- cron: "0 12 * * 5"
workflow_dispatch:
jobs:
stats_updater:
runs-on: ubuntu-20.04
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Create stats
run: |
echo "Starting"
COUNT=0
COUNT1=0
COUNT2=0
COUNT3=0
# Make sure file exists
file=Stats
touch "$file"
rm "$file"2
if ! grep -q "Date" "$file"; then
sed -i "1i Date" "$file"
fi
# Add new column
sed -i 's/^/- /' "$file"
# Add date
sed -i "/Date/s|^-|$(date '+%Y-%m-%d')|" "$file"
# Go through all folders, add to filters if not existing
# shellcheck disable=SC2086
for f in $( find -- * -maxdepth 0 -type d | sort -r ); do
DOWNLOADS=0
if [ -f "$f"/config.json ]; then
SLUG="$(jq -r '.image' "$f/config.json" | awk -F 'alexbelgium/|-{arch' '{print $2}')"
if [[ "$(jq '.arch[]' "$f/config.json")" == *"armv7"* ]]; then
ARCH=armv7 && COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')"
until [ -n "$COUNT" ]; do COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')" && sleep 5; ((c++)) && ((c==10)) && break; done
COUNT="${COUNT//,/}"
COUNT1="$COUNT"
echo "$COUNT $ARCH users with $SLUG" && DOWNLOADS="$(( DOWNLOADS + COUNT))"
else COUNT1="-"; fi
if [[ "$(jq '.arch[]' "$f/config.json")" == *"amd64"* ]]; then
ARCH=amd64 && COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')"
until [ -n "$COUNT" ]; do COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')" && sleep 5; ((c++)) && ((c==10)) && break; done
COUNT="${COUNT//,/}"
COUNT2="$COUNT"
echo "$COUNT $ARCH users with $SLUG" && DOWNLOADS="$(( DOWNLOADS + COUNT))"
else COUNT2="-"; fi
if [[ "$(jq '.arch[]' "$f/config.json")" == *"aarch64"* ]]; then
ARCH=aarch64 && COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')"
until [ -n "$COUNT" ]; do COUNT="$(curl -f -s -L https://github.com/alexbelgium/hassio-addons/pkgs/container/$SLUG-$ARCH/latest | awk '/Total downloads/{getline; print}' | awk -F '<|>' '{print $3}')" && sleep 5; ((c++)) && ((c==10)) && break; done
COUNT="${COUNT//,/}"
COUNT3="$COUNT"
echo "$COUNT $ARCH users with $SLUG" && DOWNLOADS="$(( DOWNLOADS + COUNT))"
else COUNT3="-";fi
echo "$SLUG $DOWNLOADS $COUNT1 $COUNT2 $COUNT3" >> "$file"2
# Create line if not existing
if ! grep -q "${SLUG}$" "$file"; then
sed -i "\$a$SLUG" "$file"
# shellcheck disable=SC2046
for i in $(seq $(head -n 1 $file | tr -cd ' \t' | wc -c )); do
echo "$i"
sed -i "/$SLUG/s|^|- |" "$file"
done
fi
# Add data
sed -i "/${SLUG}$/s|^-|$DOWNLOADS|" "$file"
fi
done
# Add total
SLUG=TOTAL
if ! grep -q "$SLUG$" "$file"; then
sed -i "1a$SLUG" "$file"
# shellcheck disable=SC2046
for i in $(seq $(head -n 1 $file | tr -cd ' \t' | wc -c )); do
echo "$i"
sed -i "/$SLUG$/s|^|- |" "$file"
done
fi
DOWNLOADS="$(awk 'FNR > 2 {SUM+=$1}END{print SUM}' Stats)"
sed -i "/$SLUG$/s|^-|$DOWNLOADS|" "$file"
# Sort file
(head -n 2 "$file" && tail -n +3 "$file" | sort -k1 -r -n) > tmp && mv tmp "$file"
sed -i "1i Name Total armv7 amd64 aarch64" "$file"2
sed -i "1a ------------------------------" "$file"2
#TOTAL="$(awk '{SUM+=$2}END{print SUM}' Stats)"
#TOTAL1="$(awk '{SUM+=$2}END{print SUM}' Stats)"
#TOTAL2="$(awk '{SUM+=$2}END{print SUM}' Stats)"
#TOTAL3="$(awk '{SUM+=$2}END{print SUM}' Stats)"
- name: Commit if needed
uses: EndBug/add-and-commit@v9
with:
message: "GitHub bot : README updated"
default_author: github_actions
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"
# 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: Commit if needed
uses: EndBug/add-and-commit@v9
with:
message: "GitHub bot : README updated"
default_author: github_actions