Skip to content

Commit

Permalink
Change golemsp commands to respective yagna, ya-provider commands (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
vciancio authored Mar 26, 2021
1 parent 434b11b commit da6d963
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 37 deletions.
11 changes: 3 additions & 8 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import Flask
from golem import GolemStatus, get_status
from golem import GolemStatus
from flask_cors import CORS
import hardware
import sys
Expand All @@ -22,20 +22,15 @@ def current_time():
return calendar.timegm(time.gmtime())

def golem():
o = get_status()
# print(o, file=sys.stderr)
if o is None:
return None
status = GolemStatus(o)
status = GolemStatus()
return {
"name": status.node_name(),
"version": status.version(),
"wallet": status.wallet(),
"wallet": status.account(),
"network": status.network(),
"subnet": status.subnet(),
"processedTotal": status.processed_total(),
"processedLastHour": status.processed_hour(),
"processingLastHour": status.processing_hour(),
}

@app.route('/api/status', methods=['GET'])
Expand Down
150 changes: 121 additions & 29 deletions app/golem.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,137 @@
import re
import sys
import subprocess as sp

def get_status():
o = sp.check_output(["golemsp", "status"]).decode()
if "┌─────" not in o:
return None
return o
import json

class GolemStatus:
def __init__(self, status):
self.status = status

def _get_first_group(self, regex):
matches = re.finditer(regex, self.status, re.MULTILINE)
for _, match in enumerate(matches, start=1):
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
return match.group(groupNum)
return None

def __init__(self):
self._activity = _get_activity()
self._version = _get_version()
self._config = _get_config()
self._payment = _get_payment()

def wallet(self):
return self._get_first_group(r'(0x\S+)\x1b')
def account(self):
return self._config["account"]

def node_name(self):
return self._get_first_group(r'Node\sName\s+(\S+)')
return self._config["node_name"]

def version(self):
return self._get_first_group(r'│\s+Version\s+(\S+)')

return self._version["current"]["version"]
def network(self):
return self._get_first_group(r'network\s+\x1b\S+?m(\S+)\x1b')
return self._payment["network"]

def subnet(self):
return self._get_first_group(r'Subnet\s+(\S+)')
return self._config["subnet"]

def processed_total(self):
return self._get_first_group(r'total processed\s+(\d+)')
if "Terminated" not in self._activity["total"]:
return 0
return self._activity["total"]["Terminated"]

def processed_hour(self):
return self._get_first_group(r'last 1h processed\s+(\d+)')
if "Terminated" not in self._activity["last1h"]:
return 0
return self._activity["last1h"]["Terminated"]

def _run_return_json(command):
raw = sp.check_output(command, shell=True)
return json.loads(raw)

def _get_activity():
'''
Returns:
{
"last1h": 6,
"total": 358,
}
---------
Command: yagna activity status --json
Returns Json:
{
"last1h": {
"Terminated": 6
},
"lastActivityTs": null,
"total": {
"New": 87,
"Ready": 2,
"Terminated": 358,
"Unresponsive": 1
}
}
'''
return _run_return_json('yagna activity status --json')

def _get_config():
'''
Command: ya-provider config get --json
Returns Json:
{
"node_name": "rambolicious",
"subnet": "public-beta",
"account": "0x123...fff"
}
'''
return _run_return_json('ya-provider config get --json')

def _get_version():
'''
Command: yagna version show --json
Returns Json:
{
"current": {
"insertionTs": "2021-03-23T16:40:56",
"name": "v0.6.2 Zagajewski Armstrong",
"releaseTs": "2021-03-22T23:04:41",
"seen": false,
"updateTs": "2021-03-23T16:40:56",
"version": "0.6.2"
},
"pending": null
}
'''
return _run_return_json('yagna version show --json')

def processing_hour(self):
return self._get_first_group(r'last 1h in progress\s+(\d+)')
def _get_payment():
'''
Command: yagna payment status --json
Returns Json:
{
"amount": "0",
"driver": "zksync",
"incoming": {
"accepted": {
"agreementsCount": 0,
"totalAmount": "0"
},
"confirmed": {
"agreementsCount": 0,
"totalAmount": "0"
},
"requested": {
"agreementsCount": 0,
"totalAmount": "0"
}
},
"network": "rinkeby",
"outgoing": {
"accepted": {
"agreementsCount": 0,
"totalAmount": "0"
},
"confirmed": {
"agreementsCount": 0,
"totalAmount": "0"
},
"requested": {
"agreementsCount": 0,
"totalAmount": "0"
}
},
"reserved": "0",
"token": "tGLM"
}
'''
return _run_return_json('yagna payment status --json')

0 comments on commit da6d963

Please sign in to comment.