[06.02.20] Updated to work with graphql backend
This commit is contained in:
parent
8a87d0cc8b
commit
9b8240b014
2
configuration/dbgateway.env
Normal file
2
configuration/dbgateway.env
Normal file
@ -0,0 +1,2 @@
|
||||
URI="http://localhost"
|
||||
PORT="8080"
|
||||
@ -3,13 +3,15 @@
|
||||
import sys, json, os
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from time import sleep
|
||||
|
||||
from pricing.bitfinex import bitfinexPublicTicker, bitfinexHighLowVol
|
||||
from pricing.coinbase import coinbasePublicTicker
|
||||
from pricing.gemini import geminiPublicTicker, geminiHighLowVol, geminiOpenClose
|
||||
|
||||
from util.databaseConnect import connect, closeConnection
|
||||
from utils.databaseConnect import send
|
||||
|
||||
btc_usd="resources/sql/V1_INSERT_NEW_PRICE_RECORD_BTC.sql"
|
||||
btc_usd="resources/sql/V1_INSERT_NEW_PRICE_RECORD_BTC.graphql"
|
||||
|
||||
def getInsertForType(type):
|
||||
if type == "btc_usd":
|
||||
@ -73,15 +75,16 @@ def getHighLowVol(type):
|
||||
|
||||
return high, low, vol
|
||||
|
||||
def saveToDatabase(type, timestamp, av_price, high, low, vol, open, close):
|
||||
def sendToGateway(type, timestamp, av_price, high, low, vol, open, close):
|
||||
try:
|
||||
cur = connect()
|
||||
with open(btc_usd, 'r') as queryFile:
|
||||
data = queryFile.read()
|
||||
query = data % (type, timestamp, av_price, high, low, vol, open, close)
|
||||
|
||||
sql = getInsertForType(type)
|
||||
status, response = send(query)
|
||||
|
||||
cur.execute(sql, (type, timestamp, av_price, high, low, open, close, vol))
|
||||
|
||||
closeConnection(cur)
|
||||
print("Status: ", status)
|
||||
print("Response: ", response)
|
||||
return True
|
||||
except BaseException as exception:
|
||||
print("Error: %s" % str(exception))
|
||||
@ -103,4 +106,4 @@ def collector(type):
|
||||
high, low, vol = getHighLowVol(type)
|
||||
open, close = getOpenClose(type)
|
||||
|
||||
saveToDatabase(type, timestamp, av_price, high, low, vol, open, close)
|
||||
sendToGateway(type, timestamp, av_price, high, low, vol, open, close)
|
||||
@ -0,0 +1 @@
|
||||
mutation { createBtc(createdDate: %s, type: %s, average_price: %f, high_price: %f, low_price: %f, open_price: %f, close_price: %f, volume: %f){ id } }
|
||||
@ -1,2 +0,0 @@
|
||||
INSERT INTO btc_price(timestamp, symbol, av_price, h_price, l_price, o_price, c_price, volume)
|
||||
VALUES(%s, %s, %f, %f, %f, %f, %f, %f);
|
||||
@ -1,17 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from configparser import ConfigParser
|
||||
|
||||
def config(filename='configuration/database.ini', section='postgresql'):
|
||||
parser = ConfigParser()
|
||||
parser.read(filename)
|
||||
|
||||
db = {}
|
||||
if parser.has_section(section):
|
||||
params = parser.items(section)
|
||||
for param in params:
|
||||
db[param[0]] = param[1]
|
||||
else:
|
||||
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
|
||||
|
||||
return db
|
||||
@ -1,39 +1,26 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import psycopg2
|
||||
from utils.databaseConfig import config
|
||||
import requests, os, json
|
||||
|
||||
def connect():
|
||||
""" Connect to the PostgreSQL database server """
|
||||
conn = None
|
||||
try:
|
||||
# read connection parameters
|
||||
params = config()
|
||||
from dotenv import load_dotenv
|
||||
from pathlib import Path # python3 only
|
||||
env_path = Path('.') / 'configuration/dbgateway.env.env'
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
|
||||
# connect to the PostgreSQL server
|
||||
print('Connecting to the PostgreSQL database...')
|
||||
conn = psycopg2.connect(**params)
|
||||
class keys():
|
||||
|
||||
# create a cursor
|
||||
cur = conn.cursor()
|
||||
def __init__(self):
|
||||
self.uri = os.getenv('URI')
|
||||
self.port = os.getenv("PORT")
|
||||
|
||||
# execute a statement
|
||||
print('PostgreSQL database version:')
|
||||
cur.execute('SELECT version()')
|
||||
def send(query):
|
||||
|
||||
# display the PostgreSQL database server version
|
||||
db_version = cur.fetchone()
|
||||
print(db_version)
|
||||
uri = keys().uri + ":"+ keys().port + "/graphql"
|
||||
headers = {'Content-type': 'application/graphql'}
|
||||
|
||||
# close the communication with the PostgreSQL
|
||||
# cur.close()
|
||||
return cur;
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
finally:
|
||||
if conn is not None:
|
||||
conn.close()
|
||||
print('Database connection closed.')
|
||||
response = requests.post(uri, data=query, headers=headers)
|
||||
|
||||
def closeConnection(cur):
|
||||
cur.close()
|
||||
statusCode = json.loads(response.status_code)
|
||||
response = json.loads(response.text)
|
||||
|
||||
return statusCode, response
|
||||
Loading…
x
Reference in New Issue
Block a user