Group findings by time

Demonstrates how to group active findings for specific points in time

Code sample

Java

static ImmutableList<GroupResult> groupActiveFindingsWithSourceAtTime(SourceName sourceName) {
  try (SecurityCenterClient client = SecurityCenterClient.create()) {
    // SourceName sourceName = SourceName.of(/*organization=*/"123234324",/*source=*/
    // "423432321");

    // 1 day ago
    Instant oneDayAgo = Instant.now().minusSeconds(60 * 60 * 24);

    GroupFindingsRequest.Builder request =
        GroupFindingsRequest.newBuilder()
            .setParent(sourceName.toString())
            .setGroupBy("category")
            .setFilter("state=\"ACTIVE\"")
            .setReadTime(
                Timestamp.newBuilder()
                    .setSeconds(oneDayAgo.getEpochSecond())
                    .setNanos(oneDayAgo.getNano()));

    // Call the API.
    GroupFindingsPagedResponse response = client.groupFindings(request.build());

    // This creates one list for all findings.  If your organization has a large number of
    // findings
    // this can cause out of memory issues.  You can process them batches by returning
    // the Iterable returned response.iterateAll() directly.
    ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
    System.out.println("Findings:");
    System.out.println(results);
    return results;
  } catch (IOException e) {
    throw new RuntimeException("Couldn't create client.", e);
  }
}

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.