/
bugout.go
139 lines (119 loc) · 3.93 KB
/
bugout.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
package main
// Much of this code is copied from waggle: https://github.com/bugout-dev/waggle/blob/main/main.go
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"os"
"strings"
bugout "github.com/bugout-dev/bugout-go/pkg"
spire "github.com/bugout-dev/bugout-go/pkg/spire"
)
func CleanTimestamp(rawTimestamp string) string {
return strings.ReplaceAll(rawTimestamp, " ", "T")
}
func GetCursorFromJournal(client *bugout.BugoutClient, token, journalID, cursorName string) (string, error) {
query := fmt.Sprintf("context_type:waggle tag:type:cursor tag:cursor:%s", cursorName)
parameters := map[string]string{
"order": "desc",
"content": "true", // We may use the content in the future, even though we are simply using context_url right now
}
results, err := client.Spire.SearchEntries(token, journalID, query, 1, 0, parameters)
if err != nil {
return "", err
}
if results.TotalResults == 0 {
return "", nil
}
return results.Results[0].ContextUrl, nil
}
func WriteCursorToJournal(client *bugout.BugoutClient, token, journalID, cursorName, cursor, queryTerms string) error {
title := fmt.Sprintf("waggle cursor: %s", cursorName)
entryContext := spire.EntryContext{
ContextType: "waggle",
ContextID: cursor,
ContextURL: cursor,
}
tags := []string{
"type:cursor",
fmt.Sprintf("cursor:%s", cursorName),
fmt.Sprintf("waggle_version:%s", WAGGLE_VERSION),
}
content := fmt.Sprintf("Cursor: %s at %s\nQuery: %s", cursorName, cursor, queryTerms)
_, err := client.Spire.CreateEntry(token, journalID, title, content, tags, entryContext)
return err
}
func ReportsIterator(client *bugout.BugoutClient, token, journalID, cursor, queryTerms string, limit, offset int) (spire.EntryResultsPage, error) {
var query string = fmt.Sprintf("!tag:type:cursor %s", queryTerms)
if cursor != "" {
cleanedCursor := CleanTimestamp(cursor)
query = fmt.Sprintf("%s created_at:>%s", query, cleanedCursor)
fmt.Fprintln(os.Stderr, "query:", query)
}
parameters := map[string]string{
"order": "asc",
"content": "false",
}
return client.Spire.SearchEntries(token, journalID, query, limit, offset, parameters)
}
func LoadDropperReports(searchResults spire.EntryResultsPage) ([]DropperClaimMessage, error) {
reports := make([]DropperClaimMessage, len(searchResults.Results))
for i, result := range searchResults.Results {
parseErr := json.Unmarshal([]byte(result.Content), &reports[i])
if parseErr != nil {
return reports, parseErr
}
}
return reports, nil
}
func DropperReportsToCSV(reports []DropperClaimMessage, header bool, w io.Writer) error {
numRecords := len(reports)
startIndex := 0
if header {
numRecords++
startIndex++
}
records := make([][]string, numRecords)
if header {
records[0] = []string{
"dropId", "requestID", "claimant", "blockDeadline", "amount", "signer", "signature",
}
}
for i, report := range reports {
records[i+startIndex] = []string{
report.DropId,
report.RequestID,
report.Claimant,
report.BlockDeadline,
report.Amount,
report.Signer,
report.Signature,
}
}
csvWriter := csv.NewWriter(w)
return csvWriter.WriteAll(records)
}
func ProcessDropperClaims(client *bugout.BugoutClient, bugoutToken, journalID, cursorName, query string, batchSize int, header bool, w io.Writer) error {
cursor, cursorErr := GetCursorFromJournal(client, bugoutToken, journalID, cursorName)
if cursorErr != nil {
return cursorErr
}
searchResults, searchErr := ReportsIterator(client, bugoutToken, journalID, cursor, query, batchSize, 0)
if searchErr != nil {
return searchErr
}
reports, loadErr := LoadDropperReports(searchResults)
if loadErr != nil {
return loadErr
}
writeErr := DropperReportsToCSV(reports, header, w)
if writeErr != nil {
return writeErr
}
var processedErr error
if len(searchResults.Results) > 0 {
processedErr = WriteCursorToJournal(client, bugoutToken, journalID, cursorName, searchResults.Results[len(searchResults.Results)-1].CreatedAt, query)
}
return processedErr
}