forked from decred/dcrdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasaver.go
251 lines (209 loc) · 6.53 KB
/
datasaver.go
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
// Copyright (c) 2017, Jonathan Chappelow
// See LICENSE for details.
// Interface for saving/storing BlockData.
// Create a BlockDataSaver by implementing Store(*BlockData).
package blockdata
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sync"
)
// BlockDataSaver is an interface for saving/storing BlockData
type BlockDataSaver interface {
Store(data *BlockData) error
}
////////////////////////////////////////////////////////////////////////////////
// The rest of the file contains some simple savers. MAKE YOUR OWN to satisfy
// BlockDataSaver.
// /////////////////////////////////////////////////////////////////////////////
// BlockDataToJSONStdOut implements BlockDataSaver interface for JSON output to
// stdout
type BlockDataToJSONStdOut struct {
mtx *sync.Mutex
}
// BlockDataToSummaryStdOut implements BlockDataSaver interface for plain text
// summary to stdout
type BlockDataToSummaryStdOut struct {
mtx *sync.Mutex
sdiffWinSize int64
}
type fileSaver struct {
folder string
nameBase string
file os.File
mtx *sync.Mutex
}
// BlockDataToJSONFiles implements BlockDataSaver interface for JSON output to
// the file system
type BlockDataToJSONFiles struct {
fileSaver
}
// BlockDataToMySQL implements BlockDataSaver interface for output to a
// MySQL database
// type BlockDataToMySQL struct {
// mtx *sync.Mutex
// }
// NewBlockDataToJSONStdOut creates a new BlockDataToJSONStdOut with optional
// existing mutex
func NewBlockDataToJSONStdOut(m ...*sync.Mutex) *BlockDataToJSONStdOut {
if len(m) > 1 {
panic("Too many inputs.")
}
if len(m) > 0 {
return &BlockDataToJSONStdOut{m[0]}
}
return &BlockDataToJSONStdOut{}
}
// NewBlockDataToSummaryStdOut creates a new BlockDataToSummaryStdOut with
// optional existing mutex
func NewBlockDataToSummaryStdOut(sdiffWinSize int64, m ...*sync.Mutex) *BlockDataToSummaryStdOut {
if len(m) > 1 {
panic("Too many inputs.")
}
if len(m) > 0 {
return &BlockDataToSummaryStdOut{m[0], sdiffWinSize}
}
return &BlockDataToSummaryStdOut{sdiffWinSize: sdiffWinSize}
}
// NewBlockDataToJSONFiles creates a new BlockDataToJSONFiles with optional
// existing mutex
func NewBlockDataToJSONFiles(folder string, fileBase string,
m ...*sync.Mutex) *BlockDataToJSONFiles {
if len(m) > 1 {
panic("Too many inputs.")
}
var mtx *sync.Mutex
if len(m) > 0 {
mtx = m[0]
} else {
mtx = new(sync.Mutex)
}
return &BlockDataToJSONFiles{
fileSaver: fileSaver{
folder: folder,
nameBase: fileBase,
file: os.File{},
mtx: mtx,
},
}
}
// Store writes BlockData to stdout in JSON format
func (s *BlockDataToJSONStdOut) Store(data *BlockData) error {
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
// Marshall all the block data results in to a single JSON object, indented
jsonConcat, err := JSONFormatBlockData(data)
if err != nil {
return err
}
// Write JSON to stdout with guards to delimit the object from other text
fmt.Printf("\n--- BEGIN BlockData JSON ---\n")
_, err = writeFormattedJSONBlockData(jsonConcat, os.Stdout)
fmt.Printf("--- END BlockData JSON ---\n\n")
return err
}
// Store writes BlockData to stdout as plain text summary
func (s *BlockDataToSummaryStdOut) Store(data *BlockData) error {
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
fmt.Printf("\nBlock %v:\n", data.Header.Height)
fmt.Printf(" Stake difficulty: %9.3f -> %.3f (current -> next block)\n",
data.CurrentStakeDiff.CurrentStakeDifficulty,
data.CurrentStakeDiff.NextStakeDifficulty)
fmt.Printf(" Estimated price in next window: %9.3f / [%.2f, %.2f] ([min, max])\n",
data.EstStakeDiff.Expected, data.EstStakeDiff.Min, data.EstStakeDiff.Max)
fmt.Printf(" Window progress: %3d / %3d in price window number %v\n",
data.IdxBlockInWindow, s.sdiffWinSize, data.PriceWindowNum)
fmt.Printf(" Ticket fees: %.4f, %.4f, %.4f (mean, median, std), n=%d\n",
data.FeeInfo.Mean, data.FeeInfo.Median, data.FeeInfo.StdDev,
data.FeeInfo.Number)
if data.PoolInfo.Value >= 0 {
fmt.Printf(" Ticket pool: %v (size), %.3f (avg. price), %.2f (total DCR locked)\n",
data.PoolInfo.Size, data.PoolInfo.ValAvg, data.PoolInfo.Value)
}
fmt.Printf(" Node connections: %d\n", data.Connections)
return nil
}
// Store writes BlockData to a file in JSON format
// The file name is nameBase+height+".json".
func (s *BlockDataToJSONFiles) Store(data *BlockData) error {
if s.mtx != nil {
s.mtx.Lock()
defer s.mtx.Unlock()
}
// Marshall all the block data results in to a single JSON object, indented
jsonConcat, err := JSONFormatBlockData(data)
if err != nil {
return err
}
// Write JSON to a file with block height in the name
height := data.Header.Height
fname := fmt.Sprintf("%s%d.json", s.nameBase, height)
fullfile := filepath.Join(s.folder, fname)
fp, err := os.Create(fullfile)
if err != nil {
log.Errorf("Unable to open file %v for writing.", fullfile)
return err
}
defer fp.Close()
s.file = *fp
_, err = writeFormattedJSONBlockData(jsonConcat, &s.file)
return err
}
func writeFormattedJSONBlockData(jsonConcat *bytes.Buffer, w io.Writer) (int, error) {
n, err := fmt.Fprintln(w, jsonConcat.String())
// there was once more, perhaps again.
return n, err
}
// JSONFormatBlockData concatenates block data results into a single JSON
// object with primary keys for the result type
func JSONFormatBlockData(data *BlockData) (*bytes.Buffer, error) {
var jsonAll bytes.Buffer
jsonAll.WriteString("{\"estimatestakediff\": ")
stakeDiffEstJSON, err := json.Marshal(data.EstStakeDiff)
if err != nil {
return nil, err
}
jsonAll.Write(stakeDiffEstJSON)
//stakeDiffEstJSON, err := json.MarshalIndent(data.eststakediff, "", " ")
//fmt.Println(string(stakeDiffEstJSON))
jsonAll.WriteString(",\"currentstakediff\": ")
stakeDiffJSON, err := json.Marshal(data.CurrentStakeDiff)
if err != nil {
return nil, err
}
jsonAll.Write(stakeDiffJSON)
jsonAll.WriteString(",\"ticketfeeinfo_block\": ")
feeInfoJSON, err := json.Marshal(data.FeeInfo)
if err != nil {
return nil, err
}
jsonAll.Write(feeInfoJSON)
jsonAll.WriteString(",\"block_header\": ")
blockHeaderJSON, err := json.Marshal(data.Header)
if err != nil {
return nil, err
}
jsonAll.Write(blockHeaderJSON)
jsonAll.WriteString(",\"ticket_pool_info\": ")
poolInfoJSON, err := json.Marshal(data.PoolInfo)
if err != nil {
return nil, err
}
jsonAll.Write(poolInfoJSON)
jsonAll.WriteString("}")
var jsonAllIndented bytes.Buffer
err = json.Indent(&jsonAllIndented, jsonAll.Bytes(), "", " ")
if err != nil {
return nil, err
}
return &jsonAllIndented, err
}