---
policy: /ce/ca/google/compute-engine/instance-underutilized
logic: /ce/ca/google/compute-engine/instance-underutilized/prod.logic.yaml
executionTime: 2025-10-01T12:50:12.210131469Z
generationMs: 67
executionMs: 1407
rows:
  - id: test1
    match: true
    status:
      expected: DISAPPEARED
      actual: DISAPPEARED
    conditionIndex:
      expected: 99
      actual: 99
    conditionText:
      expected: isDisappeared(CA10__disappearanceTime__c)
      actual: isDisappeared(CA10__disappearanceTime__c)
    runtimeError: {}
  - id: test2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__status__c') != 'RUNNING'
      actual: extract('CA10__status__c') != 'RUNNING'
    runtimeError: {}
  - id: test3
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: extract('CA10__creationDate__c').withinLastDays(14)
      actual: extract('CA10__creationDate__c').withinLastDays(14)
    runtimeError: {}
  - id: test4
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__averageCpuTwoWeeks__c').isEmpty() || extract('CA10__maxCpuTwoWeeks__c').isEmpty()
      actual: extract('CA10__averageCpuTwoWeeks__c').isEmpty() || extract('CA10__maxCpuTwoWeeks__c').isEmpty()
    runtimeError: {}
  - id: test5
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 599
      actual: 599
    conditionText:
      expected: extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(50.0)
      actual: extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(50.0)
    runtimeError: {}
  - id: test6
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: extract('CA10__averageCpuTwoWeeks__c') < number(5.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(15.0) && extract('CA10__sumTwoWeekNetworkInMb__c') < number(100.0)
        && extract('CA10__sumTwoWeekNetworkOutMb__c') < number(100.0)
      actual: extract('CA10__averageCpuTwoWeeks__c') < number(5.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(15.0) && extract('CA10__sumTwoWeekNetworkInMb__c') < number(100.0)
        && extract('CA10__sumTwoWeekNetworkOutMb__c') < number(100.0)
    runtimeError: {}
  - id: test7
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 599
      actual: 599
    conditionText:
      expected: extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(50.0)
      actual: extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c')
        < number(50.0)
    runtimeError: {}
  - id: test8
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 600
      actual: 600
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
usedFiles:
  - path: /ce/ca/google/compute-engine/instance-underutilized/policy.yaml
    md5Hash: E866D62BD3C610EDEC967B6975F4F295
    content: "---\nnames:\n  full: Google GCE Instance is underutilized\n  contextual:\
      \ Instance is underutilized\ndescription: \"GCE Instances are considered underutilized\
      \ if their average \n  CPU utilization is consistently below 40% and maximum\
      \ CPU \n  utilization does not spike above 50% over a 14-day period. These instances\
      \ \n  might be oversized for their workload.\"\ntype: \"COMPLIANCE_POLICY\"\n\
      categories:\n  - \"COST\"\n  - \"PERFORMANCE\"\nframeworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/resource-right-sizing\"\
      \n  - \"/frameworks/cloudaware/resource-performance/workload-efficiency\""
  - path: /ce/ca/google/compute-engine/instance-underutilized/prod.logic.yaml
    md5Hash: E256CAB834F6676B3B8AD106DE3A4580
    content: |
      ---
      inputType: "CA10__CaGoogleGceInstance__c"
      importExtracts:
        - file: "/types/CA10__CaGoogleGceInstance__c/object.extracts.yaml"
      testData:
        - file: 'test-data.json'
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "The Instance is not running."
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__status__c"
              right:
                TEXT: "RUNNING"
        - status: "INAPPLICABLE"
          currentStateMessage: "The instance has been running for less than 14 days."
          check:
            IS_WITHIN_LAST_DAYS:
              offsetDays: 14
              arg:
                EXTRACT: "CA10__creationDate__c"
        - status: "UNDETERMINED"
          currentStateMessage: "There is not enough data in the CMDB to identify if the instance is underutilized."
          check:
            OR:
              args:
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__averageCpuTwoWeeks__c"
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__maxCpuTwoWeeks__c"
        - status: "INAPPLICABLE"
          currentStateMessage: "The Instance is idle and considered by the 'Google Cloud SQL Instance is idle' policy."
          check:
            AND:
              args:
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__averageCpuTwoWeeks__c
                    right:
                      NUMBER: 5.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__maxCpuTwoWeeks__c
                    right:
                      NUMBER: 15.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__sumTwoWeekNetworkInMb__c
                    right:
                      NUMBER: 100.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__sumTwoWeekNetworkOutMb__c
                    right:
                      NUMBER: 100.0
        - status: "INCOMPLIANT"
          currentStateMessage: "The instance is underutilized with Average CPU Utilization < 40%\
            \ and Max CPU Utilization < 50%."
          remediationMessage: "Consider rightsizing, consolidating or terminating the instance."
          check:
            AND:
              args:
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__averageCpuTwoWeeks__c
                    right:
                      NUMBER: 40.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__maxCpuTwoWeeks__c
                    right:
                      NUMBER: 50.0
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: 'The Instance is not underutilized.'
  - path: /ce/ca/google/compute-engine/instance-underutilized/test-data.json
    md5Hash: A47C0D042C4101E69DFCC3FDE006727B
    content: |-
      [
          {
              "expectedResult": {
                  "status": "DISAPPEARED",
                  "conditionIndex": "99",
                  "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test1",
              "CA10__disappearanceTime__c": "2025-09-21T19:39:32Z",
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2025-09-19T16:15:39Z",
              "CA10__averageCpuTwoWeeks__c": 5,
              "CA10__maxCpuTwoWeeks__c": 50,
              "CA10__sumTwoWeekNetworkInMb__c": 5400,
              "CA10__sumTwoWeekNetworkOutMb__c": 1491
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "199",
                  "conditionText": "extract('CA10__status__c') != 'RUNNING'",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test2",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "TERMINATED",
              "CA10__creationDate__c": "2025-07-22T14:07:50Z",
              "CA10__averageCpuTwoWeeks__c": 5,
              "CA10__maxCpuTwoWeeks__c": 20,
              "CA10__sumTwoWeekNetworkInMb__c": 390,
              "CA10__sumTwoWeekNetworkOutMb__c": 419
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "299",
                  "conditionText": "extract('CA10__creationDate__c').withinLastDays(14)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test3",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2025-09-19T15:01:00Z",
              "CA10__averageCpuTwoWeeks__c": 5,
              "CA10__maxCpuTwoWeeks__c": 35,
              "CA10__sumTwoWeekNetworkInMb__c": 2526,
              "CA10__sumTwoWeekNetworkOutMb__c": 4579
          },
          {
              "expectedResult": {
                  "status": "UNDETERMINED",
                  "conditionIndex": "399",
                  "conditionText": "extract('CA10__averageCpuTwoWeeks__c').isEmpty() || extract('CA10__maxCpuTwoWeeks__c').isEmpty()",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test4",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2024-09-19T15:01:00Z",
              "CA10__averageCpuTwoWeeks__c": null,
              "CA10__maxCpuTwoWeeks__c": null,
              "CA10__sumTwoWeekNetworkInMb__c": null,
              "CA10__sumTwoWeekNetworkOutMb__c": null
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "599",
                  "conditionText": "extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c') < number(50.0)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test5",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2024-09-19T15:01:00Z",
              "CA10__averageCpuTwoWeeks__c": 1,
              "CA10__maxCpuTwoWeeks__c": 1,
              "CA10__sumTwoWeekNetworkInMb__c": null,
              "CA10__sumTwoWeekNetworkOutMb__c": null
          },
          {
              "expectedResult": {
                  "status": "INAPPLICABLE",
                  "conditionIndex": "499",
                  "conditionText": "extract('CA10__averageCpuTwoWeeks__c') < number(5.0) && extract('CA10__maxCpuTwoWeeks__c') < number(15.0) && extract('CA10__sumTwoWeekNetworkInMb__c') < number(100.0) && extract('CA10__sumTwoWeekNetworkOutMb__c') < number(100.0)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test6",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2024-09-19T15:01:00Z",
              "CA10__averageCpuTwoWeeks__c": 1,
              "CA10__maxCpuTwoWeeks__c": 1,
              "CA10__sumTwoWeekNetworkInMb__c": 1,
              "CA10__sumTwoWeekNetworkOutMb__c": 1
          },
          {
              "expectedResult": {
                  "status": "INCOMPLIANT",
                  "conditionIndex": "599",
                  "conditionText": "extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c') < number(50.0)",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test7",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2025-06-16T18:48:32Z",
              "CA10__averageCpuTwoWeeks__c": 1,
              "CA10__maxCpuTwoWeeks__c": 10,
              "CA10__sumTwoWeekNetworkInMb__c": 397,
              "CA10__sumTwoWeekNetworkOutMb__c": 4089
          },
          {
              "expectedResult": {
                  "status": "COMPLIANT",
                  "conditionIndex": "600",
                  "conditionText": "otherwise",
                  "runtimeError": null
              },
              "context": {
                  "snapshotTime": "2025-09-21T23:29:57Z"
              },
              "Id": "test8",
              "CA10__disappearanceTime__c": null,
              "CA10__status__c": "RUNNING",
              "CA10__creationDate__c": "2025-06-21T20:39:56Z",
              "CA10__averageCpuTwoWeeks__c": 40,
              "CA10__maxCpuTwoWeeks__c": 90,
              "CA10__sumTwoWeekNetworkInMb__c": 5113,
              "CA10__sumTwoWeekNetworkOutMb__c": 2901
          }
      ]
  - path: /types/CA10__CaGoogleGceInstance__c/object.extracts.yaml
    md5Hash: 75A538D7EA49443F6DA07C5D35BF9021
    content: "---\nextracts:\n  - name: \"Name\"\n    value: \n      FIELD:\n    \
      \    path: \"Name\"\n        undeterminedIf:\n          isEmpty:  \"Name should\
      \ not be empty\"\n  - name: \"CA10__labels__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__labels__c\"\n        undeterminedIf:\n          isEmpty:\
      \  \"Labels should not be empty\"\n  - name: \"CA10__ipForwarding__c\"\n   \
      \ value: \n      FIELD:\n        path: \"CA10__ipForwarding__c\"\n        undeterminedIf:\n\
      \          isEmpty: \"IP forwarding can't be empty\"\n  - name: \"CA10__shieldedIntegrityMonitoringState__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__shieldedIntegrityMonitoringState__c\"\
      \n  - name: \"CA10__shieldedVtpmState__c\"\n    value: \n      FIELD:\n    \
      \    path: \"CA10__shieldedVtpmState__c\"\n  - name: \"CA10__confidentialComputeStatus__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__confidentialComputeStatus__c\"\
      \n  - name: \"CA10__machineTypeName__c\"\n    value: \n      FIELD:\n      \
      \  path: \"CA10__machineTypeName__c\"\n  - name: \"CA10__metadataJson__c\"\n\
      \    value: \n      FIELD:\n        path: \"CA10__metadataJson__c\"\n      \
      \  returnType: BYTES\n  - name: \"caJsonFrom__metadataJson__c\"\n    value:\
      \ \n      JSON_FROM:\n        arg: \n          EXTRACT: \"CA10__metadataJson__c\"\
      \n        undeterminedIf:\n          isInvalid: \"Metadata JSON is invalid.\"\
      \n          isEmpty: \"Metadata JSON is empty.\"\n  - name: \"caJsonText__metadataJsonBlockProjectSshKeysValue\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__metadataJson__c\"\
      \n        expression: \"to_string(items[?key=='block-project-ssh-keys'].value)\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return a text.\"\n\
      \          resultIsEmpty: \"The JSON query did not return a value.\"\n  - name:\
      \ \"caJsonText__metadataJsonEnableOsloginValue\"\n    value: \n      JSON_QUERY_TEXT:\n\
      \        arg:\n          EXTRACT: \"caJsonFrom__metadataJson__c\"\n        expression:\
      \ \"to_string(items[?key=='enable-oslogin'].value)\"\n        undeterminedIf:\n\
      \          evaluationError: \"The JSON query has failed.\"\n          resultTypeMismatch:\
      \ \"The JSON query did not return a text.\"\n          resultIsEmpty: \"The\
      \ JSON query did not return a value.\"\n  - name: \"caJsonText__metadataJsonSerialPortEnableValue\"\
      \n    value: \n      JSON_QUERY_TEXT:\n        arg:\n          EXTRACT: \"caJsonFrom__metadataJson__c\"\
      \n        expression: \"to_string(items[?key=='serial-port-enable'].value)\"\
      \n        undeterminedIf:\n          evaluationError: \"The JSON query has failed.\"\
      \n          resultTypeMismatch: \"The JSON query did not return a text.\"\n\
      \          resultIsEmpty: \"The JSON query did not return a value.\"\n# Nullable\n\
      \  - name: \"CA10__averageCpuTwoWeeks__c\"\n    value: \n      FIELD:\n    \
      \    path: \"CA10__averageCpuTwoWeeks__c\"\n# Nullable\n  - name: \"CA10__maxCpuTwoWeeks__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__maxCpuTwoWeeks__c\"\n# Nullable\n\
      \  - name: \"CA10__sumTwoWeekNetworkInMb__c\"\n    value: \n      FIELD:\n \
      \       path: \"CA10__sumTwoWeekNetworkInMb__c\"\n# Nullable\n  - name: \"CA10__sumTwoWeekNetworkOutMb__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__sumTwoWeekNetworkOutMb__c\"\
      \n  - name: \"CA10__status__c\"\n    value: \n      FIELD:\n        path: \"\
      CA10__status__c\"\n  - name: \"CA10__creationDate__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__creationDate__c\"\n# Text\n  - name: \"CA10__regionName__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__regionName__c\"\n"
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "test1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__status__c') != 'RUNNING'",
      "runtimeError" : null
    }
  }, {
    "Id" : "test3",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "299",
      "conditionText" : "extract('CA10__creationDate__c').withinLastDays(14)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test4",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__averageCpuTwoWeeks__c').isEmpty() || extract('CA10__maxCpuTwoWeeks__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "test5",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "599",
      "conditionText" : "extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c') < number(50.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test6",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "499",
      "conditionText" : "extract('CA10__averageCpuTwoWeeks__c') < number(5.0) && extract('CA10__maxCpuTwoWeeks__c') < number(15.0) && extract('CA10__sumTwoWeekNetworkInMb__c') < number(100.0) && extract('CA10__sumTwoWeekNetworkOutMb__c') < number(100.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test7",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "599",
      "conditionText" : "extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c') < number(50.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "test8",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "600",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaGoogleGceInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__status__c STRING,
      CA10__creationDate__c TIMESTAMP,
      CA10__averageCpuTwoWeeks__c FLOAT64,
      CA10__maxCpuTwoWeeks__c FLOAT64,
      CA10__sumTwoWeekNetworkInMb__c FLOAT64,
      CA10__sumTwoWeekNetworkOutMb__c FLOAT64,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test1",
    "CA10__disappearanceTime__c" : new Date("2025-09-21T19:39:32Z"),
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2025-09-19T16:15:39Z"),
    "CA10__averageCpuTwoWeeks__c" : 5,
    "CA10__maxCpuTwoWeeks__c" : 50,
    "CA10__sumTwoWeekNetworkInMb__c" : 5400,
    "CA10__sumTwoWeekNetworkOutMb__c" : 1491
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test2",
    "CA10__status__c" : "TERMINATED",
    "CA10__creationDate__c" : new Date("2025-07-22T14:07:50Z"),
    "CA10__averageCpuTwoWeeks__c" : 5,
    "CA10__maxCpuTwoWeeks__c" : 20,
    "CA10__sumTwoWeekNetworkInMb__c" : 390,
    "CA10__sumTwoWeekNetworkOutMb__c" : 419
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test3",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2025-09-19T15:01:00Z"),
    "CA10__averageCpuTwoWeeks__c" : 5,
    "CA10__maxCpuTwoWeeks__c" : 35,
    "CA10__sumTwoWeekNetworkInMb__c" : 2526,
    "CA10__sumTwoWeekNetworkOutMb__c" : 4579
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test4",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2024-09-19T15:01:00Z"),
    "CA10__averageCpuTwoWeeks__c" : null,
    "CA10__maxCpuTwoWeeks__c" : null,
    "CA10__sumTwoWeekNetworkInMb__c" : null,
    "CA10__sumTwoWeekNetworkOutMb__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test5",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2024-09-19T15:01:00Z"),
    "CA10__averageCpuTwoWeeks__c" : 1,
    "CA10__maxCpuTwoWeeks__c" : 1,
    "CA10__sumTwoWeekNetworkInMb__c" : null,
    "CA10__sumTwoWeekNetworkOutMb__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test6",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2024-09-19T15:01:00Z"),
    "CA10__averageCpuTwoWeeks__c" : 1,
    "CA10__maxCpuTwoWeeks__c" : 1,
    "CA10__sumTwoWeekNetworkInMb__c" : 1,
    "CA10__sumTwoWeekNetworkOutMb__c" : 1
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test7",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2025-06-16T18:48:32Z"),
    "CA10__averageCpuTwoWeeks__c" : 1,
    "CA10__maxCpuTwoWeeks__c" : 10,
    "CA10__sumTwoWeekNetworkInMb__c" : 397,
    "CA10__sumTwoWeekNetworkOutMb__c" : 4089
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-09-21T23:29:57Z")
    },
    "Id" : "test8",
    "CA10__status__c" : "RUNNING",
    "CA10__creationDate__c" : new Date("2025-06-21T20:39:56Z"),
    "CA10__averageCpuTwoWeeks__c" : 40,
    "CA10__maxCpuTwoWeeks__c" : 90,
    "CA10__sumTwoWeekNetworkInMb__c" : 5113,
    "CA10__sumTwoWeekNetworkOutMb__c" : 2901
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaGoogleGceInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__status__c STRING,
          CA10__creationDate__c TIMESTAMP,
          CA10__averageCpuTwoWeeks__c FLOAT64,
          CA10__maxCpuTwoWeeks__c FLOAT64,
          CA10__sumTwoWeekNetworkInMb__c FLOAT64,
          CA10__sumTwoWeekNetworkOutMb__c FLOAT64
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var TextLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
          };
          this.isEmpty = function(arg) {
              return this.normalize(arg) == '';
          };
          this.isNotEmpty = function(arg) {
              return this.normalize(arg) != '';
          };
          this.equal = function(left, right) {
              return this.normalize(left) == this.normalize(right);
          };
          this.notEqual = function(left, right) {
              return this.normalize(left) != this.normalize(right);
          };
          this.startsWith = function(arg, substring) {
              return this.normalize(arg).startsWith(this.normalize(substring));
          };
          this.endsWith = function(arg, substring) {
              return this.normalize(arg).endsWith(this.normalize(substring));
          };
          this.contains = function(arg, substring) {
              return this.normalize(arg).includes(this.normalize(substring));
          };
          this.containsAll = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.every(sub => normalizedArg.includes(this.normalize(sub)));
          };
          this.containsAny = function(arg, substrings) {
              if (substrings == null || substrings.length === 0) return false;
              let normalizedArg = this.normalize(arg);
              return substrings.some(sub => normalizedArg.includes(this.normalize(sub)));
          };
      }();
      var todayMinus14Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-14 * 86400000);
      var todayPlus1Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (1 * 86400000);
      var IsEmptyLib = new function () {
          this.simpleIsEmpty = function(arg) {
              return arg == null;
          };
          this.simpleIsNotEmpty = function(arg) {
              return arg != null;
          };
      }();
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From Google [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[1], conditionIndex:[100..199]
      function extract3() { if (!this.out) { this.out = obj.CA10__status__c; } return this.out; };
      references1.push('Status [obj.CA10__status__c]: ' + obj.CA10__status__c);
      if (TextLib.notEqual(extract3.call(extract3), 'RUNNING')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__status__c') != 'RUNNING'", currentStateMessage: "The Instance is not running.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__creationDate__c; } return this.out; };
      references1.push('Creation Date [obj.CA10__creationDate__c]: ' + obj.CA10__creationDate__c);
      if (extract6.call(extract6) != null && extract6.call(extract6).getTime() >= todayMinus14Day && extract6.call(extract6).getTime() < todayPlus1Day) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "extract('CA10__creationDate__c').withinLastDays(14)", currentStateMessage: "The instance has been running for less than 14 days.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      function extract10() { if (!this.out) { this.out = obj.CA10__averageCpuTwoWeeks__c; } return this.out; };
      function extract13() { if (!this.out) { this.out = obj.CA10__maxCpuTwoWeeks__c; } return this.out; };
      references1.push('Metrics: CPU, 14-Day [obj.CA10__averageCpuTwoWeeks__c]: ' + obj.CA10__averageCpuTwoWeeks__c);
      references1.push('Metrics: CPU Max, 14-Day [obj.CA10__maxCpuTwoWeeks__c]: ' + obj.CA10__maxCpuTwoWeeks__c);
      if (IsEmptyLib.simpleIsEmpty(extract10.call(extract10)) || IsEmptyLib.simpleIsEmpty(extract13.call(extract13))) {
          return {status: 'UNDETERMINED', conditionIndex: 399, conditionText: "extract('CA10__averageCpuTwoWeeks__c').isEmpty() || extract('CA10__maxCpuTwoWeeks__c').isEmpty()", currentStateMessage: "There is not enough data in the CMDB to identify if the instance is underutilized.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      function extract17() { if (!this.out) { this.out = obj.CA10__averageCpuTwoWeeks__c; } return this.out; };
      function extract21() { if (!this.out) { this.out = obj.CA10__maxCpuTwoWeeks__c; } return this.out; };
      function extract25() { if (!this.out) { this.out = obj.CA10__sumTwoWeekNetworkInMb__c; } return this.out; };
      function extract29() { if (!this.out) { this.out = obj.CA10__sumTwoWeekNetworkOutMb__c; } return this.out; };
      references1.push('Metrics: Network In, MB, 14-Day [obj.CA10__sumTwoWeekNetworkInMb__c]: ' + obj.CA10__sumTwoWeekNetworkInMb__c);
      references1.push('Metrics: Network Out, MB, 14-Day [obj.CA10__sumTwoWeekNetworkOutMb__c]: ' + obj.CA10__sumTwoWeekNetworkOutMb__c);
      if ((extract17.call(extract17) != null && 5.0 != null && extract17.call(extract17) < 5.0) && (extract21.call(extract21) != null && 15.0 != null && extract21.call(extract21) < 15.0) && (extract25.call(extract25) != null && 100.0 != null && extract25.call(extract25) < 100.0) && (extract29.call(extract29) != null && 100.0 != null && extract29.call(extract29) < 100.0)) {
          return {status: 'INAPPLICABLE', conditionIndex: 499, conditionText: "extract('CA10__averageCpuTwoWeeks__c') < number(5.0) && extract('CA10__maxCpuTwoWeeks__c') < number(15.0) && extract('CA10__sumTwoWeekNetworkInMb__c') < number(100.0) && extract('CA10__sumTwoWeekNetworkOutMb__c') < number(100.0)", currentStateMessage: "The Instance is idle and considered by the 'Google Cloud SQL Instance is idle' policy.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[5], conditionIndex:[500..599]
      function extract33() { if (!this.out) { this.out = obj.CA10__averageCpuTwoWeeks__c; } return this.out; };
      function extract37() { if (!this.out) { this.out = obj.CA10__maxCpuTwoWeeks__c; } return this.out; };
      if ((extract33.call(extract33) != null && 40.0 != null && extract33.call(extract33) < 40.0) && (extract37.call(extract37) != null && 50.0 != null && extract37.call(extract37) < 50.0)) {
          return {status: 'INCOMPLIANT', conditionIndex: 599, conditionText: "extract('CA10__averageCpuTwoWeeks__c') < number(40.0) && extract('CA10__maxCpuTwoWeeks__c') < number(50.0)", currentStateMessage: "The instance is underutilized with Average CPU Utilization < 40% and Max CPU Utilization < 50%.", currentStateReferences: references1.join('\n'), remediation: "Consider rightsizing, consolidating or terminating the instance.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 600, conditionText: "otherwise", currentStateMessage: "The Instance is not underutilized.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.Id AS Id,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__status__c AS CA10__status__c,
          sObject.CA10__creationDate__c AS CA10__creationDate__c,
          sObject.CA10__averageCpuTwoWeeks__c AS CA10__averageCpuTwoWeeks__c,
          sObject.CA10__maxCpuTwoWeeks__c AS CA10__maxCpuTwoWeeks__c,
          sObject.CA10__sumTwoWeekNetworkInMb__c AS CA10__sumTwoWeekNetworkInMb__c,
          sObject.CA10__sumTwoWeekNetworkOutMb__c AS CA10__sumTwoWeekNetworkOutMb__c,
          process_CA10__CaGoogleGceInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__status__c AS CA10__status__c,
                  sObject.CA10__creationDate__c AS CA10__creationDate__c,
                  sObject.CA10__averageCpuTwoWeeks__c AS CA10__averageCpuTwoWeeks__c,
                  sObject.CA10__maxCpuTwoWeeks__c AS CA10__maxCpuTwoWeeks__c,
                  sObject.CA10__sumTwoWeekNetworkInMb__c AS CA10__sumTwoWeekNetworkInMb__c,
                  sObject.CA10__sumTwoWeekNetworkOutMb__c AS CA10__sumTwoWeekNetworkOutMb__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaGoogleGceInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
