worldcontroller.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "worldcontroller.h"
  2. #include "world.h"
  3. #include "pworld.h"
  4. #include "mngworld.h"
  5. #include <TCache>
  6. #include <TSqlQuery>
  7. void WorldController::index()
  8. {
  9. QList<World> worldList = World::getAll();
  10. texport(worldList);
  11. render();
  12. }
  13. void WorldController::plain()
  14. {
  15. setContentType(QByteArrayLiteral("text/plain"));
  16. renderText(QStringLiteral("Hello, World!"));
  17. }
  18. void WorldController::show(const QString &pk)
  19. {
  20. World world = World::get(pk.toUInt());
  21. texport(world);
  22. render();
  23. }
  24. void WorldController::queries()
  25. {
  26. queries("1");
  27. }
  28. void WorldController::queries(const QString &num)
  29. {
  30. QVariantList worlds;
  31. int d = std::min(std::max(num.toInt(), 1), 500);
  32. for (int i = 0; i < d; ++i) {
  33. int id = Tf::random(1, 10000);
  34. worlds << World::get(id).toVariantMap();
  35. }
  36. renderJson(worlds);
  37. }
  38. void WorldController::cached_queries()
  39. {
  40. cached_queries("1");
  41. }
  42. void WorldController::cached_queries(const QString &num)
  43. {
  44. constexpr int SECONDS = 60 * 10; // cache time
  45. QVariantList worlds;
  46. QVariantMap world;
  47. int d = std::min(std::max(num.toInt(), 1), 500);
  48. for (int i = 0; i < d; ++i) {
  49. int id = Tf::random(1, 10000);
  50. auto key = QByteArray::number(id);
  51. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  52. if (randomNumber.isEmpty()) {
  53. auto w = World::get(id);
  54. worlds << w.toVariantMap();
  55. // Cache the value
  56. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  57. } else {
  58. world.insert("id", id);
  59. world.insert("randomNumber", randomNumber.toInt());
  60. worlds << world;
  61. }
  62. }
  63. renderJson(worlds);
  64. }
  65. void WorldController::random()
  66. {
  67. int id = Tf::random(1, 10000);
  68. World world = World::get(id);
  69. renderJson(world.toVariantMap());
  70. }
  71. void WorldController::entry()
  72. {
  73. renderEntry();
  74. }
  75. void WorldController::create()
  76. {
  77. if (httpRequest().method() != Tf::Post) {
  78. return;
  79. }
  80. QVariantMap form = httpRequest().formItems("world");
  81. World world = World::create(form);
  82. if (!world.isNull()) {
  83. QString notice = "Created successfully.";
  84. tflash(notice);
  85. redirect(urla("show", world.id()));
  86. } else {
  87. QString error = "Failed to create.";
  88. texport(error);
  89. renderEntry(form);
  90. }
  91. }
  92. void WorldController::renderEntry(const QVariantMap &world)
  93. {
  94. texport(world);
  95. render("entry");
  96. }
  97. void WorldController::edit(const QString &pk)
  98. {
  99. World world = World::get(pk.toUInt());
  100. if (!world.isNull()) {
  101. renderEdit(world.toVariantMap());
  102. } else {
  103. redirect(urla("entry"));
  104. }
  105. }
  106. void WorldController::save(const QString &pk)
  107. {
  108. if (httpRequest().method() != Tf::Post) {
  109. return;
  110. }
  111. QString error;
  112. World world = World::get(pk.toUInt());
  113. if (world.isNull()) {
  114. error = "Original data not found. It may have been updated/removed by another transaction.";
  115. tflash(error);
  116. redirect(urla("edit", pk));
  117. return;
  118. }
  119. QVariantMap form = httpRequest().formItems("world");
  120. world.setProperties(form);
  121. if (world.save()) {
  122. QString notice = "Updated successfully.";
  123. tflash(notice);
  124. redirect(urla("show", pk));
  125. } else {
  126. error = "Failed to update.";
  127. texport(error);
  128. renderEdit(form);
  129. }
  130. }
  131. void WorldController::renderEdit(const QVariantMap &world)
  132. {
  133. texport(world);
  134. render("edit");
  135. }
  136. void WorldController::updates()
  137. {
  138. updates("1");
  139. }
  140. void WorldController::updates(const QString &num)
  141. {
  142. QVariantList worlds;
  143. int d = std::min(std::max(num.toInt(), 1), 500);
  144. World world;
  145. for (int i = 0; i < d; ++i) {
  146. int id = Tf::random(1, 10000);
  147. world = World::get(id);
  148. world.setRandomNumber( Tf::random(1, 10000) );
  149. world.update();
  150. worlds << world.toVariantMap();
  151. }
  152. renderJson(worlds);
  153. }
  154. void WorldController::remove(const QString &pk)
  155. {
  156. if (httpRequest().method() != Tf::Post) {
  157. return;
  158. }
  159. World world = World::get(pk.toUInt());
  160. world.remove();
  161. redirect(urla("index"));
  162. }
  163. /*
  164. * PostgreSQL
  165. */
  166. void WorldController::prandom()
  167. {
  168. int id = Tf::random(1, 10000);
  169. PWorld world = PWorld::get(id);
  170. renderJson(world.toVariantMap());
  171. }
  172. void WorldController::pqueries()
  173. {
  174. pqueries("1");
  175. }
  176. void WorldController::pqueries(const QString &num)
  177. {
  178. QVariantList worlds;
  179. int d = std::min(std::max(num.toInt(), 1), 500);
  180. for (int i = 0; i < d; ++i) {
  181. int id = Tf::random(1, 10000);
  182. worlds << PWorld::get(id).toVariantMap();
  183. }
  184. renderJson(worlds);
  185. }
  186. void WorldController::cached_pqueries()
  187. {
  188. cached_pqueries("1");
  189. }
  190. void WorldController::cached_pqueries(const QString &num)
  191. {
  192. constexpr int SECONDS = 60 * 30; // cache time
  193. QVariantList worlds;
  194. QVariantMap world;
  195. int d = std::min(std::max(num.toInt(), 1), 500);
  196. for (int i = 0; i < d; ++i) {
  197. int id = Tf::random(1, 10000);
  198. auto key = QByteArray::number(id);
  199. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  200. if (randomNumber.isEmpty()) {
  201. auto w = PWorld::get(id);
  202. worlds << w.toVariantMap();
  203. // Cache the value
  204. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  205. } else {
  206. world.insert("id", id);
  207. world.insert("randomnumber", randomNumber.toInt());
  208. worlds << world;
  209. }
  210. }
  211. renderJson(worlds);
  212. }
  213. void WorldController::pupdates(const QString &num)
  214. {
  215. const QString statement("UPDATE world SET randomnumber = CASE id");
  216. QVariantList worlds;
  217. QString ids;
  218. QString q = statement;
  219. q.reserve(4096);
  220. int d = std::min(std::max(num.toInt(), 1), 500);
  221. PWorld world;
  222. auto blkupdate = [&q, &ids, &statement]() {
  223. if (!ids.isEmpty()) {
  224. ids.chop(1);
  225. q += QStringLiteral(" END WHERE id IN (%1)").arg(ids);
  226. TSqlQuery query;
  227. query.exec(q);
  228. ids.clear();
  229. q = statement;
  230. }
  231. };
  232. for (int i = 0; i < d; ++i) {
  233. int id = Tf::random(1, 10000);
  234. world = PWorld::get(id);
  235. world.setRandomNumber( Tf::random(1, 10000) );
  236. q += QLatin1String(" WHEN ");
  237. q += QString::number(world.id());
  238. q += QLatin1String(" THEN ");
  239. q += QString::number(world.randomNumber());
  240. ids += QString::number(world.id());
  241. ids += ',';
  242. worlds << world.toVariantMap();
  243. if (!((i + 1) % 200)) {
  244. blkupdate();
  245. }
  246. }
  247. if (d == 1) {
  248. world.update();
  249. } else {
  250. blkupdate();
  251. }
  252. renderJson(worlds);
  253. }
  254. void WorldController::pupdates()
  255. {
  256. pupdates("1");
  257. }
  258. /*
  259. * MongoDB
  260. */
  261. void WorldController::mqueries()
  262. {
  263. mqueries("1");
  264. }
  265. void WorldController::mqueries(const QString &num)
  266. {
  267. QVariantList worlds;
  268. int d = std::min(std::max(num.toInt(), 1), 500);
  269. for (int i = 0; i < d; ++i) {
  270. QString id = QString::number(Tf::random(1, 10000));
  271. worlds << MngWorld::get(id).toVariantMap();
  272. }
  273. renderJson(worlds);
  274. }
  275. void WorldController::cached_mqueries()
  276. {
  277. cached_mqueries("1");
  278. }
  279. void WorldController::cached_mqueries(const QString &num)
  280. {
  281. constexpr int SECONDS = 60 * 10; // cache time
  282. QVariantList worlds;
  283. QVariantMap world;
  284. int d = std::min(std::max(num.toInt(), 1), 500);
  285. for (int i = 0; i < d; ++i) {
  286. int id = Tf::random(1, 10000);
  287. QByteArray key = QByteArray::number(id);
  288. auto randomNumber = Tf::cache()->get(key); // Gets from cache
  289. if (randomNumber.isEmpty()) {
  290. auto w = MngWorld::get(key);
  291. worlds << w.toVariantMap();
  292. // Cache the value
  293. Tf::cache()->set(key, QByteArray::number(w.randomNumber()), SECONDS);
  294. } else {
  295. world.insert("id", id);
  296. world.insert("randomNumber", randomNumber.toInt());
  297. worlds << world;
  298. }
  299. }
  300. renderJson(worlds);
  301. }
  302. void WorldController::mrandom()
  303. {
  304. QString id = QString::number(Tf::random(1, 10000));
  305. auto world = MngWorld::get(id);
  306. renderJson(world.toVariantMap());
  307. }
  308. void WorldController::mupdates()
  309. {
  310. mupdates("1");
  311. }
  312. void WorldController::mupdates(const QString &num)
  313. {
  314. QVariantList worlds;
  315. int d = std::min(std::max(num.toInt(), 1), 500);
  316. MngWorld world;
  317. for (int i = 0; i < d; ++i) {
  318. QString id = QString::number(Tf::random(1, 10000));
  319. world = MngWorld::get(id);
  320. world.setRandomNumber( Tf::random(1, 10000) );
  321. world.update();
  322. worlds << world.toVariantMap();
  323. }
  324. renderJson(worlds);
  325. }
  326. // Don't remove below this line
  327. T_DEFINE_CONTROLLER(WorldController)