def overview(): nodelist = Node.select().where((Node.user == flask.session['UserID'])) results = [] enabled = 0 attention = 0 x = max_last_paid_for_ten_pct() for node in nodelist: if node.node_status == 'ENABLED': enabled = enabled + 1 if node.node_last_paid_block < x: # node.label = '<b>' + node.label + '</b>' node.top_ten = True # TODO: This is gross else: attention = attention + 1 return flask.render_template('overview.html', nodes=nodelist, enabled=enabled, attention=attention)
def main(should_send_mail): if not is_synced(): print('List is not synced (AssetID != 999)') return cache = znode_list() all_nodes = Node.select() status_alerts = [] pose_score_alerts = [] for node in all_nodes: # print(node.txid) try: node_result = cache[node.txid] #print(str(node_result)) except: node_result = {} should_alert_status = False should_alert_pose_score = False old_status = node.node_status if node_result.get('status', 'NOT_ON_LIST') != old_status and old_status != None: should_alert_status = True # print('{2} : {1} -> {0}'.format(node_result[NODE_STATUS], node.node_status, node.user.email)) node.node_collat_addr = node_result.get('collateralAddress', None) node.node_status = node_result.get('status', 'NOT_ON_LIST') old_pose_score = node.node_pose_score node.node_pose_score = node_result.get('state', {}).get('PoSePenalty', None) try: if int(old_pose_score) < int(node.node_pose_score): should_alert_pose_score = True except Exception as e: pass # for pose scores of none node.node_ip = node_result.get('state', {}).get('service', None) node.node_last_paid_time = None if node_result.get( 'lastpaidtime', 0) == 0 else datetime.datetime.fromtimestamp( int(node_result['lastpaidtime'])) node.node_last_paid_block = node_result.get('lastpaidblock', None) node.node_payout_addr = node_result.get('state', {}).get('payoutAddress', None) node.node_owner_addr = node_result.get('state', {}).get('ownerAddress', None) node.node_voting_addr = node_result.get('state', {}).get('votingAddress', None) node.node_protx_hash = node_result.get('proTxHash', None) node.node_oper_pubkey = node_result.get('state', {}).get( 'pubKeyOperator', None) node.node_oper_reward = node_result.get('operatorReward') node.save() if should_alert_status: status_alerts.append((node, old_status)) if should_alert_pose_score and not should_alert_status: pose_score_alerts.append((node, old_pose_score)) print('Processed {0} nodes, sending {1} emails'.format( len(all_nodes), len(status_alerts) + len(pose_score_alerts))) for alert in status_alerts: if should_send_mail: send_status_change_alert(*alert) else: print('would send mail') print(*alert) for alert in pose_score_alerts: if should_send_mail: send_score_increase_alert(*alert) else: print('would send mail') print(*alert)
def remove(): if 'UserID' not in flask.session: return flask.abort(404) nodelist = Node.select().where((Node.user == flask.session['UserID'])) return flask.render_template('remove.html', nodes=nodelist)
def node(nid): try: n = Node.select().join(User).where((Node.id == nid) & (User.id==flask.session['UserID']))[0] except: return flask.abort(404) return flask.render_template('node.html', node=n)
async def restart_lab_api(): ''' try: hosts_ids = request.json.get('hosts', list) except (ValueError, AttributeError): return abort(400) hosts = Host.select().where(Host.id in hosts_ids) if len(hosts) == 0: return abort(400) elif len(hosts) == 1: runtime = 'docker' else: runtime = 'cluster' ''' data = await request.json if data is None or 'name' not in data: return abort(400) name = data.get('name') lab, created = Lab.get_or_create(name=name, defaults={'runtime': runtime}) tc_options = data.get('tc_options', {}) hosts = Host.get_all_configs() if created: ret, killed_routers = start_lab(lab_path(name), prefix(name), *(() if runtime!='cluster' else \ (hosts,)), default_tc_options=tc_options, runtime=runtime) else: ret, killed_routers = restart_lab(lab_path(name), prefix(name), *(() if runtime!='cluster' else \ (lab.get_host_map(), hosts)), default_tc_options=tc_options, runtime=runtime) print('\n[ deleting old nodes & network ]\n') Node.delete().where(Node.lab_id == lab.id).execute() Network.delete().where(Network.lab_id == lab.id).execute() print('\n[ mid-state ]', list(Node.select())) if runtime == 'cluster': hosts_map = ret for host_name, (containers, networks) in ret.items(): Node.bulk_create([ Node(name=c.name, short_id=c.short_id, lab=lab, host_id=hosts[host_name]['id']) for c in containers ]) Network.bulk_create([ Network(name=n, lab=lab, host=hosts[host_name]['id']) for n in networks ]) msgr = Messenger(name) # with open(os.path.join('/sockets', name, 'lab.sock'), 'w') as f: # f.write('no') for container_name in killed_routers: routers_cache.set(container_name, True) return 'OK', 200
'subject': f'{node.label} from {old} to {node.node_status}', 'html': htmlx }, auth=('api', config['mailgun_key'])) print(r.status_code) print(r.text) except Exception as e: print('I FAILED TO SEND AN EMAIL') print(str(e)) print(str(node), str(old)) if __name__ == "__main__": nodeobj = Node.select().where((Node.id == 28))[0] print(nodeobj.label) send_status_change_alert(nodeobj, 'ENABLED') send_score_increase_alert(nodeobj, 9999) def send_pw_rst(email, token): htmlx = """\ <html> <head> </head> <body> <h1>{2} password reset</h1> <p><a href="https://{2}/forgot/{0}/{1}">https://{2}/forgot/{0}/{1}</a></p> </body> </html>