PythonからElixir Reportのレポートサーバーにアクセスするサンプルコード
Revision | c1f47a3d2115f927f8fe55f2e424f95b9111e114 (tree) |
---|---|
Time | 2014-12-03 01:19:15 |
Author | Hiromichi Matsushima <hylom@Hiro...> |
Commiter | Hiromichi Matsushima |
add handler to get xml data from report server
@@ -0,0 +1,14 @@ | ||
1 | +#!/usr/bin/python | |
2 | +# -*- coding: utf-8 -*- | |
3 | + | |
4 | +config = { | |
5 | + "report_server": { | |
6 | + "user": "admin", | |
7 | + "password": "sa", | |
8 | + "host": "127.0.0.1", | |
9 | + "port": 7001 | |
10 | + }, | |
11 | + "data_source": { | |
12 | + "traffic": "/WebAnalytics/" | |
13 | + } | |
14 | +} |
@@ -1,20 +1,63 @@ | ||
1 | 1 | #!/usr/bin/python |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | |
4 | +import os | |
5 | +from datetime import date, timedelta | |
6 | + | |
4 | 7 | from tornado import ioloop |
5 | 8 | from tornado import web |
6 | -import os | |
9 | + | |
10 | +from config import config | |
11 | +from report_server import ReportServer | |
7 | 12 | |
8 | 13 | # configurations |
9 | 14 | CSS_PATH = os.path.join(os.getcwd(), "www_root/css") |
10 | 15 | JS_PATH = os.path.join(os.getcwd(), "www_root/js") |
11 | 16 | |
12 | 17 | class MainHandler(web.RequestHandler): |
18 | + "index.htmlを返すハンドラ" | |
13 | 19 | def get(self): |
14 | 20 | self.render("index.html") |
15 | 21 | |
16 | 22 | |
23 | +class TrafficDataHandler(web.RequestHandler): | |
24 | + "トラフィックデータリクエストを処理するハンドラ" | |
25 | + def get(self): | |
26 | + try: | |
27 | + year = int(self.get_argument("y")) | |
28 | + month = int(self.get_argument("m")) | |
29 | + except ValueError: | |
30 | + self.send_err(400) | |
31 | + return | |
32 | + | |
33 | + # パラメータで指定された年/月から | |
34 | + # 開始/終了年月日を生成する | |
35 | + start_date = date(year, month, 1) | |
36 | + if month == 12: | |
37 | + end_date = date(year + 1, 1, 1) + timedelta(-1) | |
38 | + else: | |
39 | + end_date = date(year, month + 1, 1) + timedelta(-1) | |
40 | + | |
41 | + # レポートサーバーに投げるパラメータを作成 | |
42 | + param = { | |
43 | + "start_date": start_date, | |
44 | + "end_date": end_date | |
45 | + } | |
46 | + | |
47 | + # リクエスト送信 | |
48 | + r = ReportServer(config["report_server"]["user"], | |
49 | + config["report_server"]["password"], | |
50 | + config["report_server"]["host"], | |
51 | + config["report_server"]["port"]) | |
52 | + xml = r.data(config["data_source"]["traffic"], param) | |
53 | + # 取得したXMLをクライアントに送信 | |
54 | + self.set_header("Content-type", "application/xml") | |
55 | + self.set_status(200) | |
56 | + self.finish(xml) | |
57 | + | |
58 | + | |
17 | 59 | class DataTestHandler(web.RequestHandler): |
60 | + "テスト用ハンドラ" | |
18 | 61 | def get(self, request_path): |
19 | 62 | data_dir = os.path.join(os.getcwd(), "testdata") |
20 | 63 | if request_path == "sample.xml": |
@@ -45,8 +88,10 @@ class DataTestHandler(web.RequestHandler): | ||
45 | 88 | else: |
46 | 89 | self.send_error(404) |
47 | 90 | |
91 | + | |
48 | 92 | application = web.Application([ |
49 | 93 | ("/", MainHandler), |
94 | +# ("/data/traffic", TrafficDataHandler), | |
50 | 95 | ("/data/(.*)", DataTestHandler), |
51 | 96 | ("/css/(.*)", web.StaticFileHandler, {"path": CSS_PATH}), |
52 | 97 | ("/js/(.*)", web.StaticFileHandler, {"path": JS_PATH}), |