Skip to content

Commit

Permalink
[cli/api] detailed gives job types
Browse files Browse the repository at this point in the history
  • Loading branch information
adfaure committed Dec 21, 2023
1 parent 0aca4ef commit 69c5b36
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 9 deletions.
7 changes: 7 additions & 0 deletions oar/api/routers/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
)


def attach_types(job, job_types):
if job["id"] in job_types:
job["types"] = job_types[job["id"]]


def attach_resources(job, jobs_resources):
job["resources"] = []
for resource in jobs_resources[job["id"]]:
Expand Down Expand Up @@ -75,9 +80,11 @@ def index(

if details:
jobs_resources = queryCollection.get_assigned_jobs_resources(page.items)
jobs_types = queryCollection.get_jobs_types(page.items)
pass
for item in page:
if details:
attach_types(item, jobs_types)
attach_resources(item, jobs_resources)
attach_nodes(item, jobs_resources)
data["items"].append(item)
Expand Down
11 changes: 11 additions & 0 deletions oar/cli/oarstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,17 @@ def print_jobs(
)
job.network_adresses = nodes

jobs_types = queryCollection.get_jobs_types(jobs)
for job in jobs:
if job.id in jobs_types:
types = []
for job_type, value in jobs_types[job.id].items():
if type(value) == bool:
types.append(f"{job_type}")
else:
types.append(f"{job_type}={value}")
job.types = ", ".join(types)

if format:
to_dump = {}
# to_dict() doesn't incorporate attributes not defined in the class, thus the dict merging
Expand Down
24 changes: 24 additions & 0 deletions oar/lib/basequery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from sqlalchemy import text
from sqlalchemy.orm import Load

from oar.lib.job_handling import parse_job_type

# from . import db
from .exceptions import DoesNotExist
from .models import (
AssignedResource,
GanttJobsPredictionsVisu,
GanttJobsResourcesVisu,
Job,
JobType,
MoldableJobDescription,
Resource,
)
Expand Down Expand Up @@ -214,6 +217,27 @@ def get_assigned_jobs_resources(self, jobs):
)
return self.groupby_jobs_resources(jobs, query)

def get_jobs_types(self, jobs):
"""Returns the list of assigned resources associated to the job passed
in parameter."""
db = self.session

results = (
db.query(JobType)
.filter(JobType.job_id.in_([job.id for job in jobs]))
.order_by(JobType.job_id)
.all()
)

from itertools import groupby

res = {}
for job_id, group_types in groupby(results, lambda type: type.job_id):
types_map = parse_job_type([j.type for j in list(group_types)])
res[job_id] = types_map

return res

def get_assigned_one_job_resources(self, job):
"""Returns the list of assigned resources associated to the job passed
in parameter."""
Expand Down
19 changes: 13 additions & 6 deletions oar/lib/job_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import random
import re
from typing import List

from procset import ProcSet
from sqlalchemy import distinct, func, insert, text
Expand Down Expand Up @@ -1572,13 +1573,9 @@ def get_job_current_hostnames(session, job_id):
return [r[0] for r in results]


def get_job_types(session, job_id):
"""Returns a hash table with all types for the given job ID."""

results = session.query(JobType.type).filter(JobType.job_id == job_id).all()
def parse_job_type(job_types: List[str]):
res = {}
for t in results:
typ = t[0]
for typ in job_types:
match = re.match(r"^\s*(token)\s*\:\s*(\w+)\s*=\s*(\d+)\s*$", typ)
if match:
res[match.group(1)] = {match.group(2): match.group(3)}
Expand All @@ -1591,6 +1588,16 @@ def get_job_types(session, job_id):
return res


def get_job_types(session, job_id):
"""Returns a hash table with all types for the given job ID."""

results = session.query(JobType.type).filter(JobType.job_id == job_id).all()
# Pay attention to the comma that unpack the tuple of one element return by the request
res = parse_job_type([t for t, in results])

return res


def add_current_job_types(session, job_id, j_type):
req = insert(JobType).values({"job_id": job_id, "type": j_type})
session.execute(req)
Expand Down
33 changes: 32 additions & 1 deletion tests/api/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,16 @@ def test_jobs_get_all(client, minimal_db_initialization):

def test_app_jobs_details(client, minimal_db_initialization):
insert_job(
minimal_db_initialization, res=[(60, [("resource_id=4", "")])], properties=""
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
properties="",
types=["be", "test=value"],
)
insert_job(
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
properties="",
types=["cosystem"],
)
res = client.get("/jobs?details=details")

Expand Down Expand Up @@ -676,3 +685,25 @@ def test_app_job_post_ar(

assert job_ids == []
assert res.status_code == 403


def test_app_jobs_details_check_types(client, minimal_db_initialization):
insert_job(
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
properties="",
types=["be", "test=value"],
)
insert_job(
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
properties="",
types=["cosystem"],
)
res = client.get("/jobs?details=details")

print(res.json(), len(res.json()))

assert res.json()["items"][0]["types"]["test"] == "value"
assert res.json()["items"][0]["types"]["be"]
assert res.json()["items"][1]["types"]["cosystem"]
7 changes: 5 additions & 2 deletions tests/cli/test_oardel.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,15 @@ def test_oardel_simple_cosystem(minimal_db_initialization, setup_config):
job_id = insert_job(
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
types=["cosystem"],
types=["cosystem", "lol"],
state="Running",
)
runner = CliRunner()
result = runner.invoke(
cli, ["-s", "USR1", str(job_id)], obj=(minimal_db_initialization, config)
cli,
["-s", "USR1", str(job_id)],
obj=(minimal_db_initialization, config),
catch_exceptions=True,
)
print(result.output)
import traceback
Expand Down
25 changes: 25 additions & 0 deletions tests/cli/test_oarstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,3 +563,28 @@ def test_oarstat_job_id_error(minimal_db_initialization, setup_config):
except ValueError:
assert False
assert result.exit_code == 0


def test_oarstat_job_types(minimal_db_initialization, setup_config):
jid = insert_job(
minimal_db_initialization,
res=[(60, [("resource_id=4", "")])],
user="toto",
types=["inner=4", "cosystem"],
properties="",
)

runner = CliRunner()
result = runner.invoke(
cli,
["-j", f"{jid}", "-f"],
obj=minimal_db_initialization,
catch_exceptions=False,
)
print(result.output)
for line in result.output.splitlines():
if line.startswith(" types = "):
line = line.strip()
assert line == "types = inner=4, cosystem"

assert result.exit_code == 0

0 comments on commit 69c5b36

Please sign in to comment.