app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const gun = Gun("https://hybrid-osn.herokuapp.com/gun");
  2. const gunId = "hybridOSN-v1.0.0";
  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. hashtagEntry["hashtags"]
  76. .split("|")
  77. .forEach(hashtag => this.hashtags.push(hashtag));
  78. }
  79. }
  80. }
  81. // finish loading
  82. this.showLoading = false;
  83. }
  84. }
  85. });