app.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const gun = Gun("https://hybrid-osn.herokuapp.com/gun");
  2. const gunId = "hybridOSN-beta003/hashtags";
  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. hashtagKeys() {
  15. if (this.date in this.keysByDate) {
  16. return this.keysByDate[this.date];
  17. } else {
  18. return [];
  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.get(gunId).then();
  36. const keys = Object.keys(data).filter(val => /^[0-9]+$/.test(val));
  37. this.keysByDate = _.groupBy(keys, timestamp =>
  38. new Date(parseInt(timestamp)).toISOString().slice(0, 10)
  39. );
  40. this.loadHashtags();
  41. },
  42. methods: {
  43. async loadHashtags() {
  44. // show loading
  45. this.showLoading = true;
  46. // (re)set vars
  47. this.total = this.hashtagKeys.length;
  48. this.hashtags = [];
  49. // fetch tweets
  50. for (let i = 0; i < this.total; i++) {
  51. this.done = i + 1;
  52. let data = await gun
  53. .get(gunId)
  54. .get(this.hashtagKeys[i])
  55. .then();
  56. this.hashtags.push(data["hashtags"]);
  57. }
  58. // finish loading
  59. this.showLoading = false;
  60. }
  61. }
  62. });