-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from unepwcmc/gdoc-indicator-working
GDoc Importer actually works (with command and update)
- Loading branch information
Showing
6 changed files
with
354 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,109 @@ | ||
Promise = require 'bluebird' | ||
Indicator = require('../models/indicator').model | ||
_ = require 'underscore' | ||
|
||
GDocWrapper = require('./gdoc_wrapper') | ||
Indicator = require('../models/indicator').model | ||
Theme = require('../models/theme').model | ||
|
||
DEFAULT_INDICATOR_DEFINITION = | ||
"period": "yearly", | ||
"xAxis": "year", | ||
"yAxis": "value", | ||
"geometryField": "geometry", | ||
"fields": [ | ||
{ | ||
"source": { | ||
"name": "periodStart", | ||
"type": "integer" | ||
}, | ||
"name": "year", | ||
"type": "integer" | ||
}, { | ||
"source": { | ||
"name": "value", | ||
"type": "text" | ||
}, | ||
"name": "value", | ||
"type": "integer" | ||
}, { | ||
"source": { | ||
"name": "text", | ||
"type": "text" | ||
}, | ||
"name": "text", | ||
"type": "text" | ||
} | ||
] | ||
|
||
mergeAttributesWithDefaults = (attributes) -> | ||
attributes.indicatorDefinition = _.extend( | ||
DEFAULT_INDICATOR_DEFINITION, attributes.indicatorDefinition | ||
) | ||
return attributes | ||
|
||
extractRangesFromWorksheet = (worksheet) -> | ||
index = 2 | ||
|
||
ranges = [] | ||
while (range = worksheet[index.toString()])? | ||
ranges.push( | ||
threshold: range['1'].value | ||
text: range['2'].value | ||
minValue: parseFloat(range['1'].value, 10) | ||
message: range['2'].value | ||
) | ||
index = index + 1 | ||
|
||
return ranges | ||
|
||
module.exports = | ||
import: (key) -> | ||
|
||
spreadsheet = null | ||
definition = {} | ||
|
||
GDocWrapper.importByKey(key).then((spr)-> | ||
spreadsheet = spr | ||
|
||
spreadsheet.getWorksheetData('Definition') | ||
).then((worksheet) -> | ||
|
||
definition.name = worksheet['2']['1'].value | ||
definition.theme = worksheet['2']['2'].value | ||
definition.unit = worksheet['2']['3'].value | ||
|
||
spreadsheet.getWorksheetData('Ranges') | ||
).then((worksheet) -> | ||
|
||
definition.indicatorationConfig = | ||
module.exports = class GDocIndicatorImporter | ||
constructor: (key) -> | ||
@indicatorProperties = { | ||
indicatorationConfig: | ||
source: 'gdoc' | ||
spreadsheet_key: key | ||
range: extractRangesFromWorksheet(worksheet) | ||
} | ||
|
||
@import: (key) -> | ||
GDocWrapper.importByKey(key).then((spreadsheet) -> | ||
Promise.all([ | ||
spreadsheet.getWorksheetData('Definition'), | ||
spreadsheet.getWorksheetData('Ranges') | ||
]) | ||
).spread((definitionWorksheet, rangesWorksheet) -> | ||
indicatorImporter = new GDocIndicatorImporter(key) | ||
indicatorImporter.setDefinitionFromWorksheet( | ||
definitionWorksheet | ||
).then( -> | ||
indicatorImporter.setRangesFromWorksheet(rangesWorksheet) | ||
indicatorImporter.createOrUpdateIndicator() | ||
) | ||
) | ||
|
||
setDefinitionFromWorksheet: (worksheet) -> | ||
themeTitle = worksheet['2']['2'].value | ||
|
||
indicator = Indicator.buildWithDefaults(definition) | ||
return Theme.findOrCreateByTitle(themeTitle).then((theme) => | ||
_.extend(@indicatorProperties, { | ||
short_name: worksheet['2']['1'].value | ||
title: worksheet['2']['1'].value | ||
theme: theme._id | ||
indicatorDefinition: | ||
unit: worksheet['2']['3'].value | ||
short_unit: worksheet['2']['3'].value | ||
}) | ||
) | ||
|
||
Promise.promisify(indicator.save, indicator)() | ||
setRangesFromWorksheet: (worksheet) -> | ||
@indicatorProperties.indicatorationConfig.range = extractRangesFromWorksheet( | ||
worksheet | ||
) | ||
|
||
createOrUpdateIndicator: -> | ||
existingIndicator = Promise.promisify(Indicator.findOne, Indicator)( | ||
'indicatorationConfig.spreadsheet_key': @indicatorProperties.indicatorationConfig.spreadsheet_key | ||
).then( (indicator) => | ||
@indicatorProperties = mergeAttributesWithDefaults(@indicatorProperties) | ||
if indicator? | ||
Promise.promisify(indicator.update, indicator)(@indicatorProperties) | ||
else | ||
Promise.promisify(Indicator.create, Indicator)(@indicatorProperties) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.