OVH-server-tracker/src/db.py

85 lines
2.5 KiB
Python

#!/usr/bin/env python3
# coding: utf-8
# -*- coding: utf-8 -*-
import sqlite3
from sqlite3 import Error
from src.logs import Logger
class DB:
def __init__(self, db, config):
self.logger = Logger(config["log"]["level"], config["log"]["path"])
try:
self.connection = sqlite3.connect(db)
except Error as e:
self.logger.error("Can't connect to {}. Error : {}".format(db, e))
def create(self):
cursor = self.connection.cursor()
cursor.execute(
"""CREATE TABLE IF NOT EXISTS server (
name VARCHAR(10) NOT NULL,
datacenter VARCHAR(10) NOT NULL,
available int(1) NOT NULL,
notified int(1) NOT NULL
);"""
)
self.connection.commit()
def insertServer(self, server, datacenter):
data = (server, datacenter)
cursor = self.connection.cursor()
cursor.execute(
"""INSERT INTO server (name, datacenter, available, notified)
VALUES (?,?,0,0)""",
data,
)
self.connection.commit()
def updateAvailable(self, server, datacenter, status):
data = (status, server, datacenter)
cursor = self.connection.cursor()
cursor.execute(
"""UPDATE server
SET available = ?
WHERE name = ? AND datacenter = ?""",
data,
)
self.connection.commit()
def updateNotified(self, server, datacenter, notified):
data = (notified, server, datacenter)
cursor = self.connection.cursor()
cursor.execute(
"""UPDATE server
SET notified = ?
WHERE name = ? AND datacenter = ?""",
data,
)
self.connection.commit()
def isServerExist(self, server, datacenter):
data = (server, datacenter)
cursor = self.connection.cursor()
cursor.execute(
"SELECT name FROM server WHERE name = ? AND datacenter = ?", data
)
if len(cursor.fetchall()) == 0:
return False
else:
return True
def isNotified(self, server, datacenter, available):
data = (server, datacenter, available)
cursor = self.connection.cursor()
cursor.execute(
"SELECT name FROM server WHERE name = ? AND datacenter = ? AND available = ? AND notified = 1",
data,
)
if len(cursor.fetchall()) == 0:
return False
else:
return True