rec.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* global $ */
  2. var config = {
  3. polling_interval: 500,
  4. date_write: function (d) {
  5. return Math.floor(d.getTime() / 1000)
  6. },
  7. date_read: function (unix_timestamp) {
  8. return new Date(unix_timestamp * 1000)
  9. },
  10. datetimeformat: function (d) {
  11. if (Math.abs(new Date() - d) > (3 * 60 * 60 * 1000)) {
  12. return d.toLocaleString()
  13. }
  14. return d.toLocaleTimeString()
  15. }
  16. }
  17. var RecAPI = {
  18. create: function () {
  19. return $.ajax('/api/create', {
  20. method: 'POST',
  21. dataType: 'json'
  22. })
  23. },
  24. stop: function (rec) {
  25. return $.post('/api/update/' + rec.id, {
  26. starttime: rec.starttime
  27. })
  28. },
  29. update: function (id, data) {
  30. return $.post('/api/update/' + id, data)
  31. },
  32. fullcreate: function (name, start, end) {
  33. return $.ajax(
  34. '/api/create', {
  35. method: 'POST',
  36. dataType: 'json',
  37. data: { name: name,
  38. starttime: config.date_write(start),
  39. endtime: config.date_write(end)
  40. }
  41. })
  42. },
  43. generate: function (rec) {
  44. return $.post('/api/generate', {
  45. id: rec.id
  46. })
  47. },
  48. get_archive: function () {
  49. return $.getJSON('/api/get/archive')
  50. },
  51. get_ongoing: function () {
  52. return $.getJSON('/api/get/ongoing')
  53. }
  54. }
  55. function poll_job (job_id, callback) {
  56. $.getJSON('/api/jobs/' + job_id)
  57. .done(function (data) {
  58. if (data.job_status !== 'WIP') {
  59. console.log('polling completed for job[' + job_id + ']', data)
  60. callback(data)
  61. } else {
  62. setTimeout(function () { poll_job(job_id, callback) },
  63. config.polling_interval)
  64. }
  65. })
  66. }