Skip to content

Commit fcf0821

Browse files
committed
Adds ocm jobs to cpt home page
Signed-off-by: Vicente Zepeda Mas <[email protected]>
1 parent c13254c commit fcf0821

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

backend/app/api/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from app.api.v1.endpoints.quay import quayGraphs
1111
from app.api.v1.endpoints.telco import telcoJobs
1212
from app.api.v1.endpoints.telco import telcoGraphs
13+
from app.api.v1.endpoints.ocm import ocmJobs
1314

1415

1516
router = APIRouter()
@@ -35,3 +36,6 @@
3536

3637
# Horreum endpoint
3738
router.include_router(horreum.router, tags=['horreum'])
39+
40+
# OCM endpoint
41+
router.include_router(ocmJobs.router, tags=['ocm'])

backend/app/api/v1/commons/ocm.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from datetime import date, datetime
2+
import pandas as pd
3+
from app.services.search import ElasticService
4+
5+
6+
async def getData(start_datetime: date, end_datetime: date, configpath: str):
7+
query = {
8+
"query": {
9+
"bool": {
10+
"filter": {
11+
"range": {
12+
"metrics.earliest": {
13+
"format": "yyyy-MM-dd"
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}
20+
21+
es = ElasticService(configpath=configpath)
22+
response = await es.post(query=query, start_date=start_datetime, end_date=end_datetime, timestamp_field='metrics.earliest')
23+
await es.close()
24+
tasks = [item['_source'] for item in response]
25+
jobs = pd.json_normalize(tasks)
26+
if len(jobs) == 0:
27+
return jobs
28+
29+
if 'buildUrl' not in jobs.columns:
30+
jobs.insert(len(jobs.columns), "buildUrl", "")
31+
jobs.fillna('', inplace=True)
32+
jobs['ciSystem'] = jobs.apply(fillCiSystem, axis=1)
33+
jobs['jobStatus'] = jobs.apply(convertJobStatus, axis=1)
34+
return jobs
35+
36+
37+
def fillCiSystem(row):
38+
currDate = datetime.strptime(row["metrics.earliest"][:26], '%Y-%m-%dT%H:%M:%S.%f')
39+
if currDate > datetime(2024, 6, 24):
40+
return "Jenkins"
41+
else:
42+
return "Airflow"
43+
44+
45+
def convertJobStatus(row):
46+
if row["metrics.success"] >= 0.80:
47+
return "success"
48+
elif row["metrics.success"] < 0.40:
49+
return "failure"
50+
else:
51+
return "unstable"

backend/app/api/v1/endpoints/cpt/cptJobs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .maps.quay import quayMapper
1111
from .maps.hce import hceMapper
1212
from .maps.telco import telcoMapper
13+
from .maps.ocm import ocmMapper
1314
from ...commons.example_responses import cpt_200_response, response_422
1415
from fastapi.param_functions import Query
1516

@@ -20,8 +21,10 @@
2021
"quay": quayMapper,
2122
"hce": hceMapper,
2223
"telco": telcoMapper,
24+
"ocm": ocmMapper,
2325
}
2426

27+
2528
@router.get('/api/v1/cpt/jobs',
2629
summary="Returns a job list from all the products.",
2730
description="Returns a list of jobs in the specified dates. \
@@ -69,6 +72,7 @@ async def jobs(start_date: date = Query(None, description="Start date for search
6972
jsonstring = json.dumps(response)
7073
return jsonstring
7174

75+
7276
async def fetch_product_async(product, start_date, end_date):
7377
try:
7478
df = await products[product](start_date, end_date)
@@ -79,5 +83,6 @@ async def fetch_product_async(product, start_date, end_date):
7983
print(f"Error in mapper for product {product}: {e}")
8084
return pd.DataFrame()
8185

86+
8287
def fetch_product(product, start_date, end_date):
8388
return asyncio.run(fetch_product_async(product, start_date, end_date))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from ....commons.ocm import getData
2+
from datetime import date
3+
4+
5+
################################################################
6+
# This will return a DataFrame from OCM required by the CPT endpoint
7+
################################################################
8+
async def ocmMapper(start_datetime: date, end_datetime: date):
9+
df = await getData(start_datetime, end_datetime, f'ocm.elasticsearch')
10+
if len(df) == 0:
11+
return df
12+
df.insert(len(df.columns), "product", "ocm")
13+
df.insert(len(df.columns), "releaseStream", "Nightly")
14+
df["testName"] = df["attack"]
15+
df["startDate"] = df["metrics.earliest"]
16+
df["endDate"] = df["metrics.end"]
17+
18+
return df
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
from fastapi import Response
3+
from datetime import datetime, timedelta, date
4+
from fastapi import APIRouter
5+
from ...commons.ocm import getData
6+
from ...commons.example_responses import ocp_200_response, response_422
7+
from fastapi.param_functions import Query
8+
9+
router = APIRouter()
10+
11+
12+
@router.get('/api/v1/ocm/jobs',
13+
summary="Returns a job list",
14+
description="Returns a list of jobs in the specified dates. \
15+
If not dates are provided the API will default the values. \
16+
`startDate`: will be set to the day of the request minus 5 days.\
17+
`endDate`: will be set to the day of the request.",
18+
responses={
19+
200: ocp_200_response(),
20+
422: response_422(),
21+
},)
22+
async def jobs(start_date: date = Query(None, description="Start date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-10"]),
23+
end_date: date = Query(None, description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"]),
24+
pretty: bool = Query(False, description="Output contet in pretty format.")):
25+
if start_date is None:
26+
start_date = datetime.utcnow().date()
27+
start_date = start_date - timedelta(days=5)
28+
29+
if end_date is None:
30+
end_date = datetime.utcnow().date()
31+
32+
if start_date > end_date:
33+
return Response(content=json.dumps({'error': "invalid date format, start_date must be less than end_date"}), status_code=422)
34+
35+
results = await getData(start_date, end_date, 'ocm.elasticsearch')
36+
37+
if len(results) >= 1:
38+
response = {
39+
'startDate': start_date.__str__(),
40+
'endDate': end_date.__str__(),
41+
'results': results.to_dict('records')
42+
}
43+
else:
44+
response = {
45+
'startDate': start_date.__str__(),
46+
'endDate': end_date.__str__(),
47+
'results': []
48+
}
49+
50+
if pretty:
51+
json_str = json.dumps(response, indent=4)
52+
return Response(content=json_str, media_type='application/json')
53+
54+
jsonstring = json.dumps(response)
55+
return jsonstring

0 commit comments

Comments
 (0)