const gun = Gun("https://hybrid-osn.herokuapp.com/gun"); const gunId = "hybridOSN-beta003/hashtags"; const app = new Vue({ el: "#app", data: { date: new Date().toISOString().slice(0, 10), keysByDate: {}, done: 0, total: 0, showLoading: false, hashtags: [] }, computed: { hashtagKeys() { if (this.date in this.keysByDate) { return this.keysByDate[this.date]; } else { return []; } }, hashtagRanking() { const ranking = _.countBy(this.hashtags, hashtag => hashtag); const sortableHelper = []; for (let entry in ranking) { sortableHelper.push({ hashtags: entry, counts: ranking[entry] }); } const sortedRanking = sortableHelper.sort( (a, b) => b["counts"] - a["counts"] ); return sortedRanking; } }, async mounted() { // load data and group by date const data = await gun.get(gunId).then(); const keys = Object.keys(data).filter(val => /^[0-9]+$/.test(val)); this.keysByDate = _.groupBy(keys, timestamp => new Date(parseInt(timestamp)).toISOString().slice(0, 10) ); this.loadHashtags(); }, methods: { async loadHashtags() { // show loading this.showLoading = true; // (re)set vars this.total = this.hashtagKeys.length; this.hashtags = []; // fetch tweets for (let i = 0; i < this.total; i++) { this.done = i + 1; let data = await gun .get(gunId) .get(this.hashtagKeys[i]) .then(); this.hashtags.push(data["hashtags"]); } // finish loading this.showLoading = false; } } });