Skip to content

Commit

Permalink
audits
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Nov 4, 2024
1 parent 6c9d9c5 commit 59b8544
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 54 deletions.
15 changes: 8 additions & 7 deletions www/src/components/audits/AuditChloropleth.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useQuery } from '@apollo/client'
import { useRef, useState } from 'react'

import lookup from 'country-code-lookup'
Expand All @@ -8,7 +7,10 @@ import { Card, PageTitle, SubTab, TabList } from '@pluralsh/design-system'

import { Chloropleth } from '../utils/Chloropleth'

import { AUDIT_METRICS, LOGIN_METRICS } from './queries'
import {
useAuditMetricsQuery,
useLoginMetricsQuery,
} from '../../generated/graphql'

const DIRECTORY = [
{ key: 'audit-logs', label: 'Audit logs' },
Expand All @@ -19,14 +21,13 @@ export function AuditChloropleth() {
const [selectedKey, setSelectedKey] = useState<any>('audit-logs')
const tabStateRef = useRef<any>(null)

const { data } = useQuery(
selectedKey === 'logins' ? LOGIN_METRICS : AUDIT_METRICS,
{ fetchPolicy: 'cache-and-network' }
)
const query =
selectedKey === 'logins' ? useLoginMetricsQuery : useAuditMetricsQuery
const { data } = query({ fetchPolicy: 'cache-and-network' })

if (!data) return null

const results = data.auditMetrics || data.loginMetrics
const results = data['auditMetrics'] || data['loginMetrics']
const metrics = results.map(({ country, count }) => ({
// @ts-expect-error
id: lookup.byIso(country).iso3,
Expand Down
17 changes: 9 additions & 8 deletions www/src/components/audits/LoginAudits.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { useQuery } from '@apollo/client'
import { Box } from 'grommet'
import { memo, useCallback, useMemo } from 'react'
import { Date, IconFrame, PageTitle, Table } from '@pluralsh/design-system'
import { createColumnHelper } from '@tanstack/react-table'
import isEmpty from 'lodash/isEmpty'
import { Flex } from 'honorable'

import { extendConnection } from '../../utils/graphql'
import { extendConnection, mapExistingNodes } from '../../utils/graphql'
import LoadingIndicator from '../utils/LoadingIndicator'

import { AuditUser } from './AuditUser'
import { Location } from './Location'
import { LOGINS_Q } from './queries'
import { useLoginsQuery } from '../../generated/graphql'

const FETCH_MARGIN = 30

Expand Down Expand Up @@ -87,12 +86,14 @@ const LoginAuditsTable = memo(({ logins, fetchMoreOnBottomReached }: any) =>
)

export function LoginAudits() {
const { data, loading, fetchMore } = useQuery(LOGINS_Q, {
const { data, loading, fetchMore } = useLoginsQuery({
fetchPolicy: 'cache-and-network',
})
const pageInfo = data?.oidcLogins?.pageInfo
const edges = data?.oidcLogins?.edges
const logins = useMemo(() => edges?.map(({ node }) => node) || [], [edges])
const logins = useMemo(
() => mapExistingNodes(data?.oidcLogins) || [],
[data?.oidcLogins]
)

const fetchMoreOnBottomReached = useCallback(
(element?: HTMLDivElement | undefined) => {
Expand All @@ -104,10 +105,10 @@ export function LoginAudits() {
if (
scrollHeight - scrollTop - clientHeight < FETCH_MARGIN &&
!loading &&
pageInfo.hasNextPage
pageInfo?.hasNextPage
) {
fetchMore({
variables: { cursor: pageInfo.endCursor },
variables: { cursor: pageInfo?.endCursor },
updateQuery: (prev, { fetchMoreResult: { oidcLogins } }) =>
extendConnection(prev, oidcLogins, 'oidcLogins'),
})
Expand Down
39 changes: 0 additions & 39 deletions www/src/components/audits/queries.ts

This file was deleted.

Loading

0 comments on commit 59b8544

Please sign in to comment.