Skip to content

Commit 10d078b

Browse files
committed
mapping is working
1 parent 5031cb9 commit 10d078b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import datetime
2+
import random
3+
from uuid import uuid4
4+
5+
from locust import User, between
6+
7+
from mapswipe_workers.utils import user_management
8+
9+
10+
class MapSwipeUser(User):
11+
wait_time = between(0.5, 10)
12+
13+
def __init__(self):
14+
self.project_id = "-MYg8CEf2k1-RitN62X0"
15+
random_string = uuid4()
16+
self.email = f"test_{random_string}@mapswipe.org"
17+
self.username = f"test_{random_string}"
18+
self.password = "mapswipe"
19+
self.user_id = None
20+
self.signed_in_user = None
21+
22+
def set_up_user(self):
23+
# check if is already signed in
24+
if self.signed_in_user is None:
25+
print("user is not signed in. Will create a new user.")
26+
# create user if not exists
27+
user = user_management.create_user(self.email, self.username, self.password)
28+
self.user_id = user.uid
29+
30+
# sign in user
31+
self.signed_in_user = user_management.sign_in_with_email_and_password(
32+
self.email, self.password
33+
)
34+
print("Created a new user.")
35+
else:
36+
print("user is already signed in.")
37+
pass
38+
39+
def create_mock_result(self, group):
40+
start_time = datetime.datetime.utcnow().isoformat()[0:-3] + "Z"
41+
end_time = datetime.datetime.utcnow().isoformat()[0:-3] + "Z"
42+
43+
x_min = int(group["xMin"])
44+
x_max = int(group["xMax"])
45+
y_min = int(group["yMin"])
46+
y_max = int(group["yMax"])
47+
48+
results = {}
49+
for x in range(x_min, x_max):
50+
for y in range(y_min, y_max):
51+
task_id = f"18-{x}-{y}"
52+
result = random.choices([0, 1, 2, 3])[0] # no, yes, maybe, bad_imagery
53+
results[task_id] = result
54+
55+
data = {
56+
"results": results,
57+
"timestamp": end_time,
58+
"startTime": start_time,
59+
"endTime": end_time,
60+
}
61+
return data
62+
63+
def map_a_group(self):
64+
"""Get a group from Firebase for this user.
65+
66+
Make sure that this user has not worked on this group before.
67+
"""
68+
69+
# get the groups that need to be mapped
70+
path = f"/v2/groups/{self.project_id}"
71+
# make sure to set '&' at the end of the string
72+
custom_arguments = 'orderBy="requiredCount"&limitToLast=15&'
73+
new_groups = user_management.get_firebase_db(
74+
path, custom_arguments, self.signed_in_user["idToken"]
75+
)
76+
77+
# get the groups the user has worked on already
78+
path = f"/v2/users/{self.user_id}/contributions/{self.project_id}"
79+
# make sure to set & at the end of the string
80+
custom_arguments = "shallow=True&"
81+
existing_groups = user_management.get_firebase_db(
82+
path, custom_arguments, self.signed_in_user["idToken"]
83+
)
84+
85+
# pick group for mapping
86+
# Get difference between new_groups and existing groups.
87+
# We should get the groups the user has not yet worked on.
88+
if existing_groups is None:
89+
next_group_id = random.choice(list(new_groups.keys()))
90+
else:
91+
existing_groups.pop(
92+
"taskContributionCount", None
93+
) # need to remove this since it is no groupId
94+
remaining_group_ids = list(
95+
set(new_groups.keys()) - set(existing_groups.keys())
96+
)
97+
next_group_id = random.choice(remaining_group_ids)
98+
99+
# get group object
100+
next_group = new_groups[next_group_id]
101+
102+
# create mock result for this group
103+
result = self.create_mock_result(next_group)
104+
105+
# upload results in firebase
106+
path = f"/v2/results/{self.project_id}/{next_group_id}/{self.user_id}"
107+
user_management.set_firebase_db(path, result, self.signed_in_user["idToken"])
108+
109+
110+
if __name__ == "__main__":
111+
mapswipe_user = MapSwipeUser()
112+
mapswipe_user.set_up_user()
113+
mapswipe_user.map_a_group()
114+
mapswipe_user.map_a_group()
115+
mapswipe_user.map_a_group()

0 commit comments

Comments
 (0)