def delete(self, item): """""" path = self.on_get_path(item.id) if path: self.row_deleted(path) Queue.delete(self, item) self.cache.clear()
def index(request): will = Player("Will") will.add_games(["TicTacToe"]) jason = Player("Jason") jason.add_games(["TicTacToe", "Poker"]) queue = Queue() queue.add_players([will, jason]) games = ["TicTacToe", "Chess", "Poker"] context = {'queue': queue, 'games': games} return render(request, 'web/index.html', context)
def move(self, item, direction): """""" if item: path = self.on_get_path(item.parent_id) if path: iter = self.get_iter(path) else: iter = None prev = self.get_children(item.parent_id) self.cache.clear() Queue.move(self, item, direction) next = self.get_children(item.parent_id) self.rows_reordered(path, iter, [next.index(item) for item in prev])
def __init__(self, icons): """(status_icon, name, progress, current_size, total_size, speed, time, info)""" gtk.GenericTreeModel.__init__(self) Queue.__init__(self) self.cache = Cache() self.column_types = (gtk.gdk.Pixbuf, str, int, str, str, str, str, str) self.column_values = (lambda x: icons.get_icon(x.type, x.status), lambda x: x.get_name(), lambda x: x.get_progress(), lambda x: x.get_current_size(), lambda x: x.get_total_size(), lambda x: x.get_speed(), lambda x: x.get_time(), lambda x: x.get_info())
def add(self, items): """""" if Queue.add(self, items): for id in [item.id for item in items]: path = self.on_get_path(id) self.row_inserted(path, self.get_iter(path)) return True
def __init__(self, icons): """(status_icon, name, progress, current_size, total_size, speed, time, info)""" gtk.GenericTreeModel.__init__(self) Queue.__init__(self) self.cache = Cache() self.column_types = (gtk.gdk.Pixbuf, str, int, str, str, str, str, str) self.column_values = ( lambda x: icons.get_icon(x.type, x.status), lambda x: x.get_name(), lambda x: x.get_progress(), lambda x: x.get_current_size(), lambda x: x.get_total_size(), lambda x: x.get_speed(), lambda x: x.get_time(), lambda x: x.get_info() )
def ping(service): print("ping", service) if service == "store": Store = store.get_store( StoreConfig( label="store", type="es_vec", parameters=StoreParameters( host_name="192.46.212.142", image_index_name="image", video_index_name="video", text_index_name="text", ), )) Store.connect() response = Store.ping() click.echo(response) elif service == "queue": queue = Queue.make( QueueConfig( label="Queue", type="rabbitmq", parameters=QueueParameters( host_name="rabbitmq", queues=[ { "name": "tattle-search-index-queue" }, { "name": "tattle-search-report-queue" }, ], ), )) queue.connect()
def __init__(self, mode, lambd, mu, theta, servers_count, core_servers_count, L, H, simulation_time, max_queue_size, is_debug): self.lambd = lambd self.mu = mu self.theta = theta self.servers_count = int(servers_count) self.core_servers_count = int(core_servers_count) self.L = int(L) self.H = int(H) self.simulation_time = simulation_time self.is_debug = is_debug self.auto_continue = not self.is_debug self.mode = mode self.flow = Flow(lambd, mu, is_debug) self.queue = Queue(int(max_queue_size), is_debug) self.generated_request = Request(-1, 0, 0, 0) self.system_state = States.IDLE self.prev_system_state = States.IDLE self.served_count = 0 self.served_sum_w = 0 self.served_sum_wq = 0 self.servers = [] self.generated_requests = [] self.time = 0 self.prev_time = 0 self.up_down_time = 0 self.prev_up_down_time = 0 self.up_down_count = 0 self.up_down_mean = 0 self.state_time = dict.fromkeys(States.get_States_list(States), 0) self.state_count = dict.fromkeys(States.get_States_list(States), 0) for i in range(int(servers_count)): self.servers.append( Server(i, True if i < int(core_servers_count) else False, is_debug))
def __init__(self, *player_data): self.queue = Queue() for player in player_data: name = player[0] list_of_games = player[1:] player = Player(name) player.add_games(list_of_games) self.queue.add_player(player)
def __init__(self, configPath): self.config = config.load(configPath) if self.config.operators: self.operators = Operator(self.config.operators) if self.config.store: self.store = store.get_store(self.config.store) if self.config.queue: # print("---> 1", self.config.queue) self.queue = Queue.make(self.config.queue) if self.config.server: self.server = Server(self.config.server)
class SelectingChessFixture(object): @convert_arg(using=IterableConverter()) def __init__(self, *player_data): self.queue = Queue() for player in player_data: name = player[0] list_of_games = player[1:] player = Player(name) player.add_games(list_of_games) self.queue.add_player(player) def add_player(self, name): self.queue.add_player(Player(name)) def player_chooses_game(self, game): self.game = game def filtered_players(self): filtered_players = self.queue.filter(self.game) return [player.name for player in filtered_players]
def __init__(self, urllist, visits, min, max): self.bots = 3 # max amount of bots to use; can be changed at later time; atm used for sorted amount self.count = 0 # returning bots self.ip = None self.alive = True self.targets = {} # {url: visits} self.recentIPs = Queue(10) self.min = int(min) self.max = int(max) self.visits = int(visits) if not path.exists(urllist): exit('Error: Unable to locate `{}`'.format(urllist)) # read the url list with open(urllist, 'r') as f: try: for url in [_ for _ in f.read().split('\n') if _]: self.targets[url] = 0 # initial view except Exception as err:exit('Error:', err)
def reset(service): print("reset", service) if service == "store": Store = store.get_store( StoreConfig( label="store", type="es_vec", parameters=StoreParameters( host_name="es", image_index_name="image", video_index_name="video", text_index_name="text", ), )) Store.connect() Store.reset() print("Store reset") elif service == "queue": queue = Queue.make( QueueConfig( label="Queue", type="rabbitmq", parameters=QueueParameters( host_name="rabbitmq", queues=[ { "name": "tattle-search-index-queue" }, { "name": "tattle-search-report-queue" }, ], ), )) queue.connect() queue.reset()
def __init__(self, urllist, visits, min, max): super().__init__() self.bots = 2 # max amount of bots to use self.count = 0 # returning bots self.ip = None self.alive = True self.targets = {} # {url: visits} self.recentIPs = Queue(5) self.min = int(min) self.max = int(max) self.visits = int(visits) if not path.exists(urllist): exit(f'Error: Unable to locate {format(urllist)}') # read the url list with open(urllist, 'r') as f: try: for url in [_ for _ in f.read().split('\n') if _]: self.targets[url] = 0 # initial view except Exception as err: exit(f'Error: ? {err}')
def setUp(self): """""" self.manager = UploadManager(Queue()) #disable scheduler self.manager.scheduling = True self.manager.add_package(PACKAGE)
class TestQueue(unittest.TestCase): """""" def setUp(self): """""" self.queue = Queue() self.queue.add_package(FILE_LIST) def test_delete_package(self): """""" package = self.queue.add_package(FILE_LIST) packages = self.queue.get_children() self.assertEqual(len(packages), 2) self.queue.delete(package) self.assertEqual(len(self.queue.get_children()), 1) self.assertEqual(self.queue.get_item(package.id), None) def test_delete_file(self): """""" package = self.queue.add_package(FILE_LIST) file1, file2 = self.queue.get_children(package.id) old_size = self.queue.get_item(package.id).total_size self.queue.delete(file1) size = self.queue.get_item(package.id).total_size self.assertEqual(size, old_size-file1.total_size) self.assertEqual(len(self.queue.get_children(package.id)), 1) self.assertEqual(self.queue.get_item(file1.id), None) self.queue.delete(file2) self.assertEqual(self.queue.get_item(file2.id), None) self.assertEqual(self.queue.get_item(package.id), None) def test_delete_link(self): """""" package = self.queue.add_package(FILE_LIST) file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) p_size = self.queue.get_item(package.id).total_size f_size = file1.total_size self.queue.delete(link1) size = self.queue.get_item(file1.id).total_size self.assertEqual(size, f_size-link1.total_size) size = self.queue.get_item(package.id).total_size self.assertEqual(size, p_size-link1.total_size) self.assertEqual(len(self.queue.get_children(file1.id)), 1) self.assertEqual(self.queue.get_item(link1.id), None) self.queue.delete(file2) self.queue.delete(link2) self.assertEqual(self.queue.get_item(link2.id), None) self.assertEqual(self.queue.get_item(file1.id), None) self.assertEqual(self.queue.get_item(package.id), None) def test_move_up_link(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) self.queue.move_up(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link4) self.assertEqual(link2, link3) self.queue.move_up(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) self.queue.move_up(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) def test_move_down_link(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) self.queue.move_down(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link4) self.assertEqual(link2, link3) self.queue.move_down(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) self.queue.move_down(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) def test_move_up_file(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) self.queue.move_up(file2) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) self.queue.move_up(file2) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) def mtest_move_down_file(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) self.queue.move_down(file1) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) self.queue.move_down(file1) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) def test_move_up_package(self): """""" self.queue.add_package(FILE_LIST) package1, package2 = self.queue.get_children() file1, file2 = self.queue.get_children(package1.id) file3, file4 = self.queue.get_children(package2.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.queue.move_up(package2) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) self.queue.move_up(package2) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) def test_move_down_package(self): """""" package_id = self.queue.add_package(FILE_LIST) package1, package2 = self.queue.get_children() file1, file2 = self.queue.get_children(package1.id) file3, file4 = self.queue.get_children(package2.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.queue.move_down(package1) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) self.queue.move_down(package1) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) def test_propagate_status(self): """""" package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) #p : pend, f: pend pend, l: pend pend pend pend link1.set_status(cons.STATUS_ACTIVE) #p : active, f: active pend, l: active pend pend pend self.assertEqual(file1.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link2.set_status(cons.STATUS_WAIT) #p : active, f: active pend, l: active wait pend pend self.assertEqual(file1.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link1.set_status(cons.STATUS_PEND) #p : wait, f: wait pend, l: pend wait pend pend self.assertEqual(file1.status, cons.STATUS_WAIT) self.assertEqual(package.status, cons.STATUS_WAIT) link1.set_status(cons.STATUS_ACTIVE) #p : active, f: active pend, l: active wait pend pend link1.set_status(cons.STATUS_ERROR) link2.set_status(cons.STATUS_ERROR) #p : pend, f: error pend, l: error error pend pend self.assertEqual(file1.status, cons.STATUS_ERROR) self.assertEqual(package.status, cons.STATUS_PEND) link1.set_status(cons.STATUS_WAIT) #p : wait, f: wait pend, l: wait error pend pend self.assertEqual(file1.status, cons.STATUS_WAIT) self.assertEqual(package.status, cons.STATUS_WAIT) link1.set_status(cons.STATUS_CORRECT) link2.set_status(cons.STATUS_CORRECT) #p : pend, f: correct pend, l: correct correct pend pend self.assertEqual(file1.status, cons.STATUS_CORRECT) self.assertEqual(package.status, cons.STATUS_PEND) link3.set_status(cons.STATUS_ACTIVE) #p : active, f: correct active, l: correct correct active pend self.assertEqual(file2.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link3.set_status(cons.STATUS_CORRECT) link4.set_status(cons.STATUS_CORRECT) #p : correct, f: correct correct, l: correct correct correct correct self.assertEqual(file2.status, cons.STATUS_CORRECT) self.assertEqual(package.status, cons.STATUS_CORRECT) def test_sort_status(self): tmp = self.queue.sort_status(cons.STATUS_CORRECT, cons.STATUS_STOP) self.assertEqual(tmp, cons.STATUS_STOP) tmp = self.queue.sort_status(cons.STATUS_STOP, cons.STATUS_ERROR) self.assertEqual(tmp, cons.STATUS_ERROR) tmp = self.queue.sort_status(cons.STATUS_ERROR, cons.STATUS_PEND) self.assertEqual(tmp, cons.STATUS_PEND) tmp = self.queue.sort_status(cons.STATUS_PEND, cons.STATUS_WAIT) self.assertEqual(tmp, cons.STATUS_WAIT) tmp = self.queue.sort_status(cons.STATUS_WAIT, cons.STATUS_ACTIVE) self.assertEqual(tmp, cons.STATUS_ACTIVE) def tearDown(self): """""" del self.queue
class Simulation: """ This class represents simulation process """ def __init__(self, mode, lambd, mu, theta, servers_count, core_servers_count, L, H, simulation_time, max_queue_size, is_debug): self.lambd = lambd self.mu = mu self.theta = theta self.servers_count = int(servers_count) self.core_servers_count = int(core_servers_count) self.L = int(L) self.H = int(H) self.simulation_time = simulation_time self.is_debug = is_debug self.auto_continue = not self.is_debug self.mode = mode self.flow = Flow(lambd, mu, is_debug) self.queue = Queue(int(max_queue_size), is_debug) self.generated_request = Request(-1, 0, 0, 0) self.system_state = States.IDLE self.prev_system_state = States.IDLE self.served_count = 0 self.served_sum_w = 0 self.served_sum_wq = 0 self.servers = [] self.generated_requests = [] self.time = 0 self.prev_time = 0 self.up_down_time = 0 self.prev_up_down_time = 0 self.up_down_count = 0 self.up_down_mean = 0 self.state_time = dict.fromkeys(States.get_States_list(States), 0) self.state_count = dict.fromkeys(States.get_States_list(States), 0) for i in range(int(servers_count)): self.servers.append( Server(i, True if i < int(core_servers_count) else False, is_debug)) def update_state_time(self): self.state_time[self.prev_system_state] += self.time - self.prev_time def update_state_count(self): self.state_count[self.prev_system_state] += 1 def update_system_state(self): self.prev_system_state = self.system_state self.system_state = self.get_system_state() def get_system_state(self): """ Calculate server state """ if self.mode == "m/m/c/r": return States.IDLE if self.mode == "m/m/c[c0]/r": if self.system_state == States.IDLE: if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) == self.core_servers_count: return States.IDLE if len(self.queue.requests) > 0: return States.TURN_UP if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) > self.core_servers_count: return States.TURN_OFF return "Error[IDLE]" if self.system_state == States.TURN_UP: if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) == self.core_servers_count: return States.IDLE if len(self.queue.requests) > 0: return States.TURN_UP if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) > self.core_servers_count: return States.TURN_OFF return "Error[TURN_UP]" if self.system_state == States.TURN_OFF: if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) > self.core_servers_count: return States.TURN_OFF if len(self.queue.requests ) == 0 and self.get_deployed_servers_count( ) == self.core_servers_count: return States.IDLE if len(self.queue.requests) > 0: return States.TURN_UP return "Error[TURN_OFF]" return "Error[m/m/c[c0]/r]" if self.mode == "m/m/c[c0]/r[l,h]": if self.system_state == States.IDLE: if len(self.queue.requests) >= self.H: return States.TURN_UP if len(self.queue.requests) < self.H: return States.IDLE return "Error[IDLE]" if self.system_state == States.TURN_UP or self.system_state == States.FULL: if len(self.queue.requests) <= self.L: return States.TURN_OFF if len( self.queue.requests ) > self.L and self.servers_count == self.get_deployed_servers_count( ): return States.FULL if len(self.queue.requests) > self.L: return States.TURN_UP return "Error[TURN_UP]" if self.system_state == States.TURN_OFF: if len(self.queue.requests) >= self.H: return States.TURN_UP if len(self.queue. requests) < self.H and self.get_deployed_servers_count( ) == self.core_servers_count: return States.IDLE if len(self.queue. requests) < self.H and self.get_deployed_servers_count( ) > self.core_servers_count: return States.TURN_OFF return "Error[TURN_OFF]" return "Error[m/m/c[c0]/r[l,h]]" return "Error[mode not supported]" def update_time(self): if self.system_state == States.IDLE: self.prev_up_down_time = 0 else: if self.prev_system_state == States.IDLE: self.prev_up_down_time = self.time else: if self.system_state != self.prev_system_state: if self.system_state == States.TURN_OFF or self.prev_system_state == States.TURN_OFF: self.up_down_count += 1 self.up_down_time += self.time - self.prev_up_down_time self.up_down_mean = self.up_down_time / self.up_down_count self.prev_time = self.time self.time = self.get_system_time() def get_system_time(self): """ Calculate time after last event """ first_generated_request = self.get_first_arrived_generated_request() first_served_server = self.get_first_served_server() first_turned_server = self.get_first_turned_server() next_arrive_time = first_generated_request.arrival_time next_serve_time = float( 'inf' ) if first_served_server.ID == -1 else first_served_server.departure_time next_turn_time = float('inf') if ( first_turned_server.ID == -1 and (self.system_state != States.TURN_UP or self.system_state != States.FULL)) else first_turned_server.turn_on_time t = min([next_arrive_time, next_serve_time, next_turn_time]) if self.is_debug: t0 = "%.3f" % first_generated_request.arrival_time t1 = "never" if next_serve_time == float( 'inf') else "%.3f" % next_serve_time t2 = "never" if next_turn_time == float( 'inf') else "%.3f" % next_turn_time if t == next_arrive_time: event = "ARRIVE" if t == next_serve_time: event = "SERVE" if t == next_turn_time: event = "TURN" print(" Event=", event, ", arrive= ", t0, ", ", "serve= ", t1, "(#", first_served_server.ID, ")," "turn up= ", t2, "(#", first_turned_server.ID, ")") return t def get_free_deployed_server(self): """ Return free working server """ free_server = False for server in self.servers: if not server.is_busy and server.is_deployed and not server.to_be_turned_off: return server return free_server def get_free_deployed_servers_count(self): """ Return count of free working servers """ count = 0 for server in self.servers: if not server.is_busy and server.is_deployed and not server.to_be_turned_off: count += 1 return count def get_busy_deployed_servers_count(self): """ Return count of busy working servers """ count = 0 for server in self.servers: if server.is_busy and server.is_deployed: count += 1 return count def get_deployed_servers_count(self): """ Return working servers count """ count = 0 for server in self.servers: if server.is_deployed: count += 1 return count def get_first_arrived_generated_request(self): """ Return request to be served first """ generated_request = self.generated_requests[0] for request in self.generated_requests: if request.arrival_time < generated_request.arrival_time: generated_request = request return generated_request def get_first_served_server(self): """ Return server to be served first """ served_server = Server(-1, True, False) served_server.departure_time = float('inf') for server in self.servers: if server.is_busy and server.is_deployed and server.departure_time < served_server.departure_time: served_server = server return served_server def get_first_turned_server(self): """ Return server to be turned on first """ served_server = Server(-1, True, False) served_server.turn_on_time = float('inf') for server in self.servers: if server.to_be_turned_on and server.turn_on_time < served_server.turn_on_time: served_server = server return served_server def get_servers_to_turn_off(self): """ Return servers list to be turned off """ result = [] # sort servers by departure time self.servers.sort(key=lambda x: x.departure_time) for i in range(0, len(self.servers)): self.servers[i].ID = i # turn off c(t)-c0 served deployed servers turned_off_servers = 0 for server in self.servers: if turned_off_servers < self.get_deployed_servers_count( ) - self.core_servers_count and server.is_deployed: turned_off_servers += 1 result.append(server) return result def pop_generated_request(self, request): """ Delete request from list of generated request waiting to be served """ request_id = 0 for req in self.generated_requests: if req.ID == request.ID: self.generated_requests.pop(request_id) break request_id += 1 def has_turned_servers(self): turned_count = 0 for server in self.servers: if server.is_deployed: turned_count += 1 return True if turned_count != self.core_servers_count else False def turn_on_servers(self): # start turning up servers for server in self.servers: self.servers[server.ID].turn_on(self.time, self.theta) # time = turn up server, change server state to deployed for server in self.servers: if server.turn_on_time == self.time: self.servers[server.ID].deploy() def turn_off_servers(self): servers_to_turn_off = self.get_servers_to_turn_off() for server in self.servers: self.servers[server.ID].turn_off() for server in servers_to_turn_off: if server.departure_time == self.time and server.is_busy and server.is_deployed and server.to_be_turned_off: served_request = self.servers[server.ID].unload_and_undeploy() self.served_count += 1 self.served_sum_w += served_request.w self.served_sum_wq += served_request.wq for server in self.servers: if server.departure_time == self.time and server.is_busy and server.is_deployed and not server.to_be_turned_off: served_request = self.servers[server.ID].unload() self.served_count += 1 self.served_sum_w += served_request.w self.served_sum_wq += served_request.wq def handle_idle(self): for server in self.servers: self.servers[server.ID].idle() def handle_idle_mode(self): self.handle_idle() self.serve_request() self.handle_request() self.handle_queue() def handle_turn_on_mode(self): self.turn_on_servers() self.serve_request() self.handle_request() self.handle_queue() def handle_turn_off_mode(self): self.turn_off_servers() self.handle_request() self.handle_queue() def serve_request(self): # time = departure time, serve request for server in self.servers: if server.departure_time == self.time and server.is_busy and server.is_deployed: served_request = self.servers[server.ID].unload() self.served_count += 1 self.served_sum_w += served_request.w self.served_sum_wq += served_request.wq def handle_request(self): # time = new request arrive, handle request first_generated_request = self.get_first_arrived_generated_request() if first_generated_request.arrival_time == self.time: if self.get_free_deployed_servers_count() > 0: self.servers[self.get_free_deployed_server().ID].load( self.generated_request) else: self.queue.push(self.generated_request) self.pop_generated_request(first_generated_request) if (len(self.generated_requests) < 2 * self.servers_count): self.generated_request = self.flow.generate() self.generated_requests.append(self.generated_request) def handle_queue(self): # handle queue at current time for request in self.queue.requests: if self.get_free_deployed_servers_count() > 0: request_from_queue = self.queue.pop(request, self.time) request_from_queue.queue_size_at_serving = len( self.queue.requests) request_from_queue.server_arrival_time = self.time self.servers[self.get_free_deployed_server().ID].load( request_from_queue) else: break def start(self): """ Run simulation """ if self.is_debug: print("Simulation started") self.generated_request = self.flow.generate() self.generated_requests.append(self.generated_request) while self.time < self.simulation_time: self.run() if self.is_debug: print("Simulation ended") def start_requests(self, requestsToServe): """ Run simulation """ if self.is_debug: print("Simulation started") self.generated_request = self.flow.generate() self.generated_requests.append(self.generated_request) heating = 250000 is_heated = False while self.served_count < requestsToServe + heating: self.run() if self.served_count == heating and is_heated is False: is_heated = True self.queue.blocked = 0 self.served_count = 0 self.served_sum_w = 0 self.served_sum_wq = 0 if self.is_debug: print("Simulation ended") def run(self): if self.is_debug: print("TIME = ", self.time) # IDLE if self.system_state == States.IDLE: self.handle_idle_mode() # TURN UP or FULL elif self.system_state == States.TURN_UP or self.system_state == States.FULL: self.handle_turn_on_mode() # TURN DOWN elif self.system_state == States.TURN_OFF: self.handle_turn_off_mode() self.update_system_state() self.update_time() self.update_state_time() self.update_state_count() self.debug_run() def debug_run(self): if self.is_debug: print(" ", self.system_state) print(" Q = ", len(self.queue.requests), ", ", "blocked = ", self.queue.blocked, ", ", " busy = ", self.get_busy_deployed_servers_count(), "/", self.get_deployed_servers_count(), ",", " free = ", self.get_free_deployed_servers_count(), "/", self.get_deployed_servers_count(), ",", "C = ", self.servers_count, ",", " generated = ", self.flow.generated_count, ",", " served =", self.served_count) # for server in self.servers: # server.get_info() print(" up-down: count = ", self.up_down_count, ", mean = ", self.up_down_mean, ", total = ", self.up_down_time) print(self.state_count) print(self.state_time) if self.is_debug and not self.auto_continue: user_input = input( " Press Enter to for next step, or input 'True' to turn on auto continue mode: " ) if bool(user_input) == True: self.auto_continue = True
def setUp(self): """""" self.queue = Queue() self.queue.add_package(FILE_LIST)
def update_cb(self, item, status=None): """""" Queue.update_cb(self, item, status) self.update_row(item.id)
credentials = CredentialsLoader().get_credentials() mode = credentials['general']['mode'] operator = credentials['general']['operator'] reddit = praw.Reddit(user_agent=consts.user_agent, client_id=credentials['reddit']['client_id'], client_secret=credentials['reddit']['client_secret'], username=credentials['reddit']['username'], password=credentials['reddit']['password']) print("CoffinBot Queue Client v{} Ctrl+C to stop".format(consts.version)) failure_counter = 1 # 1 by default since it is the wait timer multiplier q = Queue() q.enter_queue() def signal_handler(sig, frame): print("Exiting...") q.exit_queue() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) while True: try: for i in q.get_jobs(): print(i, i.origin_host, i.origin_id)
class QueueTests(unittest.TestCase): def setUp(self): self.TicTacToe = "TicTacToe" self.Chess = "Chess" self.queue = Queue() def test_a_player_can_be_added(self): player = self.create_player() self.add_player_to_queue(player) self.assertEqual(1, self.get_length_of_queue()) def test_two_players_can_be_added_sequentially(self): players = self.create_players(2) self.add_player_to_queue(players[0]) self.assertEqual(1, self.get_length_of_queue()) self.add_player_to_queue(players[1]) self.assertEqual(2, self.get_length_of_queue()) def test_the_same_player_cannot_be_added_twice(self): player = self.create_player() self.add_player_to_queue(player) self.add_player_to_queue(player) self.assertEqual(1, self.get_length_of_queue()) def test_filter_single_player(self): player = self.create_player(games=[self.TicTacToe]) self.add_player_to_queue(player) self.assertItemsEqual([player], self.queue.filter(self.TicTacToe)) def test_filter_two_players_by_tic_tac_toe(self): player = self.create_player(games=[self.TicTacToe]) player_without_tic_tac_toe = self.create_player() self.add_player_to_queue(player) self.add_player_to_queue(player_without_tic_tac_toe) self.assertItemsEqual([player], self.queue.filter(self.TicTacToe)) def test_filter_two_player_by_chess(self): player = self.create_player(games=[self.Chess]) player_without_chess = self.create_player(games=[self.TicTacToe]) self.add_player_to_queue(player) self.add_player_to_queue(player_without_chess) self.assertItemsEqual([player], self.queue.filter(self.Chess)) def test_add_players_to_queue(self): number_of_players = 2 players = self.create_players(number_of_players) self.add_players_to_queue(players) self.assertEqual(number_of_players, self.get_length_of_queue()) def create_player(self, games=[], name="John"): player = Player(name) player.games = games return player def create_players(self, number_of_players, games=[]): players = [] for _ in range(number_of_players): player = self.create_player(games=games) players.append(player) return players def get_length_of_queue(self): return len(self.queue) def add_players_to_queue(self, players): self.queue.add_players(players) def add_player_to_queue(self, player): self.queue.add_player(player)
import pytest from core.queue import Queue, DequeQueue, EmptyQueueError @pytest.mark.parametrize("cls", [Queue, DequeQueue]) def test_init(cls): queue = Queue() assert len(queue) == 0 @pytest.mark.parametrize("queue", [Queue(), DequeQueue()]) def test_dequeue_empty(queue): with pytest.raises(EmptyQueueError): queue.dequeue() @pytest.mark.parametrize("queue", [Queue(), DequeQueue()]) def test_peek_empty(queue): with pytest.raises(EmptyQueueError): queue.peek() @pytest.mark.parametrize("queue", [Queue(), DequeQueue()]) def test_queue(queue): queue.enqueue(1) queue.enqueue(2) assert queue.peek() == 1 assert queue.dequeue() == 1
args = parser.parse_args() print("CoffinBot v{} Ctrl+C to stop".format(consts.version)) mark_read = [] failure_counter = 1 # 1 by default since it is the wait timer multiplier # Queue mode if args.queue: # Normal q = None else: # Use the queue from core.queue import Queue q = Queue() while True: try: failure = False if mark_read: # Needed to clear after a Reddit disconnection error reddit.inbox.mark_read(mark_read) mark_read.clear() # for all unread messages for message in reddit.inbox.unread(): # for all unread comments if message.was_comment: result = None # username mentions are simple if message.subject == "username mention": result = process_comment(reddit,
def setUp(self): self.TicTacToe = "TicTacToe" self.Chess = "Chess" self.queue = Queue()
def test_init(cls): queue = Queue() assert len(queue) == 0
class TestQueue(unittest.TestCase): """""" def setUp(self): """""" self.queue = Queue() self.queue.add_package(FILE_LIST) def test_delete_package(self): """""" package = self.queue.add_package(FILE_LIST) packages = self.queue.get_children() self.assertEqual(len(packages), 2) self.queue.delete(package) self.assertEqual(len(self.queue.get_children()), 1) self.assertEqual(self.queue.get_item(package.id), None) def test_delete_file(self): """""" package = self.queue.add_package(FILE_LIST) file1, file2 = self.queue.get_children(package.id) old_size = self.queue.get_item(package.id).total_size self.queue.delete(file1) size = self.queue.get_item(package.id).total_size self.assertEqual(size, old_size - file1.total_size) self.assertEqual(len(self.queue.get_children(package.id)), 1) self.assertEqual(self.queue.get_item(file1.id), None) self.queue.delete(file2) self.assertEqual(self.queue.get_item(file2.id), None) self.assertEqual(self.queue.get_item(package.id), None) def test_delete_link(self): """""" package = self.queue.add_package(FILE_LIST) file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) p_size = self.queue.get_item(package.id).total_size f_size = file1.total_size self.queue.delete(link1) size = self.queue.get_item(file1.id).total_size self.assertEqual(size, f_size - link1.total_size) size = self.queue.get_item(package.id).total_size self.assertEqual(size, p_size - link1.total_size) self.assertEqual(len(self.queue.get_children(file1.id)), 1) self.assertEqual(self.queue.get_item(link1.id), None) self.queue.delete(file2) self.queue.delete(link2) self.assertEqual(self.queue.get_item(link2.id), None) self.assertEqual(self.queue.get_item(file1.id), None) self.assertEqual(self.queue.get_item(package.id), None) def test_move_up_link(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) self.queue.move_up(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link4) self.assertEqual(link2, link3) self.queue.move_up(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) self.queue.move_up(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) def test_move_down_link(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) self.queue.move_down(link1) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link4) self.assertEqual(link2, link3) self.queue.move_down(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) self.queue.move_down(link2) link3, link4 = self.queue.get_children(file1.id) self.assertEqual(link1, link3) self.assertEqual(link2, link4) def test_move_up_file(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) self.queue.move_up(file2) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) self.queue.move_up(file2) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) def mtest_move_down_file(self): package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) self.queue.move_down(file1) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) self.queue.move_down(file1) file3, file4 = self.queue.get_children(package.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.assertEqual(file1, file4) self.assertEqual(file2, file3) self.assertEqual(link1, link7) self.assertEqual(link2, link8) self.assertEqual(link3, link5) self.assertEqual(link4, link6) def test_move_up_package(self): """""" self.queue.add_package(FILE_LIST) package1, package2 = self.queue.get_children() file1, file2 = self.queue.get_children(package1.id) file3, file4 = self.queue.get_children(package2.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.queue.move_up(package2) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) self.queue.move_up(package2) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) def test_move_down_package(self): """""" package_id = self.queue.add_package(FILE_LIST) package1, package2 = self.queue.get_children() file1, file2 = self.queue.get_children(package1.id) file3, file4 = self.queue.get_children(package2.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) link5, link6 = self.queue.get_children(file3.id) link7, link8 = self.queue.get_children(file4.id) self.queue.move_down(package1) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) self.queue.move_down(package1) package3, package4 = self.queue.get_children() file5, file6 = self.queue.get_children(package3.id) file7, file8 = self.queue.get_children(package4.id) link9, link10 = self.queue.get_children(file5.id) link11, link12 = self.queue.get_children(file6.id) link13, link14 = self.queue.get_children(file7.id) link15, link16 = self.queue.get_children(file8.id) self.assertEqual(package1, package4) self.assertEqual(package2, package3) self.assertEqual(file1, file7) self.assertEqual(file2, file8) self.assertEqual(file3, file5) self.assertEqual(file4, file6) self.assertEqual(link1, link13) self.assertEqual(link2, link14) self.assertEqual(link3, link15) self.assertEqual(link4, link16) self.assertEqual(link5, link9) self.assertEqual(link6, link10) self.assertEqual(link7, link11) self.assertEqual(link8, link12) def test_propagate_status(self): """""" package = self.queue.get_children().pop() file1, file2 = self.queue.get_children(package.id) link1, link2 = self.queue.get_children(file1.id) link3, link4 = self.queue.get_children(file2.id) #p : pend, f: pend pend, l: pend pend pend pend link1.set_status(cons.STATUS_ACTIVE) #p : active, f: active pend, l: active pend pend pend self.assertEqual(file1.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link2.set_status(cons.STATUS_WAIT) #p : active, f: active pend, l: active wait pend pend self.assertEqual(file1.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link1.set_status(cons.STATUS_PEND) #p : wait, f: wait pend, l: pend wait pend pend self.assertEqual(file1.status, cons.STATUS_WAIT) self.assertEqual(package.status, cons.STATUS_WAIT) link1.set_status(cons.STATUS_ACTIVE) #p : active, f: active pend, l: active wait pend pend link1.set_status(cons.STATUS_ERROR) link2.set_status(cons.STATUS_ERROR) #p : pend, f: error pend, l: error error pend pend self.assertEqual(file1.status, cons.STATUS_ERROR) self.assertEqual(package.status, cons.STATUS_PEND) link1.set_status(cons.STATUS_WAIT) #p : wait, f: wait pend, l: wait error pend pend self.assertEqual(file1.status, cons.STATUS_WAIT) self.assertEqual(package.status, cons.STATUS_WAIT) link1.set_status(cons.STATUS_CORRECT) link2.set_status(cons.STATUS_CORRECT) #p : pend, f: correct pend, l: correct correct pend pend self.assertEqual(file1.status, cons.STATUS_CORRECT) self.assertEqual(package.status, cons.STATUS_PEND) link3.set_status(cons.STATUS_ACTIVE) #p : active, f: correct active, l: correct correct active pend self.assertEqual(file2.status, cons.STATUS_ACTIVE) self.assertEqual(package.status, cons.STATUS_ACTIVE) link3.set_status(cons.STATUS_CORRECT) link4.set_status(cons.STATUS_CORRECT) #p : correct, f: correct correct, l: correct correct correct correct self.assertEqual(file2.status, cons.STATUS_CORRECT) self.assertEqual(package.status, cons.STATUS_CORRECT) def test_sort_status(self): tmp = self.queue.sort_status(cons.STATUS_CORRECT, cons.STATUS_STOP) self.assertEqual(tmp, cons.STATUS_STOP) tmp = self.queue.sort_status(cons.STATUS_STOP, cons.STATUS_ERROR) self.assertEqual(tmp, cons.STATUS_ERROR) tmp = self.queue.sort_status(cons.STATUS_ERROR, cons.STATUS_PEND) self.assertEqual(tmp, cons.STATUS_PEND) tmp = self.queue.sort_status(cons.STATUS_PEND, cons.STATUS_WAIT) self.assertEqual(tmp, cons.STATUS_WAIT) tmp = self.queue.sort_status(cons.STATUS_WAIT, cons.STATUS_ACTIVE) self.assertEqual(tmp, cons.STATUS_ACTIVE) def tearDown(self): """""" del self.queue