Permalink
Browse files

api/index supply samples interface

  • Loading branch information...
1 parent b180281 commit 891ef6b4c6949806a8e235c00d1b067fd72088bd @dista committed Dec 26, 2011
Showing with 142 additions and 89 deletions.
  1. +34 −15 src/UI/css/ui.css
  2. +7 −51 src/UI/index.py
  3. +10 −10 src/UI/templ/home.html
  4. +2 −3 src/UI/templ/samples.html
  5. BIN src/api/__init__.pyc
  6. +68 −0 src/api/index.py
  7. +8 −0 src/common/json_handler.py
  8. +1 −0 src/conf.py
  9. +4 −3 src/db.py
  10. +1 −0 src/env_set
  11. +4 −5 src/main.py
  12. +3 −2 src/service.py
View
@@ -1,24 +1,34 @@
html
{
+ background-color: #d6d8d8;
}
.wrapper
{
width: 960px;
margin: 0 auto;
- border: solid 1px #bbb;
border-top: none;
font-family: arial;
font-size: 14px;
+ padding: 10px;
+ background-color: white;
+ position: relative;
+ top: 30px;
+ -webkit-box-shadow: 2px 2px 1px #999;
+ box-shadow: 2px 2px 1px #999;
+ -moz-box-shadow: 2px 2px 1px #999;
}
#home #head
{
border-bottom: solid 1px #bbb;
- position: relative;
- background-color: #555;
+ position: fixed;
+ background-color: black;
color: white;
- padding-top: 10px;
+ margin: 0 auto;
+ width: 100%;
+ height: 30px;
+ z-index: 2;
}
#home #sample_ajax_status
@@ -71,11 +81,6 @@ html
width: 100%;
}
-#stream_list tr.simple
-{
- border-top: solid 1px #bbb;
-}
-
.status_container
{
width: 20px;
@@ -105,18 +110,23 @@ html
.detail
{
font-size: 12px;
- background-color: #f3f3f3;
+}
+
+.detail td
+{
+ padding-bottom: 30px;
}
.detail td > div
{
- margin: 2px auto;
+ margin: 4px auto;
}
.simple
{
- background-color: #eee;
- border-bottom: dotted 1px #bbb;
+ background-color: #fefefe;
+ border-bottom: solid 1px #999;
+ border-top: solid 2px #999;
}
.detail_title
@@ -138,8 +148,17 @@ html
#notify
{
- position: absolute;
- left: 440px;
+ position: fixed;
+ left: 50%;
+ margin-left: -80px;
background-color: #7a8;
padding: 4px;
}
+
+#copyright
+{
+ position: fixed;
+ left: 0px;
+ bottom: 0px;
+ color: #555;
+}
View
@@ -35,10 +35,7 @@ def get_file_contents(file_path):
def load_samples():
samples = web.template.frender(path.join(mms_ui_root, 'templ/samples.html'))
- fake_data = {
- "total_samples": 2,
- "samples": {
- "10001": {
+ one_sample = {
'stream_id': 10001,
'sample_interface': 'http://test.zz',
'unify_name': '10.33.0.28/tvie/channel1/sd',
@@ -84,56 +81,15 @@ def load_samples():
"stream_info_framerate": 812,
"stream_info_width": 235,
"stream_info_height": 235
- },
- "10002": {
- 'stream_id': 10002,
- 'sample_interface': 'http://test.zz',
- 'unify_name': 'tvie/channel1/sd',
- 'average_downstream_kbps': {
- "last_3": 100,
- "last_10": 200,
- "last_100": 150,
- "total": 120
- },
- 'average_upstream_kbps': {
- "last_3": 100,
- "last_10": 200,
- "last_100": 150,
- "total": 120
- },
- 'score': 50,
- 'score_level': 2,
- 'score_detail': {
- 'live_delay': 10,
- 'upstream': 90
- },
- 'score_level_detail': {
- 'live_delay': 1,
- 'upstream': 3
- },
- 'sample_time': '2011/12/11 02:23:12',
- "total_gops": 100123,
- "upstream_opened_count": 21,
- "upstream_connected_count": 23,
- "upstream_connection_fail_count": 23,
- "upstream_bytes_received": 32252,
- "clients_connected": 445,
- "total_clients_served": 234,
- "total_bytes_sent": 812,
- "live_delay_ms": 235,
- "stream_info_audiodatarate": 152,
- "stream_info_videodatarate": 233,
- "stream_info_audiocodecid": 658,
- "stream_info_audiosamplesize": 16,
- "stream_info_audiosamplerate": 4422,
- "stream_info_stereo": 235,
- "stream_info_videocodecid": 2414,
- "stream_info_framerate": 812,
- "stream_info_width": 235,
- "stream_info_height": 235
}
+ fake_data = {
+ "total_samples": 30,
+ "samples": {
}
}
+
+ for i in xrange(10000, 10030):
+ fake_data['samples'][str(i)] = one_sample
return samples(fake_data)
class home:
View
@@ -13,21 +13,21 @@
</script>
</head>
<body id='home'>
- <div class='wrapper'>
- <div id='head'>
- <div id='sample_ajax_status' class='left' style='display: none;'></div>
- <div class='right' id='refresh_container'>
- <span>每</span><span id='refresh_time'></span><span style='margin-right: 10px;'>秒刷新一次</span>
- <button name='toggle_refresh' id='toggle_refresh'>停止</button><span>自动刷新</span>
- </div>
- <div id='notify' style='display: none;'>已获取最新数据</div>
- <div class='clear'></div>
+ <div id='head'>
+ <div id='sample_ajax_status' class='left' style='display: none;'></div>
+ <div class='right' id='refresh_container'>
+ <span>每</span><span id='refresh_time'></span><span style='margin-right: 10px;'>秒刷新一次</span>
+ <button name='toggle_refresh' id='toggle_refresh'>停止</button><span>自动刷新</span>
</div>
+ <div id='notify' style='display: none;'>已获取最新数据</div>
+ <div class='clear'></div>
+ </div>
+ <div class='wrapper'>
<div id='stream_list'>
$:samples
</div>
</div>
- <div style='border-top: solid 1px #bbb'>
+ <div id='copyright'>
TVie &copy 2011
</div>
</body>
@@ -15,8 +15,7 @@
<div class='unify_name'>$samples['samples'][k]["unify_name"]</div>
<div class='sample_interface'>$samples['samples'][k]["sample_interface"]</div>
</td>
- <td class='sample_time'>
- <span>更新于</span>
+ <td class='sample_time' title='上次更新时间'>
$samples['samples'][k]['sample_time']
</td>
</tr>
@@ -78,4 +77,4 @@
</td>
</tr>
</tbody>
-<table>
+</table>
View
Binary file not shown.
View
@@ -0,0 +1,68 @@
+'''
+Created on 2011-12-24
+
+@author: dista
+'''
+
+import web
+import conf
+import json
+from config_reader import ConfigReader
+from common import json_handler
+
+CONFIG_PATH = "/usr/local/tvie/config/media-server-monitor.conf"
+cr = ConfigReader()
+config = cr.read_config_as_dict(conf.CONFIG_PATH)
+
+urls = (
+ "/samples/.*", "samples"
+ )
+
+class samples:
+ def GET(self):
+ path_info_arr = web.ctx.env['PATH_INFO'].split('/')
+ offset, page_size = (int(i) for i in path_info_arr[4].split('-'))
+ keyword, sort_by, sort_order, sort_by_status_first = path_info_arr[5:]
+ sort_by_status_first = int(sort_by_status_first)
+
+ if sort_order.upper() not in ["ASC", "DESC"]:
+ raise badrequest()
+
+ db = web.database(dbn='mysql', db='tvie_production2', host='10.33.0.57', user='tvie', pw='tvierocks')
+
+ sql = "SELECT * FROM mms_stream"
+
+ where_in = False
+ if keyword != "null":
+ sql += ' WHERE unify_name like "%%%%%s%%%%"' % keyword
+
+ if int(sort_by_status_first) == 1:
+ sql += " order by score, %s asc" % sort_by
+ else:
+ sql += " order by %s %s" % (sort_by, sort_order)
+
+ if int(page_size) != 0:
+ sql += " limit %d, %d" % (offset, page_size)
+
+ streams = db.query(sql)
+
+ ret_streams = {}
+ for stream in streams:
+ sample_result = db.query("SELECT * FROM mms_samples where mms_stream_id = %d order by id desc limit 1" % stream['id'])
+
+ sample = None
+ try:
+ sample = sample_result.next()
+ del sample['id'], sample['mms_stream_id']
+ except Exception:
+ pass
+
+ if sample != None:
+ ret_streams[str(stream['stream_id'])] = stream.update(sample)
+ else:
+ ret_streams[str(stream['stream_id'])] = stream
+
+ return json.dumps({"total_samples": len(ret_streams), 'samples': ret_streams}, cls=json_handler.ExtendedEncoder)
+
+
+app = web.application(urls, locals())
@@ -0,0 +1,8 @@
+import json
+import datetime
+
+class ExtendedEncoder(json.JSONEncoder):
+ def default(self, obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.strftime("%Y-%m-%d %H:%M:%S")
+ return json.JSONEncoder.default(self, obj)
View
@@ -0,0 +1 @@
+CONFIG_PATH = "/usr/local/tvie/config/media-server-monitor.conf"
View
@@ -7,14 +7,15 @@
import threading
import MySQLdb
import traceback
+import logger
from MySQLdb import connections
class DbConnection(connections.Connection):
timeout = 60
- def __init__(self, id, host, port, name, password, db_name, logger = None):
+ def __init__(self, id, host, port, name, password, db_name):
self.id = id
- self.logger = logger
+ self.logger = logger.get_logger()
self._is_used = False
connections.Connection.__init__(self, host = host, user = name, passwd = password, db = db_name, port = port, connect_timeout = DbConnection.timeout, \
@@ -55,7 +56,7 @@ class DbPool:
threads won't share DbConnection.
do not call this twice, use get_pool to get pool after created
'''
- def __init__(self, max_db, host, port, name, password, db_name, logger):
+ def __init__(self, max_db, host, port, name, password, db_name):
self.host = host
self.port = port
self.name = name
View
@@ -0,0 +1 @@
+export PYTHONPATH=/root/svn/dev/src/tviebackend/3rdparty:/root/media-server-monitor/src
View
@@ -9,8 +9,7 @@
from analyzer.analyze_thread import AnalyzeThread
from db import DbPool
from logger import Logger
-
-CONFIG_PATH = "/usr/local/tvie/config/media-server-monitor.conf"
+import conf
all_threads = {"analyze": None, "stream_monitor": None}
@@ -21,13 +20,13 @@ def terminate_program():
def main():
cr = ConfigReader()
- config = cr.read_config_as_dict(CONFIG_PATH)
+ config = cr.read_config_as_dict(conf.CONFIG_PATH)
# create the log, so other module can use it
Logger(config.logger.path, config.logger.level)
# create the pool, so other module can use it
- DbPool(config.db.host, config.db.port, config.db.name, config.db.password)
+ DbPool(4, config.db.host, config.db.port, config.db.name, config.db.password)
az_t = AnalyzeThread()
smt = StreamMonitorThread(config.get_stream_api_url, az_t)
@@ -41,4 +40,4 @@ def main():
smt.join()
if __name__ == "__main__":
- main()
+ main()
View
@@ -4,12 +4,13 @@
@author: dista
'''
-import api
+import api.index
import UI.index
import web
urls = (
- "/mms/ui", UI.index.app
+ "/mms/ui", UI.index.app,
+ "/mms/api", api.index.app
)

0 comments on commit 891ef6b

Please sign in to comment.