-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
79 lines (61 loc) · 2.78 KB
/
database.py
File metadata and controls
79 lines (61 loc) · 2.78 KB
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
#!/usr/bin/python
import sys
import sqlite3
import random
#number_of_nodes = 0 # global variable
class ComputingCenters:
def __init__(self, filename):
self.filename = filename
self.conn = sqlite3.connect(self.filename)
self.num_of_nodes = 0
self.conn.execute('PRAGMA foreign_keys = ON')
print("Opened database successfully")
def create_tables(self):
self.conn.execute('''CREATE TABLE COMPUTING_CENTERS
(`ID` INT PRIMARY KEY NOT NULL,
`NUM_OF_NODES` INT NOT NULL,
`RAM` INT,
`CPU` INT );''')
self.conn.execute('''CREATE TABLE NODES
(`ID` INT PRIMARY KEY NOT NULL,
`CC_ID` INT ,
`RAM` INT,
`CPU` INT,
`CPU_TYPE` INT,
FOREIGN KEY(CC_ID)REFERENCES COMPUTING_CENTERS(ID) );''')
print("Tables created successfully")
#self.conn.close()
def insert_db(self):
#self.conn = sqlite3.connect(self.filename)
#print("Opened database successfully")
num_cc = random.randint(10, 50)
#print(num_cc)
count = 1
for i in range(1, num_cc):
num_nodes = random.randint(1, 5)
ram = random.randint(128, 1024)
cpu = random.randint(100, 300)
self.conn.execute("INSERT INTO COMPUTING_CENTERS (ID,NUM_OF_NODES,RAM,CPU) \
VALUES ("+str(i) + ","+str(num_nodes) + "," + str(ram*num_nodes) + "," + str(cpu*num_nodes)+" )");
with open('computing_centers.txt', 'a') as out:
out.write("ID: "+str(i)+" num of nodes: "+str(num_nodes)+" ram: "+str(ram*num_nodes)+" cpu: "+str(cpu*num_nodes) + '\n\n' + "#################" + '\n\n')
for j in range(0,num_nodes):
cpu_type = random.choice([1,2])
self.conn.execute("INSERT INTO NODES (ID,CC_ID,RAM,CPU,CPU_TYPE) \
VALUES (" + str(count) + "," + str(i) + "," + str(ram) + "," + str(cpu) + "," + str(cpu_type)+" )");
with open('nodes.txt', 'a') as out:
out.write("ID: " + str(count) + " CC_id: " + str(i) + " ram: " + str(ram) + " cpu: " + str(cpu) +" cpu_type :"+str(cpu_type)+ '\n\n' + "#################" + '\n\n')
count += 1
self.num_of_nodes = count-1
global number_of_nodes
number_of_nodes = self.num_of_nodes
print("num nodes")
print(self.num_of_nodes)
self.conn.commit()
print("insert data successfully")
self.conn.close()
def get_num_of_nodes(self):
return self.num_of_nodes
def select_random_node():
#print(number_of_nodes)
return random.randint(1, number_of_nodes)