package org.sas.benchmark.req.spo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class PythonRunner {

	static String prefix = "/Users/" + System.getProperty("user.name") + "/research/monitor/";
	static String test_data = "       [\"p0-p0\",650,616,674,606,578,792,618,674,632,652,704,730,622,692,652,688,646,628,682,618,652,670,628,728,598,622,624,628,604,630,670,618,636,598,614,606,760,676,604,600,620,658,630,606,624,664,602,656,624,688,692,614,640,620,654,788,582,612,700,634,716,652,698,630,626,606,778,598,674,906,686,636,736,846,650,624,806,718,610,634,616,640,606,628,616,904,640,668,620,612,596,648,664,610,694,624,640,620,728,680],\n" + 
			"[\"p0-p1\",642,590,646,636,612,698,650,652,754,598,690,598,658,594,618,640,626,610,660,638,640,600,610,598,716,766,634,564,600,600,638,618,766,630,616,604,744,630,592,624,638,684,892,600,642,586,632,700,614,932,620,654,624,626,642,674,594,596,784,666,608,606,688,578,598,694,630,594,654,588,608,652,620,592,670,652,590,850,586,680,626,626,638,660,646,854,590,610,792,658,576,696,678,650,622,662,642,774,658,668],\n" + 
			"[\"p1-p0\",846,842,744,1554,746,650,686,920,1050,892,1870,750,696,604,848,716,936,782,832,824,788,1132,722,664,866,796,1138,1242,842,744,818,858,950,864,688,828,884,748,840,820,780,822,746,888,960,694,696,848,700,1134,762,972,898,708,734,1834,678,1176,784,732,726,866,896,676,678,1726,910,666,1024,1016,1020,798,1080,740,702,828,730,722,942,782,708,778,800,1004,968,702,708,1310,920,644,1510,754,1146,1106,1790,1038,692,798,926,604],\n" + 
			"[\"p0-p2\",658,598,644,580,596,752,606,626,622,858,660,584,692,694,740,594,784,612,680,586,604,576,654,630,630,604,666,574,666,640,588,570,638,810,680,1442,624,622,692,590,586,608,586,686,580,566,590,620,572,612,600,650,614,650,650,654,626,602,716,594,596,620,546,580,622,588,1374,590,624,592,636,632,604,576,568,604,576,592,634,644,582,594,606,638,624,670,672,610,640,570,602,722,620,604,630,648,630,630,596,634],\n" + 
			"[\"p2-p0\",1714,914,812,828,816,944,846,854,780,700,736,796,858,950,1040,1066,782,1066,770,676,1316,940,886,710,1220,794,920,668,814,1308,772,844,748,1548,808,816,726,798,810,848,912,1226,978,786,814,1192,1042,790,912,786,760,842,1134,674,970,746,1018,782,816,1060,1000,718,1112,750,936,674,1504,1140,996,870,1200,1532,822,722,814,804,788,712,1272,800,1268,1794,870,1228,752,1028,680,944,2438,902,868,680,932,960,898,758,864,658,1148,708],\n" + 
			"[\"p0-p3\",778,590,664,776,636,686,734,662,722,844,636,616,638,640,626,636,650,694,624,658,662,628,612,638,628,596,584,588,604,636,580,612,666,668,662,642,616,652,590,666,634,634,652,686,626,640,628,674,588,676,684,708,618,622,660,606,644,622,676,694,698,666,724,698,676,648,652,810,618,656,680,770,658,630,616,652,628,744,658,704,696,650,624,724,570,662,604,654,650,624,726,614,650,722,616,620,698,582,610,662],\n" + 
			"[\"p3-p0\",618,704,576,620,650,636,662,590,604,636,714,664,666,646,612,632,640,600,666,708,676,620,576,782,624,594,628,578,612,628,630,630,576,588,816,620,610,654,624,646,608,664,630,652,592,640,612,702,700,602,638,602,600,600,632,590,596,696,606,680,636,776,594,582,602,840,662,596,664,676,658,626,564,636,596,678,622,660,672,608,588,632,626,606,718,684,580,628,592,622,670,716,608,676,606,596,662,618,622,664],\n" + 
			"[\"p1-p1\",572,564,614,574,606,584,578,578,606,578,620,554,584,590,570,658,610,608,630,572,610,564,610,588,570,616,614,586,604,594,598,594,608,660,560,596,670,562,560,594,664,610,602,608,626,692,572,614,586,596,622,604,630,652,618,626,668,580,614,628,596,584,638,574,600,640,608,646,586,616,638,628,652,598,638,616,604,658,618,596,688,600,600,592,592,600,656,612,604,632,618,612,580,612,558,572,616,594,582,612],\n" + 
			"[\"p2-p2\",558,602,594,594,584,562,624,578,580,808,574,578,586,574,592,618,608,572,578,642,608,630,582,562,598,570,630,614,610,648,620,580,576,640,618,592,610,586,606,588,592,624,568,600,710,576,574,612,640,576,644,578,598,584,596,616,578,622,580,614,624,640,632,656,586,604,564,572,578,614,682,608,662,596,576,658,678,630,630,608,586,624,640,584,596,594,612,620,638,596,598,582,580,568,614,608,596,604,592,574],\n" + 
			"[\"p3-p3\",618,636,574,606,694,670,620,644,626,632,638,614,586,740,612,646,730,674,644,684,654,598,624,606,602,590,604,670,718,654,624,606,638,722,594,666,650,694,598,650,646,626,644,638,732,680,666,650,650,672,678,710,666,618,652,624,678,684,702,606,694,622,696,1040,688,636,654,668,696,650,666,912,640,686,682,644,616,668,598,674,696,646,662,604,658,626,688,710,582,654,702,680,618,696,600,684,690,652,622,730],\n" + 
			"[\"p1-p2\",620,582,624,588,602,574,636,568,604,598,580,584,628,584,580,602,600,610,574,588,656,636,778,618,594,576,580,624,626,592,620,574,674,638,614,668,614,560,570,588,610,592,588,586,620,614,638,584,610,666,564,598,596,576,584,572,614,590,618,566,624,620,580,620,676,566,602,582,582,636,580,590,566,614,678,564,598,582,588,608,606,654,596,602,598,646,592,586,606,636,584,574,612,620,728,598,640,608,702,582],\n" + 
			"[\"p2-p1\",616,566,592,606,588,662,586,636,602,570,630,628,662,570,590,592,586,590,604,614,568,658,574,588,580,578,588,612,568,580,568,630,618,668,580,584,580,584,588,574,610,590,578,642,584,610,564,608,572,630,598,642,638,642,586,592,574,594,566,608,592,566,586,644,658,646,568,568,570,606,562,572,562,588,592,570,634,630,580,602,620,604,594,584,584,608,630,578,582,586,608,590,580,642,588,584,618,606,584,550],\n" + 
			"[\"p1-p3\",928,846,764,792,778,978,830,2430,924,692,1642,1532,702,724,792,720,902,930,984,732,1230,752,728,1034,754,834,1110,680,1166,642,878,720,958,1018,816,710,1188,852,796,750,1566,880,1142,652,784,774,958,810,662,1666,924,1450,702,828,702,830,934,776,840,702,612,822,1016,668,902,1092,774,694,656,894,1168,742,1922,652,784,1604,1246,794,716,794,724,740,840,764,754,918,690,770,754,988,1434,950,742,812,788,794,774,1098,1022,798],\n" + 
			"[\"p3-p1\",662,640,644,596,616,556,578,620,630,626,614,618,576,674,614,622,652,574,612,624,616,630,616,608,624,722,678,588,722,714,660,610,634,630,592,680,804,656,620,650,646,638,570,598,602,782,636,612,602,592,664,772,1212,636,576,658,590,656,600,688,606,640,630,592,644,594,582,606,608,708,654,640,642,1084,636,720,618,664,700,600,582,592,608,618,586,654,724,576,606,612,618,646,724,624,668,656,814,586,674,666],\n" + 
			"[\"p2-p3\",1772,712,770,720,760,686,974,832,918,1004,706,968,830,734,740,984,682,836,1042,868,862,1158,674,882,878,702,960,850,1220,788,842,598,740,808,672,786,1342,980,792,1208,1072,746,758,766,606,820,926,884,762,1012,2042,632,748,838,674,872,804,1036,830,830,686,814,756,914,932,1654,1420,1332,1514,682,644,800,954,652,1480,738,948,882,638,946,758,936,1962,770,1056,1328,988,780,716,1074,1612,692,940,1024,1096,1028,1162,864,666,676],\n" + 
			"[\"p3-p2\",634,594,650,694,666,676,596,574,624,594,650,592,652,678,652,646,606,592,626,720,596,616,628,620,656,658,614,614,676,754,808,692,642,582,642,594,602,624,616,584,630,632,594,670,610,636,606,1192,806,634,588,604,570,738,606,546,660,606,652,678,572,646,610,628,634,584,678,712,624,622,730,638,604,662,668,620,614,590,620,638,618,682,614,620,604,954,636,684,1170,688,596,608,656,710,670,710,616,624,682,556]\n" + 
			"";

	public static void main(String[] arg) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		/*run(map);*/

		
		
		start(map, test_data);
		for (String s : map.keySet()) {
			System.out.print(s + "="+map.get(s) + "\n");
		}
	}
	
	public static void start(Map<String, Integer> map, String data) {
		String main = "";
		try {
			BufferedReader reader = new BufferedReader(new FileReader(prefix + "stats2.py"));
			String line = null;


			while ((line = reader.readLine()) != null) {
				main += line + "\n";
			}
			reader.close();
			
			
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		main = main.replace("[java_filling_data]", data +"\n");
		
		//System.out.print(main);
		
		try {
			File f = new File(prefix + "stats2-copy.py");
			if(f.exists()) {
				f.delete();
			}
		
			
			
			BufferedWriter bw = new BufferedWriter(new FileWriter(prefix + "stats2-copy.py", false));		
			
			bw.write(main);
			bw.flush();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		run(map);
	}

	public static void run(Map<String, Integer> map) {
		ProcessBuilder processBuilder = new ProcessBuilder();
		// Windows
		processBuilder.command("python", prefix + "stats2-copy.py", "--demo");
		processBuilder.redirectErrorStream(true);

		try {

			Process process = processBuilder.start();
			int exitCode = process.waitFor();

			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

			String line;
			while ((line = reader.readLine()) != null) {

				// System.out.print(line + "\n");
				if (!line.trim().equals("")) {
					String[] s = line.split(",");
					if (!map.containsKey(s[1].trim())) {
						map.put(s[1].trim(), 0);
					}

					map.put(s[1].trim(), map.get(s[1].trim()) + Integer.parseInt(s[0].trim()));
				}
			}

			System.out.println("\nExited with error code : " + exitCode);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	



}
