Skip to content

Commit 99b4e5c

Browse files
authored
Merge pull request #7 from ChunweiYan/feature/link_sdk_with_server
2 parents b0d0f50 + eb6d0cc commit 99b4e5c

File tree

11 files changed

+199
-85
lines changed

11 files changed

+199
-85
lines changed

server/visualdl/visual_dl.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from visualdl.log import logger
1414
import visualdl.mock.data as mock_data
1515
import visualdl.mock.tags as mock_tags
16+
import storage
1617

1718
app = Flask(__name__, static_url_path="")
1819

@@ -31,7 +32,14 @@ def option_parser():
3132
default=8040,
3233
action="store",
3334
dest="port",
34-
help="rest api service port")
35+
help="api service port")
36+
parser.add_option(
37+
"-t",
38+
"--host",
39+
type=str,
40+
default="0.0.0.0",
41+
action="store",
42+
help="api service ip")
3543
parser.add_option(
3644
"--logdir", action="store", dest="logdir", help="log file directory")
3745
return parser.parse_args()
@@ -42,6 +50,8 @@ def option_parser():
4250
static_file_path = "./frontend/dist/"
4351
mock_data_path = "./mock_data/"
4452

53+
storage = storage.StorageReader(options.logdir)
54+
4555

4656
# return data
4757
# status, msg, data
@@ -85,7 +95,22 @@ def runs():
8595
@app.route("/data/plugin/scalars/tags")
8696
def tags():
8797
is_debug = bool(request.args.get('debug'))
88-
result = gen_result(0, "", mock_tags.data())
98+
tag = request.args.get('tag')
99+
if is_debug:
100+
result = mock_tags.data()
101+
else:
102+
result = {}
103+
print 'modes', storage.modes()
104+
for mode in storage.modes():
105+
result[mode] = {}
106+
reader = storage.as_mode(mode)
107+
for tag in reader.tags("scalar"):
108+
result[mode][tag] = {
109+
'displayName': reader.scalar(tag).caption(),
110+
'description': ""
111+
}
112+
print 'tags', result
113+
result = gen_result(0, "", result)
89114
return Response(json.dumps(result), mimetype='application/json')
90115

91116

@@ -94,10 +119,22 @@ def scalars():
94119
run = request.args.get('run')
95120
tag = request.args.get('tag')
96121
is_debug = bool(request.args.get('debug'))
97-
result = gen_result(0, "", mock_data.sequence_data())
122+
if is_debug:
123+
result = gen_result(0, "", mock_data.sequence_data())
124+
else:
125+
reader = storage.as_mode(run)
126+
scalar = reader.scalar(tag)
127+
128+
records = scalar.records()
129+
ids = scalar.ids()
130+
timestamps = scalar.timestamps()
131+
132+
result = zip(timestamps, ids, records)
133+
result = gen_result(0, "", result)
134+
98135
return Response(json.dumps(result), mimetype='application/json')
99136

100137

101138
if __name__ == '__main__':
102139
logger.info(" port=" + str(options.port))
103-
app.run(debug=False, host="0.0.0.0", port=options.port)
140+
app.run(debug=False, host=options.host, port=options.port)

visualdl/logic/pybind.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ PYBIND11_PLUGIN(core) {
3838
return vs::components::ScalarReader<T>(std::move(tablet)); \
3939
})
4040
py::class_<vs::Reader>(m, "Reader")
41-
.def(
42-
"__init__",
43-
[](vs::Reader& instance,
44-
const std::string& mode,
45-
const std::string& dir) { new (&instance) vs::Reader(mode, dir); })
41+
.def("__init__",
42+
[](vs::Reader& instance, const std::string& dir) {
43+
new (&instance) vs::Reader(dir);
44+
})
45+
.def("as_mode", &vs::Reader::AsMode)
46+
.def("modes", [](vs::Reader& self) { return self.storage().modes(); })
47+
.def("tags", &vs::Reader::tags)
4648
// clang-format off
4749
ADD_SCALAR(float)
4850
ADD_SCALAR(double)
@@ -59,8 +61,7 @@ PYBIND11_PLUGIN(core) {
5961
py::class_<vs::Writer>(m, "Writer")
6062
.def("__init__",
6163
[](vs::Writer& instance, const std::string& dir, int sync_cycle) {
62-
new (&instance) vs::Writer(dir);
63-
instance.storage().meta.cycle = sync_cycle;
64+
new (&instance) vs::Writer(dir, sync_cycle);
6465
})
6566
.def("as_mode", &vs::Writer::AsMode)
6667
// clang-format off

visualdl/logic/sdk.h

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@
66
#include "visualdl/utils/string.h"
77
namespace visualdl {
88

9+
const static std::string kDefaultMode{"default"};
10+
911
class Writer {
1012
public:
11-
Writer(const std::string& dir) {
13+
Writer(const std::string& dir, int sync_cycle) {
1214
storage_.SetDir(dir);
15+
storage_.meta.cycle = sync_cycle;
16+
}
17+
Writer(const Writer& other) {
18+
storage_ = other.storage_;
19+
mode_ = other.mode_;
1320
}
1421

15-
Writer& AsMode(const std::string& mode) {
16-
mode_ = mode;
22+
Writer AsMode(const std::string& mode) {
23+
Writer writer = *this;
1724
storage_.AddMode(mode);
18-
return *this;
25+
writer.mode_ = mode;
26+
return writer;
1927
}
2028

2129
Tablet AddTablet(const std::string& tag) {
@@ -31,23 +39,64 @@ class Writer {
3139

3240
private:
3341
Storage storage_;
34-
std::string mode_;
42+
std::string mode_{kDefaultMode};
3543
};
3644

3745
class Reader {
3846
public:
39-
Reader(const std::string& mode, const std::string& dir)
40-
: mode_(mode), reader_(dir) {}
47+
Reader(const std::string& dir) : reader_(dir) {}
48+
49+
Reader AsMode(const std::string& mode) {
50+
auto tmp = *this;
51+
tmp.mode_ = mode;
52+
return tmp;
53+
}
4154

4255
TabletReader tablet(const std::string& tag) {
4356
auto tmp = mode_ + "/" + tag;
4457
string::TagEncode(tmp);
4558
return reader_.tablet(tmp);
4659
}
4760

61+
std::vector<std::string> all_tags() {
62+
auto tags = reader_.all_tags();
63+
auto it =
64+
std::remove_if(tags.begin(), tags.end(), [&](const std::string& tag) {
65+
return !TagMatchMode(tag);
66+
});
67+
tags.erase(it + 1);
68+
return tags;
69+
}
70+
71+
std::vector<std::string> tags(const std::string& component) {
72+
auto type = Tablet::type(component);
73+
auto tags = reader_.tags(type);
74+
CHECK(!tags.empty());
75+
std::vector<std::string> res;
76+
for (const auto& tag : tags) {
77+
if (TagMatchMode(tag)) {
78+
res.push_back(GenReadableTag(tag));
79+
}
80+
}
81+
return res;
82+
}
83+
84+
StorageReader& storage() { return reader_; }
85+
86+
protected:
87+
bool TagMatchMode(const std::string& tag) {
88+
if (tag.size() <= mode_.size()) return false;
89+
return tag.substr(0, mode_.size()) == mode_;
90+
}
91+
std::string GenReadableTag(const std::string& tag) {
92+
auto tmp = tag;
93+
string::TagDecode(tmp);
94+
return tmp.substr(mode_.size() + 1); // including `/`
95+
}
96+
4897
private:
4998
StorageReader reader_;
50-
std::string mode_{"default"};
99+
std::string mode_{kDefaultMode};
51100
};
52101

53102
namespace components {
@@ -84,14 +133,13 @@ struct ScalarReader {
84133
std::vector<T> ids() const;
85134
std::vector<T> timestamps() const;
86135
std::string caption() const;
87-
size_t total_records() {return reader_.total_records();}
136+
size_t total_records() { return reader_.total_records(); }
88137
size_t size() const;
89138

90139
private:
91140
TabletReader reader_;
92141
};
93142

94-
95143
} // namespace components
96144
} // namespace visualdl
97145

visualdl/logic/sdk_test.cc

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ namespace visualdl {
66

77
TEST(Scalar, write) {
88
const auto dir = "./tmp/sdk_test";
9-
Storage storage;
9+
Writer writer__(dir, 1);
10+
auto writer = writer__.AsMode("train");
1011
// write disk every time
11-
storage.meta.cycle = 1;
12-
storage.SetDir(dir);
13-
auto tablet = storage.AddTablet("scalar0");
12+
auto tablet = writer.AddTablet("scalar0");
1413
components::Scalar<int> scalar(tablet);
15-
scalar.SetCaption("train");
1614
scalar.AddRecord(0, 12);
17-
storage.PersistToDisk();
15+
auto tablet1 = writer.AddTablet("model/layer/min");
16+
components::Scalar<float> scalar1(tablet1);
17+
scalar1.SetCaption("customized caption");
1818

1919
// read from disk
20-
StorageReader reader(dir);
20+
Reader reader_(dir);
21+
auto reader = reader_.AsMode("train");
2122
auto tablet_reader = reader.tablet("scalar0");
2223
auto scalar_reader = components::ScalarReader<int>(std::move(tablet_reader));
2324
auto captioin = scalar_reader.caption();
@@ -27,6 +28,16 @@ TEST(Scalar, write) {
2728
ASSERT_EQ(record.size(), 1);
2829
// check the first entry of first record
2930
ASSERT_EQ(record.front(), 12);
31+
32+
// check tags
33+
ASSERT_EQ(reader.all_tags().size(), 1);
34+
auto tags = reader.tags("scalar");
35+
ASSERT_EQ(tags.size(), 2);
36+
ASSERT_EQ(tags.front(), "scalar0");
37+
ASSERT_EQ(tags[1], "model/layer/min");
38+
components::ScalarReader<float> scalar_reader1(
39+
reader.tablet("model/layer/min"));
40+
ASSERT_EQ(scalar_reader1.caption(), "customized caption");
3041
}
3142

3243
} // namespace visualdl

visualdl/python/storage.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@
99

1010
class StorageReader(object):
1111

12-
def __init__(self, mode, dir):
13-
self.reader = core.Reader(mode, dir)
12+
def __init__(self, dir, reader=None):
13+
self.dir = dir
14+
self.reader = reader if reader else core.Reader(dir)
15+
16+
def as_mode(self, mode):
17+
tmp = StorageReader(dir, self.reader.as_mode(mode))
18+
return tmp
19+
20+
def modes(self):
21+
return self.reader.modes()
22+
23+
def tags(self, kind):
24+
return self.reader.tags(kind)
1425

1526
def scalar(self, tag, type='float'):
1627
type2scalar = {
@@ -23,12 +34,14 @@ def scalar(self, tag, type='float'):
2334

2435
class StorageWriter(object):
2536

26-
def __init__(self, dir, sync_cycle):
27-
self.writer = core.Writer(dir, sync_cycle)
37+
def __init__(self, dir, sync_cycle, writer=None):
38+
self.dir = dir
39+
self.sync_cycle = sync_cycle
40+
self.writer = writer if writer else core.Writer(dir, sync_cycle)
2841

2942
def as_mode(self, mode):
30-
self.writer = self.writer.as_mode(mode)
31-
return self
43+
tmp = StorageWriter(self.dir, self.sync_cycle, self.writer.as_mode(mode))
44+
return tmp
3245

3346
def scalar(self, tag, type='float'):
3447
type2scalar = {

visualdl/python/test_read_service.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

visualdl/python/test_storage.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
import random
55
import time
66

7+
78
class StorageTest(unittest.TestCase):
89
def setUp(self):
910
self.dir = "./tmp/storage_test"
1011

1112
def test_read(self):
1213
print 'test write'
13-
self.writer = storage.StorageWriter(self.dir, sync_cycle=1).as_mode("train")
14+
self.writer = storage.StorageWriter(
15+
self.dir, sync_cycle=1).as_mode("train")
1416
scalar = self.writer.scalar("model/scalar/min")
1517
# scalar.set_caption("model/scalar/min")
1618
for i in range(10):
1719
scalar.add_record(i, float(i))
1820

1921
print 'test read'
20-
self.reader = storage.StorageReader("train", self.dir)
22+
self.reader = storage.StorageReader(self.dir).as_mode("train")
2123
scalar = self.reader.scalar("model/scalar/min")
2224
self.assertEqual(scalar.caption(), "train")
2325
records = scalar.records()

visualdl/python/test_write_service.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)