Skip to content

Commit 84a7ef4

Browse files
committed
Update version to 0.2
read and execute sql from json files
1 parent f3356eb commit 84a7ef4

7 files changed

Lines changed: 70 additions & 51 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,7 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
# PyDev project
92+
.project
93+
.pydevproject

queries/user.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"queries": [
3+
{
4+
"id": "user1",
5+
"sql": "SELECT 'metric', 1+1;"
6+
},
7+
{
8+
"id": "user2",
9+
"sql": "SELECT 'metric', 1+1;"
10+
}
11+
]
12+
}

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ def read(fname):
88
def run_setup():
99
setup(
1010
name='sqlfeedgraphite',
11-
version='0.1',
11+
version='0.2',
1212
description='https://github.com/orachide/sql-feed-graphite',
1313
keywords = 'SQL Graphite Metrics',
1414
url='https://github.com/orachide/sql-feed-graphite',
15-
download_url = 'https://github.com/orachide/sql-feed-graphite/archive/0.1.tar.gz',
15+
download_url = 'https://github.com/orachide/sql-feed-graphite/archive/0.2.tar.gz',
1616
author='Rachide Ouattara',
1717
author_email='ouattchidi@gmail.com',
1818
license='BSD',
1919
packages=['sqlfeedgraphite'],
2020
install_requires=[
2121
'sqlalchemy',
2222
'psycopg2',
23+
'mysql-python',
2324
],
24-
test_suite='tests',
2525
zip_safe=True,
2626
classifiers=[
2727
],

sqlfeedgraphite/__init__.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,25 @@
33
import socket
44
import sys
55
import time
6+
import json
67

78
import sqlalchemy
89

9-
def get_executor(dsn):
10-
engine = sqlalchemy.create_engine(dsn, isolation_level='READ UNCOMMITTED')
10+
def get_executor(dbUrl):
11+
engine = sqlalchemy.create_engine(dbUrl, isolation_level='READ UNCOMMITTED')
1112
connection = engine.connect()
1213
return connection.execute
1314

14-
def get_info():
15-
parser = argparse.ArgumentParser(description='Send SQL results to Graphite')
16-
parser.add_argument('--graphite-host', metavar='graphite-host', type=str, nargs=1, default=None, help='Host to send metrics to')
17-
parser.add_argument('--graphite-port', metavar='graphite-port', type=int, nargs=1, default=2003, help='Graphite port to send metrics to')
18-
parser.add_argument('--graphite-prefix', metavar='graphite-prefix', type=str, nargs=1, default=['db'], help='Prefix for metrics')
19-
return parser.parse_args()
20-
21-
def run(graphite_host, graphite_port, graphite_prefix, queries, executor):
22-
data = []
15+
def run(graphite_host, graphite_port, graphite_prefix, executor , **query):
16+
resultRow = []
2317
now = time.time()
2418
sock = _socket_for_host_port(graphite_host, graphite_port)
25-
data = map(executor, queries)
26-
for result in data:
27-
for line in result:
28-
metric, value = line[:2]
29-
metric = '{0}.{1} {2} {3}\n'.format(graphite_prefix, metric, value, now)
30-
print metric,
31-
sock.sendall(metric)
19+
resultRow = executor(query['sql'])
20+
for result in resultRow:
21+
metric, value = result[:2]
22+
metric = '{0}.{1} {2} {3}\n'.format(graphite_prefix, metric, value, now)
23+
print metric,
24+
sock.sendall(metric)
3225
sock.close()
3326

3427
def _socket_for_host_port(host, port):
@@ -40,20 +33,40 @@ def _socket_for_host_port(host, port):
4033

4134

4235
def main():
43-
dsn = os.environ.get('S2G_DSN')
44-
if dsn is None:
45-
print 'You must set your DSN in the environment variable `S2G_DSN`'
36+
graphiteHost = os.environ.get('SQL_FEED_GRAPHITE_HOST')
37+
if graphiteHost is None:
38+
graphiteHost = 'localhost'
39+
graphitePort = os.environ.get('SQL_FEED_GRAPHITE_PORT')
40+
if graphitePort is None:
41+
graphitePort = 5432
42+
graphitePrefix = os.environ.get('SQL_FEED_GRAPHITE_PREFIX')
43+
if graphitePrefix is None:
44+
print 'You must set your graphitePrefix in the environment variable `SQL_FEED_GRAPHITE_PREFIX`'
4645
sys.exit(1)
47-
48-
queries = sys.stdin.readlines()
49-
args = get_info()
50-
run(
51-
args.graphite_host[0],
52-
args.graphite_port,
53-
args.graphite_prefix[0],
54-
queries,
55-
get_executor(dsn),
56-
)
46+
dbUrl = os.environ.get('SQL_FEED_GRAPHITE_DB_URL')
47+
if dbUrl is None:
48+
print 'You must set your DBUrl in the environment variable `SQL_FEED_GRAPHITE_DB_URL`'
49+
sys.exit(1)
50+
queriesDIR = os.environ.get('SQL_FEED_GRAPHITE_QUERIES_DIR')
51+
if queriesDIR is None:
52+
queriesDIR = os.getcwd()
53+
for filename in os.listdir(queriesDIR):
54+
if filename.endswith(".json"):
55+
currentFile = os.path.join(queriesDIR, filename)
56+
print('Processing file',currentFile)
57+
with open(currentFile) as data_file:
58+
data = json.load(data_file)
59+
print(data["queries"])
60+
for query in data["queries"]:
61+
print(query['sql'])
62+
run(
63+
graphiteHost,
64+
graphitePort,
65+
graphitePrefix,
66+
get_executor(dbUrl),
67+
**query
68+
)
69+
continue
5770

5871
if __name__ == '__main__':
5972
main()

sqlfeedgraphite/fiche.py

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

test/test_postgres.py

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

test_postgres.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from sqlfeedgraphite import get_executor
2+
3+
4+
dsn = 'postgresql://test:test@localhost:5432/test'
5+
executor = get_executor(dsn)
6+
result = list(executor('SELECT 1+1;'))
7+
print(result)

0 commit comments

Comments
 (0)