---
policy: /ce/ca/azure/virtual-machine/virtual-machine-underutilized
logic: /ce/ca/azure/virtual-machine/virtual-machine-underutilized/prod.logic.yaml
executionTime: 2025-05-10T12:04:15.723356294Z
rows:
  - id: 1
    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: 2
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: extract('CA10__powerState__c') != 'VM running'
      actual: extract('CA10__powerState__c') != 'VM running'
    runtimeError: {}
  - id: 3
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: not(extract('CA10__timeCreated__c').beyondLastDays(14))
      actual: not(extract('CA10__timeCreated__c').beyondLastDays(14))
    runtimeError: {}
  - id: 4
    match: true
    status:
      expected: UNDETERMINED
      actual: UNDETERMINED
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: extract('CA10__monitorCpuAvg14Day__c').isEmpty() || extract('CA10__monitorCpuMax14Day__c').isEmpty()
      actual: extract('CA10__monitorCpuAvg14Day__c').isEmpty() || extract('CA10__monitorCpuMax14Day__c').isEmpty()
    runtimeError: {}
  - id: 5
    match: true
    status:
      expected: INCOMPLIANT
      actual: INCOMPLIANT
    conditionIndex:
      expected: 599
      actual: 599
    conditionText:
      expected: extract('CA10__monitorCpuAvg14Day__c') < number(40.0) && extract('CA10__monitorCpuMax14Day__c')
        < number(50.0)
      actual: extract('CA10__monitorCpuAvg14Day__c') < number(40.0) && extract('CA10__monitorCpuMax14Day__c')
        < number(50.0)
    runtimeError: {}
  - id: 6
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 600
      actual: 600
    conditionText:
      expected: otherwise
      actual: otherwise
    runtimeError: {}
  - id: 7
    match: true
    status:
      expected: INAPPLICABLE
      actual: INAPPLICABLE
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: extract('CA10__monitorCpuAvg14Day__c') < number(5.0) && extract('CA10__monitorCpuMax14Day__c')
        < number(15.0) && extract('CA10__monitorNetworkInMbSum14Day__c') < number(100.0)
        && extract('CA10__monitorNetworkOutMbSum14Day__c') < number(100.0)
      actual: extract('CA10__monitorCpuAvg14Day__c') < number(5.0) && extract('CA10__monitorCpuMax14Day__c')
        < number(15.0) && extract('CA10__monitorNetworkInMbSum14Day__c') < number(100.0)
        && extract('CA10__monitorNetworkOutMbSum14Day__c') < number(100.0)
    runtimeError: {}
usedFiles:
  - path: /ce/ca/azure/virtual-machine/virtual-machine-underutilized/policy.yaml
    md5Hash: 798F9AB6595BD34D74F447A8BE3A373A
    content: "---\nnames:\n  full: \"Azure Virtual Machine is underutilized\"\n  contextual:\
      \ \"Virtual Machine is underutilized\"\ndescription: >\n  Identify Azure Virtual\
      \ Machines that appear to be underutilized \n  and downsize (resize) them to\
      \ optimize cloud expenditure. \n  By default, a Virtual Machine is considered\
      \ underutilized if their \n  average CPU utilization is consistently below 40%\
      \ and maximum CPU utilization \n  does not spike above 50% over a 14-day period.\n\
      categories:\n  - \"COST\"\n  - \"PERFORMANCE\"\ntype: \"COMPLIANCE_POLICY\"\n\
      frameworkMappings:\n  - \"/frameworks/cloudaware/cost-efficiency-and-optimization/resource-right-sizing\"\
      \n  - \"/frameworks/cloudaware/resource-performance/workload-efficiency\""
  - path: /ce/ca/azure/virtual-machine/virtual-machine-underutilized/prod.logic.yaml
    md5Hash: DB0D946C433EAFBC967E23AEC65A8A89
    content: |-
      ---
      inputType: "CA10__CaAzureVirtualMachine__c"
      testData:
        - file: test-data.json
      importExtracts:
        - file: /types/CA10__CaAzureVirtualMachine__c/object.extracts.yaml
      conditions:
        - status: "INAPPLICABLE"
          currentStateMessage: "Only for running VM"
          check:
            NOT_EQUAL:
              left:
                EXTRACT: "CA10__powerState__c"
              right:
                TEXT: "VM running"
        - status: "INAPPLICABLE"
          currentStateMessage: "The VM has been running for less than 14 days."
          check:
            NOT:
              arg:
                IS_BEYOND_LAST_DAYS:
                  offsetDays: 14
                  arg:
                    EXTRACT: "CA10__timeCreated__c"
        - status: UNDETERMINED
          currentStateMessage: "There is not enough data in the CMDB to identify if the VM is underutilized."
          check:
            OR:
              args:
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__monitorCpuAvg14Day__c"
                - IS_EMPTY:
                    arg:
                      EXTRACT: "CA10__monitorCpuMax14Day__c"
        - status: INAPPLICABLE
          currentStateMessage: "The vm is idle and considered by the 'Azure Virtual Machine is idle' policy."
          check:
            AND:
              args:
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__monitorCpuAvg14Day__c
                    right:
                      NUMBER: 5.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__monitorCpuMax14Day__c
                    right:
                      NUMBER: 15.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__monitorNetworkInMbSum14Day__c
                    right:
                      NUMBER: 100.0
                - LESS_THAN:
                    left:
                      EXTRACT: CA10__monitorNetworkOutMbSum14Day__c
                    right:
                      NUMBER: 100.0
        - status: "INCOMPLIANT"
          currentStateMessage: "The VM is underutilized with Average CPU Utilization < 40%\
            \ and Max CPU Utilization < 50%."
          remediationMessage: "Consider rightsizing, consolidating or terminating the VM."
          check:
            AND:
              args:
                - LESS_THAN:
                    left:
                      EXTRACT: "CA10__monitorCpuAvg14Day__c"
                    right:
                      NUMBER: 40.0
                - LESS_THAN:
                    left:
                      EXTRACT: "CA10__monitorCpuMax14Day__c"
                    right:
                      NUMBER: 50.0
      otherwise:
        status: "COMPLIANT"
        currentStateMessage: 'The VM is not underutilized.'
  - path: /types/CA10__CaAzureVirtualMachine__c/object.extracts.yaml
    md5Hash: 6E9F97B836205092BD497D4536766346
    content: "---\nextracts:\n# Values: TrustedLaunch, ConfidentialVM. Nullable. Retrieved\
      \ via Microsoft.Compute/virtualMachines\n  - name: \"CA10__securityProfileSecurityType__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__securityProfileSecurityType__c\"\
      \n# Values: Enabled, Disabled. Nullable. Retrieved via Microsoft.Compute/virtualMachines\n\
      \  - name: \"CA10__uefiSettingsVTpm__c\"\n    value: \n      FIELD:\n      \
      \  path: \"CA10__uefiSettingsVTpm__c\"\n# Values: Enabled, Disabled. Nullable.\
      \ Retrieved via Microsoft.Compute/virtualMachines\n  - name: \"CA10__uefiSettingsSecureBoot__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__uefiSettingsSecureBoot__c\"\
      \   \n# Values: \"VM creating\", \"VM starting\",  \"VM running\", \"VM stopping\"\
      ,  \"VM stopped\", \"VM deallocating\",  \"VM deallocated\"\n  - name: \"CA10__powerState__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__powerState__c\" \n# Percent\n\
      \  - name: \"CA10__monitorCpuAvg14Day__c\"\n    value: \n      FIELD:\n    \
      \    path: \"CA10__monitorCpuAvg14Day__c\" \n# Percent\n  - name: \"CA10__monitorCpuMax14Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__monitorCpuMax14Day__c\" \n\
      \  - name: \"CA10__monitorNetworkInMbSum14Day__c\"\n    value: \n      FIELD:\n\
      \        path: \"CA10__monitorNetworkInMbSum14Day__c\" \n  - name: \"CA10__monitorNetworkOutMbSum14Day__c\"\
      \n    value: \n      FIELD:\n        path: \"CA10__monitorNetworkOutMbSum14Day__c\"\
      \ \n# Date/time\n  - name: \"CA10__timeCreated__c\"\n    value:\n      FIELD:\n\
      \        path: \"CA10__timeCreated__c\""
  - path: /ce/ca/azure/virtual-machine/virtual-machine-underutilized/test-data.json
    md5Hash: 04655608ED06CF7DBAE9B1FE6C39ECE8
    content: |
      [
        {
          "expectedResult": {
            "status": "DISAPPEARED",
            "conditionIndex": "99",
            "conditionText": "isDisappeared(CA10__disappearanceTime__c)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "1",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": "2023-05-22T10:08:05Z",
          "CA10__powerState__c": "VM deallocated",
          "CA10__timeCreated__c": "2023-03-09T11:53:28Z",
          "CA10__monitorCpuAvg14Day__c": null,
          "CA10__monitorCpuMax14Day__c": null,
          "CA10__monitorNetworkInMbSum14Day__c": null,
          "CA10__monitorNetworkOutMbSum14Day__c": null,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "199",
            "conditionText": "extract('CA10__powerState__c') != 'VM running'",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "2",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM deallocated",
          "CA10__timeCreated__c": "2020-10-21T08:03:49Z",
          "CA10__monitorCpuAvg14Day__c": 10.00,
          "CA10__monitorCpuMax14Day__c": 20.00,
          "CA10__monitorNetworkInMbSum14Day__c": 1000.0,
          "CA10__monitorNetworkOutMbSum14Day__c": 1000.0,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "299",
            "conditionText": "not(extract('CA10__timeCreated__c').beyondLastDays(14))",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "3",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM running",
          "CA10__timeCreated__c": "2025-04-10T14:46:26Z",
          "CA10__monitorCpuAvg14Day__c": 3.534,
          "CA10__monitorCpuMax14Day__c": 92.43,
          "CA10__monitorNetworkInMbSum14Day__c": 1000.0,
          "CA10__monitorNetworkOutMbSum14Day__c": 1000.0,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "UNDETERMINED",
            "conditionIndex": "399",
            "conditionText": "extract('CA10__monitorCpuAvg14Day__c').isEmpty() || extract('CA10__monitorCpuMax14Day__c').isEmpty()",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "4",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM running",
          "CA10__timeCreated__c": "2020-10-21T08:03:49Z",
          "CA10__monitorCpuAvg14Day__c": null,
          "CA10__monitorCpuMax14Day__c": null,
          "CA10__monitorNetworkInMbSum14Day__c": null,
          "CA10__monitorNetworkOutMbSum14Day__c": null,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "INCOMPLIANT",
            "conditionIndex": "599",
            "conditionText": "extract('CA10__monitorCpuAvg14Day__c') < number(40.0) && extract('CA10__monitorCpuMax14Day__c') < number(50.0)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "5",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM running",
          "CA10__timeCreated__c": "2021-03-01T12:20:59Z",
          "CA10__monitorCpuAvg14Day__c": 30.62,
          "CA10__monitorCpuMax14Day__c": 41.44,
          "CA10__monitorNetworkInMbSum14Day__c": null,
          "CA10__monitorNetworkOutMbSum14Day__c": null,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "600",
            "conditionText": "otherwise",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "6",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM running",
          "CA10__timeCreated__c": "2020-01-03T17:32:05Z",
          "CA10__monitorCpuAvg14Day__c": 10.886110654679344,
          "CA10__monitorCpuMax14Day__c": 95.15,
          "CA10__monitorNetworkInMbSum14Day__c": null,
          "CA10__monitorNetworkOutMbSum14Day__c": null,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        },
        {
          "expectedResult": {
            "status": "INAPPLICABLE",
            "conditionIndex": "499",
            "conditionText": "extract('CA10__monitorCpuAvg14Day__c') < number(5.0) && extract('CA10__monitorCpuMax14Day__c') < number(15.0) && extract('CA10__monitorNetworkInMbSum14Day__c') < number(100.0) && extract('CA10__monitorNetworkOutMbSum14Day__c') < number(100.0)",
            "runtimeError": null
          },
          "context": { "snapshotTime": "2025-04-15T14:48:00Z" },
          "Id": "7",
          "RecordTypeId": "RecordTypeId",
          "CA10__disappearanceTime__c": null,
          "CA10__powerState__c": "VM running",
          "CA10__timeCreated__c": "2020-01-03T17:32:05Z",
          "CA10__monitorCpuAvg14Day__c": 1.0,
          "CA10__monitorCpuMax14Day__c": 5.0,
          "CA10__monitorNetworkInMbSum14Day__c": 10.0,
          "CA10__monitorNetworkOutMbSum14Day__c": 10.0,
          "RecordType": {
            "DeveloperName": "caAzureRMVirtualMachine",
            "Id": "RecordTypeId"
          }
        }
      ]
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" : "1",
    "expectedResult" : {
      "status" : "DISAPPEARED",
      "conditionIndex" : "99",
      "conditionText" : "isDisappeared(CA10__disappearanceTime__c)",
      "runtimeError" : null
    }
  }, {
    "Id" : "2",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "199",
      "conditionText" : "extract('CA10__powerState__c') != 'VM running'",
      "runtimeError" : null
    }
  }, {
    "Id" : "3",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "299",
      "conditionText" : "not(extract('CA10__timeCreated__c').beyondLastDays(14))",
      "runtimeError" : null
    }
  }, {
    "Id" : "4",
    "expectedResult" : {
      "status" : "UNDETERMINED",
      "conditionIndex" : "399",
      "conditionText" : "extract('CA10__monitorCpuAvg14Day__c').isEmpty() || extract('CA10__monitorCpuMax14Day__c').isEmpty()",
      "runtimeError" : null
    }
  }, {
    "Id" : "5",
    "expectedResult" : {
      "status" : "INCOMPLIANT",
      "conditionIndex" : "599",
      "conditionText" : "extract('CA10__monitorCpuAvg14Day__c') < number(40.0) && extract('CA10__monitorCpuMax14Day__c') < number(50.0)",
      "runtimeError" : null
    }
  }, {
    "Id" : "6",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "600",
      "conditionText" : "otherwise",
      "runtimeError" : null
    }
  }, {
    "Id" : "7",
    "expectedResult" : {
      "status" : "INAPPLICABLE",
      "conditionIndex" : "499",
      "conditionText" : "extract('CA10__monitorCpuAvg14Day__c') < number(5.0) && extract('CA10__monitorCpuMax14Day__c') < number(15.0) && extract('CA10__monitorNetworkInMbSum14Day__c') < number(100.0) && extract('CA10__monitorNetworkOutMbSum14Day__c') < number(100.0)",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAzureVirtualMachine__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      RecordTypeId STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__powerState__c STRING,
      CA10__timeCreated__c TIMESTAMP,
      CA10__monitorCpuAvg14Day__c FLOAT64,
      CA10__monitorCpuMax14Day__c FLOAT64,
      CA10__monitorNetworkInMbSum14Day__c FLOAT64,
      CA10__monitorNetworkOutMbSum14Day__c FLOAT64,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "1",
    "RecordTypeId" : "RecordTypeId",
    "CA10__disappearanceTime__c" : new Date("2023-05-22T10:08:05Z"),
    "CA10__powerState__c" : "VM deallocated",
    "CA10__timeCreated__c" : new Date("2023-03-09T11:53:28Z"),
    "CA10__monitorCpuAvg14Day__c" : null,
    "CA10__monitorCpuMax14Day__c" : null,
    "CA10__monitorNetworkInMbSum14Day__c" : null,
    "CA10__monitorNetworkOutMbSum14Day__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "2",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM deallocated",
    "CA10__timeCreated__c" : new Date("2020-10-21T08:03:49Z"),
    "CA10__monitorCpuAvg14Day__c" : 10.0,
    "CA10__monitorCpuMax14Day__c" : 20.0,
    "CA10__monitorNetworkInMbSum14Day__c" : 1000.0,
    "CA10__monitorNetworkOutMbSum14Day__c" : 1000.0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "3",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM running",
    "CA10__timeCreated__c" : new Date("2025-04-10T14:46:26Z"),
    "CA10__monitorCpuAvg14Day__c" : 3.534,
    "CA10__monitorCpuMax14Day__c" : 92.43,
    "CA10__monitorNetworkInMbSum14Day__c" : 1000.0,
    "CA10__monitorNetworkOutMbSum14Day__c" : 1000.0
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "4",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM running",
    "CA10__timeCreated__c" : new Date("2020-10-21T08:03:49Z"),
    "CA10__monitorCpuAvg14Day__c" : null,
    "CA10__monitorCpuMax14Day__c" : null,
    "CA10__monitorNetworkInMbSum14Day__c" : null,
    "CA10__monitorNetworkOutMbSum14Day__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "5",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM running",
    "CA10__timeCreated__c" : new Date("2021-03-01T12:20:59Z"),
    "CA10__monitorCpuAvg14Day__c" : 30.62,
    "CA10__monitorCpuMax14Day__c" : 41.44,
    "CA10__monitorNetworkInMbSum14Day__c" : null,
    "CA10__monitorNetworkOutMbSum14Day__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "6",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM running",
    "CA10__timeCreated__c" : new Date("2020-01-03T17:32:05Z"),
    "CA10__monitorCpuAvg14Day__c" : 10.886110654679344,
    "CA10__monitorCpuMax14Day__c" : 95.15,
    "CA10__monitorNetworkInMbSum14Day__c" : null,
    "CA10__monitorNetworkOutMbSum14Day__c" : null
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "Id" : "7",
    "RecordTypeId" : "RecordTypeId",
    "CA10__powerState__c" : "VM running",
    "CA10__timeCreated__c" : new Date("2020-01-03T17:32:05Z"),
    "CA10__monitorCpuAvg14Day__c" : 1.0,
    "CA10__monitorCpuMax14Day__c" : 5.0,
    "CA10__monitorNetworkInMbSum14Day__c" : 10.0,
    "CA10__monitorNetworkOutMbSum14Day__c" : 10.0
  } ];
  """;

  CREATE TEMP FUNCTION mock_RecordType()
  RETURNS ARRAY<STRUCT<
      DeveloperName STRING,
      Id STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-04-15T14:48:00Z")
    },
    "DeveloperName" : "caAzureRMVirtualMachine",
    "Id" : "RecordTypeId"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAzureVirtualMachine__c(
      obj STRUCT<
          Id STRING,
          RecordTypeId STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__powerState__c STRING,
          CA10__timeCreated__c TIMESTAMP,
          CA10__monitorCpuAvg14Day__c FLOAT64,
          CA10__monitorCpuMax14Day__c FLOAT64,
          CA10__monitorNetworkInMbSum14Day__c FLOAT64,
          CA10__monitorNetworkOutMbSum14Day__c FLOAT64,
          RecordType STRUCT<
              DeveloperName STRING,
              Id STRING
          >
      >,
      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));
          };
      }();
      var todayMinus14Day = new Date(snapshotTime.toISOString().substr(0,10)+'T00:00:00.000Z').getTime() + (-14 * 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 Azure [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__powerState__c; } return this.out; };
      references1.push('Power State [obj.CA10__powerState__c]: ' + obj.CA10__powerState__c);
      if (TextLib.notEqual(extract3.call(extract3), 'VM running')) {
          return {status: 'INAPPLICABLE', conditionIndex: 199, conditionText: "extract('CA10__powerState__c') != 'VM running'", currentStateMessage: "Only for running VM", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      function extract6() { if (!this.out) { this.out = obj.CA10__timeCreated__c; } return this.out; };
      references1.push('Time Created [obj.CA10__timeCreated__c]: ' + obj.CA10__timeCreated__c);
      if (!(extract6.call(extract6) != null && extract6.call(extract6).getTime() < todayMinus14Day)) {
          return {status: 'INAPPLICABLE', conditionIndex: 299, conditionText: "not(extract('CA10__timeCreated__c').beyondLastDays(14))", currentStateMessage: "The VM 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__monitorCpuAvg14Day__c; } return this.out; };
      function extract13() { if (!this.out) { this.out = obj.CA10__monitorCpuMax14Day__c; } return this.out; };
      references1.push('Monitor: CPU, 14-Day [obj.CA10__monitorCpuAvg14Day__c]: ' + obj.CA10__monitorCpuAvg14Day__c);
      references1.push('Monitor: CPU Max, 14-Day [obj.CA10__monitorCpuMax14Day__c]: ' + obj.CA10__monitorCpuMax14Day__c);
      if (IsEmptyLib.simpleIsEmpty(extract10.call(extract10)) || IsEmptyLib.simpleIsEmpty(extract13.call(extract13))) {
          return {status: 'UNDETERMINED', conditionIndex: 399, conditionText: "extract('CA10__monitorCpuAvg14Day__c').isEmpty() || extract('CA10__monitorCpuMax14Day__c').isEmpty()", currentStateMessage: "There is not enough data in the CMDB to identify if the VM is underutilized.", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      function extract17() { if (!this.out) { this.out = obj.CA10__monitorCpuAvg14Day__c; } return this.out; };
      function extract21() { if (!this.out) { this.out = obj.CA10__monitorCpuMax14Day__c; } return this.out; };
      function extract25() { if (!this.out) { this.out = obj.CA10__monitorNetworkInMbSum14Day__c; } return this.out; };
      function extract29() { if (!this.out) { this.out = obj.CA10__monitorNetworkOutMbSum14Day__c; } return this.out; };
      references1.push('Monitor: Network In, MB, 14-Day [obj.CA10__monitorNetworkInMbSum14Day__c]: ' + obj.CA10__monitorNetworkInMbSum14Day__c);
      references1.push('Monitor: Network Out, MB, 14-Day [obj.CA10__monitorNetworkOutMbSum14Day__c]: ' + obj.CA10__monitorNetworkOutMbSum14Day__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__monitorCpuAvg14Day__c') < number(5.0) && extract('CA10__monitorCpuMax14Day__c') < number(15.0) && extract('CA10__monitorNetworkInMbSum14Day__c') < number(100.0) && extract('CA10__monitorNetworkOutMbSum14Day__c') < number(100.0)", currentStateMessage: "The vm is idle and considered by the 'Azure Virtual Machine 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__monitorCpuAvg14Day__c; } return this.out; };
      function extract37() { if (!this.out) { this.out = obj.CA10__monitorCpuMax14Day__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__monitorCpuAvg14Day__c') < number(40.0) && extract('CA10__monitorCpuMax14Day__c') < number(50.0)", currentStateMessage: "The VM is underutilized with Average CPU Utilization < 40% and Max CPU Utilization < 50%.", currentStateReferences: references1.join('\n'), remediation: "Consider rightsizing, consolidating or terminating the VM.", runtimeError: null};
      }
      return {status: 'COMPLIANT', conditionIndex: 600, conditionText: "otherwise", currentStateMessage: "The VM 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.RecordTypeId AS RecordTypeId,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          sObject.CA10__powerState__c AS CA10__powerState__c,
          sObject.CA10__timeCreated__c AS CA10__timeCreated__c,
          sObject.CA10__monitorCpuAvg14Day__c AS CA10__monitorCpuAvg14Day__c,
          sObject.CA10__monitorCpuMax14Day__c AS CA10__monitorCpuMax14Day__c,
          sObject.CA10__monitorNetworkInMbSum14Day__c AS CA10__monitorNetworkInMbSum14Day__c,
          sObject.CA10__monitorNetworkOutMbSum14Day__c AS CA10__monitorNetworkOutMbSum14Day__c,
          STRUCT (
              `RecordType`.DeveloperName AS DeveloperName,
              `RecordType`.Id AS Id
          ) AS RecordType,
          process_CA10__CaAzureVirtualMachine__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.RecordTypeId AS RecordTypeId,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__powerState__c AS CA10__powerState__c,
                  sObject.CA10__timeCreated__c AS CA10__timeCreated__c,
                  sObject.CA10__monitorCpuAvg14Day__c AS CA10__monitorCpuAvg14Day__c,
                  sObject.CA10__monitorCpuMax14Day__c AS CA10__monitorCpuMax14Day__c,
                  sObject.CA10__monitorNetworkInMbSum14Day__c AS CA10__monitorNetworkInMbSum14Day__c,
                  sObject.CA10__monitorNetworkOutMbSum14Day__c AS CA10__monitorNetworkOutMbSum14Day__c,
                  STRUCT (
                      `RecordType`.DeveloperName AS DeveloperName,
                      `RecordType`.Id AS Id
                  ) AS RecordType
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAzureVirtualMachine__c()) AS sObject
      LEFT JOIN UNNEST(mock_RecordType()) AS `RecordType`
          ON sObject.RecordTypeId = `RecordType`.Id
  ) sObject ON sObject.Id = expectedResult.Id;
