From c87d5549cb02b02bd70dac02167d187b7bd5ad31 Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Fri, 7 Feb 2025 12:18:43 +0100 Subject: [PATCH 1/3] feat: implement new functionality with inserted_at__gte in findings and metadata --- ui/app/(prowler)/findings/page.tsx | 53 +++++++++++++++----- ui/components/filters/custom-date-picker.tsx | 6 +-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/ui/app/(prowler)/findings/page.tsx b/ui/app/(prowler)/findings/page.tsx index 39d2ad7846..746e63263c 100644 --- a/ui/app/(prowler)/findings/page.tsx +++ b/ui/app/(prowler)/findings/page.tsx @@ -13,6 +13,8 @@ import { import { Header } from "@/components/ui"; import { DataTable, DataTableFilterCustom } from "@/components/ui/table"; import { createDict } from "@/lib"; +import { subDays, format } from "date-fns"; + import { FindingProps, ProviderProps, @@ -31,7 +33,16 @@ export default async function Findings({ // Make sure the sort is correctly encoded const encodedSort = sort?.replace(/^\+/, ""); + const twoDaysAgo = format(subDays(new Date(), 2), "yyyy-MM-dd"); + + // Default filters for getMetadataInfo + const defaultFilters = { + "filter[inserted_at__gte]": twoDaysAgo, + }; + + // Extract all filter parameters and combine with default filters const filters: Record = { + ...defaultFilters, ...Object.fromEntries( Object.entries(searchParams) .filter(([key]) => key.startsWith("filter[")) @@ -44,11 +55,15 @@ export default async function Findings({ const query = filters["filter[search]"] || ""; - const metadataInfoData = await getMetadataInfo({ - query, - sort: encodedSort, - filters, - }); + const [metadataInfoData, providersData, scansData] = await Promise.all([ + getMetadataInfo({ + query, + sort: encodedSort, + filters, + }), + getProviders({}), + getScans({}), + ]); // Extract unique regions and services from the new endpoint const uniqueRegions = metadataInfoData?.data?.attributes?.regions || []; @@ -56,8 +71,6 @@ export default async function Findings({ const uniqueResourceTypes = metadataInfoData?.data?.attributes?.resource_types || []; // Get findings data - const providersData = await getProviders({}); - const scansData = await getScans({}); // Extract provider UIDs const providerUIDs = Array.from( @@ -141,7 +154,15 @@ const SSRDataTable = async ({ // Make sure the sort is correctly encoded const encodedSort = sort.replace(/^\+/, ""); + const twoDaysAgo = format(subDays(new Date(), 2), "yyyy-MM-dd"); + + // Default filters for getFindings + const defaultFilters = { + "filter[inserted_at__gte]": twoDaysAgo, + }; + const filters: Record = { + ...defaultFilters, ...Object.fromEntries( Object.entries(searchParams) .filter(([key]) => key.startsWith("filter[")) @@ -190,10 +211,18 @@ const SSRDataTable = async ({ }; return ( - + <> + {findingsData?.errors && ( +
+

Error:

+

{findingsData.errors[0].detail}

+
+ )} + + ); }; diff --git a/ui/components/filters/custom-date-picker.tsx b/ui/components/filters/custom-date-picker.tsx index 4d9708397a..7b39d6a6d8 100644 --- a/ui/components/filters/custom-date-picker.tsx +++ b/ui/components/filters/custom-date-picker.tsx @@ -16,7 +16,7 @@ export const CustomDatePicker = () => { const searchParams = useSearchParams(); const [value, setValue] = React.useState(() => { - const dateParam = searchParams.get("filter[updated_at]"); + const dateParam = searchParams.get("filter[inserted_at]"); return dateParam ? today(getLocalTimeZone()) : null; }); @@ -30,9 +30,9 @@ export const CustomDatePicker = () => { (date: any) => { const params = new URLSearchParams(searchParams.toString()); if (date) { - params.set("filter[updated_at]", date.toString()); + params.set("filter[inserted_at]", date.toString()); } else { - params.delete("filter[updated_at]"); + params.delete("filter[inserted_at]"); } router.push(`?${params.toString()}`, { scroll: false }); }, From 1f309b9453b30c83ce9f2ce3d79aaabc3850c539 Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Fri, 7 Feb 2025 12:51:39 +0100 Subject: [PATCH 2/3] fix(findings): skip default date filter if or exists --- ui/app/(prowler)/findings/page.tsx | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ui/app/(prowler)/findings/page.tsx b/ui/app/(prowler)/findings/page.tsx index 746e63263c..059438fd3f 100644 --- a/ui/app/(prowler)/findings/page.tsx +++ b/ui/app/(prowler)/findings/page.tsx @@ -1,4 +1,5 @@ import { Spacer } from "@nextui-org/react"; +import { format, subDays } from "date-fns"; import React, { Suspense } from "react"; import { getFindings, getMetadataInfo } from "@/actions/findings"; @@ -13,8 +14,6 @@ import { import { Header } from "@/components/ui"; import { DataTable, DataTableFilterCustom } from "@/components/ui/table"; import { createDict } from "@/lib"; -import { subDays, format } from "date-fns"; - import { FindingProps, ProviderProps, @@ -35,10 +34,15 @@ export default async function Findings({ const twoDaysAgo = format(subDays(new Date(), 2), "yyyy-MM-dd"); + // Check if the searchParams contain any date or scan filter + const hasDateOrScanFilter = Object.keys(searchParams).some( + (key) => key.includes("inserted_at") || key.includes("scan__in"), + ); + // Default filters for getMetadataInfo - const defaultFilters = { - "filter[inserted_at__gte]": twoDaysAgo, - }; + const defaultFilters: Record = hasDateOrScanFilter + ? {} // Do not apply default filters if there are date or scan filters + : { "filter[inserted_at__gte]": twoDaysAgo }; // Extract all filter parameters and combine with default filters const filters: Record = { @@ -156,10 +160,15 @@ const SSRDataTable = async ({ const twoDaysAgo = format(subDays(new Date(), 2), "yyyy-MM-dd"); + // Check if the searchParams contain any date or scan filter + const hasDateOrScanFilter = Object.keys(searchParams).some( + (key) => key.includes("inserted_at") || key.includes("scan__in"), + ); + // Default filters for getFindings - const defaultFilters = { - "filter[inserted_at__gte]": twoDaysAgo, - }; + const defaultFilters: Record = hasDateOrScanFilter + ? {} // Do not apply default filters if there are date or scan filters + : { "filter[inserted_at__gte]": twoDaysAgo }; const filters: Record = { ...defaultFilters, From 04f595341397a7728329af5fd8c602a922d9e05f Mon Sep 17 00:00:00 2001 From: Pablo Lara Date: Fri, 7 Feb 2025 13:18:43 +0100 Subject: [PATCH 3/3] feat: implement new functionality with inserted_at__gte in overview page --- ui/app/(prowler)/page.tsx | 19 +++++++++++++++---- .../link-to-findings/link-to-findings.tsx | 2 +- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ui/app/(prowler)/page.tsx b/ui/app/(prowler)/page.tsx index c6e0748362..e1b02c2bbe 100644 --- a/ui/app/(prowler)/page.tsx +++ b/ui/app/(prowler)/page.tsx @@ -1,4 +1,5 @@ import { Spacer } from "@nextui-org/react"; +import { format, subDays } from "date-fns"; import { Suspense } from "react"; import { getFindings } from "@/actions/findings/findings"; @@ -131,9 +132,12 @@ const SSRDataNewFindingsTable = async () => { const page = 1; const sort = "severity,-inserted_at"; + const twoDaysAgo = format(subDays(new Date(), 2), "yyyy-MM-dd"); + const defaultFilters = { "filter[status__in]": "FAIL", "filter[delta__in]": "new", + "filter[inserted_at__gte]": twoDaysAgo, }; const findingsData = await getFindings({ @@ -172,14 +176,21 @@ const SSRDataNewFindingsTable = async () => { return ( <> -
-

- Latest 10 failing findings to date by Severity -

+
+
+

+ Latest 10 failing findings to date by Severity +

+

+ Showing the latest 10 failing findings by severity from the last 2 + days. +

+
+ { return (