-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·158 lines (92 loc) · 3.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
import os
import webapp2
import jinja2
import logging
from models import Animal
from google.appengine.ext import ndb
from mapreduce import base_handler
from mapreduce import mapreduce_pipeline
from mapreduce import operation as op
from mapreduce import shuffler
from google.appengine.api import app_identity
bucket_name = app_identity.get_default_gcs_bucket_name()
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
PARENT = ndb.Key("FoodChain", 1)
class MainPage(webapp2.RequestHandler):
def get(self):
animals = [a.as_dict() for a in Animal.query(ancestor=PARENT)]
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render({"animals": animals}))
class NewAnimalHandler(webapp2.RequestHandler):
def post(self):
name = self.request.get('animal')
if name:
animal = Animal(parent=PARENT, name=name)
animal.put()
self.redirect('/')
class NewPreyHandler(webapp2.RequestHandler):
def post(self):
predator = self.request.get('predator')
prey = self.request.get('prey')
animal = Animal.get_by_id(int(predator), parent=PARENT)
animal.prey.append(ndb.Key("FoodChain", 1, Animal, int(prey)))
animal.put()
self.redirect('/')
"""
def prey_count_map(animal):
aid = animal.key.id()
pids = [p.id() for p in animal.prey]
for pid in pids:
yield (pid, aid)
def prey_count_reduce(animal_id, predator_ids):
yield "%s,%i\n" % (animal_id, len(predator_ids))
"""
def prey_count_map(animal):
for preyAnimal in animal.prey:
yield (preyAnimal.id(), animal.key.id())
def prey_count_reduce(key, values):
animal = Animal.get_by_id(int(key), parent=PARENT)
yield "%s: %d\n" % ( str(animal.name), len(values) )
class PreyCountPipeline(base_handler.PipelineBase):
def run(self):
output = yield mapreduce_pipeline.MapreducePipeline(
"prey_count",
"main.prey_count_map",
"main.prey_count_reduce",
"mapreduce.input_readers.DatastoreInputReader",
"mapreduce.output_writers.GoogleCloudStorageOutputWriter",
mapper_params={
"entity_kind": 'models.Animal'
},
reducer_params={
"output_writer": {
"bucket_name": bucket_name,
"content_type": "text/plain",
}
}, shards=1)
class MapRecuceHandler(webapp2.RequestHandler):
def post(self):
pipeline = PreyCountPipeline()
pipeline.start()
logging.info("STARTED PIPELINE %s", pipeline.pipeline_id)
self.redirect("/mr?pipeline=" + pipeline.pipeline_id)
def get(self):
pipeline_id = self.request.get("pipeline")
pipeline = mapreduce_pipeline.MapreducePipeline.from_id(pipeline_id)
if pipeline.has_finalized:
logging.info("Valamis")
self.response.headers['content-type'] = 'text/plain; charset=utf-8'
self.response.write("Valmis")
else:
logging.info("Ei valmis")
self.response.headers['content-type'] = 'text/plain; charset=utf-8'
self.response.write("Ei valmis")
app = webapp2.WSGIApplication([
('/', MainPage),
('/newanimal', NewAnimalHandler),
('/newprey', NewPreyHandler),
('/mr', MapRecuceHandler)], debug=True)