Skip to content

Instantly share code, notes, and snippets.

@gnh1201
Last active February 29, 2020 16:32
Show Gist options
  • Select an option

  • Save gnh1201/af708e3f628110bcf91639782659479e to your computer and use it in GitHub Desktop.

Select an option

Save gnh1201/af708e3f628110bcf91639782659479e to your computer and use it in GitHub Desktop.
api.cardano.php (example of gnh1201/reasonableframework - https;//catswords.re.kr/go/framework)
<?php
loadHelper("string.utils");
loadHelper("colona.v1.format");
loadHelper("networktool");
$output = "false";
$mode = get_requested_value("mode");
$ne = get_network_event();
$hostip = $ne['client'];
if($mode == "background") {
$tablename = exec_db_table_create(array(
"hostip" => array("varchar", 255),
"name" => array("varchar", 255),
"value" => array("varchar", 255),
"datetime" => array("datetime")
), "cardano_stats", array(
"setindex" => array(
"index_1" => array("name"),
"index_2" => array("datetime")
)
));
$parsed_data = decode_colona_format($requests['_RAW']);
foreach($parsed_data as $k=>$v) {
$bind = array(
"hostip" => $hostip,
"name" => $k,
"value" => trim($v, '"'),
"datetime" => get_current_datetime()
);
$sql = get_bind_to_sql_insert($tablename, $bind);
exec_db_query($sql, $bind);
}
$output = "true";
} elseif($mode == "rotate") {
$bind = false;
$end_dt = get_current_datetime(array("adjust" => "-1h"));
$sql = get_bind_to_sql_delete("cardano_stats", $bind, array(
"setwheres" => array(
array("and", array("lte", "datetime", $end_dt))
)
));
if(exec_db_query($sql, $bind)) {
$output = "true";
}
} else {
$bind = array(
"name" => "lastBlockHeight"
);
$sql = get_bind_to_sql_select("cardano_stats", $bind, array(
"setorders" => array(
array("desc", "value")
),
"setlimit" => 1,
"setpage" => 1
));
$rows = exec_db_fetch_all($sql, $bind);
foreach($rows as $row) {
if($row['hostip'] == $hostip) {
$output = "true";
}
}
}
header("Content-Type: text/plain");
echo $output;
<?php
include './vendor/_dist/jpgraph/src/jpgraph.php';
include './vendor/_dist/jpgraph/src/jpgraph_canvas.php';
include './vendor/_dist/jpgraph/src/jpgraph_table.php';
include './vendor/_dist/jpgraph/src/jpgraph_iconplot.php';
include './vendor/_dist/jpgraph/src/jpgraph_flags.php';
// Setup a basic canvas to use as graph to add the table
$graph = new CanvasGraph(820,250);
// Setup the basic table
$data = array(
//array(''),
//array(''),
array(''),
array(''),
array(''),
array(''),
array(''),
array(''),
array('')
);
$nodes = array(
"KORDA" => "34.64.198.44",
"KOR02" => "34.64.198.44",
//"ROKA1" => "34.64.198.44"
);
$nodetypes = array(
"MASTER" => "34.64.198.44",
"SECOND" => "34.64.70.111"
);
$row2 = array('Pool Name (Ticker)');
$row3 = array('Last Block Height');
$row4 = array('Server Uptime');
$row5 = array('Recv Block Count');
$row6 = array('Last Block Date');
$row7 = array('Server Status');
$row8 = array('Last Block Time');
foreach($nodes as $k=>$v) {
$last_dt = "";
$row2[] = $k;
$bind = array(
"hostip" => $v
);
$sql = get_bind_to_sql_select("cardano_stats", $bind, array(
"setwheres" => array(
array("and", array("in", "name", array("lastBlockHeight", "uptime", "blockRecvCnt", "lastBlockDate", "lastBlockTime")))
),
"setorders" => array(
array("desc", "datetime")
),
"setlimit" => 5,
"setpage" => 1
));
$rows = exec_db_fetch_all($sql, $bind);
foreach($rows as $row) {
if($row['name'] == "lastBlockHeight") {
$row3[] = number_format($row['value']);
if(empty($last_dt)) {
$last_dt = $row['datetime'];
}
}
if($row['name'] == "uptime") {
$row4[] = number_format($row['value']);
}
if($row['name'] == "blockRecvCnt") {
$row5[] = number_format($row['value']);
}
if($row['name'] == "lastBlockDate") {
$row6[] = $row['value'];
}
if($row['name'] == "lastBlockTime") {
$row8[] = str_replace('T', ' ', substr($row['value'], 0, 19));
}
}
$status = 0;
$end_dt = get_current_datetime();
$start_dt = get_current_datetime(array(
"adjust" => "-5m"
));
if(!empty($last_dt)) {
$bind = array(
"name" => "lastBlockHeight"
);
$sql = get_bind_to_sql_select("cardano_stats", $bind, array(
"fieldnames" => array("value"),
"setwheres" => array(
array("and", array("gte", "datetime", $start_dt)),
array("and", array("lte", "datetime", $end_dt))
),
"setgroups" => array("value")
));
write_common_log(get_db_binded_sql($sql, $bind));
$rows = exec_db_fetch_all($sql, $bind);
write_common_log(json_encode($rows));
if(count($rows) > 1) {
$status = 1;
}
}
if($status > 0) {
$row7[] = "GOOD";
} else {
$row7[] = "WARN";
}
}
$data[0] = $row2;
$data[1] = $row3;
$data[5] = $row4;
$data[4] = $row5;
$data[2] = $row6;
$data[6] = $row7;
$data[3] = $row8;
//$countries = array('korea', 'korea', 'korea');
// Create a basic table and default fonr
$table = new GTextTable();
$table->Set($data);
$table->SetFont(FF_DEFAULT,FS_NORMAL,10);
$table->SetBorder(3, 'pink');
// Adjust the font for row 0 and 6
$table->SetColFont(0,FF_DEFAULT,FS_BOLD,10);
$table->SetRowFont(0,FF_DEFAULT,FS_BOLD,12);
// Set the minimum heigth/width
$table->SetMinRowHeight(2,15);
$table->SetMinColWidth(270);
// Add some padding (in pixels)
//$table->SetRowPadding(2,0);
$table->SetPadding(10);
$table->SetRowGrid(3,1,'darkgray',TGRID_DOUBLE2);
// Setup the grid
$table->SetGrid(0);
//$table->SetRowGrid(3,1,'black',TGRID_DOUBLE2);
// Merge all cells in row 0
//$table->MergeRow(0);
// Set aligns
$table->SetAlign(0,0,5,2,'right');
$table->SetRowAlign(0,'center');
//$table->SetRowAlign(1,'center');
//$table->SetRowAlign(2,'center');
$table->SetColAlign(0, 'center');
$table->SetRowAlign(6,'center');
// Set background colors
//$table->SetRowFillColor(0,'[email protected]');
$table->SetColFillColor(0,'[email protected]');
// Add the country flags in row 1
/*
$n = count($countries);
for($i=0; $i < $n; ++$i ) {
$table->SetCellCountryFlag(1,$i+1,$countries[$i],0.5);
$table->SetCellImageConstrain(1,$i+1,TIMG_HEIGHT,20);
}
*/
// Add the table to the graph
$graph->Add($table);
// Send back the table graph to the client
$graph->Stroke();
#!/bin/bash
/root/jcli rest v0 node stats get -h http://127.0.0.1:3301/api > /tmp/stats.txt
curl -X POST --data-binary @/tmp/stats.txt "http://localhost:4000/?route=api.cardano&mode=background" > /tmp/lastcurl.txt
#!/bin/bash
state0=$(cat /opt/state.txt 2>/dev/null)
state1=$(curl "http://localhost:4000/?route=api.cardano" 2>/dev/null)
if [ "$state0" == "true" ] && [ "$state1" == "false" ]; then
echo "delete"
/root/delete.sh
elif [ "$state0" == "false" ] && [ "$state1" == "true" ]; then
echo "post"
/root/post#!/bin/bash
state0=$(cat /opt/state.txt 2>/dev/null)
state1=$(curl "http://34.64.198.44:4000/?route=api.cardano" 2>/dev/null)
if [ "$state0" == "true" ] && [ "$state1" == "false" ]; then
echo "delete"
/root/delete.sh
elif [ "$state0" == "false" ] && [ "$state1" == "true" ]; then
echo "post"
/root/post.sh
else
echo "nothing"
fi
echo "$state1" > /opt/state.txt.sh
else
echo "nothing"
fi
echo "$state1" > /opt/state.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment