如何读取切片附加到它?

时间:2018-02-19 14:45:03

标签: go

我想读取切片并向其添加新数据。我有一个json格式的webapi来读取统计数据。所以我想抓住统计数据,把它交给一个函数,用json之前的现有统计数据写一个新的切片。

然后我想在最后添加新数据。并使用来自json和new的旧统计信息给出“new”Slice(Struct)。

我尝试了很多,就是这样。

这是json看起来像

{"24hreward":0,"currentHashrate":0,"hashrate":0,"history":[{"currenthashrate":6024360000,"online":0,"offline":0,"timestamp":0}],"pageSize":30,"payments":null,"paymentsTotal":0,"rewards":[{"blockheight":223115,"timestamp":1518179084,"blockhash":"0x*************ee03802ee52f411102159a7c9268fec4e46571daa07e84","reward":6024360000,"percent":1,"immature":false}],"roundShares":3674,"stats":{"balance":6024360000,"blocksFound":1,"immature":0,"lastShare":1518208862},"sumrewards":[{"inverval":3600,"reward":0,"name":"Last 60 minutes","offset":0},{"inverval":43200,"reward":0,"name":"Last 12 hours","offset":0},{"inverval":86400,"reward":0,"name":"Last 24 hours","offset":0},{"inverval":604800,"reward":0,"name":"Last 7 days","offset":0},{"inverval":2592000,"reward":6024360000,"name":"Last 30 days","offset":0}],"workers":{},"workersOffline":0,"workersOnline":0,"workersTotal":0}

这是结构

type HistoryData struct {
    CurrentHashrate     int64    `json:"currenthashrate"`
    Online              int64    `json:"online"`
    Offline             int64    `json:"offline"`
    Timestamp           int64    `json:"timestamp"`
}

在那里你可以看到“历史”。我想阅读历史并将其发布到我的功能

cmds, err := tx.Exec(func() error {
    tx.ZRemRangeByScore(r.formatKey("hashrate", login), "-inf", fmt.Sprint("(", now-largeWindow))
    tx.ZRangeWithScores(r.formatKey("hashrate", login), 0, -1)
    tx.LRange(r.formatKey("lastshares"), 0, 4999)
    tx.ZRevRangeWithScores(r.formatKey("rewards", login), 0, 39)
    tx.ZRevRangeWithScores(r.formatKey("rewards", login), 0, -1)
    tx.ZRevRangeWithScores(r.formatKey("history", login), 0, 64)
    return nil
})

stats["history"] = convertHistoryResults(currentHashrate, online, offline, now, cmds[5].(*redis.ZSliceCmd))

我想在convertHistoryResults中提供“currentHashrate,online,offline,now”和 cmds [5],但没有任何内容。 json显示数据,但我认为他无法阅读:/多数民众赞成我的问题 这里有函数convertHistoryResults来构建json中“history”的列表

var count = 0;
func convertHistoryResults(currenthashrate int64, online int64, offline int64, now int64, rows ...*redis.ZSliceCmd) []*HistoryData {
    var result []*HistoryData
    log.Println("run1")
    log.Println(count)
    log.Println(rows)
    history := HistoryData{}
    for _, row := range rows {
        for _, v := range row.Val() {
                log.Println("run3")
                fields := strings.Split(v.Member.(string), ":")
                history.CurrentHashrate, _ = strconv.ParseInt(fields[0], 10, 64)
                history.Online, _ = strconv.ParseInt(fields[1], 10, 64)
                history.Offline, _ = strconv.ParseInt(fields[2], 10, 64)
                history.Timestamp, _ = strconv.ParseInt(fields[3], 10, 64)
                result = append(result, &history)
        }
    }
    if(count > 0) { // Count = 40 ~4-6min
        history.CurrentHashrate = currenthashrate


    history.Online = online
    history.Offline = offline
    history.Timestamp = now
    result = append(result, &history)
    log.Println("run2")
    //if len(result) > 300 {
    //    result = result[1:]
    //}
    count = 0;
} else {
    log.Println("run4")
    count++
}
return result
}

在这里,我构建了新的HistoryData,并希望读取“旧”行并将它们附加到HistoryData,而不是我想用新的数据将其添加到其中,因为我不想要这几秒。

但我的问题是我无法从json读出旧的“历史”,但我不知道为什么。 随着“奖励”它的工作。它来自https://github.com/sammy007/open-ethereum-pool

的代码

我尝试的是,阅读“历史”并让他们转换历史结果以建立新历史以将其附加到“历史”。

希望有人可以帮助我获得旧的“历史”并重建为新的“历史”

此脚本来自不同的用户,因此我必须为每个运行它的人重建“历史记录”。

这是日志中的打印

[ZREVRANGE miner:history:0x******************** 0 64 WITHSCORES: []]

你看,WITHSCORES是[]

我也尝试过奖励,就像那样

[ZREVRANGE miner:rewards:0x******************** 0 64 WITHSCORES: [{1.518179084e+09 6024360000:1.000000000:0:0x**********************159a7c9268fec4e46571daa07e84:223115:1518179084}]]

0 个答案:

没有答案