Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Implemented basic dashboard MVP

Blue Mouse0e39b3bd1480
diff --git a/package.json b/package.json
index 5593f56dc..6aed28387 100644
--- a/package.json
+++ b/package.json
@@ -8,10 +8,13 @@
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
- "billboard.js": "^3.1.1",
+ "billboard.js": "^3.1.2",
"bootstrap": "^4.6.0",
"bootstrap-icons": "^1.4.1",
+ "d3": "^7.0.0",
"debug": "^4.3.2",
+ "lodash": "^4.17.21",
+ "moment": "^2.29.1",
"nanoid": "^3.1.22",
"node-sass": "^5.0.0",
"react": "^17.0.1",
diff --git a/src/api/index.js b/src/api/index.js
index ea557ad2f..6ddac10a5 100644
--- a/src/api/index.js
+++ b/src/api/index.js
@@ -167,4 +167,13 @@ export const deleteProject = (id) =>
.catch(error => {
debug('%s', error)
throw error.response.data
- })
\ No newline at end of file
+ })
+
+export const getProjectData = (pid, tb = 'hour', period = '3d', from = '', to = '') =>
+ api
+ .get(`log?pid=${pid}&timeBucket=${tb}&period=${period}&from=${from}&to=${to}`)
+ .then(response => response.data)
+ .catch(error => {
+ debug('%s', error)
+ throw error.response.data
+ })
diff --git a/src/index.js b/src/index.js
index 984d2c025..9a9aa6f4e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -6,6 +6,7 @@ import { Provider } from 'react-redux'
import { transitions, positions, Provider as AlertProvider } from 'react-alert'
import AlertTemplate from 'react-alert-template-basic'
import 'bootstrap/dist/css/bootstrap.min.css'
+import 'billboard.js/dist/billboard.min.css'
import App from './App'
import reportWebVitals from './reportWebVitals'
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index 1f60c0169..148362dc3 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -51,7 +51,7 @@ const Dashboard = ({ projects }) => {
state: { name, id },
}}
className="text-decoration-none text-reset">
- <div class="card-body list-group-item-action text-reset">
+ <div className="card-body list-group-item-action text-reset">
{name}
</div>
</Link>
diff --git a/src/pages/Project/View/Panels.jsx b/src/pages/Project/View/Panels.jsx
index 2fdc3981e..a7e12b656 100644
--- a/src/pages/Project/View/Panels.jsx
+++ b/src/pages/Project/View/Panels.jsx
@@ -1,9 +1,15 @@
-import React from 'react'
+import React, { memo, Fragment } from 'react'
import Card from 'react-bootstrap/Card'
+import ProgressBar from 'react-bootstrap/ProgressBar'
+import _size from 'lodash/size'
+import _keys from 'lodash/keys'
+import _map from 'lodash/map'
+import _reduce from 'lodash/reduce'
+import _round from 'lodash/round'
const Locale = () => {
return (
- <Card style={{ height: '300px' }} className="w31-5 m-3 shadow-sm">
+ <Card style={{ height: '300px' }} className='w31-5 m-3 shadow-sm'>
<Card.Body>
<Card.Title>Locale</Card.Title>
<Card.Text>Test locale blahblah</Card.Text>
@@ -12,8 +18,37 @@ const Locale = () => {
)
}
-const LocaleMemo = React.memo(Locale)
+const Panel = ({ name, data }) => {
+ const keys = _keys(data)
+ const total = _reduce(keys, (prev, curr) => prev + data[curr], 0)
+ console.log(data)
+
+ return (
+ <Card style={{ height: '300px' }} className='w31-5 m-3 shadow-sm'>
+ <Card.Body>
+ <Card.Title>{name}</Card.Title>
+ <Card.Text>{_map(keys, key => {
+ const perc = _round(data[key] / total * 100, 2)
+
+ return (
+ <Fragment key={key}>
+ <div className='d-flex justify-content-between'>
+ {key}
+ <span className='text-secondary'>({perc}%)</span>
+ </div>
+ <ProgressBar now={perc} />
+ </Fragment>
+ )
+ })}</Card.Text>
+ </Card.Body>
+ </Card>
+ )
+}
+
+const LocaleMemo = memo(Locale)
+const PanelMemo = memo(Panel)
export {
LocaleMemo as Locale,
+ PanelMemo as Panel,
}
\ No newline at end of file
diff --git a/src/pages/Project/View/ViewProject.container.jsx b/src/pages/Project/View/ViewProject.container.jsx
index e89c2ce75..533df0fb7 100644
--- a/src/pages/Project/View/ViewProject.container.jsx
+++ b/src/pages/Project/View/ViewProject.container.jsx
@@ -1,40 +1,5 @@
-import React, { useState } from 'react'
-import { useLocation } from 'react-router-dom'
-import Spinner from 'react-bootstrap/Spinner'
-
import ViewProject from './ViewProject'
-export default () => {
- const location = useLocation()
- const { name, id } = location.state || {}
- const [project, setProject] = useState({})
-
-
- const onSubmit = () => {
- // TODO
- }
-
- const getProject = () => {
- // TODO
- }
-
- if (id && name) {
- return (
- <ViewProject name={name} id={id} />
- )
- }
-
- if (Object.keys(project).length > 0) {
- return (
- <ViewProject {...project} />
- )
- }
+// TODO: Redux connect
- return (
- <div className="container d-flex justify-content-center">
- <Spinner animation="border" role="status" variant="primary" className="spinner-lg">
- <span className="sr-only">Loading...</span>
- </Spinner>
- </div>
- )
-}
\ No newline at end of file
+export default ViewProject
diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsx
index 1c6ee5480..b85bfd49e 100644
--- a/src/pages/Project/View/ViewProject.jsx
+++ b/src/pages/Project/View/ViewProject.jsx
@@ -1,48 +1,188 @@
-import React, { useState } from 'react'
-import PropTypes from 'prop-types'
+import React, { useState, useEffect, memo } from 'react'
+import bb, { area, zoom } from 'billboard.js'
+import * as d3 from 'd3'
+import moment from 'moment'
+import { useLocation } from 'react-router-dom'
+import _keys from 'lodash/keys'
+import _map from 'lodash/map'
+import _reduce from 'lodash/reduce'
+import _size from 'lodash/size'
+import _isNull from 'lodash/isNull'
+import _isEmpty from 'lodash/isEmpty'
+import Spinner from 'react-bootstrap/Spinner'
+// import PropTypes from 'prop-types'
import ProjectSettings from 'pages/Project/Create'
-import { Locale } from './Panels'
+import { Locale, Panel } from './Panels'
+import { getProjectData } from 'api'
-const ViewProject = (props) => {
- const { name } = props
+const typeNameMapping = {
+ tz: 'Timezone',
+ pg: 'Page',
+ lc: 'Locale',
+ ref: 'Referrer',
+ sw: 'Screen width',
+ so: 'utm_source',
+ me: 'utm_medium',
+ ca: 'utm_campaign',
+ lt: 'Load time',
+}
+
+// todo: refactor
+const processData = (data) => {
+ const res = {
+ tz: {},
+ pg: {},
+ lc: {},
+ ref: {},
+ sw: {},
+ so: {},
+ me: {},
+ ca: {},
+ lt: {},
+ }
+ const whitelist = _keys(res)
+
+ for (let i = 0; i < _size(data); ++i) {
+ const tfData = data[i].data
+ for (let j = 0; j < _size(tfData); ++j) {
+ for (let z = 0; z < _size(whitelist); ++z) {
+ const currWLItem = whitelist[z]
+ const tfDataRecord = tfData[j][currWLItem]
+ if (!_isNull(tfDataRecord)) {
+ res[currWLItem][tfDataRecord] = 1 + (res[currWLItem][tfDataRecord] || 0)
+ }
+ }
+ }
+ }
+
+ return res
+}
+
+const ViewProject = () => {
+ const location = useLocation()
+ const { name, id } = location.state || {}
+ const [project, setProject] = useState({})
const [settings, setSettings] = useState(false)
+ const [data, setData] = useState([])
+ const [panelsData, setPanelsData] = useState({})
+ const [chart, setChart] = useState([])
+
+ useEffect(() => {
+ (async function() {
+ try {
+ const data = await getProjectData(id) || []
+ setData(data)
+
+ if (_isEmpty(data)) {
+ return
+ }
+
+ const processedData = processData(data)
+ console.log({
+ types: _keys(processedData),
+ data: processedData,
+ })
+ setPanelsData({
+ types: _keys(processedData),
+ data: processedData,
+ })
+
+ const settings = {
+ data: {
+ x: 'x',
+ json: {
+ x: _map(data, el => moment.utc(el.timeFrame).toDate()),
+ visits: _map(data, el => el.total),
+ },
+ type: area(),
+ xFormat: '%y-%m-%d %H:%M:%S',
+ },
+ axis: {
+ x: {
+ tick: {
+ fit: false,
+ count: 5,
+ },
+ type: 'timeseries',
+ },
+ },
+ zoom: {
+ enabled: zoom(),
+ type: 'drag',
+ },
+ tooltip: {
+ format: {
+ title: (x) => d3.timeFormat('%Y-%m-%d %H:%M:%S')(x),
+ }
+ },
+ point: {
+ focus: {
+ only: true,
+ }
+ },
+ bindto: '#dataChart',
+ }
+ console.log(settings.data)
+ const chart = bb.generate(settings)
+ setChart(chart)
+ } catch (e) {
+ console.error(e)
+ }
+ })()
+ }, [])
+
+ const onSubmit = () => {
+ // TODO
+ }
+
+ const getProject = () => {
+ // TODO
+ }
if (settings) {
return (
<ProjectSettings
onCancel={() => setSettings(false)}
- project={props}
- />
+ project={{ id, name }}
+ />
)
}
- return (
- <div className="container-fluid">
- <div className="d-flex justify-content-between">
- <h2>{name}</h2>
- <button
- onClick={() => setSettings(true)}
- className="btn btn-outline-primary h-100">
- Settings
- </button>
- </div>
- <div className="d-flex flex-wrap">
- <Locale />
- <Locale />
- <Locale />
- <Locale />
- <Locale />
- <Locale />
- <Locale />
+ if (id && name) {
+ // if (Object.keys(project).length > 0) {
+ return (
+ <div className='container-fluid'>
+ <div className='d-flex justify-content-between'>
+ <h2>{name}</h2>
+ <button
+ onClick={() => setSettings(true)}
+ className='btn btn-outline-primary h-100'>
+ Settings
+ </button>
+ </div>
+ <div className='d-flex flex-wrap'>
+ <div id='dataChart' />
+ {_map(panelsData.types, type => (
+ <Panel key={type} name={typeNameMapping[type]} data={panelsData.data[type]} />
+ ))}
+ </div>
</div>
+ )
+ }
+
+ return (
+ <div className='container d-flex justify-content-center'>
+ <Spinner animation='border' role='status' variant='primary' className='spinner-lg'>
+ <span className='sr-only'>Loading...</span>
+ </Spinner>
</div>
)
}
-ViewProject.propTypes = {
- id: PropTypes.string.isRequired,
- name: PropTypes.string.isRequired,
-}
+// ViewProject.propTypes = {
+ // id: PropTypes.string.isRequired,
+ // name: PropTypes.string.isRequired,
+// }
-export default ViewProject
\ No newline at end of file
+export default memo(ViewProject)
\ No newline at end of file