Method: GoodData::Model.upload_multiple_data
- Defined in:
- lib/gooddata/models/model.rb
.upload_multiple_data(data, project_blueprint, options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Hash
Uploads multiple data sets using batch upload interface
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/gooddata/models/model.rb', line 154 def upload_multiple_data(data, project_blueprint, = { :client => GoodData.connection, :project => GoodData.project }) client, project = GoodData.get_client_and_project() project ||= GoodData.project manifest = { 'dataSetSLIManifestList' => data.map do |d| mode = d[:options] && d[:options][:mode] ? d[:options][:mode] : [:mode] || 'FULL' GoodData::Model::ToManifest.dataset_to_manifest(project_blueprint, d[:dataset], mode) end } csv_headers = [] dir = Dir.mktmpdir begin Zip::File.open("#{dir}/upload.zip", create: true) do |zip| # TODO: make sure schema columns match CSV column names zip.get_output_stream('upload_info.json') { |f| f.puts JSON.pretty_generate(manifest) } data.zip(manifest['dataSetSLIManifestList']).each do |item| column_mapping = item[0][:options] ? item[0][:options][:column_mapping] : nil path = item[0][:data] path = item[0][:data].path if item[0][:data].respond_to? :path inline_data = !path.is_a?(String) data_to_upload = inline_data ? path : File.open(path) filename = item[1]['dataSetSLIManifest']['file'] zip.get_output_stream(filename) do |file| data_to_upload.each_with_index do |row, index| row = CSV.parse(row).first unless inline_data if index.zero? row.map! { |h| column_mapping.key(h) || h } if column_mapping csv_headers << row end file.puts row.to_csv end end end end # upload it client.upload_to_user_webdav("#{dir}/upload.zip", :directory => File.basename(dir), :client => [:client], :project => [:project]) ensure FileUtils.rm_rf dir end csv_headers.flatten! # kick the load pull = { 'pullIntegration' => File.basename(dir) } link = project.md.links('etl')['pull2'] # TODO: List uploaded datasets task = client.post(link, pull, :info_message => 'Starting the data load from user storage to dataset.') res = client.poll_on_response(task['pull2Task']['links']['poll'], :info_message => 'Getting status of the dataload task.') do |body| body['wTaskStatus']['status'] == 'RUNNING' || body['wTaskStatus']['status'] == 'PREPARED' end if res['wTaskStatus']['status'] == 'ERROR' s = StringIO.new = res['wTaskStatus']['messages'] || [] .each do |msg| GoodData.logger.error(JSON.pretty_generate(msg)) end begin client.download_from_user_webdav(File.basename(dir) + '/upload_status.json', s, :client => client, :project => project) rescue => e raise "Unable to download upload_status.json from remote server, reason: #{e.message}" end js = MultiJson.load(s.string) manifests = manifest['dataSetSLIManifestList'].map do |m| m['dataSetSLIManifest'] end parts = manifests.map do |m| m['parts'] end manifest_cols = parts.flatten.map { |c| c['columnName'] } # extract some human readable error message from the webdav file csv_headers.map!(&:to_sym) manifest_cols.map!(&:to_sym) manifest_extra = manifest_cols - csv_headers csv_extra = csv_headers - manifest_cols = begin js['error']['message'] % js['error']['parameters'] rescue NoMethodError, ArgumentError '' end m = "Load failed with error '#{error_message}'.\n" m += "Columns that should be there (manifest) but aren't in uploaded csv: #{manifest_extra}\n" unless manifest_extra.empty? m += "Columns that are in csv but shouldn't be there (manifest): #{csv_extra}\n" unless csv_extra.empty? m += "Columns in the uploaded csv: #{csv_headers}\n" m += "Columns in the manifest: #{manifest_cols}\n" m += "Original message:\n#{JSON.pretty_generate(js)}\n" m += "Manifest used for uploading:\n#{JSON.pretty_generate(manifest)}" fail m end res end |