Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
/home/masa/ywesee/oddb.org/src/util/updater.rb | 501 | 473 | 100.00%
|
100.00%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 #!/usr/bin/env ruby |
2 # Updater-- oddb -- 19.02.2003 -- hwyss@ywesee.com |
3 |
4 require 'plugin/analysis' |
5 require 'plugin/bsv_xml' |
6 require 'plugin/comarketing' |
7 require 'plugin/doctors' |
8 require 'plugin/hospitals' |
9 require 'plugin/interaction' |
10 require 'plugin/lppv' |
11 require 'plugin/medwin' |
12 require 'plugin/migel' |
13 require 'plugin/narcotic' |
14 require 'plugin/ouwerkerk' |
15 require 'plugin/rss' |
16 require 'plugin/swissmedic' |
17 require 'plugin/swissmedicjournal' |
18 require 'plugin/swissreg' |
19 require 'plugin/text_info' |
20 require 'plugin/vaccines' |
21 require 'plugin/who' |
22 require 'util/log' |
23 require 'util/persistence' |
24 require 'util/exporter' |
25 require 'ext/meddata/src/ean_factory' |
26 require 'util/schedule' |
27 |
28 module ODDB |
29 class Updater |
30 include Util::Schedule |
31 # Recipients for all Update-Logs go here... |
32 RECIPIENTS = [] |
33 LOG_RECIPIENTS = { |
34 # :powerlink => [], ## Disabled 2.3.2009, there are no Powerlink-Users at the current time |
35 :passthru => [], |
36 } |
37 LOG_FILES = { |
38 :powerlink => 'Powerlink-Statistics', |
39 #:passthru => 'Banner-Clicks', |
40 } |
41 SPONSORS = { |
42 :generika => 'Exklusiv-Sponsoring Generika.cc', |
43 :gcc => 'Exklusiv-Sponsoring ODDB.org', |
44 } |
45 def initialize(app) |
46 @app = app |
47 @smj_updated = false |
48 end |
49 def export_competition_xls(company, db_path=nil) |
50 subj = "Generika-Preisvergleich #{company.name}" |
51 wrap_update(XlsExportPlugin, subj) { |
52 plug = Exporter.new(@app).export_competition_xls(company, db_path) |
53 log = Log.new(@@today) |
54 log.update_values(log_info(plug)) |
55 log.notify(subj) |
56 } |
57 plug = XlsExportPlugin.new(@app) |
58 path = plug.export_competition(company) |
59 end |
60 def export_competition_xlss(db_path=nil) |
61 @app.companies.each_value { |comp| |
62 if(comp.competition_email) |
63 export_competition_xls(comp, db_path) |
64 end |
65 } |
66 end |
67 def export_index_therapeuticus_csv(date = @@today) |
68 subj = 'index_therapeuticus.csv' |
69 wrap_update(CsvExportPlugin, subj) { |
70 plug = CsvExportPlugin.new(@app) |
71 plug.export_index_therapeuticus |
72 log = Log.new(date) |
73 log.update_values(log_info(plug)) |
74 log.notify(subj) |
75 } |
76 end |
77 def export_oddb_csv(date = @@today) |
78 subj = 'oddb.csv' |
79 wrap_update(CsvExportPlugin, subj) { |
80 plug = CsvExportPlugin.new(@app) |
81 plug.export_drugs |
82 log = Log.new(date) |
83 log.update_values(log_info(plug)) |
84 log.notify(subj) |
85 } |
86 end |
87 def export_oddb2_csv(date = @@today) |
88 subj = 'oddb2.csv' |
89 wrap_update(CsvExportPlugin, subj) { |
90 plug = CsvExportPlugin.new(@app) |
91 plug.export_drugs_extended |
92 log = Log.new(date) |
93 log.update_values(log_info(plug)) |
94 log.notify(subj) |
95 } |
96 end |
97 def export_generics_xls(date = @@today) |
98 subj = 'Generikaliste' |
99 wrap_update(XlsExportPlugin, subj) { |
100 plug = XlsExportPlugin.new(@app) |
101 plug.export_generics |
102 log = Log.new(date) |
103 log.update_values(log_info(plug)) |
104 log.notify(subj) |
105 } |
106 end |
107 def export_patents_xls(date = @@today) |
108 subj = 'Export patents.xls' |
109 wrap_update(XlsExportPlugin, subj) { |
110 plug = XlsExportPlugin.new(@app) |
111 plug.export_patents |
112 log = Log.new(date) |
113 log.update_values(log_info(plug)) |
114 log.notify(subj) |
115 } |
116 end |
117 def export_ouwerkerk(date = @@today) |
118 subj = 'Med-Drugs' |
119 wrap_update(OuwerkerkPlugin, subj) { |
120 plug = Exporter.new(@app).export_swissdrug_xls date, |
121 :remove_newlines => true |
122 log = Log.new(date) |
123 log.update_values(log_info(plug)) |
124 log.notify(subj) |
125 } |
126 end |
127 def log_info(plugin, method=:log_info) |
128 hash = plugin.send(method) |
129 hash[:recipients] = if(rcp = hash[:recipients]) |
130 rcp + recipients |
131 else |
132 recipients |
133 end |
134 hash |
135 end |
136 def mail_logfile(name, date, subj, emails=nil) |
137 report = LogFile.read(name, date) |
138 unless report.empty? |
139 log = Log.new(date) |
140 log.report = report |
141 mails = (emails || self::class::LOG_RECIPIENTS[name] || []) |
142 log.recipients = recipients + mails |
143 log.notify(subj) |
144 end |
145 end |
146 def mail_sponsor_logs(date=@@today) |
147 self::class::SPONSORS.each { |name, subj| |
148 if sponsor = @app.sponsor(name) |
149 mail_logfile("sponsor_#{name}", date, subj, sponsor.emails) |
150 end |
151 } |
152 end |
153 def logfile_stats |
154 date = @@today << 1 |
155 if(date.day == 1) |
156 _logfile_stats(date) |
157 mail_sponsor_logs(date) |
158 end |
159 end |
160 def _logfile_stats(date) |
161 self::class::LOG_FILES.each { |name, subj| |
162 mail_logfile(name, date, subj) |
163 } |
164 end |
165 def recipients |
166 self.class::RECIPIENTS |
167 end |
168 def reconsider_bsv opts={} |
169 logs_pointer = Persistence::Pointer.new([:log_group, :bsv_sl]) |
170 logs = @app.create(logs_pointer) |
171 if(latest = logs.newest_date) |
172 klass = BsvXmlPlugin |
173 plug = klass.new(@app) |
174 subj = 'SL-Update Reconsidered' |
175 wrap_update(klass, subj) { |
176 if(plug._update) |
177 log = logs.latest |
178 change_flags = plug.change_flags || {} |
179 if previous = log.change_flags |
180 previous.each do |ptr, flgs| |
181 if flags = change_flags[ptr] |
182 flags.concat flgs |
183 flags.uniq! |
184 else |
185 change_flags[ptr] = flgs |
186 end |
187 end |
188 end |
189 @app.update(log.pointer, {:change_flags => change_flags}) |
190 partlog = Log.new(latest) |
191 partlog.update_values(log_info(plug)) |
192 partlog.notify(subj) |
193 if opts[:new_log] |
194 ## Store all subsequent BSV-Updates in next month. |
195 @app.create(logs_pointer + [:log, latest >> 1]) |
196 end |
197 end |
198 } |
199 end |
200 end |
201 def run |
202 logfile_stats |
203 if(update_swissmedic) |
204 update_swissmedic_followers |
205 end |
206 update_swissmedicjournal |
207 update_vaccines |
208 |
209 return_value_update_bsv = update_bsv |
210 LogFile.append('oddb/debug', " return_value_update_bsv=" + return_value_update_bsv.inspect.to_s, Time.now) |
211 |
212 #if(update_bsv) |
213 if(return_value_update_bsv) |
214 update_bsv_followers |
215 end |
216 update_narcotics |
217 run_on_monthday(1) { |
218 update_interactions |
219 } |
220 end |
221 def run_random |
222 update_fachinfo |
223 end |
224 def update_analysis(path, lang) |
225 klass = AnalysisPlugin |
226 subj = 'Analysis' |
227 wrap_update(klass, subj) { |
228 plug = klass.new(@app) |
229 plug.update(path, lang) |
230 } |
231 end |
232 def update_bsv |
233 |
234 LogFile.append('oddb/debug', " getin update_bsv", Time.now) |
235 |
236 logs_pointer = Persistence::Pointer.new([:log_group, :bsv_sl]) |
237 logs = @app.create(logs_pointer) |
238 this_month = Date.new(@@today.year, @@today.month) |
239 if (latest = logs.newest_date) && latest > this_month |
240 this_month = latest |
241 end |
242 klass = BsvXmlPlugin |
243 plug = klass.new(@app) |
244 subj = 'SL-Update (XML)' |
245 return_value_plug_update = nil |
246 wrap_update(klass, subj) { |
247 |
248 return_value_plug_update = plug.update |
249 LogFile.append('oddb/debug', " return_value_BsvXmlPlugin.update = " + return_value_plug_update.inspect.to_s, Time.now) |
250 |
251 #if plug.update |
252 if return_value_plug_update |
253 log_notify_bsv(plug, this_month, subj) |
254 end |
255 } |
256 return return_value_plug_update |
257 end |
258 def update_bsv_followers |
259 |
260 LogFile.append('oddb/debug', " getin update_bsv_followers", Time.now) |
261 |
262 update_trade_status |
263 update_medwin_packages |
264 update_lppv |
265 update_price_feeds |
266 export_oddb_csv |
267 # export_oddb2_csv # Disabled 4.1.2010 |
268 export_ouwerkerk |
269 export_generics_xls |
270 export_competition_xlss |
271 end |
272 def update_comarketing |
273 update_immediate(CoMarketingPlugin, 'Co-Marketing') |
274 end |
275 def update_company_textinfos *companies |
276 update_notify_simple TextInfoPlugin, |
277 "Fach- und Patienteninfo '#{companies.join(', ')}'", |
278 :import_company, [companies] |
279 end |
280 def update_textinfo_news |
281 update_notify_simple TextInfoPlugin, |
282 "Fach- und Patienteninfo News", |
283 :import_news |
284 end |
285 def update_doctors |
286 update_simple(Doctors::DoctorPlugin, 'Doctors') |
287 end |
288 def update_fachinfo(*iksnrs) |
289 if iksnrs.empty? |
290 update_textinfo_news |
291 else |
292 update_textinfos *iksnrs |
293 end |
294 end |
295 def update_hospitals |
296 update_simple(HospitalPlugin, 'Hospitals') |
297 end |
298 def update_interactions |
299 update_simple(Interaction::InteractionPlugin, 'Interaktionen') |
300 end |
301 def update_lppv |
302 update_immediate(LppvPlugin, 'Lppv prices') |
303 end |
304 def update_medwin_companies |
305 update_simple(MedwinCompanyPlugin, 'Medwin-Companies') |
306 end |
307 def update_medwin_packages |
308 update_simple(MedwinPackagePlugin, 'Medwin-Packages') |
309 end |
310 def update_price_feeds(month = @@today) |
311 RssPlugin.new(@app).update_price_feeds(month) |
312 end |
313 def update_trade_status |
314 update_immediate(MedwinPackagePlugin, 'Trade-Status', :update_trade_status) |
315 end |
316 def update_migel |
317 klass = MiGeLPlugin |
318 subj = 'MiGeL' |
319 status_report = "MiGeL is now up to date" |
320 wrap_update(klass, subj) { |
321 plug = klass.new(@app) |
322 [:de, :fr, :it].each { |lang| |
323 path = File.expand_path("../../data/csv/migel_#{lang}.csv", |
324 File.dirname(__FILE__)) |
325 plug.update(path, lang) |
326 } |
327 plug.prune_old_revisions |
328 status_report |
329 } |
330 end |
331 def update_narcotics |
332 update_notify_simple(NarcoticPlugin, 'Narcotics') |
333 end |
334 def update_swissmedic(*args) |
335 logs_pointer = Persistence::Pointer.new([:log_group, :swissmedic]) |
336 logs = @app.create(logs_pointer) |
337 klass = SwissmedicPlugin |
338 plug = klass.new(@app) |
339 return_value_plug_update = nil |
340 wrap_update(klass, "swissmedic") { |
341 #if(plug.update(*args)) |
342 if(return_value_plug_update = plug.update(*args)) |
343 month = @@today << 1 |
344 pointer = logs.pointer + [:log, Date.new(month.year, month.month)] |
345 log = @app.update(pointer.creator, log_info(plug)) |
346 log.notify('Swissmedic XLS') |
347 end |
348 } |
349 return return_value_plug_update |
350 end |
351 def update_swissmedic_followers |
352 update_trade_status |
353 update_medwin_packages |
354 reconsider_bsv :new_log => true |
355 update_comarketing |
356 update_swissreg_news |
357 update_lppv |
358 update_medwin_companies |
359 exporter = Exporter.new(@app) |
360 exporter.export_generics_xls |
361 export_patents_xls |
362 exporter.mail_swissmedic_notifications |
363 end |
364 def update_swissmedicjournal |
365 logs_pointer = Persistence::Pointer.new([:log_group, :swissmedic_journal]) |
366 logs = @app.create(logs_pointer) |
367 # The first issue of SwissmedicJournal is 2002,1 |
368 latest = logs.newest_date || Date.new(2002,4) |
369 latest_swissmedic = @app.log_group(:swissmedic).newest_date |
370 success = true |
371 while(latest < @@today && latest <= latest_swissmedic && success) |
372 latest = latest >> 1 |
373 klass = SwissmedicJournalPlugin |
374 plug = klass.new(@app) |
375 wrap_update(klass, "swissmedic-journal") { |
376 success = false |
377 success = plug.update(latest) |
378 } |
379 if(success) |
380 pointer = logs.pointer + [:log, latest.dup] |
381 log = @app.update(pointer.creator, log_info(plug)) |
382 log.notify('Swissmedic-Journal') |
383 @smj_updated = latest |
384 end |
385 end |
386 @smj_updated |
387 end |
388 def update_swissreg |
389 update_immediate(SwissregPlugin, 'Patents') |
390 end |
391 def update_swissreg_news |
392 update_immediate(SwissregPlugin, 'Patents', :update_news) |
393 end |
394 def update_textinfos *iksnrs |
395 update_notify_simple TextInfoPlugin, |
396 "Fach- und Patienteninfo '#{iksnrs.join(', ')}'", |
397 :import_fulltext, [iksnrs] |
398 end |
399 def update_vaccines |
400 update_notify_simple(VaccinePlugin, 'blutprodukte') |
401 end |
402 def update_whocc |
403 update_notify_simple WhoPlugin, "WHO-Update", :import |
404 end |
405 private |
406 def log_notify_bsv(plug, date, subj='SL-Update') |
407 |
408 LogFile.append('oddb/debug', " getin log_notify_bsv", Time.now) |
409 LogFile.append('oddb/debug', " date=" + date.inspect.to_s, Time.now) |
410 |
411 pointer = Persistence::Pointer.new([:log_group, :bsv_sl], [:log, date]) |
412 LogFile.append('oddb/debug', " after pointer creating", Time.now) |
413 values = log_info(plug) |
414 LogFile.append('oddb/debug', " after log_info(plug)", Time.now) |
415 if log = pointer.resolve(@app) |
416 change_flags = values[:change_flags] |
417 if previous = log.change_flags |
418 previous.each do |ptr, flgs| |
419 if flags = change_flags[ptr] |
420 flags.concat flgs |
421 flags.uniq! |
422 else |
423 change_flags[ptr] = flgs |
424 end |
425 end |
426 end |
427 end |
428 LogFile.append('oddb/debug', " before @app.update", Time.now) |
429 log = @app.update(pointer.creator, values) |
430 |
431 LogFile.append('oddb/debug', " after @app.update", Time.now) |
432 #log.notify(subj) |
433 return_value_log_notify = log.notify(subj) |
434 LogFile.append('oddb/debug', " the first log.notify end", Time.now) |
435 LogFile.append('oddb/debug', " return_value_log_notify = " + return_value_log_notify.inspect.to_s, Time.now) |
436 |
437 log2 = Log.new(date) |
438 log2.update_values log_info(plug, :log_info_bsv) |
439 |
440 LogFile.append('oddb/debug', " before the second mail process", Time.now) |
441 return_value_log2_notify = log2.notify(subj) |
442 LogFile.append('oddb/debug', " the second log.notify end", Time.now) |
443 LogFile.append('oddb/debug', " return_value_log2_notify = " + return_value_log2_notify.inspect.to_s, Time.now) |
444 #log2.notify(subj) |
445 return_value_log2_notify |
446 end |
447 def notify_error(klass, subj, error) |
448 log = Log.new(@@today) |
449 log.report = [ |
450 "Plugin: #{klass}", |
451 "Error: #{error.class}", |
452 "Message: #{error.message}", |
453 "Backtrace:", |
454 error.backtrace.join("\n"), |
455 ].join("\n") |
456 log.recipients = RECIPIENTS.dup |
457 log.notify("Error: #{subj}") |
458 end |
459 def wrap_update(klass, subj, &block) |
460 begin |
461 block.call |
462 rescue Exception => e #RuntimeError, StandardError => e |
463 notify_error(klass, subj, e) |
464 raise |
465 end |
466 rescue StandardError |
467 nil |
468 end |
469 def update_immediate(klass, subj, update_method=:update) |
470 plug = klass.new(@app) |
471 plug.send(update_method) |
472 log = Log.new(@@today) |
473 log.update_values(log_info(plug)) |
474 log.notify(subj) |
475 rescue StandardError => e #RuntimeError, StandardError => e |
476 notify_error(klass, subj, e) |
477 end |
478 def update_notify_simple(klass, subj, update_method=:update, args=[]) |
479 wrap_update(klass, subj) { |
480 plug = klass.new(@app) |
481 if(plug.send(update_method, *args)) |
482 log = Log.new(@@today) |
483 log.update_values(log_info(plug)) |
484 log.notify(subj) |
485 end |
486 } |
487 end |
488 def update_simple(klass, subj, *update_method) |
489 if(update_method.empty?) |
490 update_method.push(:update) |
491 end |
492 wrap_update(klass, subj) { |
493 plug = klass.new(@app) |
494 plug.send(*update_method) |
495 log = Log.new(@@today) |
496 log.update_values(log_info(plug)) |
497 log.notify(subj) |
498 } |
499 end |
500 end |
501 end |
Generated on Tue Feb 22 13:58:33 +0100 2011 with rcov 0.9.8