-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjperf.py
265 lines (251 loc) · 7.94 KB
/
jperf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# -*- coding: utf-8 -*-
# should run on win and raspi
#
# Performance of duco: every 10 seconds query the balance of user
# shows 10 seconds diff, one minute moving average
# logs written to /logs perf_username_day_hour.txt
# logs contain the total balance while screen shows
# growth since start of program
#
# see https://github.com/dansinclair25/duco-rest-api
# {"result":{"balance":187.32699041006376,"username":"targon"},"success":true}\n
uname = "targon"
tick=10
minerInfo=False
alpha=0.5 #for exp. smoothing
import time
import sys
import json
import requests
from kbhit import KBHit
kb = KBHit()
import jrests
jr=jrests.rests(uname)
try:
import privusers as priv
jr.users=priv.users
except ImportError:
jr.users=[jr.username] #0u is me
print("no privusers.py, having")
print("user=['user0','user1','user2']")
def getBalance():
global connected
connected=False
try:
r=requests.get('https://server.duinocoin.com/balances/'+jr.username)
except Exception as inst:
print ("***getBalance Exception "+str(inst))
return 0
jdic=json.loads(r.text)
if jdic['success']==True:
dic=jdic['result']
connected=True
return dic['balance']
else:
print(jdic['success'])
print (r.text)
print('***getBalance for',jr.username,'success not true')
return 0
def query():
anf=0
bal=0
prev=0
thistime=time.time()
lasttime=thistime
print(" Please wait, tick is",tick) # 10 normal, else fast
anf=float(getBalance())
if not connected:
print ('***No balance for',jr.username)
return
prev=anf
ar=[0,0,0,0,0,0]
su=0
arP=0
cnt=0
ytm1=0
logN=time.strftime("_%d_%H.txt", time.localtime())
if tick==10: #just in case 2 sessions inquire same user
logN='logs/perf_'+jr.username+logN
else:
logN='logs/fast_'+jr.username+logN
logf = open(logN, "a")
print("Logfile",logN)
tx="Starting "+time.strftime("%c", time.localtime())+" with "+str(anf )
logf.write(tx+"\n")
print(tx)
print("values below in milliDuco, allow 1 minute settling time")
if tick==10:
print( "Time Total Hash Mnr 10 sec Minute Duco/d")
else:
print( "Time Total Hash Mnr timedelt increase")
while True:
rest=tick - time.time() % tick #time to sleep
while rest>1:
if kb.kbhit():
logf.close()
kb.forgetch()
return
time.sleep(1)
rest-=1
time.sleep(tick - time.time() % tick) # wait exact
print("Query",end="\b\b\b\b\b",flush=True)
thistime=time.time()
txti=time.strftime("%H:%M:%S", time.localtime())
# REST api
bal=float(getBalance())
print("Balance",end="\b\b\b\b\b\b\b",flush=True)
cnt+=1
show=2
if tick != 10:
if bal==prev:
show=0
else:
show=1
if show>0:
if minerInfo:
txmi = " {:2d}".format(jr.getMiners())
txha = " {:6d}".format(jr.getHashTotal())
else:
txmi = " "
txha = " "
# prepare
dif10=bal-prev
tx10="{:10.3f}".format(dif10*1000) # this 10 secs
dif99=bal-anf
tx99="{:10.3f}".format(dif99*1000) # from start
su=su-ar[arP]+dif10 # moving average / min
ar[arP]=dif10
arP+=1
if(arP>5):
arP=0
txsu="{:10.3f}".format(su*1000) # per minute
txpd="{:10.2f}".format(su*1440) # per day
txab="{:15.6f}".format(bal) # abs for logf
if cnt>6 : #exp. smoothing
ytm1=alpha*(su*1440-ytm1)+ytm1
txex="{:10.2f}".format(ytm1)
else:
ytm1=su*1440
txex=" "
# write
if show==1: #fast
txtd=" ({:6.3f})".format(thistime-lasttime)
txs=txti+" "+tx99+txha+txmi+txtd+tx10
txl=txti+" "+txab+txha+txmi+txtd+tx10
lasttime=thistime
else:
txs=txti+" "+tx99+txha+txmi+tx10+txsu+txpd+txex
txl=txti+" "+txab+txha+txmi+tx10+txsu+txpd
print (txs)
logf.write(txl+"\n")
prev=bal
if not connected: #something went wrong, close log
logf.close()
print ("query terminated")
return
if kb.kbhit():
logf.close()
kb.forgetch()
return
def hilfe():
print(" \n\
a get AVR Top e.g 20a \n\
b Balance \n\
i get I2c Top e.g. 1i \n\
q Query \n\
m toggle minerinfo (if slow)\n\
\n\
o show Other users \n\
u switchUser, e.g. 3u \n\
s Show topusers \n\
t switchTopuser (after getAVR!), e.g. 1t \n\
\n\
f Fast mode: tick 1 second, only changes shown \n\
n Normal mode: tick 10 seconds \n\
\n\
x eXit \n\
\n")
def menu():
global tick
global connected
global minerInfo
global alpha
inpAkt=False
inp=0
query()
connected =True
# here after keypress
while True:
if not inpAkt: print(jr.username,"P>",end='',flush=True)
while not connected:
print ("Retry")
query()
try:
ch = kb.getch()
except Exception as inst:
print ("*** Don't use this key, Exception "+str(inst))
ch='?'
print(ch,end='',flush=True)
if ((ch >= '0') and (ch <= '9')):
if (inpAkt) :
inp = inp * 10 + (ord(ch) - 48);
else:
inpAkt = True;
inp = ord(ch) - 48;
else:
inpAkt=False
try:
if ch==" ":
pass
elif ch=="a":
jr.getAllMiners()
num=inp
if num<10:
print ("assume minimum 10")
num=10
jr.topUsers(num,'AVR')
elif ch=="A": #override 10
jr.topUsers(inp,'AVR')
elif ch=="b":
print(" Balance of ",jr.username,'is',getBalance())
elif ch=="f":
tick=1
query()
elif ch=="g":
alpha=inp/1000
print("Alpha ",alpha)
elif ch=="i":
jr.topUsers(inp,'I2C')
elif ch=="m":
minerInfo = not minerInfo
print ("Minerinfo",minerInfo)
query()
elif ch=="n":
tick=10
query()
elif ch=="q":
query()
elif ch=="o":
jr.showUsers()
elif ch=="s":
jr.showTopsers()
elif ch=="t":
if jr.switchTopser(inp):
query()
elif ch=="u":
if jr.switchUser(inp):
query()
elif ch=="x":
print(" Thanks for watching")
return
elif ch=="#" or ch=='?':
hilfe()
else:
print(" ? (ascii "+str(ord(ch))+"), type ? for help")
except Exception as inst:
print ("*** menu Exception "+str(inst))
#raise #to ease fix
if len(sys.argv)>1:
jr.username = sys.argv[1]
print ("username is ",jr.username)
menu()