def wrapper(settings, sources): """Prepare the data for the analysis then run it and save it if necessary. """ parents = sorted([_id for name, _id in sources.items()]) created_ids = {} data = {name: mongo.data(_id) for name, _id in sources.items()} need_analysis = not all([ mongo.is_analysis(type_, label, parents, stg) for label, stg in settings.items() ]) if need_analysis or FORCE_CACHE: data = func(data, settings) for label, stg in settings.items(): saved = mongo.is_analysis(type_, label, parents, stg) if not saved: _id = mongo.db.analysis.insert_one({ 'type': type_, 'label': label, 'parents': parents, 'settings': stg, 'data': data[label], }).inserted_id else: _id = mongo.db.analysis.find_one({ 'type': type_, 'label': label, 'parents': parents, 'settings': stg, })['_id'] if FORCE_CACHE: mongo.db.analysis.update({ '_id': _id, }, { '$set': { 'data': data[label] } }, upsert=False) created_ids[label] = _id return created_ids
def main(): """Create analysis with crude data in the analysis collection.""" for cow in mongo.cows(): type_ = TYPES['identity'] label = LABELS['prods'] parents = [] settings = {'cow': cow} if mongo.is_analysis(type_, label, parents, settings): print('The document must be unique: {}'.format(cow)) else: mongo.db.analysis.insert_one({ 'type': type_, 'label': label, 'parents': parents, 'settings': settings, 'data': mongo.prods(cow), })