Skip to content

Commit dbc6850

Browse files
authored
Connect text feature with back end (#341)
* Add manual Save feature. * Fix the incorrect style * Add feature to record Text * Create a simple UI for text preview. * Connect frontend with backend text data * Move a constant from parameter to the body. Remove test log
1 parent ce80732 commit dbc6850

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

frontend/src/texts/Texts.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export default {
129129
});
130130
131131
getRuns().then(({errno, data}) => {
132-
console.log(data);
133132
this.runsArray = data;
134133
this.config.runs = data;
135134
});

visualdl/logic/sdk.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,13 @@ struct HistogramReader {
284284
TabletReader reader_;
285285
};
286286

287+
/*
288+
* Text component writer
289+
*/
287290
struct Text {
288-
Text(Tablet tablet) : tablet_(tablet) {}
291+
Text(Tablet tablet) : tablet_(tablet) {
292+
tablet_.SetType(Tablet::Type::kText);
293+
}
289294
void SetCaption(const std::string cap) {
290295
tablet_.SetCaptions(std::vector<std::string>({cap}));
291296
}
@@ -304,6 +309,9 @@ struct Text {
304309
Tablet tablet_;
305310
};
306311

312+
/*
313+
* Text reader.
314+
*/
307315
struct TextReader {
308316
TextReader(TabletReader reader) : reader_(reader) {}
309317

visualdl/server/lib.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,44 @@ def get_histogram_tags(storage):
159159
return get_tags(storage, 'histogram')
160160

161161

162-
def get_histogram(storage, mode, tag, num_samples=100):
162+
def get_texts_tags(storage):
163+
return get_tags(storage, 'text')
164+
165+
166+
def get_texts(storage, mode, tag, num_records=100):
167+
with storage.mode(mode) as reader:
168+
texts = reader.text(tag)
169+
170+
records = texts.records()
171+
ids = texts.ids()
172+
timestamps = texts.timestamps()
173+
174+
data = list(zip(timestamps, ids, records))
175+
data_size = len(data)
176+
177+
if data_size <= num_records:
178+
return data
179+
180+
span = float(data_size) / (num_records - 1)
181+
span_offset = 0
182+
183+
data_idx = int(span_offset * span)
184+
sampled_data = []
185+
186+
while data_idx < data_size:
187+
sampled_data.append(data[data_size - data_idx - 1])
188+
span_offset += 1
189+
data_idx = int(span_offset * span)
190+
191+
sampled_data.append(data[0])
192+
res = sampled_data[::-1]
193+
# TODO(Superjomn) some bug here, sometimes there are zero here.
194+
if res[-1] == 0.:
195+
res = res[:-1]
196+
return res
197+
198+
199+
def get_histogram(storage, mode, tag):
163200
with storage.mode(mode) as reader:
164201
histogram = reader.histogram(tag)
165202
res = []
@@ -182,6 +219,9 @@ def get_histogram(storage, mode, tag, num_samples=100):
182219
instance = record.instance(j)
183220
data.append(
184221
[instance.left(), instance.right(), instance.frequency()])
222+
223+
# num_samples: We will only return 100 samples.
224+
num_samples = 100
185225
if len(res) < num_samples:
186226
return res
187227

visualdl/server/visualDL

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ def histogram_tags():
171171
return Response(json.dumps(result), mimetype='application/json')
172172

173173

174+
@app.route("/data/plugin/texts/tags")
175+
def texts_tags():
176+
data = cache_get("/data/plugin/texts/tags", try_call,
177+
lib.get_texts_tags, log_reader)
178+
result = gen_result(0, "", data)
179+
return Response(json.dumps(result), mimetype='application/json')
180+
181+
174182
@app.route('/data/plugin/scalars/scalars')
175183
def scalars():
176184
run = request.args.get('run')
@@ -219,6 +227,16 @@ def histogram():
219227
return Response(json.dumps(result), mimetype='application/json')
220228

221229

230+
@app.route('/data/plugin/texts/texts')
231+
def texts():
232+
run = request.args.get('run')
233+
tag = request.args.get('tag')
234+
key = os.path.join('/data/plugin/texts/texts', run, tag)
235+
data = cache_get(key, try_call, lib.get_texts, log_reader, run, tag)
236+
result = gen_result(0, "", data)
237+
return Response(json.dumps(result), mimetype='application/json')
238+
239+
222240
@app.route('/data/plugin/graphs/graph')
223241
def graph():
224242
# TODO(ChunweiYan) need to add a config for whether have graph.

visualdl/storage/storage.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ message Tablet {
107107
kScalar = 0;
108108
kHistogram = 1;
109109
kImage = 2;
110+
kText = 3;
110111
}
111112
// The unique identification for this `Tablet`. VisualDL will have no the
112113
// concept of FileWriter like TB. It will store all the tablets in a single

visualdl/storage/tablet.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ struct TabletReader;
2929
* Tablet is a helper for operations on storage::Tablet.
3030
*/
3131
struct Tablet {
32-
enum Type { kScalar = 0, kHistogram = 1, kImage = 2, kUnknown = -1 };
32+
enum Type {
33+
kScalar = 0,
34+
kHistogram = 1,
35+
kImage = 2,
36+
kText = 3,
37+
kUnknown = -1
38+
};
3339

3440
DECL_GUARD(Tablet);
3541

@@ -46,6 +52,9 @@ struct Tablet {
4652
if (name == "image") {
4753
return kImage;
4854
}
55+
if (name == "text") {
56+
return kText;
57+
}
4958
LOG(ERROR) << "unknown component: " << name;
5059
return kUnknown;
5160
}

0 commit comments

Comments
 (0)