-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from chentex/add-ocm-jobs
Adds ocm jobs to cpt home page
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from datetime import date, datetime | ||
import pandas as pd | ||
from app.services.search import ElasticService | ||
|
||
|
||
async def getData(start_datetime: date, end_datetime: date, configpath: str): | ||
query = { | ||
"query": { | ||
"bool": { | ||
"filter": { | ||
"range": { | ||
"metrics.earliest": { | ||
"format": "yyyy-MM-dd" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
es = ElasticService(configpath=configpath) | ||
response = await es.post(query=query, start_date=start_datetime, end_date=end_datetime, timestamp_field='metrics.earliest') | ||
await es.close() | ||
tasks = [item['_source'] for item in response] | ||
jobs = pd.json_normalize(tasks) | ||
if len(jobs) == 0: | ||
return jobs | ||
|
||
if 'buildUrl' not in jobs.columns: | ||
jobs.insert(len(jobs.columns), "buildUrl", "") | ||
if 'ciSystem' not in jobs.columns: | ||
jobs.insert(len(jobs.columns), "ciSystem", "") | ||
jobs.fillna('', inplace=True) | ||
jobs['jobStatus'] = jobs.apply(convertJobStatus, axis=1) | ||
return jobs | ||
|
||
|
||
def fillCiSystem(row): | ||
currDate = datetime.strptime(row["metrics.earliest"][:26], '%Y-%m-%dT%H:%M:%S.%f') | ||
if currDate > datetime(2024, 6, 24): | ||
return "Jenkins" | ||
else: | ||
return "Airflow" | ||
|
||
|
||
def convertJobStatus(row): | ||
if row["metrics.success"] >= 0.80: | ||
return "success" | ||
elif row["metrics.success"] < 0.40: | ||
return "failure" | ||
else: | ||
return "unstable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from ....commons.ocm import getData | ||
from datetime import date | ||
|
||
|
||
################################################################ | ||
# This will return a DataFrame from OCM required by the CPT endpoint | ||
################################################################ | ||
async def ocmMapper(start_datetime: date, end_datetime: date): | ||
df = await getData(start_datetime, end_datetime, f'ocm.elasticsearch') | ||
if len(df) == 0: | ||
return df | ||
df.insert(len(df.columns), "product", "ocm") | ||
df.insert(len(df.columns), "releaseStream", "Nightly") | ||
df["testName"] = df["attack"] | ||
df["startDate"] = df["metrics.earliest"] | ||
df["endDate"] = df["metrics.end"] | ||
|
||
return df |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import json | ||
from fastapi import Response | ||
from datetime import datetime, timedelta, date | ||
from fastapi import APIRouter | ||
from ...commons.ocm import getData | ||
from ...commons.example_responses import ocp_200_response, response_422 | ||
from fastapi.param_functions import Query | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get('/api/v1/ocm/jobs', | ||
summary="Returns a job list", | ||
description="Returns a list of jobs in the specified dates. \ | ||
If not dates are provided the API will default the values. \ | ||
`startDate`: will be set to the day of the request minus 5 days.\ | ||
`endDate`: will be set to the day of the request.", | ||
responses={ | ||
200: ocp_200_response(), | ||
422: response_422(), | ||
},) | ||
async def jobs(start_date: date = Query(None, description="Start date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-10"]), | ||
end_date: date = Query(None, description="End date for searching jobs, format: 'YYYY-MM-DD'", examples=["2020-11-15"]), | ||
pretty: bool = Query(False, description="Output contet in pretty format.")): | ||
if start_date is None: | ||
start_date = datetime.utcnow().date() | ||
start_date = start_date - timedelta(days=5) | ||
|
||
if end_date is None: | ||
end_date = datetime.utcnow().date() | ||
|
||
if start_date > end_date: | ||
return Response(content=json.dumps({'error': "invalid date format, start_date must be less than end_date"}), status_code=422) | ||
|
||
results = await getData(start_date, end_date, 'ocm.elasticsearch') | ||
|
||
if len(results) >= 1: | ||
response = { | ||
'startDate': start_date.__str__(), | ||
'endDate': end_date.__str__(), | ||
'results': results.to_dict('records') | ||
} | ||
else: | ||
response = { | ||
'startDate': start_date.__str__(), | ||
'endDate': end_date.__str__(), | ||
'results': [] | ||
} | ||
|
||
if pretty: | ||
json_str = json.dumps(response, indent=4) | ||
return Response(content=json_str, media_type='application/json') | ||
|
||
jsonstring = json.dumps(response) | ||
return jsonstring |