-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New props * Get azure metadata * Naming * dumps * Log name and location * 3 secs timeout * 3 secs timeout * 3 secs timeout * Updated * Set in user properties * Refactor * Timeout * Rates * Interim update * Interim update * Initial pass through * Add in the host * Fixes for host * Use one connection * Use one connection * Updates * Error in insert
- Loading branch information
1 parent
835b3e5
commit 44d5b6b
Showing
15 changed files
with
306 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,28 @@ | ||
"""Package of persistence for transaction counts. | ||
""" | ||
import os, sqlite3, mysql.connector | ||
from collections import namedtuple | ||
from ten.test.utils.properties import Properties | ||
|
||
DBConnection = namedtuple('DBConnection', 'connection type') | ||
|
||
|
||
def normalise(statement, _type): | ||
return statement if _type != 'mysql' else statement.replace('?', '%s') | ||
|
||
|
||
def get_connection(is_cloud_vm, db_dir): | ||
if is_cloud_vm: | ||
props = Properties() | ||
config = { | ||
'host': props.persistence_host(), | ||
'user': props.persistence_user(), | ||
'password': props.persistence_password(), | ||
'database': props.persistence_database(), | ||
'connection_timeout': 10 | ||
} | ||
connection = mysql.connector.connect(**config) | ||
return DBConnection(connection, 'mysql') | ||
else: | ||
connection = sqlite3.connect(os.path.join(db_dir, 'ten-test.db')) | ||
return DBConnection(connection, 'sqlite3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,83 @@ | ||
import sqlite3, os | ||
from ten.test.persistence import normalise | ||
|
||
|
||
class ContractPersistence: | ||
"""Abstracts the persistence of contract addresses into a local database. """ | ||
|
||
SQL_CREATE = "CREATE TABLE IF NOT EXISTS contracts " \ | ||
"(name TEXT, environment TEXT, address INTEGER, abi STRING, " \ | ||
SQL_CREATE = "CREATE TABLE IF NOT EXISTS contract_details " \ | ||
"(name VARCHAR(64), " \ | ||
"environment VARCHAR(64), " \ | ||
"address VARCHAR(64), " \ | ||
"abi MEDIUMTEXT, " \ | ||
"PRIMARY KEY (name, environment))" | ||
SQL_INSERT = "INSERT OR REPLACE INTO contracts VALUES (?, ?, ?, ?)" | ||
SQL_DELETE = "DELETE from contracts WHERE environment=?" | ||
SQL_SELECT = "SELECT address, abi FROM contracts WHERE name=? AND environment=? ORDER BY name DESC LIMIT 1" | ||
SQL_INSERT = "INSERT OR REPLACE INTO contract_details VALUES (?, ?, ?, ?)" | ||
SQL_DELETE = "DELETE from contract_details WHERE environment=?" | ||
SQL_SELECT = "SELECT address, abi FROM contract_details WHERE name=? AND environment=? ORDER BY name DESC LIMIT 1" | ||
|
||
SQL_CRT_PARAMS = "CREATE TABLE IF NOT EXISTS params " \ | ||
"(address INTEGER, environment TEXT, key STRING, value STRING, " \ | ||
"PRIMARY KEY (address, environment, key))" | ||
SQL_INS_PARAMS = "INSERT OR REPLACE INTO params VALUES (?, ?, ?, ?)" | ||
SQL_DEL_PARAMS = "DELETE from params WHERE environment=?" | ||
SQL_SEL_PARAMS = "SELECT value FROM params WHERE address=? AND environment=? AND key=? " \ | ||
"ORDER BY address DESC LIMIT 1" | ||
SQL_CRTPRM = "CREATE TABLE IF NOT EXISTS contract_params " \ | ||
"(address VARCHAR(64), " \ | ||
"environment VARCHAR(64), " \ | ||
"param_key VARCHAR(64), " \ | ||
"param_val VARCHAR(64), " \ | ||
"PRIMARY KEY (address, environment, param_key))" | ||
SQL_INSPRM = "INSERT OR REPLACE INTO contract_params VALUES (?, ?, ?, ?)" | ||
SQL_DELPRM = "DELETE from contract_params WHERE environment=?" | ||
SQL_SELPRM = "SELECT param_val FROM contract_params WHERE address=? AND environment=? AND param_key=? " \ | ||
"ORDER BY address DESC LIMIT 1" | ||
|
||
def __init__(self, db_dir): | ||
@classmethod | ||
def init(cls, host, dbconnection): | ||
instance = ContractPersistence(host, dbconnection) | ||
instance.create() | ||
return instance | ||
|
||
def __init__(self, host, dbconnection): | ||
"""Instantiate an instance.""" | ||
self.db = os.path.join(db_dir, 'contracts.db') | ||
self.connection = sqlite3.connect(self.db) | ||
self.cursor = self.connection.cursor() | ||
self.host = host | ||
self.dbconnection = dbconnection | ||
self.sqlins = normalise(self.SQL_INSERT, dbconnection.type) | ||
self.sqldel = normalise(self.SQL_DELETE, dbconnection.type) | ||
self.sqlsel = normalise(self.SQL_SELECT, dbconnection.type) | ||
self.insprm = normalise(self.SQL_INSPRM, dbconnection.type) | ||
self.delprm = normalise(self.SQL_DELPRM, dbconnection.type) | ||
self.selprm = normalise(self.SQL_SELPRM, dbconnection.type) | ||
self.cursor = self.dbconnection.connection.cursor() | ||
|
||
def create(self): | ||
"""Create the cursor to the underlying persistence.""" | ||
self.cursor.execute(self.SQL_CREATE) | ||
self.cursor.execute(self.SQL_CRT_PARAMS) | ||
self.cursor.execute(self.SQL_CRTPRM) | ||
|
||
def close(self): | ||
"""Close the connection to the underlying persistence.""" | ||
self.connection.close() | ||
self.cursor.close() | ||
|
||
def delete_environment(self, environment): | ||
"""Delete all stored contract details for a particular environment.""" | ||
self.cursor.execute(self.SQL_DELETE, (environment, )) | ||
self.cursor.execute(self.SQL_DEL_PARAMS, (environment, )) | ||
self.connection.commit() | ||
self.cursor.execute(self.sqldel, (environment, )) | ||
self.cursor.execute(self.delprm, (environment, )) | ||
self.dbconnection.connection.commit() | ||
|
||
def insert_contract(self, name, environment, address, abi): | ||
"""Insert a new contract into the persistence. """ | ||
self.cursor.execute(self.SQL_INSERT, (name, environment, address, abi)) | ||
self.connection.commit() | ||
self.cursor.execute(self.sqlins, (name, environment, address, abi)) | ||
self.dbconnection.connection.commit() | ||
|
||
def get_contract(self, name, environment): | ||
"""Return the address and abi for a particular deployed contract. """ | ||
self.cursor.execute(self.SQL_SELECT, (name, environment)) | ||
self.cursor.execute(self.sqlsel, (name, environment)) | ||
cursor = self.cursor.fetchall() | ||
if len(cursor) > 0: return cursor[0][0], cursor[0][1] | ||
return None, None | ||
|
||
def insert_param(self, address, environment, key, value): | ||
"""Insert a parameter for a named contract. """ | ||
self.cursor.execute(self.SQL_INS_PARAMS, (address, environment, key, value)) | ||
self.connection.commit() | ||
self.cursor.execute(self.insprm, (address, environment, key, value)) | ||
self.dbconnection.connection.commit() | ||
|
||
def get_param(self, address, environment, key): | ||
"""Return the address and abi for a particular deployed contract. """ | ||
self.cursor.execute(self.SQL_SEL_PARAMS, (address, environment, key)) | ||
self.cursor.execute(self.selprm, (address, environment, key)) | ||
cursor = self.cursor.fetchall() | ||
if len(cursor) > 0: return cursor[0][0] | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,61 @@ | ||
import sqlite3, os | ||
from ten.test.persistence import normalise | ||
|
||
|
||
class CountsPersistence: | ||
"""Abstracts the persistence of transaction counts across accounts into a local database. """ | ||
|
||
SQL_CREATE = "CREATE TABLE IF NOT EXISTS counts " \ | ||
"(name TEXT, address INTEGER, environment TEXT, time INTEGER, count TEXT, " \ | ||
"(name VARCHAR(64), " \ | ||
"address VARCHAR(64), " \ | ||
"environment VARCHAR(64), " \ | ||
"time INTEGER, " \ | ||
"count INTEGER, " \ | ||
"PRIMARY KEY (name, environment, time))" | ||
SQL_INSERT = "INSERT INTO counts VALUES (?, ?, ?, ?, ?)" | ||
SQL_DELETE = "DELETE from counts WHERE environment=?" | ||
SQL_SELECT_THREE = "SELECT time, count FROM counts WHERE name=? and environment=? ORDER BY time DESC LIMIT 3" | ||
SQL_SELECT_HOUR = "SELECT time, count FROM counts WHERE name=? and environment=? and time >= ? ORDER BY time DESC" | ||
SQL_SELTHR = "SELECT time, count FROM counts WHERE name=? and environment=? ORDER BY time DESC LIMIT 3" | ||
SQL_SELHOR = "SELECT time, count FROM counts WHERE name=? and environment=? and time >= ? ORDER BY time DESC" | ||
|
||
def __init__(self, db_dir): | ||
@classmethod | ||
def init(cls, host, dbconnection): | ||
instance = CountsPersistence(host, dbconnection) | ||
instance.create() | ||
return instance | ||
|
||
def __init__(self, host, dbconnection): | ||
"""Instantiate an instance.""" | ||
self.db = os.path.join(db_dir, 'counts.db') | ||
self.connection = sqlite3.connect(self.db) | ||
self.cursor = self.connection.cursor() | ||
self.host = host | ||
self.dbconnection = dbconnection | ||
self.sqlins = normalise(self.SQL_INSERT, dbconnection.type) | ||
self.sqldel = normalise(self.SQL_DELETE, dbconnection.type) | ||
self.sqlthr = normalise(self.SQL_SELTHR, dbconnection.type) | ||
self.sqlhor = normalise(self.SQL_SELHOR, dbconnection.type) | ||
self.cursor = self.dbconnection.connection.cursor() | ||
|
||
def create(self): | ||
"""Create the cursor to the underlying persistence.""" | ||
self.cursor.execute(self.SQL_CREATE) | ||
|
||
def close(self): | ||
"""Close the connection to the underlying persistence.""" | ||
self.connection.close() | ||
self.cursor.close() | ||
|
||
def delete_environment(self, environment): | ||
"""Delete all stored contract details for a particular environment.""" | ||
self.cursor.execute(self.SQL_DELETE, (environment, )) | ||
self.connection.commit() | ||
self.cursor.execute(self.sqldel, (environment,)) | ||
self.dbconnection.connection.commit() | ||
|
||
def insert_count(self, name, address, environment, time, count): | ||
"""Insert a new counts entry for a particular logical account.""" | ||
self.cursor.execute(self.SQL_INSERT, (name, address, environment, time, str(count))) | ||
self.connection.commit() | ||
self.cursor.execute(self.sqlins, (name, address, environment, time, str(count))) | ||
self.dbconnection.connection.commit() | ||
|
||
def get_last_three_counts(self, name, environment): | ||
"""Return the transaction count with time for a particular logical account.""" | ||
self.cursor.execute(self.SQL_SELECT_THREE, (name, environment)) | ||
self.cursor.execute(self.sqlthr, (name, environment)) | ||
return self.cursor.fetchall() | ||
|
||
def get_last_hour(self, name, environment, time): | ||
"""Return the transaction count with time for a particular logical account.""" | ||
self.cursor.execute(self.SQL_SELECT_HOUR, (name, environment, time)) | ||
self.cursor.execute(self.sqlhor, (name, environment, time)) | ||
return self.cursor.fetchall() |
Oops, something went wrong.