def insertTime(request): db = database.Database() if request.method == 'POST': game = { 'idGame': request.POST['idGame'], 'Score': ":".join([request.POST['score0'], request.POST['score1']]), 'Winner': request.POST['winner'], 'Time': request.POST['time'] } res = db.insertTime(game) db.conClose() if res != 'OK': template = loader.get_template('Error.html') context = {"error": res} return HttpResponse(template.render(context, request)) return HttpResponseRedirect(reverse('index')) template = loader.get_template('insertTime.html') context = { 'vars': { 'Commands': db.selectCommands(), 'Judges': db.selectJudges(), 'Address': db.selectAddress() } } db.conClose() return HttpResponse(template.render(context, request))
def signup(): email = None errorMessage = "" if request.method == "POST": email = request.form.get("email") password = request.form.get("password") name = request.form.get("name") authWrapper = auth.Auth() databaseWrapper = db.Database() try: authWrapper.CreateUser(name, email, password) user = firebase_admin.auth.get_user_by_email(email) publicKey, privateKey = databaseWrapper.CreateUser(user=user) session['userId'] = user.uid session['email'] = user.email session['publicKey'] = publicKey session['privateKey'] = privateKey return redirect(url_for('inbox')) except requests.exceptions.HTTPError as err: errorDict = ast.literal_eval(err.strerror) errorMessage = errorDict["error"]["message"] except ValueError as err: errorMessage = err except firebase_admin._auth_utils.EmailAlreadyExistsError as err: errorMessage = err except firebase_admin.exceptions.InvalidArgumentError as err: errorMessage = err return render_template('signup.html', errorMessage=errorMessage)
def add(): if request.method == 'POST': first_name = request.form['first_name'] last_name = request.form['last_name'] idno = request.form['idno'] yrlvl = request.form['yrlvl'] gender = request.form['gender'] college = request.form['college'] dept = request.form['dept'] course = request.form['course'] data = database.Database(first_name=first_name, last_name=last_name, idno=idno, yrlvl=yrlvl, gender=gender, college=college, dept=dept, course=course) data.add() flash("NEW INFORMATION ADDED!") return redirect(url_for('index')) else: data = database.Database.all() data1 = database.Database.college_dropdown() data2 = database.Database.dept_dropdown() data3 = database.Database.course_dropdown() return render_template('add.html', students=data, college=data1, department=data2, course=data3)
def static_anomaly(symbol, id): db = database.Database() state = 0 anomaly = db.getAnomalyById(id, state) baseTrade = anomaly.trade trades = db.getTradesForDrillDown(baseTrade.symbol, baseTrade.time, state) db.close() # Close quickly to prevent any issues return anomaly_template(trades, baseTrade, symbol, id, anomaly)
def delete_anomaly(): global _sessions global _sessionslock global _mode anomalyid = int(request.data) db = database.Database() success = db.dismissAnomaly(anomalyid, _mode) db.close() if (success == 1): _sessionslock.acquire for key in _sessions: if (_sessions[key] != session['id']): _sessions[key].put(mtrade.Anomaly(anomalyid, None, 0)) _sessionslock.release return "ok" return "fail"
def LoginUser(self, email, password): """ Logs a user in with a given username and password. Returns the response object. """ response = { "success": False, "message": None, "userId": None, "idToken": None, "refreshToken": None, "email": None, 'publicKey': None } try: user = self.auth.sign_in_with_email_and_password(email, password) user = self.auth.refresh(user['refreshToken']) userId = user['userId'] idToken = user['idToken'] refreshToken = user['refreshToken'] databaseWrapper = db.Database() publicKey = databaseWrapper.GetPublicKeyForUser(email) import json path = './app/config/privateKey.json' try: with open(path, "r") as f: data = json.load(f) privateKey = data[email] except: response[ "message"] = "Private key does not exist. Please add your private key to \'/web/flaskapp/app/config/privateKey.json\'. Click on \"Move to New Device\" in original client for more information." return response response["success"] = True response["message"] = "Successfully authenticated." response['userId'] = userId response['idToken'] = idToken response['refreshToken'] = refreshToken response['email'] = email response['publicKey'] = publicKey response['privateKey'] = privateKey except: response[ "message"] = "Failed to authenticate. Either username or password is incorrect." return response
def load_data(mode): #loads data from db global _tradevalue global _tradecounter global _anomalycounter global dbm dbm.mode = mode db = database.Database() trades = db.tradedetails(mode) _tradecounter = int(trades[1]) _anomalycounter = int(db.anomalycount(mode)) if (trades[0] is not None): _tradevalue = float(trades[0]) else: _tradevalue = float(0) db.close()
def update(request, arg): db = database.Database() idGame, Time = arg.split('and') date, comment = db.selectDateWhere(idGame) context = { 'game': { "idGame": idGame, 'Time': Time, 'Date': str(date)[:-3].replace(' ', 'T'), 'Score': db.selectScoreWhere(idGame, Time).split(':'), 'Comments': comment, 'Winner': db.selectWinner(idGame, Time), 'Loser': db.selectLoser(idGame, Time), 'Judge': db.selectJudgeWhere(idGame), 'Address': db.selectAddressWhere(idGame) } } if request.method == 'POST': if request.POST['date'] != context['game']['Date']: db.updateDate(idGame, request.POST['date'].replace('T', ' ')) if [request.POST['score0'], request.POST['score1'] ] != context['game']['Score']: db.updateScore( idGame, Time, ":".join([request.POST['score0'], request.POST['score1']])) if request.POST['comment'] != context['game']['Comments']: db.updateComment(idGame, request.POST['comment']) if request.POST['winner'] != context['game']['Winner']: db.updateWinner(idGame, Time, request.POST['winner']) if request.POST['judge'] != context['game']['Judge']: db.updateJudge(idGame, request.POST['judge']) if request.POST['address'] != context['game']['Address']: db.updateAddress(idGame, request.POST['address']) db.conClose() return HttpResponseRedirect(reverse('index')) context['vars'] = { 'Commands': db.selectCommands(), 'Judges': db.selectJudges(), 'Address': db.selectAddress() } db.conClose() template = loader.get_template('update.html') return HttpResponse(template.render(context, request))
def resetstats(): global _mode #for resetting current stats and db? global _mode db = database.Database() success = db.clearall(_mode) global _tradevalue global _tradecounter global _anomalycounter global _tradecounterlock if (success): _tradecounterlock.acquire() _tradevalue = 0 _tradecounter = 0 _anomalycounter = 0 _tradecounterlock.release() #reset machine learning print("reset") return "ok" return "fail"
def update_info(uid): if request.method == 'POST': first_name = request.form['first_name'] last_name = request.form['last_name'] idno = request.form['idno'] yrlvl = request.form['yrlvl'] gender = request.form['gender'] college = request.form['college'] dept = request.form['dept'] course = request.form['course'] data = database.Database(first_name=first_name, last_name=last_name, idno=idno, yrlvl=yrlvl, gender=gender, college=college, dept=dept, course=course, uid=uid) data.update_info(uid) flash('INFO UPDATED SUCCESSFULLY!') return redirect(url_for('index'))
def index(request): db = database.Database() context = {"db": db.selectGames()} if request.method == 'POST': if request.POST.get('Update'): url = reverse( 'update', kwargs={'arg': request.POST['Update'].replace(', ', 'and')}) response = HttpResponseRedirect(url) return response if request.POST.get('InsertGame'): return HttpResponseRedirect(reverse('insertGame')) if request.POST.get('InsertTime'): return HttpResponseRedirect(reverse('insertTime')) if request.POST.get('Load'): db.load() if request.POST.get('Delete'): idGame, Time = request.POST['Delete'].split(', ') db.delRow(idGame, Time) if request.POST.get("SearchJudge"): d = {0: "Woman", 1: "Man"} context = { "db": db.selectGamesWhereJ(request.POST['Judge']), "search": "Judge: " + d[int(request.POST['Judge'])] } if request.POST.get("SearchDate"): context = { "db": db.selectGamesWhereD(request.POST['date0'].replace("T", " "), request.POST['date1'].replace("T", " ")), "search": "Date: " + request.POST['date0'].replace("T", " ") + " - " + request.POST['date1'].replace("T", " ") } template = loader.get_template('index.html') db.conClose() return HttpResponse(template.render(context, request))
def init_data(): #get data in db global _mode db = database.Database(_mode) a = db.getAnomalies(0, _mode) #db.close() data = {} #TODO anomalies = [] for x in a: temp = x.trade.time.split() anomaly = {} anomaly['id'] = x.id anomaly['type'] = x.category anomaly['date'] = temp[0] anomaly['time'] = temp[1] anomaly['action'] = x.trade.symbol anomaly['sector'] = x.trade.sector anomalies.append(anomaly) #make into json if (len(anomalies) > 0): data["anomalies"] = anomalies data["mode"] = _mode return json.dumps(data)
def handle(self, *args, **options): with open(options.get('config')) as file_: configuration = utils.read(file_) database = db.Database(configuration['options']['datasource']) try: database.connect() cursor = connection.cursor() for index in range(0, len(TABLES)): table = TABLES[index] sq = S_QUERIES[index] iq = I_QUERIES[index] print(table) results = database.get(sq) for (idx, result) in enumerate(results): self._print_('{0}/{1}'.format(idx + 1, len(results))) cursor.execute( iq.format(','.join(['%s'] * len(result))), result ) print() finally: database.disconnect()
def databaseReset(self): global _mode dbm.mode = _mode db = database.Database() db.clearall(0)
def delete_info(uid): data = database.Database(uid=uid) data.delete_info(uid) flash("RECORD HAS BEEN DELETED SUCCESSFULLY!") return redirect(url_for('index'))
def test_missing_init(self, monkeypatch): monkeypatch.delenv("DATABASE_URL") with pytest.raises(KeyError): database.Database()
def test_valid_init(self): database.Database()
def test_get_random(self): id, joke = database.Database().get_random_joke() assert id assert joke assert isinstance(id, int) assert isinstance(joke, str)
def test_timeout_init(self, monkeypatch): monkeypatch.setenv("DATABASE_URL", "postgres://example.com:5432") with pytest.raises(psycopg2.OperationalError): database.Database()
def parsefile(self): #read file global _qlock global _staticq global _loadedfile print("Prepared File") # Immediatly read straight to database db = database.Database() print("Starting to read in the file") startTime = time.time() db.action( "load data local infile 'trades.csv' into table trans_static fields terminated by ',' lines terminated by '\n' ignore 1 lines (@col1, @col2, @col3, @col4, @col5, @col6, @col7, @col8, @col9, @col10) set id=NULL, time=@col1, buyer=@col2, seller=@col3, price=@col4, volume=@col5, currency=@col6, symbol=@col7, sector=@col8, bidPrice=@col9, askPrice=@col10", []) #print("Data read in") print("Took " + str(time.time() - startTime) + " seconds to complete") _loadedfile = 1 ''' db.getFirstId() with open('trades.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter = ',') for row in reader: if row[1] == 'buyer': continue else: row[0] = str(row[0]) row[1] = str(row[1]) #buyer row[2] = str(row[2]) #seller row[3] = str(row[3]) #price row[4] = str(row[4]) row[5] = str(row[5]) row[6] = str(row[6]) #symbol row[7] = str(row[7]) #sector row[8] = str(row[8]) row[9] = str(row[9]) #trade = mtrade.to_TradeData(row) #trade.id = db.query("select id from trans_static where(time=%s and buyer=%s and seller=%s)", [trade.time, trade.buyer, trade.seller])[0][0] _qlock.acquire() _staticq.put(mtrade.to_TradeData(row)) _qlock.release() print("Trying to load data for processing") firstId = db.query("select id from trans_static limit 1", [])[0][0] count = db.query("select count(*) from trans_static", [])[0][0] print("Beginning to process the data") # Loads in 10,000 trade groups step_increase = 500 limit = (firstId + count) currentStep = firstId print("First id is " + str(firstId)) startTime = time.time() while(currentStep <= limit): print("currentStep = " + str(currentStep)) rows = db.query("select * from trans_static where(id between %s and %s)", [currentStep, currentStep + step_increase]) print("Data is read in and ready to go") try: if(len(rows) == 0): # No more data to be read in break except KeyboardInterrupt: print("Quitting out") break for row in rows: trade = mtrade.to_TradeData(row[1:]) trade.id = row[0] _qlock.acquire() _staticq.put(trade) _qlock.release() currentStep += step_increase print("Took " + str(time.time() - startTime) + " seconds to complete") ''' with open('trades.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: if row[1] == 'buyer': continue else: row[0] = str(row[0]) row[1] = str(row[1]) #buyer row[2] = str(row[2]) #seller row[3] = str(row[3]) #price row[4] = str(row[4]) row[5] = str(row[5]) row[6] = str(row[6]) #symbol row[7] = str(row[7]) #sector row[8] = str(row[8]) row[9] = str(row[9]) _qlock.acquire() _staticq.put(mtrade.to_TradeData(row)) _qlock.release()
def processing(self): #State is processing static/live global _qlock global _running global _q global _staticq global _mode it_count = 0 previousmode = 0 db = database.Database(_mode) while (_running): global _tradecounter global _tradecounterlock global _anomalycounter global _tradevalue if (previousmode != _mode or _tradecounter == 0): #change in mode since last iteration or if it got reset #reset machine learning detection.reset() time.sleep(1) if (_mode == 1): #load from config #TODO a = 1 previousmode = _mode if (_mode == 0): trades = self.dequeue(_staticq, _qlock) it_count += 1 else: if (it_count > 0): it_count = 0 self.refreshVals() trades = self.dequeue(_q, _qlock) _tradecounterlock.acquire( ) #critical section big is fine because only '/reset' can change for t in trades: #Update counts if (not isinstance(t, mtrade.TradeData)): continue _tradecounter += 1 #TODO move elsewhere and mutex lock _tradevalue += float(t.price) * float(t.size) #trade is in TradeData format (see trade.py) #dump to db if (_mode == 1): tradeid = db.addTransaction(t, _mode) else: tradeid = -2 if (tradeid == -1): #error has occurred TODO insert better handler print("Error adding trade") # all the detection happens here trade_anomaly = detection.detect(t) #categorising and adding anomalies #key #1 price spike/trough #2 volume spike/trough #3 suspicious trader activity #4 potential pump and dump #calculate category if (len(trade_anomaly) > 0): #add anomaly to db cat = -1 if (1 in trade_anomaly): cat = 1 if (2 in trade_anomaly): cat = 2 if ((1 in trade_anomaly) & (2 in trade_anomaly)): ###possibly pump and dump? cat = 4 if (3 in trade_anomaly): #trader anomaly #move this out, in here for sake of testing and trader is overly sensitive cat = 3 if (4 in trade_anomaly): #frequency anomlay #move this out, in here for sake of testing and trader is overly sensitive cat = 5 if (cat > 0): if (_mode == 1): self.new_anomaly(db, tradeid, t, cat) else: self.new_anomaly(db, tradeid, t, cat) _tradecounterlock.release() #time.sleep(2) #REMOVE AFTER TESTING, to slow down processing #time.sleep(0.01) #good for cpu db.close()
def r6voltage(): db = database.Database(DBPATH) return render_template('voltagechart.html', labels=db.get_time_data(5), values=db.get_voltage_data(5))
def inbox(): databaseWrapper = db.Database() errorMessage = request.args.get('errorMessage') if errorMessage == None: errorMessage = "" if request.method == "GET": (messages, conversations) = databaseWrapper.GetInbox(session['email'], session['privateKey']) for messageId, messageObj in messages.items(): for message in messageObj['messages']: encryptedMessage = message['messageContents'][session['email']] decryptedMessage = encrypt.Decrypt(session['privateKey'], encryptedMessage) message['messageContents'] = decryptedMessage return render_template('inbox.html', title="Inbox", conversations=conversations, messages=messages, errorMessage=errorMessage) elif request.method == "POST": if request.form.get('formType') == "reply": plaintextMessage = request.form.get('message') publicKey = databaseWrapper.GetPublicKeyForUser( request.form.get('to')) ciphertextSender = encrypt.Encrypt(session['publicKey'], plaintextMessage) ciphertextTo = encrypt.Encrypt(publicKey, plaintextMessage) databaseWrapper.ReplyToMessage(request.form.get('msgId'), request.form.get('to'), session['email'], ciphertextSender, ciphertextTo) (messages, conversations) = databaseWrapper.GetInbox(session['email'], session['privateKey']) elif request.form.get('formType') == "newMessage": plaintextMessage = request.form.get('message') publicKey = databaseWrapper.GetPublicKeyForUser( request.form.get('to')) if not publicKey: errorMessage = "That account does not exist. Please check the email and try again." return redirect(url_for('inbox', errorMessage=errorMessage)) ciphertextSender = encrypt.Encrypt(session['publicKey'], plaintextMessage) ciphertextTo = encrypt.Encrypt(publicKey, plaintextMessage) errorMessage = databaseWrapper.CreateMessage( request.form.get('to'), session['email'], ciphertextSender, ciphertextTo) (messages, conversations) = databaseWrapper.GetInbox(session['email'], session['privateKey']) return redirect(url_for('inbox', errorMessage=errorMessage))
def main(): db = database.Database(DBPATH) return render_template('main.html')
def r6humidity(): db = database.Database(DBPATH) return render_template('humiditychart.html', labels=db.get_time_data(5), values=db.get_humidity_data(5))
def __init__(self): self.db = database.Database()
def r6temp(): db = database.Database(DBPATH) return render_template('tempchart.html', labels=db.get_time_data(5), values=db.get_temperature_data(5))
def setUp(self): self.config = user.take_config(PATH) self.db = pg.connect(**self.config['data']) self.vk_db = database.Database()
def r5pressure(): db = database.Database(DBPATH) return render_template('pressurechart.html', labels=db.get_time_data(4), values=db.get_pressure_data(4))
def __init__( self ): self.database = db.Database()