influxdb-client-python icon indicating copy to clipboard operation
influxdb-client-python copied to clipboard

RangeThreshold class pass float 0 to min or max bug

Open awesomeytj opened this issue 1 year ago • 2 comments

Specifications

  • Client Version:1.47.0
  • InfluxDB Version:2.7.10
  • Platform: docker on linux

Code sample to reproduce problem

threshold = RangeThreshold(level=CheckStatusLevel.OK, min=0, max=10, within=True)

Expected behavior

raise ValueError: Invalid value for min, must not be None

Actual behavior

use above code to create a check will raise error

Additional info

No response

awesomeytj avatar Nov 27 '24 06:11 awesomeytj

Hi @awesomeytj,

Could you please share reproducible code? It would help us greatly to quickly fix this issue.

Best regards

bednar avatar Nov 27 '24 09:11 bednar

import pandas as pd
from influxdb_client import InfluxDBClient
from django.conf import settings
from influxdb_client.domain.task_status_type import TaskStatusType
from influxdb_client.service.checks_service import ChecksService

from influxdb_client.domain.dashboard_query import DashboardQuery
from influxdb_client.domain.query_edit_mode import QueryEditMode

from influxdb_client.domain.threshold_check import ThresholdCheck
from influxdb_client.domain.lesser_threshold import LesserThreshold
from influxdb_client.domain.range_threshold import RangeThreshold
from influxdb_client.domain.greater_threshold import GreaterThreshold
from influxdb_client.domain.check_status_level import CheckStatusLevel

from influxdb_client.domain.http_notification_rule import HTTPNotificationRule
from influxdb_client.domain.task_status_type import TaskStatusType
from influxdb_client.domain.rule_status_level import RuleStatusLevel
from influxdb_client.domain.status_rule import StatusRule
from influxdb_client.service.notification_rules_service import NotificationRulesService

def activate_check():
    try:
        influxdb_client_obj = InfluxDBClient(url=settings.INFLUXDB_CONFIGS['URL'], token=settings.INFLUXDB_CONFIGS['TOKEN'], org=settings.INFLUXDB_CONFIGS['ORG_NAME'], debug=False)
        influxdb_org_obj = influxdb_client_obj.organizations_api().find_organizations(org=settings.INFLUXDB_CONFIGS['ORG_NAME'])[0]
        measurement = "device_temperature"
        every = "5s"
        threshold_level = 0
        threshold_values = [0, 100]
        bucket_name = settings.INFLUXDB_CONFIGS['BUCKET']
        enum_map = {
            0: CheckStatusLevel.OK,
            1: CheckStatusLevel.INFO,
            2: CheckStatusLevel.CRIT,
            3: CheckStatusLevel.WARN,
        }
        if len(threshold_values) != 2:
            return None
        print(float(threshold_values[0]), float(threshold_values[1]))
        threshold = RangeThreshold(level=enum_map[threshold_level], min=float(threshold_values[0]), max=float(threshold_values[1]), within=True)
        query = f'''
                from(bucket:"{bucket_name}") 
                    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
                    |> filter(fn: (r) => r["_measurement"] == "{measurement}")
                    |> filter(fn: (r) => r["device_code"] == "D00001")
                    |> filter(fn: (r) => r["_field"] == "reading")
                    |> last()        
                '''
        check = ThresholdCheck(name=f"D00001_threshold_check_{measurement}_level_{threshold_level}",
                            status_message_template="The value is on: ${ r._level } level!",
                            every=every,
                            offset="0s",
                            query=DashboardQuery(edit_mode=QueryEditMode.ADVANCED, text=query),
                            thresholds=[threshold],
                            org_id=influxdb_org_obj.id,
                            status=TaskStatusType.ACTIVE)

        checks_service = ChecksService(api_client=influxdb_client_obj.api_client)
        check = checks_service.create_check(check)
        check_info = {
            "name": f"D00001_threshold_check_{measurement}_level_{threshold_level}",
            "check_id": check.id,
        }
        return check_info
    except Exception as e:
        return None

@bednar

awesomeytj avatar Dec 02 '24 09:12 awesomeytj