Coverage for /opt/conda/envs/apienv/lib/python3.10/site-packages/daiquiri/core/metadata/ispyalchemy/autoproc.py: 70%

66 statements  

« prev     ^ index     » next       coverage.py v7.6.5, created at 2024-11-15 02:12 +0000

1# -*- coding: utf-8 -*- 

2import os 

3 

4from flask import g 

5 

6from sqlalchemy import func 

7from daiquiri.core.metadata.ispyalchemy.handler import IspyalchemyHandler 

8from daiquiri.core.metadata.ispyalchemy.utils import page, order 

9 

10 

11class AutoProcHandler(IspyalchemyHandler): 

12 exported = [ 

13 "get_autoprocprograms", 

14 "get_autoprocprogram_attachments", 

15 "get_autoprocprogram_messages", 

16 ] 

17 

18 def get_autoprocprograms( 

19 self, 

20 autoprocprogramid=None, 

21 no_context=None, 

22 exclude_sqi=None, 

23 datacollectionid=None, 

24 sampleid=None, 

25 subsampleid=None, 

26 **kwargs, 

27 ): 

28 """ 

29 Remaining arguments from `kwargs` are passed to `page` function. 

30 """ 

31 with self.session_scope() as ses: 

32 autoprocprograms = ( 

33 ses.query( 

34 self.AutoProcProgram.autoprocprogramid, 

35 self.ProcessingJob.datacollectionid, 

36 self.AutoProcProgram.processingstatus.label("status"), 

37 self.AutoProcProgram.processingmessage.label("message"), 

38 self.AutoProcProgram.processingstarttime.label("starttime"), 

39 self.AutoProcProgram.processingendtime.label("endtime"), 

40 self.AutoProcProgram.processingprograms.label("programs"), 

41 self.ProcessingJob.automatic, 

42 func.sum( 

43 func.IF(self.AutoProcProgramMessage.severity == "WARNING", 1, 0) 

44 ).label("warnings"), 

45 func.sum( 

46 func.IF(self.AutoProcProgramMessage.severity == "ERROR", 1, 0) 

47 ).label("errors"), 

48 func.time_to_sec( 

49 func.timediff( 

50 self.AutoProcProgram.processingendtime, 

51 self.AutoProcProgram.processingstarttime, 

52 ) 

53 ).label("duration"), 

54 ) 

55 .outerjoin( 

56 self.AutoProcProgramMessage, 

57 self.AutoProcProgramMessage.autoprocprogramid 

58 == self.AutoProcProgram.autoprocprogramid, 

59 ) 

60 .outerjoin( 

61 self.ImageQualityIndicators, 

62 self.ImageQualityIndicators.autoprocprogramid 

63 == self.AutoProcProgram.autoprocprogramid, 

64 ) 

65 .join( 

66 self.ProcessingJob, 

67 self.ProcessingJob.processingjobid 

68 == self.AutoProcProgram.processingjobid, 

69 ) 

70 .join( 

71 self.DataCollection, 

72 self.DataCollection.datacollectionid 

73 == self.ProcessingJob.datacollectionid, 

74 ) 

75 .join( 

76 self.DataCollectionGroup, 

77 self.DataCollectionGroup.datacollectiongroupid 

78 == self.DataCollection.datacollectiongroupid, 

79 ) 

80 .group_by(self.AutoProcProgram.autoprocprogramid) 

81 ) 

82 

83 if not no_context: 

84 autoprocprograms = autoprocprograms.filter( 

85 self.DataCollectionGroup.sessionid == g.blsession.get("sessionid") 

86 ) 

87 

88 if exclude_sqi: 

89 autoprocprograms = autoprocprograms.filter( 

90 # Cant use is with sqlalchemy, have to user overloaded == 

91 self.ImageQualityIndicators.imagenumber 

92 == None # noqa: E711 

93 ) 

94 

95 if datacollectionid: 

96 autoprocprograms = autoprocprograms.filter( 

97 self.ProcessingJob.datacollectionid == datacollectionid 

98 ) 

99 

100 if sampleid: 

101 autoprocprograms = autoprocprograms.filter( 

102 self.DataCollectionGroup.blsampleid == sampleid 

103 ) 

104 

105 if subsampleid: 

106 autoprocprograms = autoprocprograms.filter( 

107 self.DataCollection.blsubsampleid == subsampleid 

108 ) 

109 

110 if autoprocprogramid: 

111 autoprocprograms = autoprocprograms.filter( 

112 self.AutoProcProgram.autoprocprogramid == autoprocprogramid 

113 ) 

114 autoprocprogram = autoprocprograms.first() 

115 if autoprocprogram: 

116 return autoprocprogram._asdict() 

117 

118 else: 

119 total = autoprocprograms.count() 

120 autoprocprograms = order( 

121 autoprocprograms, 

122 { 

123 "autoprocprogramid": self.AutoProcProgram.autoprocprogramid, 

124 }, 

125 default=["autoprocprogramid", "asc"], 

126 **kwargs, 

127 ) 

128 autoprocprograms = page(autoprocprograms, **kwargs) 

129 return { 

130 "total": total, 

131 "rows": [r._asdict() for r in autoprocprograms.all()], 

132 } 

133 

134 def get_autoprocprogram_attachments( 

135 self, autoprocprogramattachmentid=None, **kwargs 

136 ): 

137 with self.session_scope() as ses: 

138 attachments = ( 

139 ses.query( 

140 self.AutoProcProgramAttachment.autoprocprogramattachmentid, 

141 self.AutoProcProgram.autoprocprogramid, 

142 self.AutoProcProgramAttachment.filename, 

143 self.AutoProcProgramAttachment.filepath, 

144 self.AutoProcProgramAttachment.filetype, 

145 self.AutoProcProgramAttachment.importancerank.label("rank"), 

146 ) 

147 .join( 

148 self.AutoProcProgram, 

149 self.AutoProcProgram.autoprocprogramid 

150 == self.AutoProcProgramAttachment.autoprocprogramid, 

151 ) 

152 .join( 

153 self.ProcessingJob, 

154 self.ProcessingJob.processingjobid 

155 == self.AutoProcProgram.processingjobid, 

156 ) 

157 .join( 

158 self.DataCollection, 

159 self.DataCollection.datacollectionid 

160 == self.ProcessingJob.datacollectionid, 

161 ) 

162 .join( 

163 self.DataCollectionGroup, 

164 self.DataCollectionGroup.datacollectiongroupid 

165 == self.DataCollection.datacollectiongroupid, 

166 ) 

167 ) 

168 

169 if not kwargs.get("no_context"): 

170 attachments = attachments.filter( 

171 self.DataCollectionGroup.sessionid == g.blsession.get("sessionid") 

172 ) 

173 

174 if kwargs.get("autoprocprogramid"): 

175 attachments = attachments.filter( 

176 self.AutoProcProgram.autoprocprogramid 

177 == kwargs["autoprocprogramid"] 

178 ) 

179 

180 if kwargs.get("autoprocprogramattachmentid"): 

181 attachments = attachments.filter( 

182 self.AutoProcProgramAttachment.autoprocprogramattachmentid 

183 == kwargs["autoprocprogramattachmentid"] 

184 ) 

185 

186 if autoprocprogramattachmentid: 

187 attachments = attachments.filter( 

188 self.AutoProcProgramAttachment.autoprocprogramattachmentid 

189 == autoprocprogramattachmentid 

190 ) 

191 attachment = attachments.first() 

192 if attachment: 

193 attachment = attachment._asdict() 

194 attachment["extension"] = ( 

195 os.path.splitext(attachment["filename"])[1][1:].strip().lower() 

196 ) 

197 attachment["filefullpath"] = os.path.join( 

198 attachment["filepath"], attachment["filename"] 

199 ) 

200 return attachment 

201 

202 else: 

203 total = attachments.count() 

204 attachments = page(attachments, **kwargs) 

205 return { 

206 "total": total, 

207 "rows": [r._asdict() for r in attachments.all()], 

208 } 

209 

210 def get_autoprocprogram_messages(self, autoprocprogrammessageid=None, **kwargs): 

211 with self.session_scope() as ses: 

212 messages = ( 

213 ses.query( 

214 self.AutoProcProgramMessage.autoprocprogrammessageid, 

215 self.AutoProcProgram.autoprocprogramid, 

216 self.AutoProcProgramMessage.recordtimestamp.label("timestamp"), 

217 self.AutoProcProgramMessage.severity, 

218 self.AutoProcProgramMessage.message, 

219 self.AutoProcProgramMessage.description, 

220 ) 

221 .join( 

222 self.AutoProcProgram, 

223 self.AutoProcProgram.autoprocprogramid 

224 == self.AutoProcProgramMessage.autoprocprogramid, 

225 ) 

226 .join( 

227 self.ProcessingJob, 

228 self.ProcessingJob.processingjobid 

229 == self.AutoProcProgram.processingjobid, 

230 ) 

231 .join( 

232 self.DataCollection, 

233 self.DataCollection.datacollectionid 

234 == self.ProcessingJob.datacollectionid, 

235 ) 

236 .join( 

237 self.DataCollectionGroup, 

238 self.DataCollectionGroup.datacollectiongroupid 

239 == self.DataCollection.datacollectiongroupid, 

240 ) 

241 ) 

242 

243 if not kwargs.get("no_context"): 

244 messages = messages.filter( 

245 self.DataCollectionGroup.sessionid == g.blsession.get("sessionid") 

246 ) 

247 

248 if kwargs.get("autoprocprogramid"): 

249 messages = messages.filter( 

250 self.AutoProcProgram.autoprocprogramid 

251 == kwargs["autoprocprogramid"] 

252 ) 

253 

254 if kwargs.get("autoprocprogrammessageid"): 

255 messages = messages.filter( 

256 self.AutoProcProgramMessage.autoprocprogrammessageid 

257 == kwargs["autoprocprogrammessageid"] 

258 ) 

259 

260 if autoprocprogrammessageid: 

261 messages = messages.filter( 

262 self.AutoProcProgramMessage.autoprocprogrammessageid 

263 == autoprocprogrammessageid 

264 ) 

265 message = messages.first() 

266 if message: 

267 return message._asdict() 

268 

269 else: 

270 total = messages.count() 

271 messages = page(messages, **kwargs) 

272 return {"total": total, "rows": [r._asdict() for r in messages.all()]}