{"action":"created","comment":{"url":"https://api.github.com/repos/litong01/python-monasca/pulls/comments/32937128","id":32937128,"diff_hunk":"@@ -0,0 +1,87 @@\n+# Copyright 2015 CMU\n+# Author: Yihan Wang <wangff9@gmail.com>\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\"); you may\n+# not use this file except in compliance with the License. You may obtain\n+# a copy of the License at\n+#\n+# http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n+# License for the specific language governing permissions and limitations\n+# under the License.\n+\n+\n+def calc_value(func, data_list):\n+    \"\"\"Calc float values according to 5 functions.\"\"\"\n+    ops = {'SUM': sum,\n+           'AVG': lambda x: sum(x) / len(x),\n+           'MAX': max,\n+           'MIN': min,\n+           'COUNT': len}\n+    if len(data_list) == 0 or func not in ops:\n+        return None\n+    else:\n+        return ops[func](data_list)\n+\n+\n+def compare_thresh(values, op, thresh):\n+    \"\"\"Check if value from metrics exceeds thresh.\n+\n+    Only the value in each period meet thresh, the alarm state can be 'ALARM'.\n+\n+    For example, the alarm definition defines 3 periods, values = [a,b,c].\n+    If the value in any period doesn't meet thresh,\n+    then alarm state must be 'OK';\n+    If some values are None (means no metrics in that period)\n+    but all other values meet thresh,\n+    we still don't know if the alarm can be triggered,\n+    so it's 'UNDETERMINED';\n+    otherwise, the state can be 'ALARM'\n+    \"\"\"\n+\n+    state = 'OK'\n+    for value in values:\n+        if value:\n+            if op == 'GT' and value <= thresh:\n+                return state\n+            elif op == 'LT' and value >= thresh:\n+                return state\n+            elif op == 'LTE' and value > thresh:\n+                return state\n+            elif op == 'GTE' and value < thresh:\n+                return state\n+    state = 'ALARM'\n+    for value in values:\n+        if value is None:\n+            state = 'UNDETERMINED'\n+    return state\n+\n+\n+def calc_logic(logic_operator, subs):\n+    \"\"\"Calc overall state of an alarm expression.\n+\n+    'OK' means False;\n+    'ALARM' means True;\n+    'UNDETERMINED' means either True or False.\n+    \"\"\"\n+    if logic_operator == 'AND':\n+        for o in subs:\n+            if o == 'OK':\n+                return 'OK'\n+        for o in subs:\n+            if o == 'UNDETERMINED':\n+                return 'UNDETERMINED'\n+        return 'ALARM'\n+    elif logic_operator == 'OR':\n+        for o in subs:","path":"monasca/common/alarm_expr_calculator.py","position":79,"original_position":79,"commit_id":"11ed3a14c5ce475efb499981348ca69ec4064b7d","original_commit_id":"11ed3a14c5ce475efb499981348ca69ec4064b7d","user":{"login":"yihanwan","id":10675648,"avatar_url":"https://avatars.githubusercontent.com/u/10675648?v=3","gravatar_id":"","url":"https://api.github.com/users/yihanwan","html_url":"https://github.com/yihanwan","followers_url":"https://api.github.com/users/yihanwan/followers","following_url":"https://api.github.com/users/yihanwan/following{/other_user}","gists_url":"https://api.github.com/users/yihanwan/gists{/gist_id}","starred_url":"https://api.github.com/users/yihanwan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yihanwan/subscriptions","organizations_url":"https://api.github.com/users/yihanwan/orgs","repos_url":"https://api.github.com/users/yihanwan/repos","events_url":"https://api.github.com/users/yihanwan/events{/privacy}","received_events_url":"https://api.github.com/users/yihanwan/received_events","type":"User","site_admin":false},"body":"OK, I can change it into one loop\r\nBut I don't understand why one if can work without loop.\r\nMaybe I need to make the functionality clear. Examples:\r\nAND, [ALARM, ALARM, ..., ALARM, UNDETERMINED, OK] -> OK\r\nAND, [ALARM, ALARM, ..., ALARM, UNDETERMINED] -> UNDETERMINED\r\nAND, [ALARM, ALARM, ..., ALARM] -> ALARM\r\n","created_at":"2015-06-22T14:08:09Z","updated_at":"2015-06-22T14:08:09Z","html_url":"https://github.com/litong01/python-monasca/pull/10#discussion_r32937128","pull_request_url":"https://api.github.com/repos/litong01/python-monasca/pulls/10","_links":{"self":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/comments/32937128"},"html":{"href":"https://github.com/litong01/python-monasca/pull/10#discussion_r32937128"},"pull_request":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/10"}}},"pull_request":{"url":"https://api.github.com/repos/litong01/python-monasca/pulls/10","id":38168923,"html_url":"https://github.com/litong01/python-monasca/pull/10","diff_url":"https://github.com/litong01/python-monasca/pull/10.diff","patch_url":"https://github.com/litong01/python-monasca/pull/10.patch","issue_url":"https://api.github.com/repos/litong01/python-monasca/issues/10","number":10,"state":"open","locked":false,"title":"add thresh processor","user":{"login":"nilwyh","id":8952854,"avatar_url":"https://avatars.githubusercontent.com/u/8952854?v=3","gravatar_id":"","url":"https://api.github.com/users/nilwyh","html_url":"https://github.com/nilwyh","followers_url":"https://api.github.com/users/nilwyh/followers","following_url":"https://api.github.com/users/nilwyh/following{/other_user}","gists_url":"https://api.github.com/users/nilwyh/gists{/gist_id}","starred_url":"https://api.github.com/users/nilwyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nilwyh/subscriptions","organizations_url":"https://api.github.com/users/nilwyh/orgs","repos_url":"https://api.github.com/users/nilwyh/repos","events_url":"https://api.github.com/users/nilwyh/events{/privacy}","received_events_url":"https://api.github.com/users/nilwyh/received_events","type":"User","site_admin":false},"body":"1. in microservice, add an thresh processor:\r\nthe thresh processor offers three APIs can be called by thresh engine:\r\na. process_metrics(). consume new metrics coming, giving to the sub alarm exprs who want them\r\nb. process_alarms(). scan all the metrics stored, to produce alarms\r\nc. update_thresh_processor(). to update alarm definition used in the processor.\r\n\r\n2. in common, add a set of calc functions for expr processing\r\n\r\n3. in common, modify alarm expr parser to add two functions needed by\r\nprocessor\r\n\r\n4. add unittest for all three files.","created_at":"2015-06-20T14:33:36Z","updated_at":"2015-06-22T14:08:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"70165eca6d23074cbb8924efee1552ff773ad4f3","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/litong01/python-monasca/pulls/10/commits","review_comments_url":"https://api.github.com/repos/litong01/python-monasca/pulls/10/comments","review_comment_url":"https://api.github.com/repos/litong01/python-monasca/pulls/comments{/number}","comments_url":"https://api.github.com/repos/litong01/python-monasca/issues/10/comments","statuses_url":"https://api.github.com/repos/litong01/python-monasca/statuses/11ed3a14c5ce475efb499981348ca69ec4064b7d","head":{"label":"nilwyh:thresh_process","ref":"thresh_process","sha":"11ed3a14c5ce475efb499981348ca69ec4064b7d","user":{"login":"nilwyh","id":8952854,"avatar_url":"https://avatars.githubusercontent.com/u/8952854?v=3","gravatar_id":"","url":"https://api.github.com/users/nilwyh","html_url":"https://github.com/nilwyh","followers_url":"https://api.github.com/users/nilwyh/followers","following_url":"https://api.github.com/users/nilwyh/following{/other_user}","gists_url":"https://api.github.com/users/nilwyh/gists{/gist_id}","starred_url":"https://api.github.com/users/nilwyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nilwyh/subscriptions","organizations_url":"https://api.github.com/users/nilwyh/orgs","repos_url":"https://api.github.com/users/nilwyh/repos","events_url":"https://api.github.com/users/nilwyh/events{/privacy}","received_events_url":"https://api.github.com/users/nilwyh/received_events","type":"User","site_admin":false},"repo":{"id":35345804,"name":"python-monasca","full_name":"nilwyh/python-monasca","owner":{"login":"nilwyh","id":8952854,"avatar_url":"https://avatars.githubusercontent.com/u/8952854?v=3","gravatar_id":"","url":"https://api.github.com/users/nilwyh","html_url":"https://github.com/nilwyh","followers_url":"https://api.github.com/users/nilwyh/followers","following_url":"https://api.github.com/users/nilwyh/following{/other_user}","gists_url":"https://api.github.com/users/nilwyh/gists{/gist_id}","starred_url":"https://api.github.com/users/nilwyh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nilwyh/subscriptions","organizations_url":"https://api.github.com/users/nilwyh/orgs","repos_url":"https://api.github.com/users/nilwyh/repos","events_url":"https://api.github.com/users/nilwyh/events{/privacy}","received_events_url":"https://api.github.com/users/nilwyh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nilwyh/python-monasca","description":"the python implementation of monasca","fork":true,"url":"https://api.github.com/repos/nilwyh/python-monasca","forks_url":"https://api.github.com/repos/nilwyh/python-monasca/forks","keys_url":"https://api.github.com/repos/nilwyh/python-monasca/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nilwyh/python-monasca/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nilwyh/python-monasca/teams","hooks_url":"https://api.github.com/repos/nilwyh/python-monasca/hooks","issue_events_url":"https://api.github.com/repos/nilwyh/python-monasca/issues/events{/number}","events_url":"https://api.github.com/repos/nilwyh/python-monasca/events","assignees_url":"https://api.github.com/repos/nilwyh/python-monasca/assignees{/user}","branches_url":"https://api.github.com/repos/nilwyh/python-monasca/branches{/branch}","tags_url":"https://api.github.com/repos/nilwyh/python-monasca/tags","blobs_url":"https://api.github.com/repos/nilwyh/python-monasca/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nilwyh/python-monasca/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nilwyh/python-monasca/git/refs{/sha}","trees_url":"https://api.github.com/repos/nilwyh/python-monasca/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nilwyh/python-monasca/statuses/{sha}","languages_url":"https://api.github.com/repos/nilwyh/python-monasca/languages","stargazers_url":"https://api.github.com/repos/nilwyh/python-monasca/stargazers","contributors_url":"https://api.github.com/repos/nilwyh/python-monasca/contributors","subscribers_url":"https://api.github.com/repos/nilwyh/python-monasca/subscribers","subscription_url":"https://api.github.com/repos/nilwyh/python-monasca/subscription","commits_url":"https://api.github.com/repos/nilwyh/python-monasca/commits{/sha}","git_commits_url":"https://api.github.com/repos/nilwyh/python-monasca/git/commits{/sha}","comments_url":"https://api.github.com/repos/nilwyh/python-monasca/comments{/number}","issue_comment_url":"https://api.github.com/repos/nilwyh/python-monasca/issues/comments{/number}","contents_url":"https://api.github.com/repos/nilwyh/python-monasca/contents/{+path}","compare_url":"https://api.github.com/repos/nilwyh/python-monasca/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nilwyh/python-monasca/merges","archive_url":"https://api.github.com/repos/nilwyh/python-monasca/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nilwyh/python-monasca/downloads","issues_url":"https://api.github.com/repos/nilwyh/python-monasca/issues{/number}","pulls_url":"https://api.github.com/repos/nilwyh/python-monasca/pulls{/number}","milestones_url":"https://api.github.com/repos/nilwyh/python-monasca/milestones{/number}","notifications_url":"https://api.github.com/repos/nilwyh/python-monasca/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nilwyh/python-monasca/labels{/name}","releases_url":"https://api.github.com/repos/nilwyh/python-monasca/releases{/id}","created_at":"2015-05-09T21:14:57Z","updated_at":"2015-05-09T21:14:57Z","pushed_at":"2015-06-21T00:00:52Z","git_url":"git://github.com/nilwyh/python-monasca.git","ssh_url":"git@github.com:nilwyh/python-monasca.git","clone_url":"https://github.com/nilwyh/python-monasca.git","svn_url":"https://github.com/nilwyh/python-monasca","homepage":null,"size":279,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"develop"}},"base":{"label":"litong01:master","ref":"master","sha":"e097bcdc930e0a3a8c36c11230f69116bf2b2eff","user":{"login":"litong01","id":1943243,"avatar_url":"https://avatars.githubusercontent.com/u/1943243?v=3","gravatar_id":"","url":"https://api.github.com/users/litong01","html_url":"https://github.com/litong01","followers_url":"https://api.github.com/users/litong01/followers","following_url":"https://api.github.com/users/litong01/following{/other_user}","gists_url":"https://api.github.com/users/litong01/gists{/gist_id}","starred_url":"https://api.github.com/users/litong01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/litong01/subscriptions","organizations_url":"https://api.github.com/users/litong01/orgs","repos_url":"https://api.github.com/users/litong01/repos","events_url":"https://api.github.com/users/litong01/events{/privacy}","received_events_url":"https://api.github.com/users/litong01/received_events","type":"User","site_admin":false},"repo":{"id":23794782,"name":"python-monasca","full_name":"litong01/python-monasca","owner":{"login":"litong01","id":1943243,"avatar_url":"https://avatars.githubusercontent.com/u/1943243?v=3","gravatar_id":"","url":"https://api.github.com/users/litong01","html_url":"https://github.com/litong01","followers_url":"https://api.github.com/users/litong01/followers","following_url":"https://api.github.com/users/litong01/following{/other_user}","gists_url":"https://api.github.com/users/litong01/gists{/gist_id}","starred_url":"https://api.github.com/users/litong01/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/litong01/subscriptions","organizations_url":"https://api.github.com/users/litong01/orgs","repos_url":"https://api.github.com/users/litong01/repos","events_url":"https://api.github.com/users/litong01/events{/privacy}","received_events_url":"https://api.github.com/users/litong01/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/litong01/python-monasca","description":"the python implementation of monasca","fork":false,"url":"https://api.github.com/repos/litong01/python-monasca","forks_url":"https://api.github.com/repos/litong01/python-monasca/forks","keys_url":"https://api.github.com/repos/litong01/python-monasca/keys{/key_id}","collaborators_url":"https://api.github.com/repos/litong01/python-monasca/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/litong01/python-monasca/teams","hooks_url":"https://api.github.com/repos/litong01/python-monasca/hooks","issue_events_url":"https://api.github.com/repos/litong01/python-monasca/issues/events{/number}","events_url":"https://api.github.com/repos/litong01/python-monasca/events","assignees_url":"https://api.github.com/repos/litong01/python-monasca/assignees{/user}","branches_url":"https://api.github.com/repos/litong01/python-monasca/branches{/branch}","tags_url":"https://api.github.com/repos/litong01/python-monasca/tags","blobs_url":"https://api.github.com/repos/litong01/python-monasca/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/litong01/python-monasca/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/litong01/python-monasca/git/refs{/sha}","trees_url":"https://api.github.com/repos/litong01/python-monasca/git/trees{/sha}","statuses_url":"https://api.github.com/repos/litong01/python-monasca/statuses/{sha}","languages_url":"https://api.github.com/repos/litong01/python-monasca/languages","stargazers_url":"https://api.github.com/repos/litong01/python-monasca/stargazers","contributors_url":"https://api.github.com/repos/litong01/python-monasca/contributors","subscribers_url":"https://api.github.com/repos/litong01/python-monasca/subscribers","subscription_url":"https://api.github.com/repos/litong01/python-monasca/subscription","commits_url":"https://api.github.com/repos/litong01/python-monasca/commits{/sha}","git_commits_url":"https://api.github.com/repos/litong01/python-monasca/git/commits{/sha}","comments_url":"https://api.github.com/repos/litong01/python-monasca/comments{/number}","issue_comment_url":"https://api.github.com/repos/litong01/python-monasca/issues/comments{/number}","contents_url":"https://api.github.com/repos/litong01/python-monasca/contents/{+path}","compare_url":"https://api.github.com/repos/litong01/python-monasca/compare/{base}...{head}","merges_url":"https://api.github.com/repos/litong01/python-monasca/merges","archive_url":"https://api.github.com/repos/litong01/python-monasca/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/litong01/python-monasca/downloads","issues_url":"https://api.github.com/repos/litong01/python-monasca/issues{/number}","pulls_url":"https://api.github.com/repos/litong01/python-monasca/pulls{/number}","milestones_url":"https://api.github.com/repos/litong01/python-monasca/milestones{/number}","notifications_url":"https://api.github.com/repos/litong01/python-monasca/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/litong01/python-monasca/labels{/name}","releases_url":"https://api.github.com/repos/litong01/python-monasca/releases{/id}","created_at":"2014-09-08T14:50:44Z","updated_at":"2015-04-25T13:12:07Z","pushed_at":"2015-06-22T05:36:04Z","git_url":"git://github.com/litong01/python-monasca.git","ssh_url":"git@github.com:litong01/python-monasca.git","clone_url":"https://github.com/litong01/python-monasca.git","svn_url":"https://github.com/litong01/python-monasca","homepage":null,"size":526,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":9,"forks":4,"open_issues":9,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/10"},"html":{"href":"https://github.com/litong01/python-monasca/pull/10"},"issue":{"href":"https://api.github.com/repos/litong01/python-monasca/issues/10"},"comments":{"href":"https://api.github.com/repos/litong01/python-monasca/issues/10/comments"},"review_comments":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/10/comments"},"review_comment":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/litong01/python-monasca/pulls/10/commits"},"statuses":{"href":"https://api.github.com/repos/litong01/python-monasca/statuses/11ed3a14c5ce475efb499981348ca69ec4064b7d"}}}}