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

feat: generate vote report job #844

Merged
merged 8 commits into from
Jun 16, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/node_modules
/public/hot
/public/storage
/public/VoteReport.txt
/storage/*.key
/vendor
.env
Expand Down
35 changes: 35 additions & 0 deletions app/Console/Commands/GenerateVoteReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Jobs\GenerateVoteReport as Job;
use Illuminate\Console\Command;

final class GenerateVoteReport extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'explorer:generate-vote-report';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate Vote Report file.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
(new Job())->handle();
}
}
3 changes: 3 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use App\Console\Commands\CacheMultiSignatureAddresses;
use App\Console\Commands\CacheNetworkAggregates;
use App\Console\Commands\CachePrices;
use App\Console\Commands\GenerateVoteReport;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand Down Expand Up @@ -71,6 +72,8 @@ protected function schedule(Schedule $schedule)
$schedule->command(CacheDelegatePerformance::class)->everyMinute();

$schedule->command(CacheDelegateProductivity::class)->everyMinute();

$schedule->command(GenerateVoteReport::class)->everyFiveMinutes();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions app/Jobs/GenerateVoteReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Symfony\Component\Process\Process;

final class GenerateVoteReport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function handle(): void
{
(new Process(['bash', resource_path('scripts/vote-report.sh')]))
->setTimeout(300)
->run();
}
}
2 changes: 2 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<directory suffix=".php">./app/Providers</directory>
<directory suffix=".php">./app/View/Components</directory>
<file>./app/Console/Commands/RunPlaybookCommand.php</file>
<file>./app/Console/Commands/GenerateVoteReport.php</file>
alexbarnsley marked this conversation as resolved.
Show resolved Hide resolved
<file>./app/Jobs/GenerateVoteReport.php</file>
<file>./app/Console/Kernel.php</file>
<file>./app/Http/Kernel.php</file>
<!-- Application Code -->
Expand Down
98 changes: 98 additions & 0 deletions resources/scripts/vote-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash
# Gr33nDrag0n / v1.2 / 2017-04-02
# tharude / v.2.5.1 / 2019-07-12

EXPLORER_NODE="https://wallets.ark.io/api"

SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
OutputFile="$SCRIPT_PATH/../../public/VoteReport.txt"

# echo $OutputFile
# exit

###############################################################################
# FUNCTIONS
###############################################################################

function GetVotersCount {

VotersJsonData=$( curl -s "$EXPLORER_NODE/delegates/$1/voters?page=1&limit=1" )
echo $VotersJsonData | jq '.meta.totalCount'
}

#==============================================================================

function PrintJsonData {

echo $1 | jq -c -r '.data[] | { rank, username, votes, address, publicKey, production }' | while read Line; do

Rank=$( printf %02d $( echo $Line | jq -r '.rank' ) )

Delegate=$( printf %-25s $( echo $Line | jq -r '.username' ) )
Approval=$( printf %0.2f $( echo $Line | jq -r '.production.approval' ) )

Vote=$( expr $( echo $Line | jq -r '.votes' ) / 100000000 )
Vote=$( echo $Vote | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' )
Vote=$( printf %10s $Vote )

PublicKey=$( echo $Line | jq -r '.publicKey' )
Voters=$( printf %4s $( GetVotersCount $PublicKey ) )

echo "| $Rank | $Delegate | $Approval | $Vote | $Voters |" >> $2
done
}
#==============================================================================

function PrintTotalVotingWeightData {

TotalArk=$( curl -s "$EXPLORER_NODE/blockchain" | jq -r '.data.supply' )

TotalVote=0
TotalVoters=0
while read Line; do

Vote=$( echo $Line | jq -r '.votes' )
TotalVote=$( expr $TotalVote + $Vote )

PublicKey=$( echo $Line | jq -r '.publicKey' )
Voters=$( GetVotersCount $PublicKey )
TotalVoters=$( expr $TotalVoters + $Voters )

done <<< "$( echo $1 | jq -c -r '.data[] | { rank, username, votes, address, publicKey, production }' )"

Percentage=$( bc <<< "scale=2; $TotalVote * 100 / $TotalArk" )

TotalVote=$( expr $( echo $TotalVote ) / 100000000 )
TotalVote=$( echo $TotalVote | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' )

TotalArk=$( expr $( echo $TotalArk ) / 100000000 )
TotalArk=$( echo $TotalArk | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta' )

echo -e "Top 51 Delegates Stats\n" >> $2
echo -e "=> Total Votes : $Percentage% ( $TotalVote / $TotalArk )" >> $2
echo -e "=> Total Voters : $TotalVoters\n" >> $2
}

###############################################################################
# MAIN
###############################################################################

JsonData1=$( curl -s "$EXPLORER_NODE/delegates?page=1&limit=51" )
JsonData2=$( curl -s "$EXPLORER_NODE/delegates?limit=29&offset=51" )

WorkFile='./TxtVoteReport.txt'

echo '' > $WorkFile
PrintTotalVotingWeightData $JsonData1 $WorkFile
echo '===================================================================' >> $WorkFile
echo '| Rank | Delegate | Vote % | Vote ARK | Voters |' >> $WorkFile
echo '===================================================================' >> $WorkFile
PrintJsonData $JsonData1 $WorkFile
echo '===================================================================' >> $WorkFile
PrintJsonData $JsonData2 $WorkFile
echo '===================================================================' >> $WorkFile
Date=$( date -u "+%Y-%m-%d %H:%M:%S" )
echo -e "\n $Date UTC / TxtVoteReport.sh v2.5.1 / ark.io \n" >> $WorkFile
echo -e "\n NOTE: This report is based on the 51 active delegates only! \n" >> $WorkFile

cp -f $WorkFile $OutputFile