Skip to content

Commit

Permalink
[fix] CSV Download delivers wrong entries (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
sijav authored May 14, 2024
1 parent 860abab commit f1528ec
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 39 deletions.
14 changes: 7 additions & 7 deletions src/locales/de-DE/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ msgstr "AWS Marketplace wurde erfolgreich abonniert"
msgid "Basic Information"
msgstr "Grundinformation"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:107
#: src/pages/panel/inventory/DownloadCSVButton.tsx:112
msgid "Because the data is over {0} Only first {1} items will be downloaded"
msgstr "Da die Daten über {0} liegen, werden nur die ersten {1} Elemente heruntergeladen"

Expand Down Expand Up @@ -700,8 +700,8 @@ msgstr "Sie haben noch kein Konto? Klicken Sie hier, um sich anzumelden."
msgid "Downgrade"
msgstr "Downgrade"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:100
#: src/pages/panel/inventory/DownloadCSVButton.tsx:119
#: src/pages/panel/inventory/DownloadCSVButton.tsx:105
#: src/pages/panel/inventory/DownloadCSVButton.tsx:124
msgid "Download CSV"
msgstr "CSV herunterladen"

Expand Down Expand Up @@ -1157,7 +1157,7 @@ msgstr "Nächste Synchronisierung"

#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:42
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:68
#: src/pages/panel/inventory/InventoryTable.tsx:145
#: src/pages/panel/inventory/InventoryTable.tsx:142
msgid "No"
msgstr ""

Expand Down Expand Up @@ -1688,7 +1688,7 @@ msgstr "Nicht angehängte elastische IPs"
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx:300
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowValues.tsx:149
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowValues.tsx:154
#: src/pages/panel/inventory/InventoryTable.tsx:137
#: src/pages/panel/inventory/InventoryTable.tsx:134
msgid "Undefined"
msgstr ""

Expand Down Expand Up @@ -1741,7 +1741,7 @@ msgstr "Benutzer"
msgid "Value"
msgstr "Wert"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:84
#: src/pages/panel/inventory/DownloadCSVButton.tsx:89
msgid "Warning"
msgstr "Warnung"

Expand Down Expand Up @@ -1803,7 +1803,7 @@ msgstr "Workspace-Benutzer"

#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:39
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:65
#: src/pages/panel/inventory/InventoryTable.tsx:141
#: src/pages/panel/inventory/InventoryTable.tsx:138
msgid "Yes"
msgstr ""

Expand Down
14 changes: 7 additions & 7 deletions src/locales/en-US/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ msgstr "AWS Marketplace has been successfully subscribed"
msgid "Basic Information"
msgstr "Basic Information"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:107
#: src/pages/panel/inventory/DownloadCSVButton.tsx:112
msgid "Because the data is over {0} Only first {1} items will be downloaded"
msgstr "Because the data is over {0} Only first {1} items will be downloaded"

Expand Down Expand Up @@ -700,8 +700,8 @@ msgstr "Don't have an account? Click here to Sign up."
msgid "Downgrade"
msgstr "Downgrade"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:100
#: src/pages/panel/inventory/DownloadCSVButton.tsx:119
#: src/pages/panel/inventory/DownloadCSVButton.tsx:105
#: src/pages/panel/inventory/DownloadCSVButton.tsx:124
msgid "Download CSV"
msgstr "Download CSV"

Expand Down Expand Up @@ -1157,7 +1157,7 @@ msgstr "Next Sync"

#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:42
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:68
#: src/pages/panel/inventory/InventoryTable.tsx:145
#: src/pages/panel/inventory/InventoryTable.tsx:142
msgid "No"
msgstr "No"

Expand Down Expand Up @@ -1688,7 +1688,7 @@ msgstr "Unattached Elastic IPs"
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowStringValue.tsx:300
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowValues.tsx:149
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowValues.tsx:154
#: src/pages/panel/inventory/InventoryTable.tsx:137
#: src/pages/panel/inventory/InventoryTable.tsx:134
msgid "Undefined"
msgstr "Undefined"

Expand Down Expand Up @@ -1741,7 +1741,7 @@ msgstr "Users"
msgid "Value"
msgstr "Value"

#: src/pages/panel/inventory/DownloadCSVButton.tsx:84
#: src/pages/panel/inventory/DownloadCSVButton.tsx:89
msgid "Warning"
msgstr "Warning"

Expand Down Expand Up @@ -1803,7 +1803,7 @@ msgstr "Workspace Users"

#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:39
#: src/pages/panel/inventory/inventory-form/InventoryFormFilterRowBooleanValue.tsx:65
#: src/pages/panel/inventory/InventoryTable.tsx:141
#: src/pages/panel/inventory/InventoryTable.tsx:138
msgid "Yes"
msgstr "Yes"

Expand Down
7 changes: 6 additions & 1 deletion src/pages/panel/inventory/DownloadCSVButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { postWorkspaceInventorySearchTableDownloadMutation } from 'src/pages/pan
import { GTMEventNames, panelUI } from 'src/shared/constants'
import { sendToGTM } from 'src/shared/google-tag-manager'
import { Modal } from 'src/shared/modal'
import { WorkspaceInventorySearchTableHistory, WorkspaceInventorySearchTableSort } from 'src/shared/types/server'
import { jsonToStr } from 'src/shared/utils/jsonToStr'
import { TrackJS } from 'trackjs'

interface DownloadCSVButtonProps {
query: string
history?: WorkspaceInventorySearchTableHistory
sort?: WorkspaceInventorySearchTableSort[]
hasWarning?: boolean
}

export const DownloadCSVButton = forwardRef(
({ query, hasWarning, ...tooltipProps }: DownloadCSVButtonProps, ref: ForwardedRef<HTMLButtonElement | null>) => {
({ query, history, sort, hasWarning, ...tooltipProps }: DownloadCSVButtonProps, ref: ForwardedRef<HTMLButtonElement | null>) => {
const {
i18n: { locale },
} = useLingui()
Expand All @@ -36,6 +39,8 @@ export const DownloadCSVButton = forwardRef(
onDownloadProgress: (ev) => {
setProgress(ev.total ? Math.round((ev.loaded * 100) / ev.total) : -1)
},
history,
sort,
query,
signal: abortController.current.signal,
workspaceId: selectedWorkspace?.id ?? '',
Expand Down
12 changes: 5 additions & 7 deletions src/pages/panel/inventory/InventoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useAbsoluteNavigate } from 'src/shared/absolute-navigate'
import { ErrorBoundaryFallback, NetworkErrorBoundary } from 'src/shared/error-boundary-fallback'
import { FixQueryProvider } from 'src/shared/fix-query-parser'
import { LoadingSuspenseFallback } from 'src/shared/loading'
import { WorkspaceInventorySearchTableHistoryChanges } from 'src/shared/types/server'
import { getLocationSearchValues, mergeLocationSearchValues } from 'src/shared/utils/windowLocationSearch'
import { InventoryAdvanceSearch } from './InventoryAdvanceSearch'
import { InventoryTable } from './InventoryTable'
Expand All @@ -17,7 +18,7 @@ export default function InventoryPage() {
const [hasError, setHasError] = useState(false)
const searchCrit = searchParams.get('q') || ''
const history = {
changes: searchParams.get('changes')?.split(','),
changes: (searchParams.get('changes')?.split(',') ?? []) as WorkspaceInventorySearchTableHistoryChanges[],
after: searchParams.get('after') || undefined,
before: searchParams.get('before') || undefined,
}
Expand All @@ -39,7 +40,7 @@ export default function InventoryPage() {
[navigate],
)

const hasChanges = !!history.changes
const hasChanges = !!history.changes.length

return (
<NetworkErrorBoundary FallbackComponent={ErrorBoundaryFallback}>
Expand All @@ -56,17 +57,14 @@ export default function InventoryPage() {
<NetworkErrorBoundary FallbackComponent={ErrorBoundaryFallback}>
<ResourceDetail />
</NetworkErrorBoundary>
{searchCrit && (searchCrit !== 'all' || (hasChanges && history.changes) || hasError) ? (
{searchCrit && (searchCrit !== 'all' || (hasChanges && history.changes.length) || hasError) ? (
<>
<NetworkErrorBoundary
fallbackRender={({ resetErrorBoundary }) => (
<InventoryTableError resetErrorBoundary={resetErrorBoundary} searchCrit={searchCrit} setHasError={setHasError} />
)}
>
<InventoryTable
searchCrit={searchCrit}
history={history.changes ? (history as { changes: string[]; after?: string; before?: string }) : undefined}
/>
<InventoryTable searchCrit={searchCrit} history={history.changes.length ? history : undefined} />
</NetworkErrorBoundary>
</>
) : (
Expand Down
9 changes: 3 additions & 6 deletions src/pages/panel/inventory/InventoryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { LoadingSuspenseFallback } from 'src/shared/loading'
import {
PostWorkspaceInventorySearchTableResponse,
WorkspaceInventorySearchTableColumn,
WorkspaceInventorySearchTableHistory,
WorkspaceInventorySearchTableRow,
WorkspaceInventorySearchTableSort,
} from 'src/shared/types/server'
Expand All @@ -28,11 +29,7 @@ import { DownloadCSVButton } from './DownloadCSVButton'

interface InventoryTableProps {
searchCrit: string
history?: {
after?: string
before?: string
changes: string[]
}
history?: WorkspaceInventorySearchTableHistory
}

type RowType = WorkspaceInventorySearchTableRow['row'] & {
Expand Down Expand Up @@ -212,7 +209,7 @@ export const InventoryTable = ({ searchCrit, history }: InventoryTableProps) =>
getRowId={(row: RowType) => row.INTERNAL_ID}
headerToolbar={
<Stack px={1} alignItems="end" width="100%">
<DownloadCSVButton query={searchCrit} hasWarning={dataCount > panelUI.maxCSVDownload} />
<DownloadCSVButton query={searchCrit} hasWarning={dataCount > panelUI.maxCSVDownload} history={history} sort={sorting} />
</Stack>
}
slots={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { AxiosProgressEvent } from 'axios'
import { endPoints } from 'src/shared/constants'
import { PostWorkspaceInventorySearchTableRequest } from 'src/shared/types/server'
import { axiosWithAuth } from 'src/shared/utils/axios'

type PostWorkspaceInventorySearchTableDownloadRequestParams = {
limit?: number
signal: AbortSignal
workspaceId: string
onDownloadProgress: (progressEvent: AxiosProgressEvent) => void
} & Omit<PostWorkspaceInventorySearchTableRequest, 'count' | 'skip' | 'limit'>

export const postWorkspaceInventorySearchTableDownloadMutation = async ({
signal,
query,
workspaceId,
onDownloadProgress,
}: {
signal: AbortSignal
workspaceId: string
query: string
onDownloadProgress: (progressEvent: AxiosProgressEvent) => void
}) => {
limit = 1000,
...data
}: PostWorkspaceInventorySearchTableDownloadRequestParams) => {
return workspaceId
? axiosWithAuth.post<Blob>(
endPoints.workspaces.workspace(workspaceId).inventory.search.table,
{ query, limit: 1000 },
{ headers: { Accept: 'text/csv' }, responseType: 'blob', signal, onDownloadProgress },
{ ...data, limit },
{
headers: { Accept: 'text/csv' },
responseType: 'blob',
signal,
onDownloadProgress,
},
)
: null
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
export type HistoryChanges = 'node_created' | 'node_updated' | 'node_deleted' | 'node_vulnerable' | 'node_compliant'
export type WorkspaceInventorySearchTableHistoryChanges =
| 'node_created'
| 'node_updated'
| 'node_deleted'
| 'node_vulnerable'
| 'node_compliant'

export type WorkspaceInventorySearchTableHistory = {
before?: string | null
after?: string | null
changes: HistoryChanges[]
changes: WorkspaceInventorySearchTableHistoryChanges[]
}

export type WorkspaceInventorySearchTableSort = {
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/server/requests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type { PostWorkspaceInventoryPropertyValuesRequest } from './PostWorkspac
export type {
PostWorkspaceInventorySearchTableRequest,
WorkspaceInventorySearchTableHistory,
WorkspaceInventorySearchTableHistoryChanges,
WorkspaceInventorySearchTableSort,
} from './PostWorkspaceInventorySearchTable'
export type { PostWorkspaceInviteRequest } from './PostWorkspaceInvite'
Expand Down

0 comments on commit f1528ec

Please sign in to comment.