DauPredict.java 8.67 KB
Newer Older
zhangxiaoyan committed
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
package com.cy.report.action.model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.cy.report.action.InitListener;
import com.cy.report.database.MysqlDBManager;
import com.cy.report.utils.PhoenixUtil;

public class DauPredict {

	/** 预测时长(天), 待输入,default 60 */
	public static int duration = 60;
	public static int fixRetDay = 7;
	public static final int normalType = 0;
	public static final int adType = 1;
	public static final int max = 0;
	public static final int min = 1;

	private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	/** 开始日期 */
	private String startDate = dateFormat.format(new Date());
	
	/** 稳定日dau */
//	private int normalDau = 0;
	/** 日常安装数 */
	private int normalInstall = 0;
	/** 推广的展开留存率 d1-dn */
	private double[] normalRetentionList = new double[duration];
	/** 导量 安装数*/
	private int adInstall = 0;
	/** 游戏类型 */
	private int gameType;
	/** 推广的展开留存率 d1-dn */
	private double[] adRetentionList = new double[duration];
	/** 付费率 */
	private double payRate = 0.0d;
	/** 付费率变化 */
	private double deltaPayRate = 0.0d;
	/** ARPPU */
	private double arppu = 0.0d;
	/** ARPPU变化 */
	private double deltaArppu = 0.0d;

	/** 处理留存 */
	private Retention retention = null;
	private String appkey;
	private int userid;

	public DauPredict() {
	}

	public DauPredict(String startDate, int normalInstall, int adInstall, int gameType,
			double payRate, double arppu, String appkey, int userid) {
		this.startDate = startDate;
		this.normalInstall = normalInstall;
		this.adInstall = adInstall;
		this.gameType = gameType;
		this.payRate = payRate;
		this.arppu = arppu;
		this.retention = new Retention(gameType);
		this.appkey = appkey;
		this.userid = userid;
//		setNormalDau();
		
	}

	// 设置稳定dau,预测时间前一天dau;若未上线,没数据,则设为0
//	public void setNormalDau() {
//		this.normalDau = 0;
//		try {
//			Calendar c = Calendar.getInstance();
//			c.setTime(dateFormat.parse(startDate));
//			c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
//			String preDate = dateFormat.format(c.getTime());
//
//			String sql = "select count(userid) dau from cache_dau"
//					+ " where appid='" + appkey + "' and ds = '" + preDate
//					+ "'";
//
//			ResultSet rs = PhoenixUtil.executeQuery(sql);
//			int preDau = 0;
//			while (rs.next()) {
//				preDau = rs.getInt("dau");
//			}
//			this.normalDau = preDau;
//		} catch (SQLException e) {
//			e.printStackTrace();
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//	}

	/** 第k天的dau */
//	public int dauDay(int k) {
//		int totalDau = 0;
//		// 导量dau
//		double installDau = 0.0d;
//		// k-1天累计导量dau
//		for (int i = 0; i < k - 1; i++) {
//			installDau = installDau + adInstall * retentionList[i];
//		}
//		installDau = adInstall + installDau;
//
//		totalDau = normalDau + new Double(installDau).intValue();
//		return totalDau;
//	}
	public int dauDay(int day){
		return normalDau(day) + adDau(day); 
	}
	
	public int normalDau(int day){
		return calcuteDau(day, normalInstall, normalRetentionList);
	}
	
	public int adDau(int day){
		return calcuteDau(day, adInstall, adRetentionList);
	}
	
	private int calcuteDau(int day, int install, double retentionList[]){
		double retDau = 0.0d;
		// 前k-1天累计dau
		for (int i = 0; i < day - 1; i++) {
			retDau = retDau + install * retentionList[i];
		}
		double dayDau = install + retDau;
		return new Double(dayDau).intValue();
	}
	
	/** 第k天的收入 */
	public int paymentDay(int k) {
		double payment = dauDay(k) * (payRate + deltaPayRate)
				* (arppu + deltaArppu);
		return new Double(payment).intValue();
	}

	/** 计算日期list */
	public String[] getDateList() {
		String[] dateList = new String[duration];
		Calendar c = Calendar.getInstance();
		Date start;
		try {
			start = dateFormat.parse(startDate);
			for (int i = 0; i < duration; i++) {
				c.setTime(start);
				c.set(Calendar.DATE, c.get(Calendar.DATE) + i + 1);
				dateList[i] = dateFormat.format(c.getTime());
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return dateList;
	}

	/** 计算预测daulist */
	public int[] getDauList() {
		int[] dauList = new int[duration];
		for (int i = 0; i < duration; i++) {
			dauList[i] = dauDay(i + 1);
		}
		return dauList;
	}

	public int[] getMaxDau() {
		setRetention(max);
		return getDauList();
	}

	public int[] getMinDau() {
		setRetention(min);
		return getDauList();
	}

	/** 计算每天收入list */
	public int[] getDayPaymentList() {
		int[] dayPaymentList = new int[duration];
		for (int i = 0; i < duration; i++) {
			dayPaymentList[i] = paymentDay(i + 1);
		}
		return dayPaymentList;
	}

	public int[] getMaxPayment() {
		setRetention(max);
		return getDayPaymentList();
	}

	public int[] getMinPayment() {
		setRetention(min);
		return getDayPaymentList();
	}

	/** 计算累计收入 list */
	public int[] getTotalPayList(int[] dayPaymentList) {
		int[] totalPayment = new int[duration];
		for (int i = 0; i < duration; i++) {
			int sum = 0;
			for (int j = 0; j <= i; j++) {
				sum = sum + dayPaymentList[j];
			}
			totalPayment[i] = sum;
		}
		return totalPayment;
	}

	public int[] getMaxPaytotal() {
		return getTotalPayList(getMaxPayment());
	}

	public int[] getMinPaytotal() {
		return getTotalPayList(getMinPayment());
	}

	/** 初始化留存列表 */
	public void setRetention(int maxmin) {
		if(gameType != Retention.TYPE_CUSTOMER){
			normalRetentionList = retention.getExpandedDefault(normalType, maxmin);
			adRetentionList = retention.getExpandedDefault(adType, maxmin);
		}else{
			normalRetentionList = retention.getExpandedCustom(userid, appkey, normalType, maxmin);
			adRetentionList = retention.getExpandedCustom(userid, appkey, adType, maxmin);
		}
	}

	/** 查询实际dau */
	public Integer[] getActualDau() {
		Integer[] actualDau = new Integer[duration];
		String[] dateList = getDateList();
		String endDate = dateList[duration - 1];
		Map<String, Integer> dsDauMap = new HashMap<String, Integer>();

		String sql = "select ds, count(userid) dau from cache_dau"
				+ " where appid='" + appkey + "' and ds >= '" + startDate
				+ "' and ds <= '" + endDate + "' group by ds";
		try {
			ResultSet rs = PhoenixUtil.executeQuery(sql);
			while (rs.next()) {
				String ds = rs.getString("ds");
				Integer dau = rs.getInt("dau");
				dsDauMap.put(ds, dau);
			}
			for (int i = 0; i < duration; i++) {
				actualDau[i] = dsDauMap.get(dateList[i]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return actualDau;
	}

	/** 查询实际收入 */
	public Double[] getActualPayment() {
		Double[] actualPayment = new Double[duration];
		String[] dateList = getDateList();
		String endDate = dateList[duration - 1];
		String sql = "select ds, sum(amount) payment from payment_base "
				+ "where appid='"+ appkey +"' and ds >= '" + startDate
				+ "' and ds <= '" + endDate + "' group by ds";
		Map<String, Double> dsPaymentMap = new HashMap<String, Double>();
		ResultSet rs = null;
		try {
			if (InitListener.reportUsingPath.equals("report_mysql")) {
				Connection conn = MysqlDBManager.getBaseConnection(appkey);
				rs = MysqlDBManager.query_base(conn, sql);
			} else {
				rs = PhoenixUtil.executeQuery(sql);
			}
//			ResultSet rs = MysqlDBManager.query_base(conn, sql);
//			ResultSet rs = PhoenixUtil.executeQuery(sql);
			while (rs.next()) {
				String ds = rs.getString("ds");
				Double payment = rs.getDouble("payment");
				dsPaymentMap.put(ds, payment);
			}

			for (int i = 0; i < duration; i++) {
				actualPayment[i] = dsPaymentMap.get(dateList[i]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return actualPayment;
	}

	/** 查询实际累计收入 */
	public Double[] getActualPaytotal() {
		Double[] actualPayList = getActualPayment();
		Double[] actualTotalPayList = new Double[duration];
		for (int i = 0; i < duration; i++) {
			Double actualPay = actualPayList[i];
			if (actualPay != null) {
				Double sum = 0.0d;
				for (int j = 0; j <= i; j++) {
					if (actualPayList[j] != null) {
						sum = sum + actualPayList[j];
					}
				}
				actualTotalPayList[i] = sum;
			} else {
				actualTotalPayList[i] = null;
				break;
			}
		}
		return actualTotalPayList;
	}

	public static void main(String[] args) {
		DauPredict dauPredict = new DauPredict("2013-09-30", 1000, 10000,
				Retention.TYPE_RELEX, 0.05, 100, "100101", 1);
		////System.out.println(dauPredict.getActualDau());
	}

}