Skip to content

Commit

Permalink
Merge pull request #42 from lokanandaprabhu/feature/ODC-7472
Browse files Browse the repository at this point in the history
ODC-7472: Update last run time to support unix timestamp
  • Loading branch information
openshift-merge-bot[bot] authored Dec 28, 2023
2 parents c239662 + 1035ea3 commit f13b407
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 43 deletions.
61 changes: 23 additions & 38 deletions src/components/pipelines-overview/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,45 +97,30 @@ export const formatTime = (time: string): string => {
return timestring;
};

export const formatTimeLastRunTime = (time: string): string => {
const timeValue = time?.split(/\s+/);
if (timeValue === undefined) {
return '-';
}
if (timeValue.length > 1) {
// Check for the presence of each component
const hasYear = timeValue.includes('year') || timeValue.includes('years');
const hasMonth =
timeValue.includes('month') || timeValue.includes('months');
const hasDay = timeValue.includes('day') || timeValue.includes('days');

// Return the most significant time component
if (hasYear) {
return timeValue.includes('year')
? timeValue[timeValue.indexOf('year') - 1] + ' year ago'
: timeValue[timeValue.indexOf('years') - 1] + ' years ago';
} else if (hasMonth) {
return timeValue.includes('month')
? timeValue[timeValue.indexOf('month') - 1] + ' month ago'
: timeValue[timeValue.indexOf('months') - 1] + ' months ago';
} else if (hasDay) {
return timeValue.includes('day')
? timeValue[timeValue.indexOf('day') - 1] + ' day ago'
: timeValue[timeValue.indexOf('days') - 1] + ' days ago';
} else {
return `${timeValue.pop()} ago`;
}
} else {
const [hours, minutes, seconds] = time.split(/[:.]/).map(Number);
if (!isNaN(hours) && hours > 0) {
return hours === 1 ? `${hours} hour ago` : `${hours} hours ago`;
} else if (!isNaN(minutes) && minutes > 0) {
return minutes === 1 ? `${minutes} min ago` : `${minutes} mins ago`;
} else if (!isNaN(seconds) && seconds > 0) {
return seconds === 1 ? `${seconds} sec ago` : `${seconds} secs ago`;
export const formatTimeLastRunTime = (time: number): string => {
const currentTimestamp = Math.floor(Date.now() / 1000); // Current timestamp in seconds
const timeDifference = currentTimestamp - time;

// Convert the time difference into seconds, minutes, hours, etc.
const minutes = Math.floor(timeDifference / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const months = Math.floor(days / 30); // Approximate months (30 days)
const years = Math.floor(months / 12); // Approximate years (12 months)

// Determine the output based on the time difference
if (years > 0) {
return `${years} year${years > 1 ? 's' : ''} ago`;
} else if (months > 0) {
return `${months} month${months > 1 ? 's' : ''} ago`;
} else if (days > 0) {
return `${days} day${days > 1 ? 's' : ''} ago`;
} else if (hours > 0) {
return `${hours} hour${hours > 1 ? 's' : ''} ago`;
} else if (minutes > 0) {
return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
} else {
return null; // No significant time component found
}
return `${timeDifference} second${timeDifference !== 1 ? 's' : ''} ago`;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
VirtualizedTable,
useActiveColumns,
} from '@openshift-console/dynamic-plugin-sdk';
import { SummaryProps, sortByNumbers, sortByProperty, sortTimeStrings, listPageTableColumnClasses as tableColumnClasses } from '../utils';
import { SummaryProps, sortByNumbers, sortByProperty, sortByTimestamp, sortTimeStrings, listPageTableColumnClasses as tableColumnClasses } from '../utils';
import PipelineRunsForPipelinesRow from './PipelineRunsForPipelinesRow';

type PipelineRunsForPipelinesListProps = {
Expand Down Expand Up @@ -73,7 +73,7 @@ const PipelineRunsForPipelinesList: React.FC<
{
id: 'lastRunTime',
title: t('Last run time'),
sort: (summary, direction: 'asc' | 'desc') => sortTimeStrings(summary, 'last_runtime',direction),
sort: (summary, direction: 'asc' | 'desc') => sortByTimestamp(summary, 'last_runtime',direction),
transforms: [sortable],
props: { className: tableColumnClasses[6] },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useActiveColumns,
} from '@openshift-console/dynamic-plugin-sdk';
import PipelineRunsForRepositoriesRow from './PipelineRunsForRepositoriesRow';
import { SummaryProps, sortByNumbers, sortByProperty, sortTimeStrings, listPageTableColumnClasses as tableColumnClasses } from '../utils';
import { SummaryProps, sortByNumbers, sortByProperty, sortByTimestamp, sortTimeStrings, listPageTableColumnClasses as tableColumnClasses } from '../utils';


type PipelineRunsForRepositoriesListProps = {
Expand Down Expand Up @@ -74,7 +74,7 @@ const PipelineRunsForRepositoriesList: React.FC<
{
id: 'lastRunTime',
title: t('Last run time'),
sort: (summary, direction: 'asc' | 'desc') => sortTimeStrings(summary, 'last_runtime',direction),
sort: (summary, direction: 'asc' | 'desc') => sortByTimestamp(summary, 'last_runtime',direction),
transforms: [sortable],
props: { className: tableColumnClasses[6] },
},
Expand Down
15 changes: 14 additions & 1 deletion src/components/pipelines-overview/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type SummaryProps = {
total_duration?: string;
others?: number;
group_value?: any;
last_runtime?: string;
last_runtime?: number;
};

export type mainDataType = {
Expand Down Expand Up @@ -168,6 +168,19 @@ export const sortTimeStrings = (
});
};

export const sortByTimestamp = (items: SummaryProps[], prop: string,direction: string) => {
const compareTimestamps = (a: SummaryProps, b: SummaryProps) => {
const timestampA = a[prop];
const timestampB = b[prop];

return direction === 'asc' ? timestampA - timestampB : timestampB - timestampA;
};

const sortedItems = [...items].sort(compareTimestamps);

return sortedItems;
};

export const sortByNumbers = (
array: SummaryProps[],
prop: string,
Expand Down

0 comments on commit f13b407

Please sign in to comment.