Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'dict' when running my custom rule #1727

Open
scotbab opened this issue Jun 4, 2018 · 4 comments

Comments

@scotbab
Copy link

scotbab commented Jun 4, 2018

Hi
The custom rule runs fine using elastalert-test-rule but when running using python -m elastalert.elastalert it gives the below error -
Getting TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'dict'

Below is the custom timestamp used:

timestamp_field: Timestamp
timestamp_format: '%Y-%m-%dT%H:%M:%S.%fZ'

Here is the add_data function my_rule python script..

def add_data(self, data):

    for document in data:

        # To access config options, use self.rules
        # print document['RunId']
        if isinstance(document['Timestamp'], dict):
            document['Timestamp'] = parser.parse(document['Timestamp'])
            if document['RunId'] in self.rules['RunIds']:
               buildno = self.rules['RunIds']
               build = str(buildno).replace("['","")
               build = build.replace("']","")
               act_build = str(document['RunId'])
               if document['RunId'] == build:
                 print "Runid matched"
                 if document['Name'] in self.rules['Names']:
                   stepno = self.rules['Names']
                   step = str(stepno).replace("['", "")
                   step = step.replace("']", "")
                   act_step = str(document['Name'])
                   if document['Name'] == step:
                    print "Name matched"
                    Actual = document['Total']
                    Base_value = self.rules['Base_Line']
                    if int(Actual) > int(Base_value):
                        # To add a match, use self.add_match
                        self.add_match(document)
                        break

Thanks in advance guys..

@Qmando
Copy link
Member

Qmando commented Jun 6, 2018

if isinstance(document['Timestamp'], dict):
            document['Timestamp'] = parser.parse(document['Timestamp'])

parser.parse works for strings only, assuming that's dateutil.parser. Though, you didn't post the traceback so I don't even know where the error occurred. The timestamp should have been converted to a datetime object before anyway.

Also, you could remove all the string replacement stuff and just do if document['RunId'] in document['RunIds']

@scotbab
Copy link
Author

scotbab commented Jun 8, 2018

Thanks Qmando, I tried this solution but it was not working, might be I was putting the parsing code in wrong place. Not sure.

@scotbab
Copy link
Author

scotbab commented Jun 8, 2018

I think I was able to solve it. The attribute 'query_delay' was missing in the rule.yaml file, after placing it the issue was resolved. Can you please describe regarding 'query_delay'. Thanks. I have one more question the rule that I am running using elastalert is continuously running for hours. Is there a way for the rule to execute/check once and then stop ?

@Qmando
Copy link
Member

Qmando commented Jun 8, 2018

You can give a --end argument to elastalert and it will stop once it hits that timestamp. Scheduling it for certain hours of the day is not yet available, but look for it in a future release.

query_delay simple moves all queries back in time by that amount. For example, a five minute delay would cause queries at 12:00 to end at 11:55 instead of going up to the present time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants