Skip to content

Commit

Permalink
api: sample documents to get schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Oct 3, 2022
1 parent c23afcc commit d236868
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mpcontribs-api/mpcontribs/api/projects/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from importlib import import_module
from flatten_dict import flatten
from boltons.iterutils import remap
from collections import ChainMap
from flask import current_app, render_template, url_for, request
from flask_mongoengine.documents import Document
from marshmallow import ValidationError
Expand Down Expand Up @@ -226,10 +227,13 @@ def post_save(cls, sender, document, **kwargs):
elif "columns" in delta_unset or ncontribs:
# document.columns unset by user to reinit all columns from DB
# -> get paths and units across all contributions from DB
group = {"_id": "$project", "merged": {"$mergeObjects": "$data"}}
pipeline = [{"$match": {"project": document.id}}, {"$group": group}]
result = list(Contributions.objects.aggregate(pipeline))
merged = {} if not result else result[0]["merged"]
pipeline = [
{"$match": {"project": document.id}},
{"$sample": {"size": 1000}},
{"$project": {"data": 1}}
]
result = Contributions.objects.aggregate(pipeline, allowDiskUse=True)
merged = ChainMap(*result)
flat = flatten(remap(merged, visit=visit, enter=enter), reducer="dot")

for k, v in flat.items():
Expand Down

0 comments on commit d236868

Please sign in to comment.