LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
DatabaseLogger.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 
25 
26 from lsst.cat.MySQLBase import MySQLBase
27 import MySQLdb
28 import os
29 import sys
30 import subprocess
31 
32 class DatabaseLogger(MySQLBase):
33 
34  def __init__(self, dbHostName, portNumber):
35  MySQLBase.__init__(self, dbHostName, portNumber)
36 
37  self.keywords = ['HOSTID', 'RUNID', 'SLICEID', 'LEVEL', 'LOG', 'DATE', 'NODE', 'TIMESTAMP', 'COMMENT', 'STATUS', 'PIPELINE', 'EVENTTIME', 'PUBTIME', 'TYPE', 'STAGEID', 'LOOPNUM', 'WORKERID']
38  self.keywordSet = set(self.keywords)
39  self.highwater = 10
40 
41  def insertRecords(self, dbTable, msgs):
42  cnt = len(msgs)
43  while cnt > 0:
44  if cnt > self.highwater:
45  ins = ""
46  for i in range(0,self.highwater+1):
47  event = msgs.pop(0)
48  ins = ins + self.createInsertString(dbTable, event.getPropertySet())
49  self.execCommand0(ins)
50  elif cnt > 0:
51  ins = ""
52  for i in range(0,cnt):
53  event = msgs.pop(0)
54  ins = ins + self.createInsertString(dbTable, event.getPropertySet())
55  self.execCommand0(ins)
56  cnt = len(msgs)
57 
58  def insertRecord(self, dbTable, ps):
59  ins = self.createInsertString(dbTable,ps)
60  self.execCommand0(ins)
61 
62 
63  def createInsertString(self, dbTable, ps):
64  hostId = ps.get("HOSTID")
65  hostId = MySQLdb.escape_string(hostId)
66 
67  runId = ps.get("RUNID")
68  runId = MySQLdb.escape_string(runId)
69 
70  sliceId = ps.get("SLICEID")
71  level = ps.get("LEVEL")
72 
73  log = ps.get("LOG")
74  log = MySQLdb.escape_string(log)
75 
76  date = ps.get("DATE")
77  date = MySQLdb.escape_string(date)
78 
79  ts = ps.get("TIMESTAMP")
80  eventtime = ps.get("EVENTTIME")
81  pubtime = ps.get("PUBTIME")
82  eventtype = ps.get("TYPE")
83  eventtype = MySQLdb.escape_string(eventtype)
84 
85  if ps.exists("NODE"):
86  node = ps.get("NODE")
87  else:
88  node = -1
89 
90  timestamp = ts.nsecs()
91 
92  commentList = ps.get("COMMENT")
93  comment = ""
94 
95  if ps.valueCount("COMMENT") == 1:
96  comment = commentList
97  else:
98  for i in commentList:
99  if comment == "":
100  comment = i
101  else:
102  comment = comment+";"+i
103 
104 
105  if (ps.exists("TOPIC")):
106  ps.remove("TOPIC")
107 
108  if ps.exists("STATUS"):
109  status = ps.get("STATUS")
110  status = MySQLdb.escape_string(status)
111  else:
112  status = "NULL"
113 
114  if ps.exists("PIPELINE"):
115  pipeline = ps.get("PIPELINE")
116  pipeline = MySQLdb.escape_string(pipeline)
117  else:
118  pipeline = "NULL"
119 
120  if ps.exists("STAGEID"):
121  stageid = ps.get("STAGEID")
122  else:
123  stageid = "-1"
124 
125  if ps.exists("LOOPNUM"):
126  loopnum = ps.get("LOOPNUM")
127  else:
128  loopnum = "-1"
129 
130  if ps.exists("WORKERID"):
131  workerid = ps.get("WORKERID")
132  workerid = MySQLdb.escape_string(workerid)
133  else:
134  workerid = "NULL"
135 
136 
137 
138  names = ps.names()
139  namesSet = set(names)
140 
141  diff = namesSet.difference(self.keywordSet)
142 
143  custom = ""
144  for name in diff:
145  if custom == "":
146  custom = "%s : %s;" % (name,ps.get(name))
147  else:
148  custom = custom+ "%s : %s;" % (name,ps.get(name))
149  if custom == "":
150  custom = "NULL"
151  custom = MySQLdb.escape_string(custom[0:4096])
152  comment = MySQLdb.escape_string(comment[0:2048])
153 
154  cmd = """INSERT INTO logs.%s(hostId, runId, sliceid, status, level, log, date, node, timestamp, custom, comment, pipeline, eventtime, pubtime, type, stageid, loopnum, workerid) values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s"); """ % (dbTable, hostId, runId, sliceId, status, level, log, date, node, timestamp, custom, comment, pipeline, eventtime, pubtime, eventtype, stageid, loopnum, workerid)
155 
156  return cmd