Class: ODDB::Exporter

Inherits:
Object show all
Includes:
Util::Schedule
Defined in:
src/util/exporter.rb,
test/test_util/exporter.rb

Defined Under Namespace

Classes: SessionStub

Constant Summary

EXPORT_SERVER =
StubDRbObject.new
EXPORT_DIR =
File.expand_path('../../data/downloads',
File.dirname(__FILE__))

Instance Method Summary (collapse)

Methods included from Util::Schedule

#run_on_monthday, #run_on_weekday

Constructor Details

- (Exporter) initialize(app)

A new instance of Exporter



29
30
31
# File 'src/util/exporter.rb', line 29

def initialize(app)
  @app = app
end

Instance Method Details

- (Object) export_all_csv



69
70
71
72
73
74
# File 'src/util/exporter.rb', line 69

def export_all_csv
  export_csv
  export_doc_csv
  export_index_therapeuticus_csv
  export_price_history_csv
end

- (Object) export_analysis_csv



91
92
93
94
95
96
# File 'src/util/exporter.rb', line 91

def export_analysis_csv
  plug = CsvExportPlugin.new(@app)
  plug.export_analysis
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_competition_xls(company, db_path = nil)



75
76
77
78
79
# File 'src/util/exporter.rb', line 75

def export_competition_xls(company, db_path=nil)
  plug = XlsExportPlugin.new(@app)
  plug.export_competition(company, db_path)
  plug
end

- (Object) export_csv



80
81
82
83
84
85
86
87
88
89
90
# File 'src/util/exporter.rb', line 80

def export_csv
  plug = CsvExportPlugin.new(@app)
  safe_export 'oddb.csv' do
    plug.export_drugs
  end
  safe_export 'oddb.csv' do
    plug.export_drugs_extended
  end
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_doc_csv



97
98
99
100
101
102
103
104
# File 'src/util/exporter.rb', line 97

def export_doc_csv
  safe_export 'doctors.csv' do
    plug = CsvExportPlugin.new(@app)
    plug.export_doctors
  end
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_fachinfo_pdf(langs = [:de, :fr])



105
106
107
108
109
110
111
112
113
114
115
# File 'src/util/exporter.rb', line 105

def export_fachinfo_pdf(langs = [:de, :fr])
  plug = FiPDFExporter.new(@app)
  langs.each { |lang|
    name = "fachinfos_#{lang}.pdf"
    safe_export name do
      path = File.join(EXPORT_DIR, name)
      plug.write_pdf(lang, path)
      EXPORT_SERVER.compress(EXPORT_DIR, name)
    end
  }
end

- (Object) export_generics_xls



116
117
118
119
120
# File 'src/util/exporter.rb', line 116

def export_generics_xls
  plug = XlsExportPlugin.new(@app)
  plug.export_generics
  plug
end

- (Object) export_helper(name)



64
65
66
67
68
# File 'src/util/exporter.rb', line 64

def export_helper(name)
  EXPORT_SERVER.remote_safe_export(EXPORT_DIR, name) { |path|
    yield path
  }
end

- (Object) export_index_therapeuticus_csv



130
131
132
133
134
135
136
137
# File 'src/util/exporter.rb', line 130

def export_index_therapeuticus_csv
  safe_export 'index_therapeuticus' do
    plug = CsvExportPlugin.new(@app)
    plug.export_index_therapeuticus
  end
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_migel_csv



138
139
140
141
142
143
# File 'src/util/exporter.rb', line 138

def export_migel_csv
  plug = CsvExportPlugin.new(@app)
  plug.export_migel
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_narcotics_csv



144
145
146
147
148
149
# File 'src/util/exporter.rb', line 144

def export_narcotics_csv
  plug = CsvExportPlugin.new(@app)
  plug.export_narcotics
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_oddbdat



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'src/util/exporter.rb', line 150

def export_oddbdat
      dose_missing_list = []
  safe_export 'oddbdat' do
    exporter = OdbaExporter::OddbDatExport.new(@app)
    dose_missing_list = exporter.export
    EXPORT_SERVER.clear
    sleep(30)
    run_on_weekday(1) {
      exporter.export_fachinfos
      EXPORT_SERVER.clear
      sleep(30)
    }

    # here to raise warning if package.parts is empty
    if !dose_missing_list.empty?
  log = Log.new(@@today)
  log.report = [
    "Warning: Dose data (ODDB::Package.parts, Array of ODDB::Dose instances) is empty.",
    "Message: export_oddbdat succeeded but the following package(s) do not have Dose data.",
    "Package(s):",
            dose_missing_list.collect do |list|
              list[0].to_s + ", " + \
              "http://#{SERVER_NAME}/de/gcc/resolve/pointer/%3A!registration%2C" + list[1].to_s + \
              "!sequence%2C" + list[2].to_s + "!package%2C" + list[3].to_s + ".\n"
            end
  ].join("\n")
  log.notify("Warning Export: oddbdat")
    end
  end
end

- (Object) export_patents_xls



196
197
198
199
200
# File 'src/util/exporter.rb', line 196

def export_patents_xls
  plug = XlsExportPlugin.new(@app)
  plug.export_patents
  plug
end

- (Object) export_pdf



180
181
182
# File 'src/util/exporter.rb', line 180

def export_pdf
  FiPDFExporter.new(@app).run
end

- (Object) export_price_history_csv



277
278
279
280
281
282
283
284
# File 'src/util/exporter.rb', line 277

def export_price_history_csv
  safe_export 'price_history.csv' do
    plug = CsvExportPlugin.new(@app)
    plug.export_price_history
  end
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) export_sl_pcodes



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'src/util/exporter.rb', line 183

def export_sl_pcodes
  safe_export 'sl_pcodes.txt' do
    path = File.expand_path('../../data/txt/sl_pcodes.txt',
      File.dirname(__FILE__))
    File.open(path, 'w') { |fh|
      @app.each_package { |pac|
        if(pac.sl_entry && pac.pharmacode)
          fh.puts(pac.pharmacode)
        end
      }
    }
  end
end

- (Object) export_swissdrug_xls(date = @@today, opts = {})



121
122
123
124
125
126
127
128
129
# File 'src/util/exporter.rb', line 121

def export_swissdrug_xls(date = @@today, opts={})
  plug = OuwerkerkPlugin.new(@app, "swissdrug update")
  plug.export_xls opts
  name = 'swissdrug-update.xls'
  path = File.join(EXPORT_DIR, name)
  FileUtils.cp(plug.file_path, path)
  EXPORT_SERVER.compress(EXPORT_DIR, name)
  plug
end

- (Object) export_yaml



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'src/util/exporter.rb', line 201

def export_yaml
  exporter = YamlExporter.new(@app)
  safe_export 'oddb.yaml' do
    exporter.export
  end
  safe_export 'atc.yaml' do
    exporter.export_atc_classes
  end
  safe_export 'interactions.yaml' do
    exporter.export_interactions
  end
  safe_export 'narcotics.yaml' do
    exporter.export_narcotics
  end
  safe_export 'price_history.yaml' do
    exporter.export_prices
  end
  run_on_weekday(2) {
    safe_export 'fachinfo.yaml' do
      exporter.export_fachinfos
    end
  }
  run_on_weekday(3) {
    safe_export 'patinfo.yaml' do
      exporter.export_patinfos
    end
  }
  run_on_weekday(4) {
    safe_export 'doctors.yaml' do
      exporter.export_doctors
    end
  }
  EXPORT_SERVER.clear
  sleep(30)
end

- (Object) mail_download_invoices



241
242
243
244
245
# File 'src/util/exporter.rb', line 241

def mail_download_invoices
  safe_export 'Mail Download-Invoices' do
    DownloadInvoicer.new(@app).run
  end
end

- (Object) mail_download_stats



236
237
238
239
240
# File 'src/util/exporter.rb', line 236

def mail_download_stats
  safe_export 'Mail Download-Statistics' do
    mail_stats('download')
  end
end

- (Object) mail_fachinfo_log(day = @@today)



246
247
248
249
250
251
252
253
254
255
256
257
# File 'src/util/exporter.rb', line 246

def mail_fachinfo_log(day = @@today)
  safe_export 'Mail Fachinfo-Invoices' do
    plug = FachinfoInvoicer.new(@app)
    plug.run(day)
    if report = plug.report
      log = Log.new(day)
      log.date_str = day.strftime("%d.%m.%Y")
      log.report = report
      log.notify("Fachinfo-Uploads")
    end
  end
end

- (Object) mail_feedback_stats



258
259
260
261
262
# File 'src/util/exporter.rb', line 258

def mail_feedback_stats
  safe_export 'Mail Feedback-Statistics' do
    mail_stats('feedback')
  end
end

- (Object) mail_notification_stats



263
264
265
266
267
268
269
270
271
# File 'src/util/exporter.rb', line 263

def mail_notification_stats
  file = @app.notification_logger.create_csv(@app)
  headers = {
    :filename => 'notifications.csv',
    :mime_type => 'text/csv',
    :subject => 'T├Ąglicher CSV-Export der Notifications', 
  }
  Log.new(@@today).notify_attachment(file, headers)
end

- (Object) mail_patinfo_invoices



272
273
274
275
276
# File 'src/util/exporter.rb', line 272

def mail_patinfo_invoices
  safe_export 'Mail Patinfo-Invoices' do
    PatinfoInvoicer.new(@app).run
  end
end

- (Object) mail_stats(key)



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'src/util/exporter.rb', line 285

def mail_stats(key)
  date = @@today
  if(date.mday < 8)
    date = date << 1
  end
  log = Log.new(date)
  begin
    log.report = File.read(LogFile.filename(key, date))
  rescue StandardError => e
    log.report = ([
      "Nothing to Report.",
      nil, 
      e.class,
      e.message 
    ] + e.backtrace).join("\n")
  end
  log.notify("#{key.capitalize}-Statistics")
end

- (Object) mail_swissmedic_notifications



303
304
305
# File 'src/util/exporter.rb', line 303

def mail_swissmedic_notifications
  SwissmedicPlugin.new(@app).mail_notifications
end

- (Object) run



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
# File 'src/util/exporter.rb', line 32

def run
  ## restart the export server
  EXPORT_SERVER.clear
  sleep(30)
  #
  mail_patinfo_invoices
  mail_fachinfo_log
  run_on_monthday(1) {
    mail_download_invoices
  }
  run_on_monthday(15) {
    mail_download_invoices
  }
  run_on_weekday(0) {
    mail_download_stats
    mail_feedback_stats
    #mail_notification_stats
  }
  export_sl_pcodes
  export_yaml
  export_oddbdat
  export_csv
  export_doc_csv
  export_index_therapeuticus_csv
  export_price_history_csv
=begin # inoperable atm.
  run_on_monthday(1) {
    export_fachinfo_pdf
  }
=end


  nil
end

- (Object) safe_export(subject, &block)



306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'src/util/exporter.rb', line 306

def safe_export subject, &block
  block.call
rescue StandardError => e
  EXPORT_SERVER.clear rescue nil
  log = Log.new(@@today)
  log.report = [
    "Error: #{e.class}",
    "Message: #{e.message}",
    "Backtrace:",
    e.backtrace.join("\n"),
  ].join("\n")
  log.notify("Error Export: #{subject}")
  sleep(30)
end