Skip to content

Commit 640ff6f

Browse files
committed
Add waiting jobs to counters
1 parent f381bcd commit 640ff6f

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

src/main/java/cloudgene/mapred/api/v2/server/GetCounter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import cloudgene.mapred.database.JobDao;
56
import org.restlet.representation.Representation;
67
import org.restlet.representation.StringRepresentation;
78
import org.restlet.resource.Get;
@@ -41,10 +42,13 @@ public Representation get() {
4142
for (String key : counters.keySet()) {
4243
jsonWaiting.put(key, counters.get(key));
4344
}
45+
JobDao jobDao = new JobDao(getDatabase());
46+
int waitingJobs = jobDao.findAllByState(AbstractJob.STATE_WAITING).size();
47+
jsonWaiting.put("runs", waitingJobs);
4448
jsonCounters.put("waiting", jsonWaiting);
4549

46-
UserDao dao = new UserDao(getDatabase());
47-
jsonCounters.put("users", dao.findAll().size());
50+
UserDao dao = new UserDao(getDatabase());
51+
jsonCounters.put("users", dao.countAll());
4852

4953

5054
return new StringRepresentation(jsonCounters.toString());

src/main/java/cloudgene/mapred/database/JobDao.java

+25
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,31 @@ public List<AbstractJob> findAllByState(int state) {
412412
}
413413
}
414414

415+
@SuppressWarnings("unchecked")
416+
public int countAllByState(int state) {
417+
418+
StringBuilder sql = new StringBuilder();
419+
sql.append("select count(*) ");
420+
sql.append("from job ");
421+
sql.append("where state = ? ");
422+
423+
Object[] params = new Object[1];
424+
params[0] = state;
425+
426+
int result = 0;
427+
428+
try {
429+
result = (Integer) queryForObject(sql.toString(), params, new IntegerMapper());
430+
log.debug("count all old jobs successful. results: " + result);
431+
432+
return result;
433+
} catch (SQLException e) {
434+
log.error("count all old jobs failed", e);
435+
return 0;
436+
}
437+
}
438+
439+
415440
public AbstractJob findById(String id) {
416441

417442
return findById(id, true);

src/main/java/cloudgene/mapred/database/UserDao.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,28 @@ public List<User> findAll() {
208208
}
209209
return result;
210210
}
211-
211+
212+
@SuppressWarnings("unchecked")
213+
public int countAll() {
214+
215+
StringBuilder sql = new StringBuilder();
216+
sql.append("select count(*) ");
217+
sql.append("from `user` ");
218+
219+
int result = 0;
220+
221+
try {
222+
result = (Integer) queryForObject(sql.toString(), new IntegerMapper());
223+
log.debug("count all users successful. results: " + result);
224+
225+
return result;
226+
} catch (SQLException e) {
227+
log.error("count all users failed", e);
228+
return 0;
229+
}
230+
}
231+
232+
212233
@SuppressWarnings("unchecked")
213234
public List<User> findByQuery(String query) {
214235

0 commit comments

Comments
 (0)