- 
                Notifications
    
You must be signed in to change notification settings  - Fork 145
 
fix: Add Trends Line graph on Deployment events page #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            harshit078
  wants to merge
  9
  commits into
  middlewarehq:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
harshit078:trends-line-graph
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +862
        
        
          −152
        
        
          
        
      
    
  
  
     Open
                    Changes from 8 commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      2547521
              
                Initialsed Deployment duration and trends with Tabs support on UI
              
              
                harshit078 d7168a1
              
                Added duration type for deployment duration for logic calculation
              
              
                harshit078 9c9b074
              
                Updated logic to use run_duration and added Trends and Graphs metric
              
              
                harshit078 9cdb98f
              
                Updated logic to handle DeploymentSources and added a check for both …
              
              
                harshit078 f9e85d2
              
                Addressed comments and updated chart label and border color
              
              
                harshit078 208faed
              
                Added state to redirect the user to managa teams if PR_Merge
              
              
                harshit078 1f84876
              
                Fixed CI failing test
              
              
                harshit078 0ed9d1b
              
                Fixed esling check
              
              
                harshit078 e90e189
              
                fix: Removed stable logic and updated negative sign in Increasing
              
              
                harshit078 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
        
          
          
            182 changes: 182 additions & 0 deletions
          
          182 
        
  web-server/src/content/DoraMetrics/DoraMetricsDuration.tsx
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,182 @@ | ||
| import { | ||
| CheckCircleRounded, | ||
| AccessTimeRounded, | ||
| OpenInNew, | ||
| Code, | ||
| ArrowForwardIosRounded | ||
| } from '@mui/icons-material'; | ||
| import { Card, Tooltip, useTheme } from '@mui/material'; | ||
| import { FC, useMemo } from 'react'; | ||
| 
     | 
||
| import { FlexBox } from '@/components/FlexBox'; | ||
| import { Line } from '@/components/Text'; | ||
| import { Deployment } from '@/types/resources'; | ||
| import { getDurationString, isoDateString } from '@/utils/date'; | ||
| 
     | 
||
| type DeploymentCardType = 'Longest' | 'Shortest'; | ||
| 
     | 
||
| interface DeploymentCardProps { | ||
| deployment: Deployment; | ||
| type: DeploymentCardType; | ||
| } | ||
| 
     | 
||
| interface DoraMetricsDurationProps { | ||
| deployments: Deployment[]; | ||
| } | ||
| 
     | 
||
| const formatDeploymentDate = (dateString: string): string => { | ||
| if (!dateString) return 'Unknown Date'; | ||
| 
     | 
||
| try { | ||
| const date = new Date(dateString); | ||
| const isoDate = isoDateString(date); | ||
| const formattedDate = new Date(isoDate); | ||
| 
     | 
||
| const options: Intl.DateTimeFormatOptions = { | ||
| month: 'short', | ||
| day: 'numeric', | ||
| year: 'numeric', | ||
| hour: 'numeric', | ||
| minute: 'numeric', | ||
| hour12: true | ||
| }; | ||
| 
     | 
||
| return formattedDate.toLocaleDateString('en-US', options); | ||
| } catch (error) { | ||
| console.error('Error formatting date:', error); | ||
| return 'Invalid Date'; | ||
| } | ||
| }; | ||
| 
     | 
||
| const DeploymentCard: FC<DeploymentCardProps> = ({ deployment }) => { | ||
| const theme = useTheme(); | ||
| 
     | 
||
| const handleDeploymentClick = () => { | ||
| if (deployment.html_url) { | ||
| window.open(deployment.html_url, '_blank', 'noopener,noreferrer'); | ||
| } | ||
| }; | ||
| 
     | 
||
| return ( | ||
| <Card | ||
| elevation={0} | ||
| sx={{ | ||
| flex: 1, | ||
| p: 1.6, | ||
| width: '20vw', | ||
| maxWidth: '30vw', | ||
| border: `1px solid ${theme.palette.primary.light}`, | ||
| borderRadius: 2 | ||
| }} | ||
| > | ||
| <FlexBox col gap={1.2} sx={{ position: 'relative' }}> | ||
| <FlexBox gap={0.8} alignItems="center"> | ||
| <FlexBox alignItems="center" gap={1}> | ||
| <CheckCircleRounded color="success" fontSize="inherit" /> | ||
| <Line white semibold> | ||
| Run On {formatDeploymentDate(deployment.conducted_at)} | ||
| </Line> | ||
| </FlexBox> | ||
| <Tooltip title="Open in new tab" arrow> | ||
| <FlexBox | ||
| alignItems="center" | ||
| gap={0.5} | ||
| sx={{ | ||
| cursor: deployment.html_url ? 'pointer' : 'not-allowed', | ||
| opacity: deployment.html_url ? 1 : 0.7 | ||
| }} | ||
| onClick={handleDeploymentClick} | ||
| > | ||
| <OpenInNew fontSize="inherit" /> | ||
| </FlexBox> | ||
| </Tooltip> | ||
| </FlexBox> | ||
| 
     | 
||
| <FlexBox gap={0.8} alignItems="center" justifyContent="space-between"> | ||
| <FlexBox gap={0.8} alignItems="center" flex={1}> | ||
| <Code fontSize="small" /> | ||
| <Line sx={{ color: theme.palette.primary.light }}> | ||
| {deployment.pr_count || 0} new PR's | ||
| </Line> | ||
| <Line sx={{ color: theme.palette.primary.light }}> | ||
| {deployment.head_branch || 'Unknown Branch'} | ||
| </Line> | ||
| <AccessTimeRounded fontSize="small" /> | ||
| <Line sx={{ color: theme.palette.primary.light }}> | ||
| {getDurationString(deployment.run_duration)} | ||
| </Line> | ||
| </FlexBox> | ||
| <ArrowForwardIosRounded | ||
| sx={{ | ||
| position: 'absolute', | ||
| right: 0, | ||
| top: '35%', | ||
| bottom: '35%' | ||
| }} | ||
| fontSize="medium" | ||
| /> | ||
| </FlexBox> | ||
| </FlexBox> | ||
| </Card> | ||
| ); | ||
| }; | ||
| 
     | 
||
| export const DoraMetricsDuration: FC<DoraMetricsDurationProps> = ({ | ||
| deployments | ||
| }) => { | ||
| const { longestDeployment, shortestDeployment } = useMemo(() => { | ||
| if (!Array.isArray(deployments) || !deployments.length) { | ||
| return { longestDeployment: null, shortestDeployment: null }; | ||
| } | ||
| 
     | 
||
| const validDeployments = deployments | ||
| .filter((d): d is Deployment => | ||
| Boolean(d.conducted_at && typeof d.run_duration === 'number') | ||
| ) | ||
| .filter((d) => d.run_duration >= 0); | ||
| 
     | 
||
| if (!validDeployments.length) { | ||
| return { longestDeployment: null, shortestDeployment: null }; | ||
| } | ||
| 
     | 
||
| // Function to calculate Longest and shortest deployments | ||
| const { longest, shortest } = validDeployments.reduce( | ||
| (acc, current) => ({ | ||
| longest: | ||
| !acc.longest || current.run_duration > acc.longest.run_duration | ||
| ? current | ||
| : acc.longest, | ||
| shortest: | ||
| !acc.shortest || current.run_duration < acc.shortest.run_duration | ||
| ? current | ||
| : acc.shortest | ||
| }), | ||
| { | ||
| longest: null as Deployment | null, | ||
| shortest: null as Deployment | null | ||
| } | ||
| ); | ||
| 
     | 
||
| return { longestDeployment: longest, shortestDeployment: shortest }; | ||
| }, [deployments]); | ||
| 
     | 
||
| return ( | ||
| <FlexBox col gap={1.5}> | ||
| <FlexBox gap={3}> | ||
| <FlexBox col gap={1}> | ||
| <Line white sx={{ fontSize: '1.0rem' }} semibold> | ||
| Longest Deployment | ||
| </Line> | ||
| <DeploymentCard deployment={longestDeployment} type="Longest" /> | ||
| </FlexBox> | ||
| 
     | 
||
| <FlexBox col gap={1}> | ||
| <Line white sx={{ fontSize: '1.0rem' }} semibold> | ||
| Shortest Deployment | ||
| </Line> | ||
| <DeploymentCard deployment={shortestDeployment} type="Shortest" /> | ||
| </FlexBox> | ||
| </FlexBox> | ||
| </FlexBox> | ||
| ); | ||
| }; | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
DeploymentCardcrashes whendeploymentisnulllongestDeployment/shortestDeploymentcan benull, yet the prop is typed as non-nullable.This causes a TypeScript error and a potential runtime exception when there are no valid deployments.
Add an early return inside the component:
And adjust the caller: