-
Notifications
You must be signed in to change notification settings - Fork 1
/
DB.py
81 lines (72 loc) · 2.89 KB
/
DB.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
77
78
79
80
81
import os
import psycopg2
import datetime
import json
database = os.environ['DATABASE']
c = None
conn = None
def _connect(database):
global conn
global c
conn = psycopg2.connect(database)
c = conn.cursor()
#_connect(database)
def logais(ais):
try:
c.execute("INSERT INTO ais VALUES (%s, %s)", (datetime.datetime.utcnow().isoformat(), json.dumps(ais)))
conn.commit()
except psycopg2.InterfaceError as exc:
_connect(database)
logais(ais)
def logtraffic(ais):
try:
c.execute("INSERT INTO trafficlog VALUES (%s, %s, %s)", (datetime.datetime.utcnow().isoformat(), ais["mmsi"], json.dumps(ais)))
conn.commit()
except psycopg2.InterfaceError as exc:
_connect(database)
logtraffic(ais)
def logcapture(ais):
try:
c.execute("INSERT INTO captures VALUES (%s, %s, %s, %s)", (datetime.datetime.utcnow().isoformat(), ais["mmsi"], ais['y'], ais['x']))
conn.commit()
except psycopg2.InterfaceError as exc:
_connect(database)
logcapture(ais)
def getvessel(mmsi):
try:
c.execute("SELECT name, details, size, notes FROM vesselinfo WHERE mmsi = %s", (mmsi,))
val = c.fetchone()
if not val:
return None
return {
'name': val[0],
'details': val[1],
'size': val[2],
'notes': val[3]
}
except psycopg2.InterfaceError as exc:
_connect(database)
return getvessel(mmsi)
def updatevessel(mmsi, ignored, identified, fullinfo):
try:
if fullinfo == None:
c.execute("INSERT INTO vesselinfo (mmsi, ignored, identified) VALUES (%s, %s, %s) ON CONFLICT (mmsi) DO UPDATE SET ignored = EXCLUDED.ignored", (mmsi, ignored, identified))
else:
c.execute("INSERT INTO vesselinfo (mmsi, ignored, url, identified, name, details, size, gross_tonnage, notes, flag) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) ON CONFLICT (mmsi) DO UPDATE SET ignored = EXCLUDED.ignored", (mmsi, ignored, fullinfo["url"], identified, fullinfo["name"], fullinfo["details"], fullinfo["size"], fullinfo["gross_tonnage"], fullinfo["notes"], fullinfo["flag"]))
conn.commit()
except psycopg2.InterfaceError as exc:
_connect(database)
updatevessel(mmsi, ignored, identified, fullinfo)
def shouldprocess(ais):
'''Return list [identified, ignored]'''
try:
c.execute("SELECT identified, ignored, forcecapture FROM vesselinfo WHERE mmsi = %s", (ais["mmsi"],))
status = c.fetchone()
if status == None:
return [False, True] # Ignore all unknown ships
identified, ignored, forcecapture = status
ignored = not forcecapture and ignored == True # Forcecapture trumps ignored - if it is set
return [identified, ignored]
except psycopg2.InterfaceError as exc:
_connect(database)
return shouldprocess(ais)