Module: ODDB::Export::Server

Defined in:
lib/oddb/export/server.rb

Class Method Summary collapse

Class Method Details

.compress(dir, name) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/oddb/export/server.rb', line 114

def Server.compress(dir, name)
  FileUtils.mkdir_p(dir)
  Dir.chdir(dir)
  tmp_name = name + '.tmp'
  gz_name = tmp_name + '.gz'
  zip_name = tmp_name + '.zip'
  gzwriter = 	Zlib::GzipWriter.open(gz_name)
  zipwriter = Zip::ZipOutputStream.open(zip_name)
  zipwriter.put_next_entry(name)
  File.open(name, "r") { |fh|
    fh.each { |line|
      gzwriter << line
      zipwriter.puts(line)
    }
  }
  gzwriter.close if(gzwriter)
  zipwriter.close if(zipwriter)
  FileUtils.mv(gz_name, name + '.gz')
  FileUtils.mv(zip_name, name + '.zip')
  name
end

.export_chde_xlsObject



23
24
25
26
27
# File 'lib/oddb/export/server.rb', line 23

def Server.export_chde_xls
  if uri = ODDB.config.remote_databases.first
    safe_export Export::Xls::ComparisonDeCh, 'chde.xls', uri
  end
end

.export_csvObject



17
18
19
20
21
22
# File 'lib/oddb/export/server.rb', line 17

def Server.export_csv
  pacs = Drugs::Package.all
  components = [ :pzn, :product, :active_agents, :size, :price_exfactory,
                 :price_public, :price_festbetrag, :ddd_prices, :company ]
  safe_export Csv::Packages, 'de.oddb.csv', pacs, components, :de
end

.export_fachinfo_yamlObject



31
32
33
# File 'lib/oddb/export/server.rb', line 31

def Server.export_fachinfo_yaml
  safe_export Export::Yaml::Fachinfos, "fachinfos.de.oddb.yaml"
end

.export_patinfo_yamlObject



34
35
36
# File 'lib/oddb/export/server.rb', line 34

def Server.export_patinfo_yaml
  safe_export Export::Yaml::Patinfos, "patinfos.de.oddb.yaml"
end

.export_yamlObject



28
29
30
# File 'lib/oddb/export/server.rb', line 28

def Server.export_yaml
  safe_export Export::Yaml::Drugs, 'de.oddb.yaml'
end

.on_monthday(day, today = Date.today, &block) ⇒ Object



69
70
71
72
73
# File 'lib/oddb/export/server.rb', line 69

def Server.on_monthday(day, today = Date.today, &block)
  if(today.day == day)
    block.call
  end
end

.on_weekday(wday, today = Date.today, &block) ⇒ Object



74
75
76
77
78
79
# File 'lib/oddb/export/server.rb', line 74

def Server.on_weekday(wday, today = Date.today, &block)
  # 0-6, 0:Sunday
  if(today.wday == wday)
    block.call
  end
end

.report_downloadObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/oddb/export/server.rb', line 37

def Server.report_download
  # Note: if the day is in new month from the last week,
  # it may not include all the log information.
  subject = sprintf("de.ODDB.org Report - Download-Statistics - %s", 
                    Time.now.strftime('%m/%Y'))
  time = Time.now
  log_dir = File.join(ODDB.config.download_log_dir, time.year.to_s)
  log_file = File.join(log_dir, time.month.to_s + '.log')
  begin
    lines = File.readlines(log_file)
  rescue StandardError => e
    lines = [
       "Nothing to Report.",
       nil,
       e.class,
       e.message
    ] + e.backtrace
  end
  Util::Mail.notify_admins(subject, lines)
end

.run(today = Date.today) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oddb/export/server.rb', line 57

def Server.run(today = Date.today)
  on_monthday(1, today) {
    export_chde_xls
  }
  on_monthday(2, today) do
    export_info_yaml :fachinfo
    export_info_yaml :patinfo
  end
  on_weekday(0, today) do
    report_download
  end
end

.safe_export(exporter_class, name, *args, &block) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/oddb/export/server.rb', line 80

def Server.safe_export(exporter_class, name, *args, &block)
  dir = ODDB.config.export_dir or raise "Please configure 'export_dir'"
  FileUtils.mkdir_p(dir)
  Tempfile.open(name, dir) { |fh|
    exporter = exporter_class.new
    args.push fh
    exporter.export *args
    fh.close
    newpath = File.join(dir, name)
    FileUtils.mv(fh.path, newpath)
    FileUtils.chmod(0644, newpath)
    compress(dir, name)
    # This is a temporary solution for a NoMethodError bug
    # See the bug http://dev.ywesee.com/wiki.php/Masa/20101020-debug-importChdeXls#DebugChde
    if(exporter_class ==  Export::Xls::ComparisonDeCh)
      unless(exporter.error_data.empty?)
        message = "\nThe chde.xls file was generated successfully.\n\n"
        message += "The following data was not able to be compared due to a NoMethodError:\n"
        backtrace_info = "The original backtrace information:\n" + exporter.backtrace_info.join("\n").to_s + "\n"
        raise NoMethodError, message + exporter.error_data.join("\n").to_s + "\n\n" + backtrace_info
      end
    end
  }
  name
rescue StandardError => err
  subject = sprintf("%s: %s", 
                    Time.now.strftime('%c'), exporter_class)
  lines = [
    sprintf("%s: %s#export", 
            Time.now.strftime('%c'), exporter_class)
  ]
  lines.push(err.class, err.message, *err.backtrace)
  Util::Mail.notify_admins(subject, lines)
end