Skip to content

Commit

Permalink
Merge pull request #257 from Tauffer-Consulting/fix/workflows-table
Browse files Browse the repository at this point in the history
Fix/workflows table
  • Loading branch information
vinicvaz authored Mar 21, 2024
2 parents 90ab882 + 1108648 commit d81b131
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, {
useState,
} from "react";
import { useNavigate } from "react-router-dom";
import { secondsToHMS } from "utils";

import { States } from "./States";
import { WorkflowRunTableFooter } from "./WorkflowRunTableFooter";
Expand Down Expand Up @@ -78,13 +79,13 @@ export const WorkflowRunsTable = forwardRef<WorkflowRunsTableRef, Props>(
valueFormatter: ({ value }) => new Date(value).toLocaleString(),
},
{
field: "execution_date",
headerName: "Execution Date",
field: "duration_in_seconds",
headerName: "Duration",
headerAlign: "center",
align: "center",
minWidth: 150,
flex: 1,
valueFormatter: ({ value }) => new Date(value).toLocaleString(),
valueFormatter: ({ value }) => (value ? secondsToHMS(value) : "N/A"),
},
{
field: "state",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export const WorkflowList: React.FC = () => {
align: "center",
headerAlign: "center",
sortable: false,
minWidth: 100,
},
{ field: "name", headerName: "Workflow Name", flex: 2 },
{ field: "name", headerName: "Workflow Name", flex: 2, minWidth: 220 },
{
field: "start_date",
headerName: (
Expand All @@ -104,15 +105,35 @@ export const WorkflowList: React.FC = () => {
) as any,
flex: 1,
align: "center",
minWidth: 220,

valueFormatter: ({ value }) => new Date(value).toLocaleString(),
headerAlign: "center",
},
{
field: "end_date",
headerName: (
<Tooltip title="End Date is the date when your workflow stop to be scheduled. You cannot run workflows after end date.">
<span style={{ display: "flex", alignItems: "center" }}>
End Date{" "}
<InfoOutlined style={{ marginLeft: "5px" }} fontSize="small" />
</span>
</Tooltip>
) as any,
headerAlign: "center",
align: "center",
type: "string",
flex: 1,
minWidth: 220,
valueFormatter: ({ value }) =>
value ? new Date(value).toLocaleString() : "None",
},
{
field: "created_at",
headerName: "Created At",
flex: 1,
align: "center",
minWidth: 220,

valueFormatter: ({ value }) => new Date(value).toLocaleString(),
headerAlign: "center",
Expand All @@ -124,6 +145,7 @@ export const WorkflowList: React.FC = () => {
align: "center",
valueFormatter: ({ value }) => new Date(value).toLocaleString(),
headerAlign: "center",
minWidth: 220,
},
{
field: "schedule",
Expand All @@ -132,13 +154,15 @@ export const WorkflowList: React.FC = () => {
align: "center",
headerAlign: "center",
sortable: false,
minWidth: 100,
},
{
field: "next_dagrun",
headerName: "Next Run",
flex: 1,
align: "center",
headerAlign: "center",
minWidth: 220,
sortable: false,
valueFormatter: ({ value }) =>
value ? new Date(value).toLocaleString() : "none",
Expand Down Expand Up @@ -168,6 +192,7 @@ export const WorkflowList: React.FC = () => {
headerAlign: "center",
align: "center",
sortable: false,
minWidth: 150,
},
],
[],
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/utils/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function secondsToHMS(totalSeconds: number) {
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds % 3600) / 60);
const seconds = Math.floor(totalSeconds % 60);

const formattedHours = hours < 10 ? "0" + hours : hours;
const formattedMinutes = minutes < 10 ? "0" + minutes : minutes;
const formattedSeconds = seconds < 10 ? "0" + seconds : seconds;

return formattedHours + ":" + formattedMinutes + ":" + formattedSeconds;
}
1 change: 1 addition & 0 deletions frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { useInterval } from "./useInterval";
export { useMouseProximity } from "./useMouseProximity";
export { exportToJson } from "./downloadJson";
export { isEmpty } from "./isEmpty";
export { secondsToHMS } from "./datetime";
2 changes: 2 additions & 0 deletions rest/schemas/responses/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class GetWorkflowsResponseData(BaseModel):
name: str
created_at: datetime
start_date: datetime
end_date: Optional[datetime]
last_changed_at: datetime
last_changed_by: int
created_by: int
Expand Down Expand Up @@ -153,6 +154,7 @@ class GetWorkflowRunsResponseData(BaseModel):
start_date: Optional[datetime] = None
end_date: Optional[datetime] = None
execution_date: Optional[datetime] = None
duration_in_seconds: Optional[float] = None
state: Optional[WorkflowRunState] = None

@field_validator('state')
Expand Down
20 changes: 16 additions & 4 deletions rest/services/workflow_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async def list_workflows(
name=dag_data.name,
created_at=dag_data.created_at,
start_date=dag_data.start_date,
end_date=dag_data.end_date,
last_changed_at=dag_data.last_changed_at,
last_changed_by=dag_data.last_changed_by,
created_by=dag_data.created_by,
Expand Down Expand Up @@ -520,9 +521,12 @@ def run_workflow(self, workflow_id: int):
raise ResourceNotFoundException("Workflow not found")

# Check if start date is in the past
if workflow.start_date and workflow.start_date > datetime.utcnow().replace(tzinfo=timezone.utc):
if workflow.start_date and workflow.start_date > datetime.now(tz=timezone.utc):
raise ForbiddenException('Workflow start date is in the future. Can not run it now.')

if workflow.end_date and workflow.end_date < datetime.now(tz=timezone.utc):
raise ForbiddenException('You cannot run workflows that have ended.')

airflow_workflow_id = workflow.uuid_name

# Force unpause workflow
Expand Down Expand Up @@ -641,9 +645,17 @@ def list_workflow_runs(self, workflow_id: int, page: int, page_size: int):
else:
dag_runs = response_data['dag_runs']

data = [
GetWorkflowRunsResponseData(**run) for run in dag_runs
]
data = []
for run in dag_runs:
#duration = run.get('end_date') - run.get('start_date')
end_date_dt = datetime.fromisoformat(run.get('end_date'))
start_date_dt = datetime.fromisoformat(run.get('start_date'))
duration = end_date_dt - start_date_dt
run['duration_in_seconds'] = duration.total_seconds()
data.append(
GetWorkflowRunsResponseData(**run)
)

response = GetWorkflowRunsResponse(
data=data,
metadata=dict(
Expand Down

0 comments on commit d81b131

Please sign in to comment.