Commit b6c00f5235248f747a7a2356fd0ee843650c7593
1 parent
ca3ad958
Exists in
master
and in
1 other branch
Cleaned code and populated strategies set.
Showing
1 changed file
with
86 additions
and
107 deletions
Show diff stats
src/web/survey.py
| ... | ... | @@ -25,65 +25,25 @@ sys.setrecursionlimit(50000) |
| 25 | 25 | |
| 26 | 26 | class Index: |
| 27 | 27 | def GET(self): |
| 28 | - return render.survey_index() | |
| 28 | + return render.index_survey() | |
| 29 | 29 | |
| 30 | 30 | class About: |
| 31 | 31 | def GET(self): |
| 32 | - return render.about() | |
| 32 | + return render.about_survey() | |
| 33 | 33 | |
| 34 | 34 | class Thanks: |
| 35 | 35 | def POST(self): |
| 36 | 36 | web_input = web.input() |
| 37 | 37 | user_id = web_input['user_id'].encode('utf8') |
| 38 | - with open("/var/www/AppRecommender/src/web/submissions/%s/ident" % user_id,'w') as ident: | |
| 38 | + with open("/var/www/AppRecommender/src/web/submissions/%s/personal" % user_id,'w') as ident: | |
| 39 | 39 | for key in ["name","email","comments"]: |
| 40 | 40 | if web_input.has_key(key): |
| 41 | - ident.write("%s: %s\n" % (key,web_input[key].encode("utf-8"))) | |
| 41 | + ident.write("# %s\n%s\n" % (key.capitalize(),web_input[key].encode("utf-8"))) | |
| 42 | 42 | return render.thanks_id() |
| 43 | 43 | |
| 44 | 44 | class Fake: |
| 45 | 45 | def GET(self): |
| 46 | - return render.index() | |
| 47 | - #return render_plain.fake() | |
| 48 | - | |
| 49 | -class Package: | |
| 50 | - def GET(self, pkg): | |
| 51 | - result = self.get_details_from_dde(pkg) | |
| 52 | - return render_plain.package(result) | |
| 53 | - | |
| 54 | - def get_details_from_dde(self, pkg): | |
| 55 | - json_source = Config().dde_url % pkg | |
| 56 | - json_data = json.load(urllib.urlopen(json_source)) | |
| 57 | - # parse tags | |
| 58 | - tags = self._debtags_list_to_dict(json_data['r']['tag']) | |
| 59 | - json_data['r']['tag'] = tags | |
| 60 | - return json_data['r'] | |
| 61 | - | |
| 62 | - def _debtags_list_to_dict(self, debtags_list): | |
| 63 | - """ in: | |
| 64 | - ['use::editing', | |
| 65 | - 'works-with-format::gif', | |
| 66 | - 'works-with-format::jpg', | |
| 67 | - 'works-with-format::pdf'] | |
| 68 | - out: | |
| 69 | - {'use': [editing], | |
| 70 | - 'works-with-format': ['gif', 'jpg', 'pdf']' | |
| 71 | - } | |
| 72 | - """ | |
| 73 | - debtags = {} | |
| 74 | - subtags = [] | |
| 75 | - for tag in debtags_list: | |
| 76 | - match = re.search(r'^(.*)::(.*)$', tag) | |
| 77 | - if not match: | |
| 78 | - log.error("Could not parse debtags format from tag: %s", tag) | |
| 79 | - facet, subtag = match.groups() | |
| 80 | - subtags.append(subtag) | |
| 81 | - if facet not in debtags: | |
| 82 | - debtags[facet] = subtags | |
| 83 | - else: | |
| 84 | - debtags[facet].append(subtag) | |
| 85 | - subtags = [] | |
| 86 | - return debtags | |
| 46 | + return render_plain.fake() | |
| 87 | 47 | |
| 88 | 48 | class Save: |
| 89 | 49 | def POST(self): |
| ... | ... | @@ -110,16 +70,18 @@ class Save: |
| 110 | 70 | for item in value: |
| 111 | 71 | output.write(item+"\n") |
| 112 | 72 | with open(os.path.join(output_dir,"report"),'w') as report: |
| 113 | - report.write("# User: %s\n# Strategy: %s\n# TP FP\n%d %d\n" % | |
| 73 | + report.write("# User: %s\n# Strategy: %s\n# TP FP S\n%d %d %d\n" % | |
| 114 | 74 | (user_id,strategy, |
| 115 | 75 | len(evaluations["good"])+len(evaluations["surprising"]), |
| 116 | - len(evaluations["poor"]))) | |
| 76 | + len(evaluations["poor"]),len(evaluations["surprising"]))) | |
| 77 | + if web_input.has_key("comments"): | |
| 78 | + report.write("# Comments\n%s\n" % web_input['comments'].encode("utf-8")) | |
| 117 | 79 | if web_input.has_key('continue_button'): |
| 118 | 80 | return Survey().POST() |
| 119 | 81 | elif web_input.has_key('finish_button'): |
| 120 | 82 | return render.thanks(user_id) |
| 121 | 83 | else: |
| 122 | - return render.survey_index() | |
| 84 | + return render.index_survey() | |
| 123 | 85 | |
| 124 | 86 | class Request: |
| 125 | 87 | def __init__(self,web_input,submissions_dir): |
| ... | ... | @@ -170,26 +132,30 @@ class Survey: |
| 170 | 132 | def POST(self): |
| 171 | 133 | web_input = web.input(pkgs_file={}) |
| 172 | 134 | logging.debug("Survey web_input %s" % str(web_input)) |
| 173 | - self.strategies = ["knn","knn_eset","cbd_eset","knnco"]#,"demo_colco"] | |
| 135 | + self.strategies = ["cb","cbh","cb_eset","cbh_eset", | |
| 136 | + "knn","knn_eset","knn_plus", | |
| 137 | + "knnco","knnco_eset"] | |
| 174 | 138 | request = Request(web_input,self.submissions_dir) |
| 175 | - if not request.validates(): | |
| 176 | - return render.error_survey() | |
| 139 | + if len(request.user.pkg_profile)<10: | |
| 140 | + return render.error(["Could not extract profile from uploaded file. It must have at least 10 applications."], | |
| 141 | + "/survey/","START") | |
| 177 | 142 | else: |
| 178 | - # Check the remaining strategies and select a new one | |
| 179 | - old_strategies = [dirs for root, dirs, files in | |
| 180 | - os.walk(os.path.join(self.submissions_dir, | |
| 181 | - request.user_id))] | |
| 182 | - if old_strategies: | |
| 183 | - strategies = [s for s in self.strategies if s not in old_strategies[0]] | |
| 184 | - logging.info("Already used strategies %s" % old_strategies[0]) | |
| 185 | - else: | |
| 186 | - strategies = self.strategies | |
| 187 | - if not strategies: | |
| 188 | - return render.thanks(request.user_id) | |
| 189 | - request.strategy = random.choice(strategies) | |
| 190 | - logging.info("Selected \'%s\' from %s" % (request.strategy,strategies)) | |
| 191 | - # Get recommendation | |
| 192 | - self.rec.set_strategy(request.strategy) | |
| 143 | + ## Check the remaining strategies and select a new one | |
| 144 | + #old_strategies = [dirs for root, dirs, files in | |
| 145 | + # os.walk(os.path.join(self.submissions_dir, | |
| 146 | + # request.user_id))] | |
| 147 | + #if old_strategies: | |
| 148 | + # strategies = [s for s in self.strategies if s not in old_strategies[0]] | |
| 149 | + # logging.info("Already used strategies %s" % old_strategies[0]) | |
| 150 | + #else: | |
| 151 | + # strategies = self.strategies | |
| 152 | + #if not strategies: | |
| 153 | + # return render.thanks(request.user_id) | |
| 154 | + #request.strategy = random.choice(strategies) | |
| 155 | + #logging.info("Selected \'%s\' from %s" % (request.strategy,strategies)) | |
| 156 | + ## Get recommendation | |
| 157 | + #self.rec.set_strategy(request.strategy) | |
| 158 | + request.strategy = self.set_rec_strategy(request.user_id) | |
| 193 | 159 | prediction = self.rec.get_recommendation(request.user,10).get_prediction() |
| 194 | 160 | logging.info("Prediction for user %s" % request.user_id) |
| 195 | 161 | logging.info(str(prediction)) |
| ... | ... | @@ -202,64 +168,77 @@ class Survey: |
| 202 | 168 | (request.user_id,request.strategy)) |
| 203 | 169 | recommendation = [result[0] for result in prediction] |
| 204 | 170 | |
| 205 | - # Check connection to DDE | |
| 206 | - try: | |
| 207 | - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| 208 | - s.connect((self.cfg.dde_server,self.cfg.dde_port)) | |
| 209 | - dde = 1 | |
| 210 | - s.close() | |
| 211 | - except: | |
| 212 | - dde =0 | |
| 213 | - logging.debug("Could not connect to dde") | |
| 214 | 171 | # Load packages details |
| 215 | 172 | pkgs_details = [] |
| 216 | 173 | for pkg_name in recommendation: |
| 217 | 174 | logging.info("Getting details of package %s" % pkg_name) |
| 218 | 175 | pkg = DebianPackage(pkg_name) |
| 219 | - if dde: | |
| 220 | - pkg.load_details_from_dde(self.cfg.dde_server,self.cfg.dde_port) | |
| 221 | - else: | |
| 222 | - pkg.load_details_from_apt() | |
| 176 | + pkg.load_details() | |
| 223 | 177 | pkgs_details.append(pkg) |
| 178 | + | |
| 224 | 179 | if pkgs_details: |
| 225 | 180 | logging.info("Rendering survey slide...") |
| 226 | 181 | return render.survey(pkgs_details, request) |
| 227 | 182 | else: |
| 228 | - return render.error_survey() | |
| 229 | - | |
| 230 | -def add_global_hook(): | |
| 231 | - g = web.storage({"counter": "1"}) | |
| 232 | - def _wrapper(handler): | |
| 233 | - web.ctx.globals = g | |
| 234 | - return handler() | |
| 235 | - return _wrapper | |
| 183 | + return render.error(["No recommendation produced for the uploaded file."],"/survey/","START") | |
| 184 | + | |
| 185 | + def set_rec_strategy(self,user_id): | |
| 186 | + # Check the remaining strategies and select a new one | |
| 187 | + old_strategies = [dirs for root, dirs, files in | |
| 188 | + os.walk(os.path.join(self.submissions_dir, | |
| 189 | + user_id))] | |
| 190 | + if old_strategies: | |
| 191 | + strategies = [s for s in self.strategies if s not in old_strategies[0]] | |
| 192 | + logging.info("Already used strategies %s" % old_strategies[0]) | |
| 193 | + else: | |
| 194 | + strategies = self.strategies | |
| 195 | + if not strategies: | |
| 196 | + return render.thanks(request.user_id) | |
| 197 | + selected_strategy = random.choice(strategies) | |
| 198 | + logging.info("Selected \'%s\' from %s" % (selected_strategy,strategies)) | |
| 199 | + k=50 | |
| 200 | + n=50 | |
| 201 | + if selected_strategy == "cb": | |
| 202 | + pass | |
| 203 | + if selected_strategy == "cbh": | |
| 204 | + pass | |
| 205 | + if selected_strategy == "cb_eset": | |
| 206 | + pass | |
| 207 | + if selected_strategy == "cbh_eset": | |
| 208 | + pass | |
| 209 | + if selected_strategy == "knn": | |
| 210 | + pass | |
| 211 | + if selected_strategy == "knn_eset": | |
| 212 | + pass | |
| 213 | + if selected_strategy == "knn_plus": | |
| 214 | + pass | |
| 215 | + if selected_strategy == "knnco": | |
| 216 | + pass | |
| 217 | + if selected_strategy == "knnco_eset": | |
| 218 | + pass | |
| 219 | + self.rec.set_strategy(selected_strategy,k,n) | |
| 220 | + return selected_strategy | |
| 221 | + | |
| 222 | +#def add_global_hook(): | |
| 223 | +# g = web.storage({"counter": "1"}) | |
| 224 | +# def _wrapper(handler): | |
| 225 | +# web.ctx.globals = g | |
| 226 | +# return handler() | |
| 227 | +# return _wrapper | |
| 236 | 228 | |
| 237 | 229 | render = web.template.render('/var/www/AppRecommender/src/web/templates/', base='layout', globals={'hasattr':hasattr}) |
| 238 | -render_plain = web.template.render('/var/www/AppRecommender/src/web/templates/') | |
| 239 | 230 | |
| 240 | -urls = ('/survey', 'Index', | |
| 241 | - '/survey/', 'Index', | |
| 242 | - '/survey/survey', 'Survey', | |
| 243 | - '/survey/apprec', 'Survey', | |
| 244 | - '/survey/thanks', 'Thanks', | |
| 245 | - '/survey/save', 'Save', | |
| 246 | - '/survey/thanks', 'Thanks', | |
| 247 | - '/survey/about', 'About', | |
| 248 | - '/survey/package/(.*)', 'Package', | |
| 249 | - '/', 'Index', | |
| 250 | - '/index', 'Index' | |
| 251 | - #'/', 'Fake', | |
| 252 | - #'/index', 'Fake' | |
| 231 | +urls = ('', 'Index', | |
| 232 | + '/', 'Index', | |
| 233 | + '/survey', 'Survey', | |
| 234 | + '/apprec', 'Survey', | |
| 235 | + '/thanks', 'Thanks', | |
| 236 | + '/save', 'Save', | |
| 237 | + '/about', 'About', | |
| 253 | 238 | ) |
| 254 | 239 | |
| 255 | 240 | web.webapi.internalerror = web.debugerror |
| 256 | 241 | |
| 257 | -#if __name__ == "__main__": | |
| 258 | 242 | cfg = Config() |
| 259 | -# apprec = web.application(urls, globals()) | |
| 260 | -# application = web.application(urls, globals()).wsgifunc() | |
| 261 | 243 | app = web.application(urls, globals(), autoreload=False) |
| 262 | 244 | application = app.wsgifunc() |
| 263 | - | |
| 264 | -# apprec.add_processor(add_global_hook()) | |
| 265 | -# apprec.run() | ... | ... |