def get_remaining_quota()

in elkserver/docker/redelk-base/redelkinstalldata/scripts/modules/alarm_filehash/ioc_ibm.py [0:0]


    def get_remaining_quota(self):
        """Returns the number of hashes that could be queried within this run"""
        url = "https://api.xforce.ibmcloud.com/all-subscriptions/usage"
        headers = {"Accept": "application/json", "Authorization": self.basic_auth}

        # Get the quotas, if response code != 200, return 0 so we don't query further
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            json_response = response.json()
        else:
            self.logger.warning(
                "Error retrieving IBM X-Force Quota (HTTP Status code: %d)",
                response.status_code,
            )
            return 0

        remaining_quota = 0

        # Extract the hourly, daily and monthly remaining quotas
        for result in json_response:
            # Only take the relevant results (usageData for 'api' type)
            if (
                "subscriptionType" in result
                and result["subscriptionType"] == "api"
                and "usageData" in result
            ):
                # Get the monthly quota (limit)
                entitlement = get_value("usageData.entitlement", result, 0)
                remaining_quota += int(entitlement)

                # Get the usage array (per cycle)
                usage = get_value("usageData.usage", result, [])

                # Find the current cycle and remove the current usage from that cycle from the remaining quota
                for usage_cycle in usage:
                    cycle = get_value("cycle", usage_cycle, 0)
                    if cycle == datetime.now().strftime("%Y-%m"):
                        current_usage = get_value("usage", usage_cycle, 0)
                        remaining_quota -= int(current_usage)

        self.logger.debug("Remaining quota (monthly): %d", remaining_quota)

        return remaining_quota