diff --git a/chapter2/init.sql b/chapter2/init.sql index 773ffea..ee06b37 100644 --- a/chapter2/init.sql +++ b/chapter2/init.sql @@ -86,3 +86,4 @@ create table datas ( PRIMARY KEY (`id`) ) comment = '通用配置表'; insert into datas (`key`, data) values ('from_type', '{}'); +insert into datas (`key`, data) values ('last_month_sell', '{}'); diff --git a/chapter2/mysite/myapp/static/scripts/trend.js b/chapter2/mysite/myapp/static/scripts/trend.js index 1dd5fe4..2c75b4d 100755 --- a/chapter2/mysite/myapp/static/scripts/trend.js +++ b/chapter2/mysite/myapp/static/scripts/trend.js @@ -145,7 +145,6 @@ $(function(){ ] }) - //传染病发病趋势 if (document.getElementById('lineChart1')) { var lineChart1 = echarts.init(document.getElementById('lineChart1')); if(lineChart1){ @@ -356,7 +355,7 @@ $(function(){ var lineChart2 = echarts.init(document.getElementById('lineChart2')); lineChart2.setOption({ title: { - text: '购买趋势', + text: '前5月购买趋势', textStyle:{ fontSize:16, color:'#32cd32' @@ -424,7 +423,7 @@ $(function(){ ], series : [ { - name:'购买趋势', + name:'前5月购买趋势', type:'line', smooth:true, itemStyle: {normal: {areaStyle: {type: 'default'}}}, diff --git a/chapter2/mysite/myapp/templates/myapp/result.html b/chapter2/mysite/myapp/templates/myapp/result.html index 344128c..9d7c9dc 100644 --- a/chapter2/mysite/myapp/templates/myapp/result.html +++ b/chapter2/mysite/myapp/templates/myapp/result.html @@ -69,7 +69,7 @@
-
TOP10商品排行
+
商品销售排行
diff --git a/chapter3/spark.py b/chapter3/spark.py index bc1c870..660953f 100644 --- a/chapter3/spark.py +++ b/chapter3/spark.py @@ -38,6 +38,26 @@ def getYesterday(day): yesterday=today-oneday return yesterday.strftime('%Y-%m-%d') +def get_last_month(num): + date = datetime.datetime.now() + year = date.year + month = date.month + month = month - num + if month<=0: + month = 12 - (num-1) + year -= 1 + return "%d-%d" % (year, month) + +def collect_last_month_sells(spark): + data = {} + for i in range(5): + month = get_last_month(i) + df = spark.sql("select count(*) as N from jd_comment where comment_time like '"+month+"%' ") + jd_comment_count = df.rdd.collect()[0]["N"] + data[month] = jd_comment_count + mysql_execute("update datas set data = '{}' where `key` = 'last_month_sell'".format( json.dumps(data,ensure_ascii=False) )) + + def collect_crawl_info(spark): df = spark.sql("select count(*) as N from jd_comment") @@ -116,6 +136,7 @@ if __name__ == "__main__": get_last_day_count(spark) collect_top10_sells(spark) collect_from_type(spark) + collect_last_month_sells(spark) count = 1 time.sleep(10)