forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserStatisticsDaoImpl.java
More file actions
135 lines (119 loc) · 6.3 KB
/
UserStatisticsDaoImpl.java
File metadata and controls
135 lines (119 loc) · 6.3 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.user.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.user.UserStatisticsVO;
import com.cloud.utils.DateUtil;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.Transaction;
@Local(value={UserStatisticsDao.class})
public class UserStatisticsDaoImpl extends GenericDaoBase<UserStatisticsVO, Long> implements UserStatisticsDao {
private static final Logger s_logger = Logger.getLogger(UserStatisticsDaoImpl.class);
private static final String ACTIVE_AND_RECENTLY_DELETED_SEARCH = "SELECT us.id, us.data_center_id, us.account_id, us.public_ip_address, us.device_id, us.device_type, us.network_id, us.agg_bytes_received, us.agg_bytes_sent " +
"FROM user_statistics us, account a " +
"WHERE us.account_id = a.id AND (a.removed IS NULL OR a.removed >= ?) " +
"ORDER BY us.id";
private static final String UPDATED_STATS_SEARCH = "SELECT id, current_bytes_received, current_bytes_sent, net_bytes_received, net_bytes_sent, agg_bytes_received, agg_bytes_sent from user_statistics " +
"where (agg_bytes_received < net_bytes_received + current_bytes_received) OR (agg_bytes_sent < net_bytes_sent + current_bytes_sent)";
private final SearchBuilder<UserStatisticsVO> AllFieldsSearch;
private final SearchBuilder<UserStatisticsVO> AccountSearch;
public UserStatisticsDaoImpl() {
AccountSearch = createSearchBuilder();
AccountSearch.and("account", AccountSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountSearch.done();
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("network", AllFieldsSearch.entity().getNetworkId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("ip", AllFieldsSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("device", AllFieldsSearch.entity().getDeviceId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("deviceType", AllFieldsSearch.entity().getDeviceType(), SearchCriteria.Op.EQ);
AllFieldsSearch.done();
}
@Override
public UserStatisticsVO findBy(long accountId, long dcId, long networkId, String publicIp, Long deviceId, String deviceType) {
SearchCriteria<UserStatisticsVO> sc = AllFieldsSearch.create();
sc.setParameters("account", accountId);
sc.setParameters("dc", dcId);
sc.setParameters("network", networkId);
sc.setParameters("ip", publicIp);
sc.setParameters("device", deviceId);
sc.setParameters("deviceType", deviceType);
return findOneBy(sc);
}
@Override
public UserStatisticsVO lock(long accountId, long dcId, long networkId, String publicIp, Long deviceId, String deviceType) {
SearchCriteria<UserStatisticsVO> sc = AllFieldsSearch.create();
sc.setParameters("account", accountId);
sc.setParameters("dc", dcId);
sc.setParameters("network", networkId);
sc.setParameters("ip", publicIp);
sc.setParameters("device", deviceId);
sc.setParameters("deviceType", deviceType);
return lockOneRandomRow(sc, true);
}
@Override
public List<UserStatisticsVO> listBy(long accountId) {
SearchCriteria<UserStatisticsVO> sc = AccountSearch.create();
sc.setParameters("account", accountId);
return search(sc, null);
}
@Override
public List<UserStatisticsVO> listActiveAndRecentlyDeleted(Date minRemovedDate, int startIndex, int limit) {
List<UserStatisticsVO> userStats = new ArrayList<UserStatisticsVO>();
if (minRemovedDate == null) return userStats;
Transaction txn = Transaction.currentTxn();
try {
String sql = ACTIVE_AND_RECENTLY_DELETED_SEARCH + " LIMIT " + startIndex + "," + limit;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setString(1, DateUtil.getDateDisplayString(TimeZone.getTimeZone("GMT"), minRemovedDate));
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
userStats.add(toEntityBean(rs, false));
}
} catch (Exception ex) {
s_logger.error("error saving user stats to cloud_usage db", ex);
}
return userStats;
}
@Override
public List<UserStatisticsVO> listUpdatedStats() {
List<UserStatisticsVO> userStats = new ArrayList<UserStatisticsVO>();
Transaction txn = Transaction.currentTxn();
try {
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(UPDATED_STATS_SEARCH);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
userStats.add(toEntityBean(rs, false));
}
} catch (Exception ex) {
s_logger.error("error lisitng updated user stats", ex);
}
return userStats;
}
}