Parcourir la Source

Merge branch 'master' of http://121.42.53.174:3000/jiapeng/bridge

XMTT 6 ans auparavant
Parent
commit
8b491c94e4

+ 161 - 8
src/main/java/com/ekexiu/project/bridge/collect/CollectService.java

@ -1,23 +1,67 @@
1 1
package com.ekexiu.project.bridge.collect;
2 2

3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.io.InputStreamReader;
10
import java.io.OutputStreamWriter;
11
import java.io.Writer;
12
import java.sql.Connection;
13
import java.sql.PreparedStatement;
14
import java.sql.ResultSet;
15
import java.sql.SQLException;
16
import java.util.LinkedList;
17
import java.util.List;
18
import java.util.Map;
19

3 20
import org.jfw.apt.annotation.Autowrie;
4 21
import org.jfw.apt.web.annotation.Path;
5 22
import org.jfw.apt.web.annotation.operate.Get;
6 23
import org.jfw.apt.web.annotation.param.JdbcConn;
24
import org.jfw.util.ConstData;
25
import org.jfw.util.DateUtil;
26
import org.jfw.util.ListUtil;
27
import org.jfw.util.io.IoUtil;
28
import org.jfw.util.jdbc.JdbcUtil;
7 29

8
import java.sql.Connection;
9
import java.sql.SQLException;
10
import java.util.List;
11
import java.util.Map;
30
import com.ekexiu.project.bridge.resource.dao.BridgeServerDao;
31
import com.ekexiu.project.bridge.resource.po.BridgeServer;
12 32

13 33

14 34
@Path("/collect")
15 35
public class CollectService {
16 36

17 37
	private Map<String, List<WaveDataItem>> waveCache;
18

38
	private File backupPath;
19 39
	@Autowrie
20 40
	private CollectDao collectDao;
41
	private BridgeServerDao bridgeServerDao;
42

43
	public CollectService(){
44
		try{
45
			this.backupPath = new File("/bridge/wave_backup");
46
		}catch(Exception e){}
47
		
48
	}
49
	
50
	public BridgeServerDao getBridgeServerDao() {
51
		return bridgeServerDao;
52
	}
53

54
	public void setBridgeServerDao(BridgeServerDao bridgeServerDao) {
55
		this.bridgeServerDao = bridgeServerDao;
56
	}
57

58
	public File getBackupPath() {
59
		return backupPath;
60
	}
61

62
	public void setBackupPath(File backupPath) {
63
		this.backupPath = backupPath;
64
	}
21 65

22 66
	public CollectDao getCollectDao() {
23 67
		return collectDao;
@ -47,8 +91,42 @@ public class CollectService {
47 91
	
48 92
	@Get
49 93
	@Path("/wave/server/day")
50
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String[] seq,String day)throws SQLException{
51
		return this.collectDao.query(con, seq, day+"000000", day+"235959");
94
	public List<WaveDataItem> waveByDay(@JdbcConn Connection con,String[] seq,String day)throws SQLException, IOException{
95
		List<WaveDataItem> ret = new LinkedList<WaveDataItem>();
96
		for(String s:seq){
97
			File path = new File(this.backupPath,s);
98
			if(path.exists() && path.isDirectory()){
99
				File file = new File(path,day+".wave");
100
				if(file.exists() && file.isFile()){
101
					InputStream in = null;
102
					try{
103
						in = new FileInputStream(file);
104
						BufferedReader reader = new BufferedReader(new InputStreamReader(in, ConstData.UTF8));
105
						String line;
106
						while((line = reader.readLine())!=null){
107
							List<String> ls = ListUtil.splitTrimExcludeEmpty(line,',');
108
							if(ls.size()!=5){
109
								throw new IOException("invalid context:line===>"+line);
110
							}
111
							WaveDataItem wdi = new WaveDataItem();
112
							wdi.setSeq(s);
113
							wdi.setHvalue(Integer.parseInt(ls.get(0)));
114
							wdi.setLvalue(Integer.parseInt(ls.get(1)));
115
							wdi.setCid(Integer.parseInt(ls.get(2)));
116
							wdi.setDid(Integer.parseInt(ls.get(3)));
117
							wdi.setCtime(ls.get(4));
118
							ret.add(wdi);
119
						}
120
						reader.close();
121
					}finally{
122
						if(in != null){
123
							IoUtil.close(in);
124
						}
125
					}
126
				}
127
			}
128
		}
129
		return ret;
52 130
	}
53 131
	
54 132
	@Get
@ -68,5 +146,80 @@ public class CollectService {
68 146
	}
69 147
	
70 148

71
	
149
	@Get
150
	@Path("/wave/backup")
151
	public void backup(@JdbcConn(true) Connection con)throws SQLException{
152
		List<BridgeServer> srvs = this.bridgeServerDao.queryAll(con);
153
		long time = System.currentTimeMillis();
154
		String yd = DateUtil.formatDate(time-1000*60*60*24);
155
		String yyd = DateUtil.formatDate(time-1000*60*60*24*8);
156
		
157
		List<String> error = new LinkedList<String>();
158
		
159
		for(BridgeServer srv:srvs){
160
			File path = new File(this.backupPath,srv.getSeq());
161
			if(!path.exists()){
162
				if(!path.mkdir()){
163
					error.add("mkdir:"+path.getAbsolutePath());
164
					continue;
165
				}
166
			}
167
			if(path.isFile()){
168
				if(!path.delete()){
169
					JdbcUtil.rollback(con);
170
					error.add("delete file:"+path.getAbsolutePath());
171
					continue;
172
				}
173
				if(!path.mkdir()){
174
					error.add("mkdir:"+path.getAbsolutePath());
175
					continue;
176
				}
177
			}
178
			File file = new File(path,yd+".wave");
179
			if(file.exists()){
180
				error.add("file exists:"+file.getAbsolutePath());
181
				continue;
182
			}
183
			try{
184
				FileOutputStream fos = new FileOutputStream(file);
185
				try{
186
					Writer  write = new OutputStreamWriter(fos,ConstData.UTF8);
187
					PreparedStatement ps = con.prepareStatement("SELECT HVALUE,LVALUE,CID,DIC, CTIME FROM WAVE_DATA_ITME WHERE SEQ =? AND CTIME LIKE ? ORDER BY DIC,CID,CTIME");
188
					try {
189
						ps.setString(1, srv.getSeq());
190
						ps.setString(2, yd+"%");
191
						ResultSet rs = ps.executeQuery();
192
						try {
193
							while (rs.next()) {
194
							write.write(Integer.toString(rs.getInt(1)));
195
							write.write(",");
196
							write.write(Integer.toString(rs.getInt(2)));
197
							write.write(",");
198
							write.write(Integer.toString(rs.getInt(3)));
199
							write.write(",");
200
							write.write(Integer.toString(rs.getInt(4)));
201
							write.write(",");
202
							write.write(rs.getString(5));
203
							write.write("\n");
204
							} 
205
						} finally {
206
							JdbcUtil.close(rs);
207
						}
208
					} finally {
209
						JdbcUtil.close(ps);
210
					}
211
					write.flush();
212
					write.close();
213
					fos.flush();
214
				}finally{
215
					fos.close();
216
				}
217
			}catch(IOException e){
218
				continue;
219
			}
220
		}
221
		for(BridgeServer srv:srvs){
222
			JdbcUtil.execute(con,"DELETE FROM WAVE_ITEM_DATA WHERE SEQ='"+srv.getSeq()+"' AND CTIME LIKE '"+yyd+"%'");
223
		}
224
	}
72 225
}

+ 4 - 0
src/main/java/com/ekexiu/project/bridge/resource/dao/BridgeServerDao.java

@ -79,6 +79,10 @@ public interface BridgeServerDao {
79 79
80 80
    @SelectList
81 81
    List<BridgeServer> queryByCode(Connection con, @Nullable Boolean active, @Nullable @Like String code) throws SQLException;
82
    
83
    @SelectList
84
    @Exclude( { "createTime","modifyTime","creator","modifier","remark" })
85
    List<BridgeServer> queryAll(Connection con) throws SQLException;
82 86
}
83 87
84 88