views: select the most used repo in Set repository view

Preselect the most used repository among the selected clients
for the view Commands -> Setup -> Set repository

Change get_clients_oglive() into def get_client_list_by_key()
to obtain a dictionary with the list of ips of the clients with
the same value in the field of the client payload passed as key.
For example for the key 'repo_id' it would return a dictionary
{repo_id: [ips of the clients with that repo_id]}
master v1.1.3-36
Alejandro Sirgo Rica 2024-12-13 13:00:24 +01:00
parent c7c28d6e92
commit fd8da5de26
1 changed files with 26 additions and 10 deletions

View File

@ -2470,8 +2470,8 @@ def action_mode():
selected_clients=selected_clients,
clients=clients, modes_set=modes_set)
def get_clients_oglive(ips, server):
oglives = {}
def get_client_list_by_key(ips, server, key):
ret = {}
for ip in ips:
r = server.get('/client/info', payload={"client": [ip]})
if not r:
@ -2479,12 +2479,12 @@ def get_clients_oglive(ips, server):
if r.status_code != requests.codes.ok:
raise ServerErrorCode
resp = r.json()
oglive = resp['livedir']
if oglive not in oglives:
oglives[oglive] = [ip]
val = resp[key]
if val not in ret:
ret[val] = [ip]
else:
oglives[oglive].append(ip)
return oglives
ret[val].append(ip)
return ret
@app.route('/action/oglive', methods=['GET', 'POST'])
@login_required
@ -2511,7 +2511,7 @@ def action_oglive():
server = get_server_from_clients(list(ips))
try:
oglives_set = get_clients_oglive(ips, server)
oglives_set = get_client_list_by_key(ips, server, key='livedir')
except ServerError:
return ogserver_down('commands')
except ServerErrorCode:
@ -2601,9 +2601,25 @@ def action_repo_set():
except ServerErrorCode:
return ogserver_error(r, 'commands')
form.repo.choices = [(repo["id"], repo["name"]) for repo in repositories]
scopes, clients = get_scopes(set(ips))
try:
repo_set = get_client_list_by_key(ips, server, key='repo_id')
except ServerError:
return ogserver_down('commands')
except ServerErrorCode:
return ogserver_error(r, 'commands')
most_used_repo_id = max(repo_set, key=lambda id: len(repo_set[id]))
form.repo.choices = []
for repo in repositories:
repo_entry = (repo["id"], repo["name"])
if repo["id"] == most_used_repo_id:
form.repo.choices.insert(0, repo_entry)
else:
form.repo.choices.append(repo_entry)
selected_clients = list(get_selected_clients(scopes['scope']).items())
return render_template('actions/repo_set.html', repos_set=repos_set, form=form, scopes=scopes,
selected_clients=selected_clients)