Skip to content

Commit

Permalink
Fix/request date export (#690)
Browse files Browse the repository at this point in the history
* Added date column to minimal export, only update fields of task state when it has not been created before

Signed-off-by: Aaron Chong <[email protected]>

* Use create and get_or_none, and save model if it exists

Signed-off-by: Aaron Chong <[email protected]>

---------

Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth authored Mar 20, 2023
1 parent 7ea986d commit a1ea778
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
59 changes: 36 additions & 23 deletions packages/api-server/api_server/repositories/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,48 @@ def __init__(self, user: User):
self.user = user

async def save_task_state(self, task_state: TaskState) -> None:
previous_task_state = await DbTaskState.get_or_none(id_=task_state.booking.id)
datetime_request_time = datetime.now()
task_state.booking.unix_millis_request_time = round(time.time() * 1000)

if previous_task_state is not None:
db_task_state = await DbTaskState.get_or_none(id_=task_state.booking.id)
if db_task_state is not None:
task_state.booking.unix_millis_request_time = (
int(round(previous_task_state.unix_millis_request_time.timestamp()))
* 1000
int(round(db_task_state.unix_millis_request_time.timestamp())) * 1000
)
datetime_request_time = previous_task_state.unix_millis_request_time

await ttm.TaskState.update_or_create(
{
"data": task_state.json(),
"category": task_state.category.__root__
if task_state.category
else None,
"assigned_to": task_state.assigned_to.name
db_task_state.update_from_dict(
{
"data": task_state.json(),
"category": task_state.category.__root__
if task_state.category
else None,
"assigned_to": task_state.assigned_to.name
if task_state.assigned_to
else None,
"unix_millis_start_time": task_state.unix_millis_start_time
and datetime.fromtimestamp(
task_state.unix_millis_start_time / 1000
),
"unix_millis_finish_time": task_state.unix_millis_finish_time
and datetime.fromtimestamp(
task_state.unix_millis_finish_time / 1000
),
"status": task_state.status if task_state.status else None,
}
)
await db_task_state.save()
else:
task_state.booking.unix_millis_request_time = round(time.time() * 1000)
await ttm.TaskState.create(
id_=task_state.booking.id,
data=task_state.json(),
category=task_state.category.__root__ if task_state.category else None,
assigned_to=task_state.assigned_to.name
if task_state.assigned_to
else None,
"unix_millis_start_time": task_state.unix_millis_start_time
unix_millis_start_time=task_state.unix_millis_start_time
and datetime.fromtimestamp(task_state.unix_millis_start_time / 1000),
"unix_millis_finish_time": task_state.unix_millis_finish_time
unix_millis_finish_time=task_state.unix_millis_finish_time
and datetime.fromtimestamp(task_state.unix_millis_finish_time / 1000),
"status": task_state.status if task_state.status else None,
"unix_millis_request_time": datetime_request_time,
},
id_=task_state.booking.id,
)
status=task_state.status if task_state.status else None,
unix_millis_request_time=datetime.now(),
)

async def query_task_states(
self, query: QuerySet[DbTaskState], pagination: Optional[Pagination] = None
Expand Down
5 changes: 4 additions & 1 deletion packages/dashboard/src/components/tasks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ export function downloadCsvFull(timestamp: Date, allTasks: TaskState[]) {
export function downloadCsvMinimal(timestamp: Date, allTasks: TaskState[]) {
const columnSeparator = ';';
const rowSeparator = '\n';
const keys = ['ID', 'Category', 'Assignee', 'Start Time', 'End Time', 'State'];
const keys = ['Date', 'ID', 'Category', 'Assignee', 'Start Time', 'End Time', 'State'];
let csvContent = keys.join(columnSeparator) + rowSeparator;
allTasks.forEach((task) => {
const values = [
task.booking.unix_millis_request_time
? `${new Date(task.booking.unix_millis_request_time).toLocaleDateString()}`
: 'unknown',
task.booking.id,
task.category ? task.category : 'unknown',
task.assigned_to ? task.assigned_to.name : 'unknown',
Expand Down

0 comments on commit a1ea778

Please sign in to comment.