-
Notifications
You must be signed in to change notification settings - Fork 0
/
generatePostgreSQL.py
174 lines (157 loc) · 5.32 KB
/
generatePostgreSQL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
from __future__ import print_function, absolute_import, division, nested_scopes, generators, unicode_literals
from getpass import getpass
import sys
import getopt
import jph
import os
import bcrypt
import hashlib
import base64
# -------------
# Globals
# -------------
configURL="file:static/config.json"
Codifier="Q1"
comTarget=""
comDB=False
comUser=False
comStatsAll=False
comStats=""
comDrop=False
comTruncate=False
# -------------
# Read Startup Parameters
# -------------
def usage():
print("Usage: -u <url>", __file__)
print("\t-u <url> : load the JSON configuration from a url")
print("\t-c <code>: The Codifier where the SQL parameters are stored")
print("\t-l <code>: Create only table this Codifier")
print("\t-d : Add the database & user table creation ")
print("\t-f : Force the database DROP")
print("\t-s <code>: Rebuild table for Time Series Statistics")
print("\t-S : Rebuild All Time Series Statistics")
print("\t-p <u:p> : Add a user:password to the database")
print("\t-T : Truncate database tables")
try:
opts, args = getopt.getopt(sys.argv[1:], "thSu:c:l:p:s:df", ["truncate", "help", "allStats=", "url=", "code=", "sqlto=", "database", "user=", "stats=", "force"])
for opt, arg in opts:
if opt in ("-h", "help"):
raise
elif opt in ("-t", "--truncate"):
comTruncate=True
elif opt in ("-u", "--url"):
configURL=arg
elif opt in ("-c", "--code"):
Codifier=arg
elif opt in ("-S", "--allStats"):
comStatsAll=True
elif opt in ("-s", "--stats"):
comStats=arg[:2]
elif opt in ("-l", "--sqlto"):
comTarget=arg[:2]
elif opt in ("-d", "--database"):
comDB=True
elif opt in ("-f", "--force"):
comDrop=True
elif opt in ("-p", "--user"):
comUser=True
s=arg.find(":")
if (s<1):
raise Exception("option expects 'user:password' but colon ommited")
u=arg[:s].strip()
p=arg[s+1:].strip()
except Exception as e:
print("Error: %s" % e)
usage()
sys.exit()
def sqltrunctate(s):
print("\c %s;" % dbname)
print("TRUNCATE TABLE Sensor_%s;" % (s["Codifier"]) )
def sqltable(s):
print("\c %s;" % dbname)
print("CREATE TABLE Sensor_%s (" % (s["Codifier"]) )
print("Timestamp bigint PRIMARY KEY,")
if "DataType" in s:
print("Value %s NOT NULL" % s["DataType"])
else:
print("Value float NOT NULL")
print(");")
print("GRANT ALL PRIVILEGES ON TABLE Sensor_%s to %s;" % (s["Codifier"], dbuser))
def sqlstats(s):
print("\c %s;" % dbname)
print("DROP TABLE IF EXISTS Sensor_%s_10min;" % (s["Codifier"]) )
print("CREATE TABLE Sensor_%s_10min (" % (s["Codifier"]) )
print("Timestamp bigint PRIMARY KEY,")
print("Mean float,")
print("Mode float,")
print("Median float,")
print("Max float,")
print("Min float,")
print("Stdev float")
print(");")
print("GRANT ALL PRIVILEGES ON TABLE Sensor_%s_10min to %s;" % (s["Codifier"], dbuser))
print("DROP TABLE IF EXISTS Sensor_%s_hour;" % (s["Codifier"]) )
print("CREATE TABLE Sensor_%s_hour (" % (s["Codifier"]) )
print("Timestamp bigint PRIMARY KEY,")
print("Mean float,")
print("Mode float,")
print("Median float,")
print("Max float,")
print("Min float,")
print("Stdev float")
print(");")
print("GRANT ALL PRIVILEGES ON TABLE Sensor_%s_hour to %s;" % (s["Codifier"], dbuser))
def createDB():
if comDrop:
print("DROP DATABASE IF EXISTS %s;" % (dbname))
print("CREATE USER %s WITH PASSWORD '%s';" % (dbuser, sqlpassword))
print("CREATE DATABASE %s OWNER %s;" % (dbname, dbuser) )
print("\c %s;" % dbname)
print("CREATE TABLE Graphs (")
print("name varchar(120),")
print("email varchar(120),")
print("settings text,")
print("PRIMARY KEY(name, email),")
print("UNIQUE (name, email)")
print(");")
print("GRANT ALL PRIVILEGES ON TABLE Graphs to %s;" % (dbuser))
print("\c %s;" % dbname)
print("CREATE TABLE Users (")
print("email varchar(120) PRIMARY KEY,")
print("password varchar(120)")
print(");")
print("GRANT ALL PRIVILEGES ON TABLE Users to %s;" % (dbuser))
# main
os.environ["JPH_SILENT"]="TRUE"
channel=jph.jph(configURL=configURL, Codifier=Codifier)
dbname=channel.getMySensorElement("SQL")["Database"]
dbuser=channel.getMySensorElement("SQL")["User"]
dbpass=channel.getMySensorElement("SQL")["Password"]
dbhost=channel.getMySensorElement("SQL")["Host"]
f = open(os.path.expanduser(dbpass))
sqlpassword=f.read().strip()
f.close
if comDB:
createDB()
for sensor in channel.getAllSensors():
sqltable(sensor)
sqlstats(sensor)
if comTruncate:
for sensor in channel.getAllSensors():
sqltruncate(sensor)
sqlstats(sensor)
if comTarget!="":
sqltable(channel.getSensor(comTarget))
sqlstats(channel.getSensor(comTarget))
if comStatsAll:
for sensor in channel.getAllSensors():
sqlstats(sensor)
if comStats!="":
sqlstats(channel.getSensor(comStats))
if comUser:
p_encode=p.encode('utf-8')
p_hashed = bcrypt.hashpw(p_encode, bcrypt.gensalt())
print("\c %s;" % dbname)
print("INSERT INTO Users VALUES ('%s', '%s');" % (u, p_hashed))