winlin

support query specified vhost

@@ -443,17 +443,45 @@ SrsGoApiVhosts::~SrsGoApiVhosts() @@ -443,17 +443,45 @@ SrsGoApiVhosts::~SrsGoApiVhosts()
443 443
444 int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) 444 int SrsGoApiVhosts::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
445 { 445 {
446 - std::stringstream data;  
447 - SrsStatistic* stat = SrsStatistic::instance();  
448 - int ret = stat->dumps_vhosts(data); 446 + int ret = ERROR_SUCCESS;
449 447
  448 + SrsStatistic* stat = SrsStatistic::instance();
450 std::stringstream ss; 449 std::stringstream ss;
451 450
452 - ss << SRS_JOBJECT_START  
453 - << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT  
454 - << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT  
455 - << SRS_JFIELD_ORG("vhosts", data.str())  
456 - << SRS_JOBJECT_END; 451 + // path: {pattern}{vhost_id}
  452 + // e.g. /api/v1/vhosts/100 pattern= /api/v1/vhosts/, vhost_id=100
  453 + int vid = r->parse_rest_id(entry->pattern);
  454 + SrsStatisticVhost* vhost = NULL;
  455 +
  456 + if (vid > 0 && (vhost = stat->find_vhost(vid)) == NULL) {
  457 + ret = ERROR_RTMP_STREAM_NOT_FOUND;
  458 + srs_error("vhost id=%d not found. ret=%d", vid, ret);
  459 + return srs_http_response_code(w, ret);
  460 + }
  461 +
  462 + if (r->is_http_get()) {
  463 + std::stringstream data;
  464 +
  465 + if (!vhost) {
  466 + ret = stat->dumps_vhosts(data);
  467 +
  468 + ss << SRS_JOBJECT_START
  469 + << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT
  470 + << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
  471 + << SRS_JFIELD_ORG("vhosts", data.str())
  472 + << SRS_JOBJECT_END;
  473 + } else {
  474 + ret = vhost->dumps(data);
  475 +
  476 + ss << SRS_JOBJECT_START
  477 + << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT
  478 + << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
  479 + << SRS_JFIELD_ORG("vhost", data.str())
  480 + << SRS_JOBJECT_END;
  481 + }
  482 +
  483 + return srs_http_response_json(w, ss.str());
  484 + }
457 485
458 return srs_http_response_json(w, ss.str()); 486 return srs_http_response_json(w, ss.str());
459 } 487 }
@@ -553,8 +581,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -553,8 +581,7 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
553 int cid = r->parse_rest_id(entry->pattern); 581 int cid = r->parse_rest_id(entry->pattern);
554 582
555 SrsStatisticClient* client = NULL; 583 SrsStatisticClient* client = NULL;
556 - // TODO: FIXME: implements it.  
557 - /*if (cid >= 0 && (client = stat->find_client(cid)) == NULL) { 584 + if (cid >= 0 && (client = stat->find_client(cid)) == NULL) {
558 ret = ERROR_RTMP_STREAM_NOT_FOUND; 585 ret = ERROR_RTMP_STREAM_NOT_FOUND;
559 srs_error("stream client_id=%d not found. ret=%d", cid, ret); 586 srs_error("stream client_id=%d not found. ret=%d", cid, ret);
560 587
@@ -562,12 +589,32 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) @@ -562,12 +589,32 @@ int SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
562 589
563 return srs_http_response_json(w, ss.str()); 590 return srs_http_response_json(w, ss.str());
564 591
565 - }*/ 592 + }
566 593
567 if (r->is_http_get()) { 594 if (r->is_http_get()) {
  595 + std::stringstream data;
  596 +
  597 + if (!client) {
  598 + ret = stat->dumps_clients(data, 0, 10);
  599 +
  600 + ss << SRS_JOBJECT_START
  601 + << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT
  602 + << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
  603 + << SRS_JFIELD_ORG("clients", data.str())
  604 + << SRS_JOBJECT_END;
  605 + } else {
  606 + ret = client->dumps(data);
  607 +
  608 + ss << SRS_JOBJECT_START
  609 + << SRS_JFIELD_ERROR(ret) << SRS_JFIELD_CONT
  610 + << SRS_JFIELD_ORG("server", stat->server_id()) << SRS_JFIELD_CONT
  611 + << SRS_JFIELD_ORG("client", data.str())
  612 + << SRS_JOBJECT_END;
  613 + }
568 614
  615 + return srs_http_response_json(w, ss.str());
569 } 616 }
570 - 617 +
571 return ret; 618 return ret;
572 } 619 }
573 620
@@ -62,11 +62,13 @@ int SrsStatisticVhost::dumps(stringstream& ss) @@ -62,11 +62,13 @@ int SrsStatisticVhost::dumps(stringstream& ss)
62 62
63 // dumps the config of vhost. 63 // dumps the config of vhost.
64 bool hls_enabled = _srs_config->get_hls_enabled(vhost); 64 bool hls_enabled = _srs_config->get_hls_enabled(vhost);
  65 + bool enabled = _srs_config->get_vhost_enabled(vhost);
65 66
66 ss << SRS_JOBJECT_START 67 ss << SRS_JOBJECT_START
67 << SRS_JFIELD_ORG("id", id) << SRS_JFIELD_CONT 68 << SRS_JFIELD_ORG("id", id) << SRS_JFIELD_CONT
68 << SRS_JFIELD_STR("name", vhost) << SRS_JFIELD_CONT 69 << SRS_JFIELD_STR("name", vhost) << SRS_JFIELD_CONT
69 - << SRS_JFIELD_ORG("cleints", nb_clients) << SRS_JFIELD_CONT 70 + << SRS_JFIELD_BOOL("enabled", enabled) << SRS_JFIELD_CONT
  71 + << SRS_JFIELD_ORG("clients", nb_clients) << SRS_JFIELD_CONT
70 << SRS_JFIELD_ORG("send_bytes", kbps->get_send_bytes()) << SRS_JFIELD_CONT 72 << SRS_JFIELD_ORG("send_bytes", kbps->get_send_bytes()) << SRS_JFIELD_CONT
71 << SRS_JFIELD_ORG("recv_bytes", kbps->get_recv_bytes()) << SRS_JFIELD_CONT 73 << SRS_JFIELD_ORG("recv_bytes", kbps->get_recv_bytes()) << SRS_JFIELD_CONT
72 << SRS_JFIELD_NAME("hls") << SRS_JOBJECT_START 74 << SRS_JFIELD_NAME("hls") << SRS_JOBJECT_START
@@ -163,6 +165,26 @@ void SrsStatisticStream::close() @@ -163,6 +165,26 @@ void SrsStatisticStream::close()
163 status = STATISTIC_STREAM_STATUS_IDLING; 165 status = STATISTIC_STREAM_STATUS_IDLING;
164 } 166 }
165 167
  168 +SrsStatisticClient::SrsStatisticClient()
  169 +{
  170 + id = 0;
  171 +}
  172 +
  173 +SrsStatisticClient::~SrsStatisticClient()
  174 +{
  175 +}
  176 +
  177 +int SrsStatisticClient::dumps(stringstream& ss)
  178 +{
  179 + int ret = ERROR_SUCCESS;
  180 +
  181 + ss << SRS_JOBJECT_START
  182 + << SRS_JFIELD_ORG("id", id)
  183 + << SRS_JOBJECT_END;
  184 +
  185 + return ret;
  186 +}
  187 +
166 SrsStatistic* SrsStatistic::_instance = new SrsStatistic(); 188 SrsStatistic* SrsStatistic::_instance = new SrsStatistic();
167 189
168 SrsStatistic::SrsStatistic() 190 SrsStatistic::SrsStatistic()
@@ -178,14 +200,14 @@ SrsStatistic::~SrsStatistic() @@ -178,14 +200,14 @@ SrsStatistic::~SrsStatistic()
178 srs_freep(kbps); 200 srs_freep(kbps);
179 201
180 if (true) { 202 if (true) {
181 - std::map<std::string, SrsStatisticVhost*>::iterator it; 203 + std::map<int64_t, SrsStatisticVhost*>::iterator it;
182 for (it = vhosts.begin(); it != vhosts.end(); it++) { 204 for (it = vhosts.begin(); it != vhosts.end(); it++) {
183 SrsStatisticVhost* vhost = it->second; 205 SrsStatisticVhost* vhost = it->second;
184 srs_freep(vhost); 206 srs_freep(vhost);
185 } 207 }
186 } 208 }
187 if (true) { 209 if (true) {
188 - std::map<std::string, SrsStatisticStream*>::iterator it; 210 + std::map<int64_t, SrsStatisticStream*>::iterator it;
189 for (it = streams.begin(); it != streams.end(); it++) { 211 for (it = streams.begin(); it != streams.end(); it++) {
190 SrsStatisticStream* stream = it->second; 212 SrsStatisticStream* stream = it->second;
191 srs_freep(stream); 213 srs_freep(stream);
@@ -198,6 +220,11 @@ SrsStatistic::~SrsStatistic() @@ -198,6 +220,11 @@ SrsStatistic::~SrsStatistic()
198 srs_freep(client); 220 srs_freep(client);
199 } 221 }
200 } 222 }
  223 +
  224 + vhosts.clear();
  225 + rvhosts.clear();
  226 + streams.clear();
  227 + rstreams.clear();
201 } 228 }
202 229
203 SrsStatistic* SrsStatistic::instance() 230 SrsStatistic* SrsStatistic::instance()
@@ -205,16 +232,29 @@ SrsStatistic* SrsStatistic::instance() @@ -205,16 +232,29 @@ SrsStatistic* SrsStatistic::instance()
205 return _instance; 232 return _instance;
206 } 233 }
207 234
208 -SrsStatisticStream* SrsStatistic::find_stream(int stream_id) 235 +SrsStatisticVhost* SrsStatistic::find_vhost(int vid)
  236 +{
  237 + std::map<int64_t, SrsStatisticVhost*>::iterator it;
  238 + if ((it = vhosts.find(vid)) != vhosts.end()) {
  239 + return it->second;
  240 + }
  241 + return NULL;
  242 +}
  243 +
  244 +SrsStatisticStream* SrsStatistic::find_stream(int sid)
  245 +{
  246 + std::map<int64_t, SrsStatisticStream*>::iterator it;
  247 + if ((it = streams.find(sid)) != streams.end()) {
  248 + return it->second;
  249 + }
  250 + return NULL;
  251 +}
  252 +
  253 +SrsStatisticClient* SrsStatistic::find_client(int cid)
209 { 254 {
210 std::map<int, SrsStatisticClient*>::iterator it; 255 std::map<int, SrsStatisticClient*>::iterator it;
211 - for (it = clients.begin(); it != clients.end(); it++) {  
212 - SrsStatisticClient* client = it->second;  
213 - SrsStatisticStream* stream = client->stream;  
214 -  
215 - if (stream_id == stream->id) {  
216 - return stream;  
217 - } 256 + if ((it = clients.find(cid)) != clients.end()) {
  257 + return it->second;
218 } 258 }
219 return NULL; 259 return NULL;
220 } 260 }
@@ -338,14 +378,14 @@ SrsKbps* SrsStatistic::kbps_sample() @@ -338,14 +378,14 @@ SrsKbps* SrsStatistic::kbps_sample()
338 { 378 {
339 kbps->sample(); 379 kbps->sample();
340 if (true) { 380 if (true) {
341 - std::map<std::string, SrsStatisticVhost*>::iterator it; 381 + std::map<int64_t, SrsStatisticVhost*>::iterator it;
342 for (it = vhosts.begin(); it != vhosts.end(); it++) { 382 for (it = vhosts.begin(); it != vhosts.end(); it++) {
343 SrsStatisticVhost* vhost = it->second; 383 SrsStatisticVhost* vhost = it->second;
344 vhost->kbps->sample(); 384 vhost->kbps->sample();
345 } 385 }
346 } 386 }
347 if (true) { 387 if (true) {
348 - std::map<std::string, SrsStatisticStream*>::iterator it; 388 + std::map<int64_t, SrsStatisticStream*>::iterator it;
349 for (it = streams.begin(); it != streams.end(); it++) { 389 for (it = streams.begin(); it != streams.end(); it++) {
350 SrsStatisticStream* stream = it->second; 390 SrsStatisticStream* stream = it->second;
351 stream->kbps->sample(); 391 stream->kbps->sample();
@@ -365,7 +405,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss) @@ -365,7 +405,7 @@ int SrsStatistic::dumps_vhosts(stringstream& ss)
365 int ret = ERROR_SUCCESS; 405 int ret = ERROR_SUCCESS;
366 406
367 ss << SRS_JARRAY_START; 407 ss << SRS_JARRAY_START;
368 - std::map<std::string, SrsStatisticVhost*>::iterator it; 408 + std::map<int64_t, SrsStatisticVhost*>::iterator it;
369 for (it = vhosts.begin(); it != vhosts.end(); it++) { 409 for (it = vhosts.begin(); it != vhosts.end(); it++) {
370 SrsStatisticVhost* vhost = it->second; 410 SrsStatisticVhost* vhost = it->second;
371 411
@@ -387,7 +427,7 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -387,7 +427,7 @@ int SrsStatistic::dumps_streams(stringstream& ss)
387 int ret = ERROR_SUCCESS; 427 int ret = ERROR_SUCCESS;
388 428
389 ss << SRS_JARRAY_START; 429 ss << SRS_JARRAY_START;
390 - std::map<std::string, SrsStatisticStream*>::iterator it; 430 + std::map<int64_t, SrsStatisticStream*>::iterator it;
391 for (it = streams.begin(); it != streams.end(); it++) { 431 for (it = streams.begin(); it != streams.end(); it++) {
392 SrsStatisticStream* stream = it->second; 432 SrsStatisticStream* stream = it->second;
393 433
@@ -404,19 +444,47 @@ int SrsStatistic::dumps_streams(stringstream& ss) @@ -404,19 +444,47 @@ int SrsStatistic::dumps_streams(stringstream& ss)
404 return ret; 444 return ret;
405 } 445 }
406 446
  447 +int SrsStatistic::dumps_clients(stringstream& ss, int start, int count)
  448 +{
  449 + int ret = ERROR_SUCCESS;
  450 +
  451 + ss << SRS_JARRAY_START;
  452 + std::map<int, SrsStatisticClient*>::iterator it = clients.begin();
  453 + for (int i = 0; i < count && it != clients.end(); it++) {
  454 + if (i < start) {
  455 + continue;
  456 + }
  457 +
  458 + SrsStatisticClient* client = it->second;
  459 +
  460 + if (i != start) {
  461 + ss << SRS_JFIELD_CONT;
  462 + }
  463 +
  464 + if ((ret = client->dumps(ss)) != ERROR_SUCCESS) {
  465 + return ret;
  466 + }
  467 + }
  468 + ss << SRS_JARRAY_END;
  469 +
  470 +
  471 + return ret;
  472 +}
  473 +
407 SrsStatisticVhost* SrsStatistic::create_vhost(SrsRequest* req) 474 SrsStatisticVhost* SrsStatistic::create_vhost(SrsRequest* req)
408 { 475 {
409 SrsStatisticVhost* vhost = NULL; 476 SrsStatisticVhost* vhost = NULL;
410 477
411 // create vhost if not exists. 478 // create vhost if not exists.
412 - if (vhosts.find(req->vhost) == vhosts.end()) { 479 + if (rvhosts.find(req->vhost) == rvhosts.end()) {
413 vhost = new SrsStatisticVhost(); 480 vhost = new SrsStatisticVhost();
414 vhost->vhost = req->vhost; 481 vhost->vhost = req->vhost;
415 - vhosts[req->vhost] = vhost; 482 + rvhosts[req->vhost] = vhost;
  483 + vhosts[vhost->id] = vhost;
416 return vhost; 484 return vhost;
417 } 485 }
418 486
419 - vhost = vhosts[req->vhost]; 487 + vhost = rvhosts[req->vhost];
420 488
421 return vhost; 489 return vhost;
422 } 490 }
@@ -428,17 +496,18 @@ SrsStatisticStream* SrsStatistic::create_stream(SrsStatisticVhost* vhost, SrsReq @@ -428,17 +496,18 @@ SrsStatisticStream* SrsStatistic::create_stream(SrsStatisticVhost* vhost, SrsReq
428 SrsStatisticStream* stream = NULL; 496 SrsStatisticStream* stream = NULL;
429 497
430 // create stream if not exists. 498 // create stream if not exists.
431 - if (streams.find(url) == streams.end()) { 499 + if (rstreams.find(url) == rstreams.end()) {
432 stream = new SrsStatisticStream(); 500 stream = new SrsStatisticStream();
433 stream->vhost = vhost; 501 stream->vhost = vhost;
434 stream->stream = req->stream; 502 stream->stream = req->stream;
435 stream->app = req->app; 503 stream->app = req->app;
436 stream->url = url; 504 stream->url = url;
437 - streams[url] = stream; 505 + rstreams[url] = stream;
  506 + streams[stream->id] = stream;
438 return stream; 507 return stream;
439 } 508 }
440 509
441 - stream = streams[url]; 510 + stream = rstreams[url];
442 511
443 return stream; 512 return stream;
444 } 513 }
@@ -115,6 +115,11 @@ struct SrsStatisticClient @@ -115,6 +115,11 @@ struct SrsStatisticClient
115 public: 115 public:
116 SrsStatisticStream* stream; 116 SrsStatisticStream* stream;
117 int id; 117 int id;
  118 +public:
  119 + SrsStatisticClient();
  120 + virtual ~SrsStatisticClient();
  121 +public:
  122 + virtual int dumps(std::stringstream& ss);
118 }; 123 };
119 124
120 class SrsStatistic 125 class SrsStatistic
@@ -123,10 +128,19 @@ private: @@ -123,10 +128,19 @@ private:
123 static SrsStatistic *_instance; 128 static SrsStatistic *_instance;
124 // the id to identify the sever. 129 // the id to identify the sever.
125 int64_t _server_id; 130 int64_t _server_id;
126 - // key: vhost name, value: vhost object.  
127 - std::map<std::string, SrsStatisticVhost*> vhosts;  
128 - // key: stream url, value: stream object.  
129 - std::map<std::string, SrsStatisticStream*> streams; 131 +private:
  132 + // key: vhost id, value: vhost object.
  133 + std::map<int64_t, SrsStatisticVhost*> vhosts;
  134 + // key: vhost url, value: vhost Object.
  135 + // @remark a fast index for vhosts.
  136 + std::map<std::string, SrsStatisticVhost*> rvhosts;
  137 +private:
  138 + // key: stream id, value: stream Object.
  139 + std::map<int64_t, SrsStatisticStream*> streams;
  140 + // key: stream url, value: stream Object.
  141 + // @remark a fast index for streams.
  142 + std::map<std::string, SrsStatisticStream*> rstreams;
  143 +private:
130 // key: client id, value: stream object. 144 // key: client id, value: stream object.
131 std::map<int, SrsStatisticClient*> clients; 145 std::map<int, SrsStatisticClient*> clients;
132 // server total kbps. 146 // server total kbps.
@@ -137,7 +151,10 @@ private: @@ -137,7 +151,10 @@ private:
137 public: 151 public:
138 static SrsStatistic* instance(); 152 static SrsStatistic* instance();
139 public: 153 public:
140 - virtual SrsStatisticStream* find_stream(int stream_id); 154 + virtual SrsStatisticVhost* find_vhost(int vid);
  155 + virtual SrsStatisticStream* find_stream(int sid);
  156 + virtual SrsStatisticClient* find_client(int cid);
  157 +public:
141 /** 158 /**
142 * when got video info for stream. 159 * when got video info for stream.
143 */ 160 */
@@ -198,6 +215,12 @@ public: @@ -198,6 +215,12 @@ public:
198 * dumps the streams to sstream in json. 215 * dumps the streams to sstream in json.
199 */ 216 */
200 virtual int dumps_streams(std::stringstream& ss); 217 virtual int dumps_streams(std::stringstream& ss);
  218 + /**
  219 + * dumps the clients to sstream in json.
  220 + * @param start the start index, from 0.
  221 + * @param count the max count of clients to dump.
  222 + */
  223 + virtual int dumps_clients(std::stringstream& ss, int start, int count);
201 private: 224 private:
202 virtual SrsStatisticVhost* create_vhost(SrsRequest* req); 225 virtual SrsStatisticVhost* create_vhost(SrsRequest* req);
203 virtual SrsStatisticStream* create_stream(SrsStatisticVhost* vhost, SrsRequest* req); 226 virtual SrsStatisticStream* create_stream(SrsStatisticVhost* vhost, SrsRequest* req);