Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Create a generic chart lens and use it for pull requests #3411

Merged
14 commits merged into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/server/default-cards/contrib/view-all-pull-requests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"slug": "view-all-pull-requests",
"name": "All pull requests",
"type": "[email protected]",
"markers": [],
"markers": [ "org-balena" ],
"data": {
"allOf": [
{
Expand All @@ -25,7 +25,7 @@
"lenses": [
"lens-list",
"lens-kanban",
"lens-pull-request-chart"
"lens-chart"
]
}
}
5 changes: 3 additions & 2 deletions apps/ui/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
env:
browser: true
node: false

parserOptions:
ecmaVersion: 11
rules:
arrow-body-style:
off
0
2 changes: 1 addition & 1 deletion apps/ui/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ RUN make build-ui \
SERVER_HOST=$SERVER_HOST \
SERVER_PORT=$SERVER_PORT \
OAUTH_REDIRECT_BASE_URL=https://jel.ly.fish \
NODE_OPTIONS=--max-old-space-size=2048
NODE_OPTIONS=--max-old-space-size=3072
###########################################################
# Runtime
###########################################################
Expand Down
37 changes: 37 additions & 0 deletions apps/ui/components/SafeLazy.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) Balena.io - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
*/

import React, {
Suspense
} from 'react'
import ErrorBoundary from '@balena/jellyfish-ui-components/lib/shame/ErrorBoundary'
import Splash from './Splash'

const SafeLazy = ({
children
}) => {
return (
<ErrorBoundary>
<Suspense fallback={<Splash />}>
{children}
</Suspense>
</ErrorBoundary>
)
}

export const createLazyComponent = (fn) => {
const LazyComponent = React.lazy(fn)

return (props) => {
return (
<SafeLazy>
<LazyComponent {...props}/>
</SafeLazy>
)
}
}

export default SafeLazy
4 changes: 2 additions & 2 deletions apps/ui/lens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Kanban from './misc/Kanban'
import SupportAuditChart from './misc/SupportAuditChart'
import Table from './misc/Table'
import CRMTable from './misc/CRMTable'
import PullRequestChart from './misc/PullRequestChart'
import Chart from './misc/Chart'

import FullLenses from './full'
import ListLenses from './list'
Expand Down Expand Up @@ -53,7 +53,7 @@ const lenses = {
SupportAuditChart,
Table,
CRMTable,
PullRequestChart
Chart
]
}

Expand Down
109 changes: 109 additions & 0 deletions apps/ui/lens/misc/Chart/Chart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) Balena.io - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited.
* Proprietary and confidential.
*/

import * as _ from 'lodash'
import React, {
useState
} from 'react'
import PlotlyEditor from 'react-chart-editor'
import 'react-chart-editor/lib/react-chart-editor.css'
import * as flatten from 'flat'
import {
createGlobalStyle
} from 'styled-components'

// HACK: Work-around for the fact that plotly throws a fit if you run it outside
// of a browser environment.
const plotly = window.isUnitTest ? null : require('plotly.js/dist/plotly')

// The plotly styles don't render 100% correctly on JF, causing the panel
// headers to get squashed. This fixes the issue
const GlobalStyle = createGlobalStyle `
.plotly_editor .editor_controls .fold__top {
height: 30px;
}
`

const config = {
editable: true
}

export default React.memo(({
tail
}) => {
// Flatten all the cards into single level objects so that plotly can handle
// the data
const getDataSources = React.useCallback(() => {
const flattenedData = _.map(tail, flatten)
const combinedFlatKeys = _.uniq(_.flatMap(flattenedData, _.keys))

const dataSources = _.mapValues(_.keyBy(combinedFlatKeys), (key) => _.map(flattenedData, key))

const dataSourceOptions = Object.keys(dataSources).map((name) => ({
value: name,
label: name
}))
return {
dataSources, dataSourceOptions
}
}, [ tail ])

const {
dataSources, dataSourceOptions
} = getDataSources()

const defaultSettings = {
data: [
// This config sets up a simple histogram that groups data by day, using
// the `created_at` field.
{
type: 'histogram',
mode: 'markers',
// eslint-disable-next-line id-length
x: dataSources.created_at,
xsrc: 'created_at',
xbins: {
size: 86400000
},
marker: {
color: 'rgb(0, 174, 239)'
}
}
],
layout: {
bargap: 0.09
},
frames: []
}

const [ settings, setSettings ] = useState(defaultSettings)

return (
<React.Fragment>
<GlobalStyle />

<PlotlyEditor
data={settings.data}
layout={settings.layout}
config={config}
frames={settings.frame}
dataSources={dataSources}
dataSourceOptions={dataSourceOptions}
plotly={plotly}
onUpdate={(data, layout, frames) => {
setSettings({
data,
layout,
frames
})
}}
useResizeHandler
debug
advancedTraceTypeSelector
/>
</React.Fragment>
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,22 @@
* Proprietary and confidential.
*/

import * as _ from 'lodash'
import {
connect
} from 'react-redux'
import * as redux from 'redux'
import {
actionCreators,
selectors
} from '../../../core'
import PullRequestChart from './PullRequestChart'

const mapStateToProps = (state, ownProps) => {
return {
channels: selectors.getChannels(state)
}
}
createLazyComponent
} from '../../../components/SafeLazy'

const mapDispatchToProps = (dispatch) => {
return {
actions: redux.bindActionCreators(
_.pick(actionCreators, [
'getActor'
]),
dispatch
)
}
}
// eslint-disable-next-line
const ChartLazy = createLazyComponent(() => import(/* webpackChunkName: "chart" */ './Chart'))

const lens = {
slug: 'lens-pull-request-chart',
slug: 'lens-chart',
type: 'lens',
version: '1.0.0',
name: 'Pull request chart lens',
name: 'Generic chart lens',
data: {
icon: 'chart-bar',
format: 'list',
renderer: connect(mapStateToProps, mapDispatchToProps)(PullRequestChart),
renderer: ChartLazy,
filter: {
type: 'array',
items: {
Expand Down
97 changes: 0 additions & 97 deletions apps/ui/lens/misc/PullRequestChart/PullRequestChart.jsx

This file was deleted.

41 changes: 0 additions & 41 deletions apps/ui/lens/misc/PullRequestChart/tests/PullRequestChart.spec.jsx

This file was deleted.

Loading