-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
102 lines (80 loc) · 3.48 KB
/
application.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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from flask import Flask, request
from flask_restful import Resource, Api
from flask_cors import CORS, cross_origin
from json import dumps
from flask_jsonpify import jsonify
import psycopg2
import psycopg2.extras
import simplejson as json
from database import PowerPlantData
connection = psycopg2.connect(user = "plantadmin",
password = "theadminoftheplant",
host = "127.0.0.1",
port = "5432",
database = "powerplant")
app = Flask(__name__)
api = Api(app)
CORS(app)
class Bolt(Resource):
def get(self):
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute("SELECT statecode, statename FROM statecodetoname")
records = cursor.fetchall()
return records
class StateEnv(Resource):
def get(self):
statename = request.args.get('statename')
#conn_string="host='localhost' dbname='powerplant' user='plantadmin' password='theadminoftheplant'"
powerplantData=PowerPlantData()
#powerplantData.get_air_quality_info_by_state(statename)
output=powerplantData.get_air_quality_info_by_state(statename)
return output[0]
class ViewPlants(Resource):
def get(self):
plantname = request.args.get('plantname')
#conn_string="host='localhost' dbname='powerplant' user='plantadmin' password='theadminoftheplant'"
powerplantData=PowerPlantData()
#powerplantData.get_air_quality_info_by_state(statename)
output=powerplantData.search_by_plant_name(plantname)
return output
class ElectricityData(Resource):
def get(self):
statename = request.args.get('statename')
#conn_string="host='localhost' dbname='powerplant' user='plantadmin' password='theadminoftheplant'"
powerplantData=PowerPlantData()
#powerplantData.get_air_quality_info_by_state(statename)
output=powerplantData.electricity_consumption(statename)
return output
class insertNewPlant(Resource):
def get(self):
plantid = request.args.get('plantid')
plantname = request.args.get('plantname')
statecode = request.args.get('statecode')
countyname = request.args.get('countyname')
noxrate = request.args.get('noxrate')
pmrate = request.args.get('pmrate')
totalcost = request.args.get('totalcost')
totalfuelconsumed = request.args.get('totalfuelconsumed')
totalgenerated = request.args.get('totalgenerated')
powerplantData=PowerPlantData()
#powerplantData.get_air_quality_info_by_state(statename)
output=powerplantData.insert_new_plant(plantid, plantname, statecode, countyname, noxrate, pmrate, totalcost, totalfuelconsumed, totalgenerated)
return output
class countyInformation(Resource):
def get(self):
countyname=request.args.get('countyname')
powerplantData=PowerPlantData()
output=powerplantData.countyinformation(countyname)
return output
api.add_resource(Bolt, '/bolt') # Route_1
api.add_resource(StateEnv, '/fetchstateenv') # Route_2
api.add_resource(ViewPlants, '/viewplants') #Route_3
api.add_resource(ElectricityData, '/electricitydata') #Route_4
api.add_resource(insertNewPlant, '/insernewplant') #Route_5
api.add_resource(countyInformation, '/countyinformation') #Route_5
if __name__ == '__main__':
app.run(port=1300)
# In[ ]: