PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/AjaxController.java

https://bitbucket.org/iliax/onlinechecker
Java | 130 lines | 99 code | 25 blank | 6 comment | 16 complexity | 347a00e4f66473478a8ee1e6cda3e85f MD5 | raw file
Possible License(s): LGPL-3.0
  1. package controllers;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.google.gson.Gson;
  8. import com.google.gson.JsonArray;
  9. import com.google.gson.JsonElement;
  10. import com.google.gson.JsonParser;
  11. import com.mongodb.BasicDBObject;
  12. import com.mongodb.DBCollection;
  13. import com.mongodb.DBCursor;
  14. import com.mongodb.DBObject;
  15. import com.mongodb.util.JSON;
  16. import dbstuff.DbManager;
  17. import jobs.JobInvoker;
  18. import play.Logger;
  19. import play.libs.WS.HttpResponse;
  20. import play.libs.WS.WSRequest;
  21. import play.mvc.Controller;
  22. import static util.StaticDataHolder.*;
  23. import static util.VkMotitorConstants.URL_BEGINING;
  24. import static util.VkMotitorConstants.URL_ENDING;
  25. public class AjaxController extends Controller {
  26. public static void time() {
  27. renderText(System.currentTimeMillis());
  28. }
  29. public static void getFirstTargetRecordTime(String uid) {
  30. DBCursor cur = dbm.records.find(new BasicDBObject().append("uid", uid))
  31. .sort(new BasicDBObject("time", new Integer(1))).limit(1);
  32. if (cur.hasNext()) {
  33. String s = String.valueOf(cur.next().get("time"));
  34. cur.close();
  35. renderText(s);
  36. } else {
  37. cur.close();
  38. renderText("");
  39. }
  40. }
  41. /**
  42. * Возвращает 0 или 1 или ошибку
  43. *
  44. * @param name
  45. * идентификатор(не обязательно uid) юзера
  46. */
  47. public static void isUserOnline(String name) {
  48. if (name == null || name.trim().isEmpty()) {
  49. renderText("name is null or empty");
  50. }
  51. try {
  52. ConciseStats stats = Application.getConciseStats(name);
  53. if (stats == null) {
  54. renderText("error.");
  55. }
  56. renderText(stats.getStatus());
  57. } catch (Exception e) {
  58. renderText("error.");
  59. }
  60. }
  61. public static void getConciseStats(String name) {
  62. if (name == null || name.trim().isEmpty()) {
  63. renderText("name is null or empty");
  64. }
  65. try {
  66. ConciseStats stats = Application.getConciseStats(name);
  67. if (stats == null) {
  68. renderText("error.");
  69. }
  70. String json = new Gson().toJson(stats);
  71. renderText(json);
  72. } catch (Exception e) {
  73. renderText("error.");
  74. }
  75. }
  76. public static void getTargetInfo(String uid, String start, String end) {
  77. DBCursor cur = null;
  78. try {
  79. Long st = Long.parseLong(start);
  80. Long en = Long.parseLong(end);
  81. if (st >= en) {
  82. renderJSON(JSON.serialize(Collections.singletonMap("err",
  83. "invalid params")));
  84. }
  85. cur = dbm.records.find(new BasicDBObject().append("uid", uid)
  86. .append("time",
  87. new BasicDBObject().append("$gt", start).append(
  88. "$lt", end)));
  89. List<Map> jsonArr = new ArrayList<Map>();
  90. while (cur.hasNext()) {
  91. DBObject obj = cur.next();
  92. jsonArr.add(obj.toMap());
  93. }
  94. cur.close();
  95. renderJSON(JSON.serialize(jsonArr));
  96. } catch (Exception e) {
  97. Logger.error(e, "Error in getTargetInfo(..)");
  98. dbLogger.log("Error in getTargetInfo(..)", e);
  99. if (cur != null) {
  100. cur.close();
  101. }
  102. error();
  103. }
  104. }
  105. }