# -*- coding: utf-8 -*- """ ページの流れをもとにしたレコメンドロジック """ import datetime as dt import sys sys.path.append("/var/local/mode2") from .base import ModelBase from lib.WebRecommend.utils.dataAccess import dataAccess class userDevice(ModelBase): def __init__(self, domain=False): self.recommend_category = 'user_device' self.domain = domain def getDeviceSummary(self, **kwargs): res = [] da = dataAccess(self.domain) userdevice = da.getUserDevice() datadict = {} if 'results' in userdevice: return res else: print(userdevice) datadict = {'desktop': 0, 'smartphone': 0, 'tablet': 0, 'feature+phone': 0, 'other': 0} for k, v in userdevice.items(): for j in v: if len(j) != 0: if j['nb_actions'] > 0: if j.get('segment', None): print(j['segment'][12:]) devicetype = j['segment'][12:] if devicetype in datadict.keys(): datadict[devicetype] += int( j['nb_actions']) else: datadict['other'] += int(j['nb_actions']) else: datadict['other'] += int(j['nb_actions']) # calc total nums total_nums = 0 for ki, vi in datadict.items(): total_nums += vi # calc percentage among devices res = dict(desktop=float(datadict['desktop'])/float(total_nums), smartphone=float( datadict['smartphone']) / float(total_nums), tablet=float(datadict['tablet']) / float(total_nums), feature=float( datadict['feature+phone']) / float(total_nums), other=float(datadict['other']) / float(total_nums)) print(res) return res def getRecommend(self): res = [] data = self.getDeviceSummary() if data['smartphone'] > 0.4: text = "スマートフォンからのアクセスが4割以上と比較的多いようです。"\ "今後さらにスマホからのアクセスが増える可能性があります。"\ "スマホ向けに最適化したページの作成を推奨します。" res.append(dict(date=str(dt.date.today()), text=text)) elif data['desktop'] > 0.8: text = "PCからのアクセスが8割を超えていますが、"\ "スマートフォンからのアクセスが増加しているようであれば"\ "スマホ向けサイトの作成を推奨します。" res.append(dict(date=str(dt.date.today()), text=text)) return res