app.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const gun = Gun("https://hybrid-osn.herokuapp.com/gun");
  2. const gunId = "hybridOSN-beta010";
  3. const app = new Vue({
  4. el: "#app",
  5. data: {
  6. date: new Date().toISOString().slice(0, 10),
  7. keysByDate: {},
  8. done: 0,
  9. total: 0,
  10. showLoading: false,
  11. hashtags: []
  12. },
  13. computed: {
  14. hashtagKey() {
  15. if (this.date in this.keysByDate) {
  16. return this.keysByDate[this.date];
  17. } else {
  18. return null;
  19. }
  20. },
  21. hashtagRanking() {
  22. const ranking = _.countBy(this.hashtags, hashtag => hashtag);
  23. const sortableHelper = [];
  24. for (let entry in ranking) {
  25. sortableHelper.push({ hashtags: entry, counts: ranking[entry] });
  26. }
  27. const sortedRanking = sortableHelper.sort(
  28. (a, b) => b["counts"] - a["counts"]
  29. );
  30. return sortedRanking;
  31. }
  32. },
  33. async mounted() {
  34. // load data and group by date
  35. const data = await gun
  36. .get(gunId)
  37. .get("hashtags")
  38. .then();
  39. if (data === undefined) {
  40. console.error("Currently there is no data on Gun provided.");
  41. } else {
  42. const keys = Object.keys(data).filter(val => /^[0-9]+$/.test(val));
  43. this.createDateKeyMap(keys);
  44. this.loadHashtags();
  45. }
  46. },
  47. methods: {
  48. createDateKeyMap(keys) {
  49. const tzoffset = new Date().getTimezoneOffset() * 60000;
  50. this.keysByDate = keys.reduce((map, el) => {
  51. map[new Date(parseInt(el) - tzoffset).toISOString().slice(0, 10)] = el;
  52. return map;
  53. }, {});
  54. },
  55. async loadHashtags() {
  56. this.hashtags = [];
  57. // show loading
  58. this.showLoading = true;
  59. if (this.hashtagKey === null) {
  60. console.info("No hashtags posted on this day (" + this.date + ")");
  61. } else {
  62. let data = await gun
  63. .get(gunId)
  64. .get("hashtags")
  65. .get(this.hashtagKey)
  66. .then();
  67. const keys = Object.keys(data).filter(val => /^[0-9]+$/.test(val));
  68. // (re)set vars
  69. this.total = keys.length;
  70. // fetch hashtags
  71. for (let i = 0; i < this.total; i++) {
  72. this.done = i + 1;
  73. const hashtagEntry = await gun.get(keys[i]).then();
  74. if (hashtagEntry !== undefined && hashtagEntry["hashtags"]) {
  75. this.hashtags.push(hashtagEntry["hashtags"]);
  76. }
  77. }
  78. }
  79. // finish loading
  80. this.showLoading = false;
  81. }
  82. }
  83. });