forked from Dmaina5054/InfluxLarkbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachealert.py
76 lines (54 loc) · 1.45 KB
/
cachealert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from larkconn import sendalert
import asyncio
import csv
import json
from redis import Redis
from dotenv import load_dotenv
from collections import Counter
import requests
import json
import time
import os
import asyncio
import numpy as np
load_dotenv()
webhookurl = os.getenv('webhookurl')
redisclient = Redis(db=1)
#use as factory function
def cachealert(alrt):
# print(f'{alrt} will be cached...')
itema = unique(alrt)
#print(itema)
return cacher(itema)
#only get unique from list
def unique(listel):
x = np.array(listel)
return np.unique(x)
allalarms= []
def cacher(itemms):
ts = itemms.tolist()
for d in ts:
host = f'{d.split()[0]}'
alarmkey = f"{host+ ':' + d.split()[1]}"
bldgname = f'{d.split()[1]}'
alarmserial = {alarmkey:bldgname}
if redisclient.hgetall(alarmkey):
print('incache')
else:
pipe = redisclient.pipeline()
print('caching')
pipe.hmset(alarmkey,alarmserial)
pipe.expire(alarmkey,1800)
print('Cached')
pipe.execute()
#print(alarmkey)
print(alarmkey)
allalarms.append(alarmkey)
sendalert(allalarms)
print(f'Before clear..{allalarms}')
try:
allalarms.clear()
print(f'After clear...{allalarms}')
return None
except Exception as e:
print(e)