3、数据接口处理

问题描述:

为数据可视化化提供数据接口支持
包括页面访问情况、用户注册情况

核心源代码:

package demo;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

import demo.model.IpVisitDetailesModel;
import demo.model.PageViewsModel;
import demo.model.PageVistiDetailsModel;
import demo.model.RegisterNumberModel;

public class APIController extends Controller {
    public void index(){
        renderText("api的index方法正常");
    }
    
    /**
     * 获取ip地区分布状况
     */
    public void getIpDistribution(){
        String sql="select region as name,count(*) as value from IpVisitDetailes group by region";
        List datas= IpVisitDetailesModel.DAO.find(sql);
        
        String result="";
        for(int i=0;i datas=PageViewsModel.DAO.find(sql);
        sql="select sum(number) as total from PageViews";
        PageViewsModel model=PageViewsModel.DAO.findFirst(sql);
        BigDecimal total=model.getBigDecimal("total");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("datas", datas);
        jsonObject.put("total", total);
        renderJson(jsonObject);
    }
    
    /**
     * 获取注册用户数量-以小时为单位
     */
    public void getRegisterNumber(){
        String sql="select * from RegisterNumber";
        List datas=RegisterNumberModel.DAO.find(sql);
        sql="select sum(number) as total from RegisterNumber";
        RegisterNumberModel model=RegisterNumberModel.DAO.findFirst(sql);
        BigDecimal total=model.getBigDecimal("total");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("datas", datas);
        jsonObject.put("total", total);
        renderJson(jsonObject);
    }
    
    /**
     * 获取页面访问详情
     */
    public void getPageVistiDetails(){
        String sql="select * from PageVistiDetails";
        List datas=PageVistiDetailsModel.DAO.find(sql);
        renderJson(datas);
    }
    
    /**
     * 获取跳出率
     */
    public void getJumpPencent(){
        String sql="select count(*) as total from IpVisitDetailes";
        Long total=IpVisitDetailesModel.DAO.findFirst(sql).getLong("total");
        
        sql="select count(*) as total from IpVisitDetailes where number=1";
        Long jumpNumber=IpVisitDetailesModel.DAO.findFirst(sql).getLong("total");
        
        double jumpPencent=(double)jumpNumber/(double)total;
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("total", total);
        jsonObject.put("jumpPencent", jumpPencent);
        jsonObject.put("jumpNumber", jumpNumber);
        renderJson(jsonObject);
    }
}

接口数据样例:

#http://localhost:9999/api/getPageView
{
    "total": 169857,
    "datas": [
        { "number": 24425, "time": "2013053019" },
        { "number": 25420, "time": "2013053020" },
        { "number": 28361, "time": "2013053021" },
        { "number": 30799, "time": "2013053022" },
        { "number": 7901, "time": "2013053017" },
        { "number": 22839, "time": "2013053018" },
        { "number": 30112, "time": "2013053023" }
    ]
}

#http://localhost:9999/api/getRegisterNumber
{
    "total": 28,
    "datas": [
        { "number": 1, "time": "2013053017" },
        { "number": 5, "time": "2013053018" },
        { "number": 1, "time": "2013053021" },
        { "number": 3, "time": "2013053022" },
        { "number": 8, "time": "2013053019" },
        { "number": 5, "time": "2013053020" },
        { "number": 5, "time": "2013053023" }
    ]
}

#http://localhost:9999/api/getPageVistiDetails
[
    {"number":26666,"page":"home.php"},
    {"number":2184,"page":"like.php"},
    {"number":4719,"page":"member.php"},
    {"number":12177,"page":"api.php"},
    {"number":760,"page":"connect.php"},
    {"number":50754,"page":"forum"},
    {"number":2446,"page":"misc.php"},
    {"number":865,"page":"my.php"},
    {"number":550,"page":"search.php"},
    {"number":2184,"page":"api/connect/like.php"},
    {"number":66552,"page":"other"}
]

#http://localhost:9999/api/getIpDistribution
{
    "total": 10411,
    "datas": [
        { "name": null, "value": 8970 },
        { "name": "上海", "value": 84 },
        { "name": "云南", "value": 2 },
        { "name": "内蒙古", "value": 9 },
        { "name": "北京", "value": 179 },
        { "name": "台湾", "value": 25 },
        { "name": "吉林", "value": 17 },
        { "name": "四川", "value": 57 },
        { "name": "天津", "value": 11 },
        { "name": "安徽", "value": 24 },
        { "name": "山东", "value": 39 },
        { "name": "山西", "value": 12 },
        { "name": "广东", "value": 339 },
        { "name": "广西", "value": 18 },
        { "name": "新疆", "value": 9 },
        { "name": "江苏", "value": 103 },
        { "name": "江西", "value": 14 },
        { "name": "河北", "value": 44 },
        { "name": "河南", "value": 57 },
        { "name": "浙江", "value": 52 },
        { "name": "海南", "value": 4 },
        { "name": "湖北", "value": 55 },
        { "name": "湖南", "value": 31 },
        { "name": "甘肃", "value": 7 },
        { "name": "福建", "value": 110 },
        { "name": "贵州", "value": 3 },
        { "name": "辽宁", "value": 15 },
        { "name": "重庆", "value": 50 },
        { "name": "陕西", "value": 38 },
        { "name": "青海", "value": 1 },
        { "name": "香港", "value": 10 },
        { "name": "黑龙江", "value": 22 }
    ]
}

#http://localhost:9999/api/getJumpPencent
{ "jumpNumber": 3749, "jumpPencent": 0.3600998943425223, "total": 10411 }

你可能感兴趣的:(3、数据接口处理)