-
Notifications
You must be signed in to change notification settings - Fork 6
/
samples.go
170 lines (139 loc) · 3.79 KB
/
samples.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
//
// August 2016, Christian Cassar
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
//
//
// Packages exporting message samples for test purposes.
package mdt_msg_samples
import (
"bufio"
"encoding/json"
telem "github.com/cisco/bigmuddy-network-telemetry-proto/proto_go"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"strings"
)
type sampleTelemetryTable []SampleTelemetryTableEntry
type SampleTelemetryTableEntry struct {
Sample *telem.Telemetry
SampleStreamGPB []byte
SampleStreamJSON []byte
SampleStreamJSONKV []byte
Leaves int
Events int
}
type SampleTelemetryDatabaseID int
const (
SAMPLE_TELEMETRY_DATABASE_BASIC SampleTelemetryDatabaseID = iota
)
var sampleTelemetryDatabase map[SampleTelemetryDatabaseID]sampleTelemetryTable
func MDTSampleTelemetryTableFetchOne(
dbindex SampleTelemetryDatabaseID) *SampleTelemetryTableEntry {
if len(sampleTelemetryDatabase) <= int(dbindex) {
return nil
}
table := sampleTelemetryDatabase[dbindex]
return &table[0]
}
type MDTContext interface{}
type MDTSampleCallback func(sample *SampleTelemetryTableEntry, context MDTContext) (abort bool)
//
// MDTSampleTelemetryTableIterate iterates over table of samples
// calling caller with function MDTSampleCallback and opaque context
// MDTContext provided, for every known sample. The number of samples
// iterated over is returned.
func MDTSampleTelemetryTableIterate(
dbindex SampleTelemetryDatabaseID,
fn MDTSampleCallback,
c MDTContext) (applied int) {
if len(sampleTelemetryDatabase) <= int(dbindex) {
return 0
}
count := 0
table := sampleTelemetryDatabase[dbindex]
for _, entry := range table {
count++
if fn(&entry, c) {
break
}
}
return count
}
func MDTLoadMetrics() string {
b, e := ioutil.ReadFile("mdt_msg_samples/dump.metrics")
if e == nil {
return string(b)
}
return ""
}
func init() {
sampleTelemetryDatabase = make(map[SampleTelemetryDatabaseID]sampleTelemetryTable)
sampleTelemetryDatabase[SAMPLE_TELEMETRY_DATABASE_BASIC] = sampleTelemetryTable{}
marshaller := &jsonpb.Marshaler{
EmitDefaults: true,
OrigName: true,
}
kv, err := os.Open("dump.jsonkv")
if err != nil {
kv, err = os.Open("mdt_msg_samples/dump.jsonkv")
if err != nil {
log.Fatal(err)
}
}
defer kv.Close()
dump := bufio.NewReader(kv)
decoder := json.NewDecoder(dump)
_, err = decoder.Token()
if err != nil {
log.Fatal(err)
}
// Read the messages and build the db.
for decoder.More() {
var m telem.Telemetry
err := jsonpb.UnmarshalNext(decoder, &m)
if err != nil {
log.Fatal(err)
}
gpbstream, err := proto.Marshal(&m)
if err != nil {
log.Fatal(err)
}
jsonstream, err := marshaller.MarshalToString(&m)
if err != nil {
log.Fatal(err)
}
entry := SampleTelemetryTableEntry{
Sample: &m,
SampleStreamGPB: gpbstream,
SampleStreamJSONKV: json.RawMessage(jsonstream),
Leaves: strings.Count(jsonstream, "\"name\""),
Events: strings.Count(jsonstream, "\"content\""),
}
sampleTelemetryDatabase[SAMPLE_TELEMETRY_DATABASE_BASIC] =
append(sampleTelemetryDatabase[SAMPLE_TELEMETRY_DATABASE_BASIC], entry)
}
jsondump, err := ioutil.ReadFile("dump.json")
if err != nil {
jsondump, err = ioutil.ReadFile("mdt_msg_samples/dump.json")
if err != nil {
// No validation if we don't have the results
return
}
}
var rows []json.RawMessage
err = json.Unmarshal(jsondump, &rows)
if err != nil {
log.Fatal("Failed to unmarshall verification data")
}
for i, row := range rows {
sample := &sampleTelemetryDatabase[SAMPLE_TELEMETRY_DATABASE_BASIC][i]
sample.SampleStreamJSON = row
}
// log.Printf("%v", sampleTelemetryDatabase[SAMPLE_TELEMETRY_DATABASE_BASIC])
}