LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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 ##
33 # @deprecated insert logging records into the database. Note that this is
34 # completely depedent on the CAT schema for this database, depends on writing
35 # to a file, and needs to be completely replaced.
36 class DatabaseLogger(MySQLBase):
37 
38  ## initialize
39  def __init__(self, dbHostName, portNumber):
40  MySQLBase.__init__(self, dbHostName, portNumber)
41 
42  ## table keywords
43  self.keywords = ['HOSTID', 'RUNID', 'LOG', 'workerid', 'stagename', 'SLICEID', 'STAGEID', 'LOOPNUM', 'STATUS', 'LEVEL', 'DATE', 'TIMESTAMP', 'node', 'custom', 'timereceived', 'visitid', 'COMMENT', 'PIPELINE', 'TYPE', 'EVENTTIME', 'PUBTIME', 'usertime', 'systemtime']
44 
45  ## set of table keywords
46  self.keywordSet = set(self.keywords)
47  ## @deprecated high water mark
48  self.highwater = 10
49 
50  ## insert the messages into the database, using a file
51  def insertRecords(self, dbTable, msgs, filename):
52  cnt = len(msgs)
53 
54  file = open(filename,"w")
55  for i in range(0,cnt):
56  #event = msgs.pop(0)
57  #ins = self.createInsertString(dbTable, event.getPropertySet())
58  propSet = msgs.pop(0)
59  ins = self.createInsertString(dbTable, propSet)
60  file.write(ins)
61  file.close()
62  cmd = "LOAD DATA LOCAL INFILE '%s' INTO TABLE %s FIELDS OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' SET timereceived=NOW();" % (filename, dbTable)
63  self.execCommand0(cmd)
64 
65  ## insert a record into the table "dbTable" , given the property set "ps"
66  def insertRecord(self, dbTable, ps):
67  ins = self.createInsertString(dbTable,ps)
68  self.execCommand0(ins)
69 
70  ## create an insert string based on the keywords for the table, filling
71  # in defaults as needed.
72  def createInsertString(self, dbTable, ps):
73  hostId = ps.getString("HOSTID")
74  runId = ps.getString("RUNID")
75  sliceId = ps.getInt("SLICEID")
76  level = ps.getInt("LEVEL")
77  log = ps.getString("LOG")
78  date = ps.getString("DATE")
79  ts = ps.get("TIMESTAMP")
80  eventtime = ps.get("EVENTTIME")
81  pubtime = ps.get("PUBTIME")
82  eventtype = ps.getString("TYPE")
83 
84  if ps.exists("node"):
85  node = ps.getInt("node")
86  else:
87  node = -1
88 
89  timestamp = ts.nsecs()
90 
91  commentList = ps.getString("COMMENT")
92  comment = ""
93 
94  if ps.valueCount("COMMENT") == 1:
95  comment = commentList
96  else:
97  for i in commentList:
98  if comment == "":
99  comment = i
100  else:
101  comment = comment+";"+i
102 
103 
104  if (ps.exists("TOPIC")):
105  ps.remove("TOPIC")
106 
107  if ps.exists("STATUS"):
108  status = ps.getString("STATUS")
109  else:
110  status = "NULL"
111 
112  if ps.exists("PIPELINE"):
113  pipeline = ps.getString("PIPELINE")
114  else:
115  pipeline = "NULL"
116 
117  if ps.exists("STAGEID"):
118  stageid = ps.getInt("STAGEID")
119  else:
120  stageid = "-1"
121 
122  if ps.exists("LOOPNUM"):
123  loopnum = ps.getInt("LOOPNUM")
124  else:
125  loopnum = "-1"
126 
127  if ps.exists("workerid"):
128  workerid = ps.getString("workerid")
129  else:
130  workerid = "NULL"
131 
132 
133  if ps.exists("usertime"):
134  usertime = ps.getDouble("usertime")
135  else:
136  usertime = 0
137 
138  if ps.exists("systemtime"):
139  systemtime = ps.getDouble("systemtime")
140  else:
141  systemtime = 0
142 
143  if ps.exists("stagename"):
144  stagename = ps.getString("stagename")
145  else:
146  stagename = "unknown"
147 
148  visitid="-1"
149 
150  names = ps.names()
151  namesSet = set(names)
152 
153  diff = namesSet.difference(self.keywordSet)
154 
155  custom = ""
156  for name in diff:
157  if custom == "":
158  custom = "%s : %s;" % (name,ps.get(name))
159  else:
160  custom = custom+ "%s : %s;" % (name,ps.get(name))
161  if custom == "":
162  custom = "NULL"
163 
164  #custom = MySQLdb.escape_string(custom[0:256])
165  #comment = MySQLdb.escape_string(comment[0:256])
166  custom = MySQLdb.escape_string(custom[0:4096])
167  comment = MySQLdb.escape_string(comment)
168 
169  #custom = custom[0:256]
170  #comment = comment[0:256]
171 
172  timereceived = "0000-00-00 00:00:00"
173 
174  datalist = []
175  datalist.append(0)
176  datalist.append(hostId)
177  datalist.append(runId)
178  datalist.append(log)
179  datalist.append(workerid)
180  datalist.append(stagename)
181  datalist.append(sliceId)
182  datalist.append(stageid)
183  datalist.append(loopnum)
184  datalist.append(status)
185  datalist.append(level)
186  datalist.append(date)
187  datalist.append(timestamp)
188  datalist.append(node)
189  datalist.append(custom)
190  datalist.append(timereceived)
191  datalist.append(visitid)
192  datalist.append(comment)
193  datalist.append(pipeline)
194  datalist.append(eventtype)
195  datalist.append(eventtime)
196  datalist.append(pubtime)
197  datalist.append(usertime)
198  datalist.append(systemtime)
199 
200  cmd = ""
201  for i in datalist:
202  if cmd == "":
203  cmd = str(i)
204  else:
205  cmd = cmd + "\t" + str(i)
206  cmd = cmd + "\n"
207 
208  return cmd
def createInsertString
create an insert string based on the keywords for the table, filling in defaults as needed...
def insertRecords
insert the messages into the database, using a file
def insertRecord
insert a record into the table &quot;dbTable&quot; , given the property set &quot;ps&quot;