-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6aa59af
commit bed0fa5
Showing
7 changed files
with
224 additions
and
64 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,206 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { subMonths } from 'date-fns' | ||
import moment from 'moment' | ||
import CustomPieGraph from '../../../../app/routes/Dashboards/Grapths/CustomPieGraph' | ||
import ActiveUsersGraph from '../../../../app/routes/Dashboards/Grapths/ActiveUsersGraph' | ||
import DatePicker from 'react-datepicker' | ||
import 'react-datepicker/dist/react-datepicker.css' | ||
import GluuLoader from '../../../../app/routes/Apps/Gluu/GluuLoader' | ||
import GluuViewWrapper from '../../../../app/routes/Apps/Gluu/GluuViewWrapper' | ||
import { getMau } from '../../../../app/redux/actions/MauActions' | ||
import { getClients } from '../../../../app/redux/actions/InitActions' | ||
import applicationstyle from '../../../../app/routes/Apps/Gluu/styles/applicationstyle' | ||
import GluuLabel from '../../../../app/routes/Apps/Gluu/GluuLabel' | ||
import GluuRibbon from '../../../../app/routes/Apps/Gluu/GluuRibbon' | ||
import { | ||
Button, | ||
Card, | ||
CardFooter, | ||
CardBody, | ||
FormGroup, | ||
Col, | ||
Row, | ||
} from '../../../../app/components' | ||
import { | ||
hasBoth, | ||
buildPayload, | ||
STAT_READ, | ||
STAT_JANS_READ, | ||
} from '../../../../app/utils/PermChecker' | ||
import { useTranslation } from 'react-i18next' | ||
import { connect } from 'react-redux' | ||
|
||
function MauGraph({ statData, permissions, clients, loading, dispatch }) { | ||
const { t } = useTranslation() | ||
const [startDate, setStartDate] = useState(subMonths(new Date(), 3)) | ||
const [endDate, setEndDate] = useState(new Date()) | ||
const userAction = {} | ||
const options = {} | ||
useEffect(() => { | ||
let count = 0 | ||
const interval = setInterval(() => { | ||
if (statData.length === 0 && count < 2) { | ||
search() | ||
} | ||
if (clients.length === 0 && count < 2) { | ||
buildPayload(userAction, 'Fetch openid connect clients', {}) | ||
dispatch(getClients(userAction)) | ||
} | ||
count++ | ||
}, 1000) | ||
return () => clearInterval(interval) | ||
}, [1000]) | ||
|
||
function search() { | ||
options['month'] = getFormattedMonth() | ||
buildPayload(userAction, 'GET MAU', options) | ||
dispatch(getMau(userAction)) | ||
} | ||
|
||
function doDataAugmentation(input) { | ||
let stat = input | ||
if (stat && stat.length >= 1) { | ||
let flattendStat = stat.map((entry) => entry['month']) | ||
let aRange = generateDateRange(moment(startDate), moment(endDate)) | ||
for (const ele of aRange) { | ||
const currentMonth = getYearMonth(new Date(ele)) | ||
if (flattendStat.indexOf(parseInt(currentMonth, 10)) === -1) { | ||
let newEntry = new Object() | ||
newEntry['month'] = parseInt(getYearMonth(new Date(ele)), 10) | ||
newEntry['mau'] = 0 | ||
newEntry['client_credentials_access_token_count'] = 0 | ||
newEntry['authz_code_access_token_count'] = 0 | ||
newEntry['authz_code_idtoken_count'] = 0 | ||
stat.push(newEntry) | ||
} | ||
} | ||
return Array.from(new Set(stat)).sort( | ||
(a, b) => parseInt(a.month, 10) - parseInt(b.month, 10), | ||
) | ||
} | ||
return stat | ||
} | ||
function getYearMonth(date) { | ||
return date.getFullYear() + getMonth(date) | ||
} | ||
function getFormattedMonth() { | ||
return getYearMonth(startDate) + '%' + getYearMonth(endDate) | ||
} | ||
function getMonth(aDate) { | ||
let value = String(aDate.getMonth() + 1) | ||
if (value.length > 1) { | ||
return value | ||
} else { | ||
return '0' + value | ||
} | ||
} | ||
|
||
function generateDateRange(start, end) { | ||
var result = [] | ||
while (start.isBefore(end)) { | ||
result.push(start.format('YYYY-MM-01')) | ||
start.add(1, 'month') | ||
} | ||
return result | ||
} | ||
|
||
const CustomButton = React.forwardRef(({ value, onClick }, ref) => ( | ||
<Button | ||
color="primary" | ||
outline | ||
style={applicationstyle.customButtonStyle} | ||
className="example-custom-input" | ||
onClick={onClick} | ||
ref={ref} | ||
> | ||
{value} | ||
</Button> | ||
)) | ||
|
||
return ( | ||
<GluuLoader blocking={loading}> | ||
<GluuViewWrapper | ||
canShow={hasBoth(permissions, STAT_READ, STAT_JANS_READ)} | ||
> | ||
<Card> | ||
<GluuRibbon title={t('fields.monthly_active_users')} fromLeft /> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
|
||
<CardBody> | ||
<Row> | ||
<Col sm={2}></Col> | ||
<Col sm={10}> | ||
<GluuLabel label="Select a date range" size="4" /> | ||
<DatePicker | ||
selected={startDate} | ||
onChange={(date) => setStartDate(date)} | ||
selectsStart | ||
isClearable | ||
startDate={startDate} | ||
dateFormat="MM/yyyy" | ||
showMonthYearPicker | ||
endDate={endDate} | ||
customInput={<CustomButton />} | ||
/> | ||
| ||
<DatePicker | ||
selected={endDate} | ||
onChange={(date) => setEndDate(date)} | ||
selectsEnd | ||
isClearable | ||
startDate={startDate} | ||
endDate={endDate} | ||
dateFormat="MM/yyyy" | ||
showMonthYearPicker | ||
minDate={startDate} | ||
maxDate={new Date()} | ||
customInput={<CustomButton />} | ||
/> | ||
| ||
<Button | ||
style={applicationstyle.customButtonStyle} | ||
color="primary" | ||
onClick={search} | ||
> | ||
<i className="fa fa-search mr-2"></i> | ||
{t('actions.view')} | ||
</Button> | ||
</Col> | ||
</Row> | ||
<FormGroup row /> | ||
<FormGroup row /> | ||
<FormGroup row> | ||
<Col sm={6}> | ||
<ActiveUsersGraph data={doDataAugmentation(statData)} /> | ||
</Col> | ||
<Col sm={6}> | ||
<CustomPieGraph | ||
data={statData.filter((entry) => entry.mau !== 0)} | ||
dataKey="mau" | ||
nameKey="month" | ||
/> | ||
</Col> | ||
</FormGroup> | ||
</CardBody> | ||
<CardFooter className="p-4 bt-0"></CardFooter> | ||
</Card> | ||
</GluuViewWrapper> | ||
</GluuLoader> | ||
) | ||
} | ||
|
||
const mapStateToProps = (state) => { | ||
return { | ||
statData: state.mauReducer.stat, | ||
loading: state.mauReducer.loading, | ||
clients: state.initReducer.clients, | ||
permissions: state.authReducer.permissions, | ||
} | ||
} | ||
|
||
export default connect(mapStateToProps)(MauGraph) |
This file contains 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