Skip to content

Commit

Permalink
truncate execution output in default view
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Nov 9, 2021
1 parent 5703402 commit 75ac753
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
48 changes: 47 additions & 1 deletion dkron/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (h *HTTPTransport) APIRoutes(r *gin.RouterGroup, middleware ...gin.HandlerF
// Place fallback routes last
jobs.GET("/:job", h.jobGetHandler)
jobs.GET("/:job/executions", h.executionsHandler)
jobs.GET("/:job/execution/:execution", h.executionHandler)
}

// MetaMiddleware adds middleware to the gin Context.
Expand Down Expand Up @@ -336,6 +337,10 @@ func (h *HTTPTransport) executionsHandler(c *gin.Context) {
sort = "started_at"
}
order := c.DefaultQuery("_order", "DESC")
output_size, err := strconv.Atoi(c.DefaultQuery("output_size", ""))
if err != nil {
output_size = -1
}

job, err := h.agent.Store.GetJob(jobName, nil)
if err != nil {
Expand All @@ -357,14 +362,55 @@ func (h *HTTPTransport) executionsHandler(c *gin.Context) {
return
}

if output_size > 0 {
// truncate execution output
for _, execution := range executions {
_s := len(execution.Output)
if _s > output_size {
execution.Output = execution.Output[_s-output_size:]
}
}
}

c.Header("X-Total-Count", strconv.Itoa(len(executions)))
renderJSON(c, http.StatusOK, executions)
}

func (h *HTTPTransport) executionHandler(c *gin.Context) {
jobName := c.Param("job")
executionName := c.Param("execution")

job, err := h.agent.Store.GetJob(jobName, nil)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}

executions, err := h.agent.Store.GetExecutions(job.Name,
&ExecutionOptions{
Sort: "",
Order: "",
Timezone: job.GetTimeLocation(),
},
)

if err != nil {
h.logger.Error(err)
return
}

for _, execution := range executions {
if execution.Id == executionName {
renderJSON(c, http.StatusOK, execution)
return
}
}
}

type MId struct {
serf.Member

Id string `json:"id"`
Id string `json:"id"`
StatusText string `json:"statusText"`
}

Expand Down
1 change: 1 addition & 0 deletions ui/src/dataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const myDataProvider = {
_order: order,
_start: (page - 1) * perPage,
_end: page * perPage,
output_size: 200,
};
const url = `${apiUrl}/${params.target}/${params.id}/${resource}?${stringify(query)}`;

Expand Down
50 changes: 48 additions & 2 deletions ui/src/jobs/JobShow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import {
TabbedShowLayout,
Tab,
ReferenceManyField,
useNotify, useRedirect, fetchStart, fetchEnd, Button,
} from 'react-admin';
import { OutputPanel } from "../executions/BusyList";
import ToggleButton from "./ToggleButton"
import RunButton from "./RunButton"
import { JsonField } from "react-admin-json-view";
import ZeroDateField from "./ZeroDateField";
import JobIcon from '@material-ui/icons/Update';
import FullIcon from '@material-ui/icons/BatteryFull';
import { Tooltip } from '@material-ui/core';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import { apiUrl } from '../dataProvider';

const JobShowActions = ({ basePath, data, resource }: any) => (
<TopToolbar>
Expand All @@ -32,6 +36,48 @@ const SuccessField = (props: any) => {
return (props.record["finished_at"] === null ? <Tooltip title="Running"><JobIcon /></Tooltip> : <BooleanField {...props} />);
};

const FullButton = ({record}: any) => {
const dispatch = useDispatch();
const notify = useNotify();
const [loading, setLoading] = useState(false);
const handleClick = () => {
setLoading(true);
dispatch(fetchStart()); // start the global loading indicator
fetch(`${apiUrl}/jobs/${record.job_name}/execution/${record.id}`)
.then((response) => {
if (response.ok) {
notify('Success loading full output');
return response.json()
}
throw response
})
.then((data) => {
record.output = data.output
})
.catch((e) => {
notify('Error on loading full output', 'warning')
})
.finally(() => {
setLoading(false);
dispatch(fetchEnd()); // stop the global loading indicator
});
};
return (
<Button
label="Load full output"
onClick={handleClick}
disabled={loading}
>
<FullIcon/>
</Button>
);
};

const SpecialOutputPanel = ({ id, record, resource }: any) => {
// FIXME: hide button if not required
return (<div className="execution-output"><div><FullButton record={record} /></div>{record.output || "Empty output"}</div>);
};

const JobShow = (props: any) => (
<Show actions={<JobShowActions {...props}/>} {...props}>
<TabbedShowLayout>
Expand Down Expand Up @@ -82,7 +128,7 @@ const JobShow = (props: any) => (
</Tab>
<Tab label="executions" path="executions">
<ReferenceManyField reference="executions" target="jobs" label="Executions">
<Datagrid rowClick="expand" isRowSelectable={ record => false } expand={<OutputPanel {...props} />}>
<Datagrid rowClick="expand" isRowSelectable={ record => false } expand={<SpecialOutputPanel {...props} />}>
<TextField source="id" />
<TextField source="group" sortable={false} />
<TextField source="job_name" sortable={false} />
Expand Down

0 comments on commit 75ac753

Please sign in to comment.