-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* task metrics * types update * Update useCreateTask.ts * review changes * review updates * update to MUI breakpoints * Review updates * Update TaskCompletionHandler.js
- Loading branch information
1 parent
1ee66fb
commit 12a9021
Showing
22 changed files
with
234 additions
and
9 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
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
79 changes: 79 additions & 0 deletions
79
packages/datatrak-web-server/src/routes/TaskMetricsRoute.ts
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,79 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { Request } from 'express'; | ||
import { Route } from '@tupaia/server-boilerplate'; | ||
import { getOffsetForTimezone } from '@tupaia/utils'; | ||
import { DatatrakWebTaskMetricsRequest, TaskStatus } from '@tupaia/types'; | ||
import { QUERY_CONJUNCTIONS, RECORDS } from '@tupaia/database'; | ||
|
||
export type TaskMetricsRequest = Request< | ||
DatatrakWebTaskMetricsRequest.Params, | ||
DatatrakWebTaskMetricsRequest.ResBody, | ||
DatatrakWebTaskMetricsRequest.ReqBody, | ||
DatatrakWebTaskMetricsRequest.ReqQuery | ||
>; | ||
|
||
export class TaskMetricsRoute extends Route<TaskMetricsRequest> { | ||
public async buildResponse() { | ||
const { params, models } = this.req; | ||
const { projectId } = params; | ||
const baseQuery = { 'survey.project_id': projectId }; | ||
const baseJoin = { joinWith: RECORDS.SURVEY, joinCondition: ['survey.id', 'task.survey_id'] }; | ||
|
||
const unassignedTasks = await models.task.count( | ||
{ | ||
...baseQuery, | ||
[QUERY_CONJUNCTIONS.RAW]: { | ||
sql: `assignee_id IS NULL`, | ||
}, | ||
}, | ||
baseJoin, | ||
); | ||
|
||
const overdueTasks = await models.task.count( | ||
{ | ||
...baseQuery, | ||
due_date: { | ||
comparator: '<=', | ||
comparisonValue: new Date().getTime(), | ||
}, | ||
}, | ||
baseJoin, | ||
); | ||
|
||
const completedTasks = await models.task.find( | ||
// @ts-ignore | ||
{ | ||
...baseQuery, | ||
status: TaskStatus.completed, | ||
[QUERY_CONJUNCTIONS.RAW]: { | ||
sql: `repeat_schedule IS NULL`, | ||
}, | ||
}, | ||
{ | ||
columns: ['due_date', 'data_time', 'timezone', 'project_id'], | ||
}, | ||
); | ||
|
||
const onTimeCompletedTasks = completedTasks.filter(record => { | ||
if (!record.due_date || !record.data_time) { | ||
return false; | ||
} | ||
const { data_time: dataTime, timezone } = record; | ||
const offset = getOffsetForTimezone(timezone, new Date(dataTime)); | ||
const formattedDate = `${dataTime.toString().replace(' ', 'T')}${offset}`; | ||
return new Date(formattedDate).getTime() <= record.due_date; | ||
}); | ||
|
||
const onTimeCompletionRate = (completedTasks.length / onTimeCompletedTasks.length) * 100 || 0; | ||
|
||
return { | ||
unassignedTasks, | ||
overdueTasks, | ||
onTimeCompletionRate, | ||
}; | ||
} | ||
} |
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
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
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 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2024 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { useQuery } from 'react-query'; | ||
import { DatatrakWebTaskMetricsRequest } from '@tupaia/types'; | ||
import { get } from '../api'; | ||
|
||
export const useTaskMetrics = (projectId?: string) => { | ||
return useQuery( | ||
['taskMetric', projectId], | ||
(): Promise<DatatrakWebTaskMetricsRequest.ResBody> => get(`taskMetrics/${projectId}`), | ||
{ | ||
enabled: !!projectId, | ||
}, | ||
); | ||
}; |
51 changes: 51 additions & 0 deletions
51
packages/datatrak-web/src/components/TaskMetrics/TaskMetric.tsx
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,51 @@ | ||
import { SpinningLoader } from '@tupaia/ui-components'; | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
const MetricWrapper = styled.div` | ||
display: flex; | ||
border: 1px solid; | ||
border-radius: 3px; | ||
margin-inline: 0.5rem; | ||
margin-block-end: auto; | ||
${({ theme }) => theme.breakpoints.down('xs')} { | ||
width: inherit; | ||
margin-block-start: 0.5rem; | ||
margin-inline: 0; | ||
} | ||
`; | ||
|
||
const MetricNumber = styled.p` | ||
font-size: 0.875rem; | ||
line-height: 1.75; | ||
padding: 0.5rem 1.75rem; | ||
border-right: 1px solid ${({ theme }) => theme.palette.divider}; | ||
min-width: 3rem; | ||
padding-inline: 0.9rem; | ||
align-content: center; | ||
text-align: center; | ||
font-weight: 500; | ||
margin: 0; | ||
`; | ||
|
||
const MetricText = styled.p` | ||
line-height: 1.75; | ||
letter-spacing: 0; | ||
padding: 0.5rem 1.75rem; | ||
padding-inline-end: 1.2rem; | ||
padding-inline-start: 0.9rem; | ||
font-weight: 500; | ||
margin: 0; | ||
${({ theme }) => theme.breakpoints.up('lg')} { | ||
min-width: 12rem; | ||
} | ||
`; | ||
|
||
export const TaskMetric = ({ number, text, isLoading }) => { | ||
return ( | ||
<MetricWrapper> | ||
<MetricNumber>{isLoading ? <SpinningLoader spinnerSize={14} /> : number}</MetricNumber> | ||
<MetricText>{text}</MetricText> | ||
</MetricWrapper> | ||
); | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/datatrak-web/src/components/TaskMetrics/TaskMetrics.tsx
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,33 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { TaskMetric } from './TaskMetric'; | ||
import { useCurrentUserContext, useTaskMetrics } from '../../api'; | ||
|
||
const TaskMetricsContainer = styled.div` | ||
margin-block-end: 0; | ||
gap: 0.5rem; | ||
${({ theme }) => theme.breakpoints.up('xs')} { | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
} | ||
${({ theme }) => theme.breakpoints.down('xs')} { | ||
width: inherit; | ||
} | ||
`; | ||
|
||
export const TaskMetrics = () => { | ||
const { projectId } = useCurrentUserContext(); | ||
const { data: metrics, isLoading } = useTaskMetrics(projectId); | ||
return ( | ||
<TaskMetricsContainer> | ||
<TaskMetric text="Unassigned tasks" number={metrics?.unassignedTasks} isLoading={isLoading} /> | ||
<TaskMetric text="Overdue tasks" number={metrics?.overdueTasks} isLoading={isLoading} /> | ||
<TaskMetric | ||
text="On-time completion rate" | ||
number={`${metrics?.onTimeCompletionRate}%`} | ||
isLoading={isLoading} | ||
/> | ||
</TaskMetricsContainer> | ||
); | ||
}; |
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,6 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
export { TaskMetrics } from './TaskMetrics'; |
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
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
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
8 changes: 8 additions & 0 deletions
8
packages/types/src/types/requests/datatrak-web-server/TaskMetricsRequest.ts
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,8 @@ | ||
export type Params = Record<string, never>; | ||
export interface ResBody { | ||
unassignedTasks: number; | ||
overdueTasks: number; | ||
onTimeCompletionRate: number; | ||
} | ||
export type ReqBody = Record<string, never>; | ||
export type ReqQuery = Record<string, never>; |
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
Oops, something went wrong.