-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package daemons | ||
|
||
import ( | ||
"github.com/c-darwin/dcoin-go/packages/utils" | ||
"time" | ||
) | ||
|
||
func Stats(chBreaker chan bool, chAnswer chan string) { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error("daemon Recovered", r) | ||
panic(r) | ||
} | ||
}() | ||
|
||
GoroutineName := "Stats" | ||
d := new(daemon) | ||
d.DCDB = DbConnect(chBreaker, chAnswer, GoroutineName) | ||
if d.DCDB == nil { | ||
return | ||
} | ||
d.goRoutineName = GoroutineName | ||
d.chAnswer = chAnswer | ||
d.chBreaker = chBreaker | ||
d.sleepTime = 86400 | ||
if !d.CheckInstall(chBreaker, chAnswer, GoroutineName) { | ||
return | ||
} | ||
d.DCDB = DbConnect(chBreaker, chAnswer, GoroutineName) | ||
if d.DCDB == nil { | ||
return | ||
} | ||
|
||
BEGIN: | ||
for { | ||
log.Info(GoroutineName) | ||
MonitorDaemonCh <- []string{GoroutineName, utils.Int64ToStr(utils.Time())} | ||
|
||
// проверим, не нужно ли нам выйти из цикла | ||
if CheckDaemonsRestart(chBreaker, chAnswer, GoroutineName) { | ||
break BEGIN | ||
} | ||
// Get the current time. | ||
t := time.Now() | ||
variables, err := d.GetAllVariables() | ||
if err != nil { | ||
if d.dPrintSleep(err, d.sleepTime) { | ||
break BEGIN | ||
} | ||
continue BEGIN | ||
} | ||
CurrencyList, err := d.GetCurrencyList(false) | ||
if err != nil { | ||
if d.dPrintSleep(err, d.sleepTime) { | ||
break BEGIN | ||
} | ||
continue BEGIN | ||
} | ||
for currencyId, _ := range CurrencyList { | ||
sumPromisedAmount, err := d.Single(` | ||
SELECT sum(amount) as sum_amount | ||
FROM promised_amount | ||
WHERE status = 'mining' AND | ||
del_block_id = 0 AND | ||
(cash_request_out_time = 0 OR cash_request_out_time > ?) AND | ||
currency_id = ?`, utils.Time()-variables.Int64["cash_request_time"], currencyId).Float64() | ||
if err != nil { | ||
if d.dPrintSleep(err, d.sleepTime) { | ||
break BEGIN | ||
} | ||
continue BEGIN | ||
} | ||
// получаем кол-во DC на кошельках | ||
sumWallets, err := d.Single("SELECT sum(amount) as sum_amount FROM wallets WHERE currency_id = ?", currencyId).Float64() | ||
if err != nil { | ||
if d.dPrintSleep(err, d.sleepTime) { | ||
break BEGIN | ||
} | ||
continue BEGIN | ||
} | ||
// получаем кол-во TDC на обещанных суммах, плюсуем к тому, что на кошельках | ||
sumTdc, err := d.Single("SELECT sum(tdc_amount) as sum_amount FROM promised_amount WHERE currency_id = ?", currencyId).Float64() | ||
if err != nil { | ||
if d.dPrintSleep(err, d.sleepTime) { | ||
break BEGIN | ||
} | ||
continue BEGIN | ||
} | ||
sumWallets += sumTdc | ||
|
||
err = d.ExecSql(`INSERT INTO stats (day, month, year, currency_id, dc, promised_amount) VALUES (?, ?, ?, ?, ?, ?)`, t.Day(), int(t.Month()), t.Year(), currencyId, sumWallets, sumPromisedAmount) | ||
if err != nil { | ||
log.Error("%v", err) | ||
} | ||
} | ||
if d.dSleep(d.sleepTime) { | ||
break BEGIN | ||
} | ||
} | ||
log.Debug("break BEGIN %v", GoroutineName) | ||
} |