Skip to content

Commit 70d6682

Browse files
authored
Merge pull request #190 from mapswipe/dev
fix write size limit
2 parents 3fa5fcc + f33bd74 commit 70d6682

File tree

7 files changed

+72
-13
lines changed

7 files changed

+72
-13
lines changed

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Welcome to MapSwipe Back-End's documentation!
2828

2929
configuration
3030
installation
31+
tutorials
3132
updating
3233
testing
3334
debugging

docs/source/tutorials.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Tutorials
22
For each project type there should be at least one tutorial. Tutorials are similar to actual projects. To display the tutorial we will use the same design and screens as if a user would map for real.
33

4+
## Deploy tutorials to Firebase Database from Docker
5+
You can upload tutorial data to Firebase like this:
6+
7+
```bash
8+
docker run --name mapswipe_workers_local -it pythonmapswipeworkers_mapswipe_workers bash`
9+
mapswipe_workers --verbose create-tutorial --input_file=sample_data/build_area_tutorial.json
10+
exit
11+
docker rm mapswipe_workers_local
12+
```
13+
14+
If you want to use a customized tutorial make sure to adjust the `.json` file in the sample data folder and also add the file for the tasks.
15+
416
## Data Perspective
517
Tutorials and projects have the following in common:
618
* both have groups with the same structure (there can also be several groups per tutorial)

mapswipe_workers/mapswipe_workers/base/base_project.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from mapswipe_workers import auth
99
from mapswipe_workers.definitions import DATA_PATH
1010
from mapswipe_workers.definitions import logger
11+
from mapswipe_workers.definitions import CustomError
1112

1213

1314
class BaseProject(metaclass=ABCMeta):
@@ -137,22 +138,24 @@ def save_project(self, fb_db):
137138
f' to firebase'
138139
)
139140
return True
140-
except Exception:
141+
except Exception as e:
142+
self.delete_from_postgres()
141143
logger.exception(
142144
f'{self.projectId}'
143145
f' - the project could not be saved'
144146
f' to firebase. '
145147
)
146-
self.delete_from_postgres()
147-
return False
148-
except Exception:
148+
149+
logger.info(f'{self.projectId} deleted project data from postgres')
150+
raise CustomError(e)
151+
except Exception as e:
149152
logger.exception(
150153
f'{self.projectId}'
151154
f' - the project could not be saved'
152155
f' to postgres and will therefor not be '
153156
f' saved to firebase'
154157
)
155-
return False
158+
raise CustomError(e)
156159

157160
def save_to_firebase(self, fb_db, project, groups, groupsOfTasks):
158161

@@ -164,16 +167,38 @@ def save_to_firebase(self, fb_db, project, groups, groupsOfTasks):
164167

165168

166169
ref = fb_db.reference('')
170+
# save project
167171
ref.update({
168-
f'v2/projects/{self.projectId}': project,
169-
f'v2/groups/{self.projectId}': groups,
170-
f'v2/tasks/{self.projectId}': groupsOfTasks,
172+
f'v2/projects/{self.projectId}': project
171173
})
172174
logger.info(
173175
f'{self.projectId} -'
174-
f' uploaded project, groups and'
175-
f' tasks to firebase realtime database'
176+
f' uploaded project to firebase realtime database'
176177
)
178+
# save groups
179+
ref.update({
180+
f'v2/groups/{self.projectId}': groups
181+
})
182+
logger.info(
183+
f'{self.projectId} -'
184+
f' uploaded groups to firebase realtime database'
185+
)
186+
# save tasks, to avoid firebase write size limit we write chunks of task
187+
# we write the tasks for 250 groups at once
188+
task_upload_dict = {}
189+
190+
logger.info(f'there are {len(groupsOfTasks)} groups for this project')
191+
for group_id, tasks_list in groupsOfTasks.items():
192+
task_upload_dict[f'v2/tasks/{self.projectId}/{group_id}'] = tasks_list
193+
194+
if len(task_upload_dict) % 150 == 0:
195+
ref.update(task_upload_dict)
196+
logger.info(
197+
f'{self.projectId} -'
198+
f' uploaded 150 groups with tasks to firebase realtime database'
199+
)
200+
task_upload_dict = {}
201+
177202
ref = fb_db.reference(f'v2/projectDrafts/{self.projectId}')
178203
ref.set({})
179204

mapswipe_workers/mapswipe_workers/firebase_to_postgres/update_data.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def update_user_data(user_ids=None):
3232
# No users in the Postgres database yet.
3333
# Get all users from Firebase.
3434
users = fb_ref.get()
35-
print(users)
3635
else:
3736
# Get only new users from Firebase.
3837
last_updated = last_updated.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

mapswipe_workers/mapswipe_workers/mapswipe_workers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def _run_create_projects(project_draft_ids=None):
348348
project.calc_required_results()
349349
if project.save_project(fb_db):
350350
created_project_ids.append(project.projectId)
351-
newline = '\nnewline'
351+
newline = '\n'
352352
message = (
353353
f'### PROJECT CREATION SUCCESSFUL ###{newline}'
354354
f'Project Name: {project.name}{newline}'

mapswipe_workers/sample_data/build_area_project_drafts.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,27 @@
3232
"wmtsLayerName": "FALSE_COLOR"
3333
},
3434
"zoomLevel": 14
35+
},
36+
"build_area_big": {
37+
"createdBy": "Sample Manager",
38+
"geometry": {
39+
"type": "FeatureCollection",
40+
"name": "Bukindon_South",
41+
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
42+
"features": [
43+
{ "type": "Feature", "properties": { }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 124.552337022305508, 8.013183564718995 ], [ 125.377278964821471, 8.029047832844302 ], [ 125.37539673, 8.02042961000001 ], [ 125.38337708, 8.00566959 ], [ 125.38165283, 7.98554993 ], [ 125.40270996, 7.96005011 ], [ 125.40054321, 7.93393993 ], [ 125.42089081, 7.91193008 ], [ 125.41744232, 7.81731987000001 ], [ 125.44413757, 7.75562 ], [ 125.44290924, 7.71647978 ], [ 125.42626953, 7.68472004 ], [ 125.43073273, 7.65559006 ], [ 125.42360687, 7.64100981 ], [ 125.39592743, 7.62140989 ], [ 125.37480164, 7.63410997 ], [ 125.35057831, 7.63816023 ], [ 125.32256317, 7.62744999 ], [ 125.30198669, 7.63472986 ], [ 125.30090332, 7.64885998 ], [ 125.28074646, 7.64744997 ], [ 125.28038025, 7.64074993 ], [ 125.25241089, 7.64373016000002 ], [ 125.25203705, 7.57984018000001 ], [ 125.21894073, 7.59195995 ], [ 125.21656799, 7.57053995 ], [ 125.19329834, 7.57212019 ], [ 125.16586304, 7.5229702 ], [ 125.15531921, 7.46537018 ], [ 125.13262939, 7.46412992 ], [ 125.11971283, 7.45313978000001 ], [ 125.06778717, 7.45175982000001 ], [ 125.05996704, 7.41956997 ], [ 125.04620361, 7.40458012000001 ], [ 124.97190857, 7.40071011000001 ], [ 124.94645691, 7.41130018 ], [ 124.92875671, 7.43205023 ], [ 124.89972687, 7.41360998 ], [ 124.88018036, 7.41554022 ], [ 124.87835693, 7.43260002 ], [ 124.86456299, 7.4320302 ], [ 124.84023285, 7.45099020000001 ], [ 124.84063721, 7.47724009 ], [ 124.82692719, 7.48998022000001 ], [ 124.81472015, 7.49597979000001 ], [ 124.7882309, 7.48022985 ], [ 124.79981232, 7.51733017 ], [ 124.82507324, 7.54148006 ], [ 124.82501221, 7.55834007 ], [ 124.81430054, 7.56812 ], [ 124.82051086, 7.57296991 ], [ 124.81165314, 7.57566977 ], [ 124.81788635, 7.59229994 ], [ 124.80444336, 7.5985198 ], [ 124.8025589, 7.62318993 ], [ 124.75379944, 7.65322018000001 ], [ 124.75311279, 7.67986012 ], [ 124.74120331, 7.68281984 ], [ 124.74056244, 7.69629002000001 ], [ 124.72891235, 7.70124006 ], [ 124.72543335, 7.7186799 ], [ 124.73486328, 7.73115015 ], [ 124.70761108, 7.75507021 ], [ 124.70349884, 7.77635002 ], [ 124.68052673, 7.79621983 ], [ 124.66120911, 7.83530998000002 ], [ 124.63118744, 7.84638977 ], [ 124.62483215, 7.87036991 ], [ 124.61730957, 7.87075996 ], [ 124.62412262, 7.89082003 ], [ 124.59819794, 7.90516996000001 ], [ 124.60048676, 7.9162302 ], [ 124.55732727, 7.9840498 ], [ 124.56124115, 7.99703979000002 ], [ 124.552337022305508, 8.013183564718995 ] ] ] ] } }
44+
]
45+
},
46+
"image": "http://www.redcrosseth.org/media/k2/items/cache/5a05a447acfdf6fcc40548cc4c1cea8d_L.jpg",
47+
"lookFor": "BUILDINGS",
48+
"name": "BuildArea Big",
49+
"projectDetails": "This project uses Bing as the tile server and zoom level 18. This is the setup most MapSwipe projects used so far.",
50+
"verificationNumber": 3,
51+
"groupSize": 120,
52+
"tileServer": {
53+
"name": "bing",
54+
"credits": "© 2019 Microsoft Corporation, Earthstar Geographics SIO"
55+
},
56+
"projectType": 1
3557
}
3658
}

mapswipe_workers/tests/test_01_create_projects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def create_project_manager(email, username, password):
1818
def load_sample_project_drafts():
1919
sample_data = {}
2020
test_dir = os.path.dirname(os.path.abspath(__file__))
21-
sample_data_dir = os.path.join(test_dir, 'sample_data/')
21+
sample_data_dir = os.path.join(test_dir, '../sample_data/')
2222
for sample_project_drafts_json in glob.glob(sample_data_dir + '*_drafts.json'):
2323
if sample_project_drafts_json == 'build_area_to_big_project_drafts_test.json':
2424
continue

0 commit comments

Comments
 (0)