-
Tweets total
-
Retweets total
-
Followers total
-
Sentiment
+
-
-
-
+
+
Positive Likes
+
Negative Likes
+
+
-
+
-
diff --git a/client/src/app.js b/client/src/app.js
index 6c78549..547a8d8 100644
--- a/client/src/app.js
+++ b/client/src/app.js
@@ -24,13 +24,14 @@ document.addEventListener('DOMContentLoaded', () => {
const charts = document.querySelector('#charts')
const chartView = new ChartView(charts);
- const totals = document.querySelector('#summary');
- const totalsView = new TotalsView(totals);
+ const positiveTotals = document.querySelector('#summary1');
+ const negativeTotals = document.querySelector('#summary2');
+ const totalsView = new TotalsView(positiveTotals, negativeTotals);
formView.bindEvents();
listView.bindEvents();
- happySearches.bindEvents('%20%3A%29'); //'%20%3A%29' - happy
+ happySearches.bindEvents('%20%3A%29', true); //'%20%3A%29' - happy
sadSearches.bindEvents('%20%3A%28'); // '%20%3A%28' - sad
mapView.bindEvents();
chartView.bindEvents();
diff --git a/client/src/models/searches.js b/client/src/models/searches.js
index 33a6113..999fbba 100644
--- a/client/src/models/searches.js
+++ b/client/src/models/searches.js
@@ -7,9 +7,15 @@ const Searches = function (url) {
this.APIrequest = new Request('http://localhost:3000/api/search-results');
}
-Searches.prototype.bindEvents = function (sentimentQuery) {
+Searches.prototype.bindEvents = function (sentimentQuery, primary) {
PubSub.subscribe('FormView:search-term-submitted', (event) => {
- this.postSearch(event.detail);
+
+ // todo move the db stuff to another bit
+ if (primary) {
+ this.postSearch(event.detail);
+ this.renderLoadingSpinner();
+ }
+
this.getSearchResults(event.detail.searchTerm, sentimentQuery);
// console.log(event.detail.searchTerm);
})
@@ -30,12 +36,14 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) {
let query = null;
let tweetChannel = null;
let calculationChannel = null;
+ let coordinateChannel = null;
// if there is no sentiment query
// search without one and set the channel variable to be generic
if (!sentimentQuery){
query = `?query=${searchTerm}`;
tweetChannel = 'Searches:tweet-data-loaded';
calculationChannel = 'Searches:totals-calulated';
+ coordinateChannel = 'Searches:tweet-coordinates-loaded';
// if there is a sentiment query
// add the query to the search
} else
@@ -45,6 +53,7 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) {
if (sentimentQuery === '%20%3A%29'){
tweetChannel = 'Searches:happy-tweet-data-loaded';
calculationChannel = 'Searches:happy-totals-calulated';
+ coordinateChannel = 'Searches:happy-tweet-coordinates-loaded';
}
// if the extra query is for sad sentiment
@@ -52,7 +61,11 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) {
else if (sentimentQuery === '%20%3A%28'){
tweetChannel = 'Searches:sad-tweet-data-loaded';
calculationChannel = 'Searches:sad-totals-calulated';
+ coordinateChannel = 'Searches:sad-tweet-coordinates-loaded';
}
+
+console.log("searching for " + query);
+
this.APIrequest.get(query)
.then((tweets) => {
const totals = this.calculator(tweets);
@@ -72,10 +85,7 @@ Searches.prototype.getSearchResults = function (searchTerm, sentimentQuery) {
}
})
PubSub.publish(tweetChannel, tweets);
-
- const stringifiedCoordinates = JSON.stringify(tweetsCoordinates);
- // console.log('stringifiedtweetscoordinates: ', stringifiedCoordinates);
- PubSub.publish('Searches:tweet-coordinates-loaded', tweetsCoordinates);
+ PubSub.publish(coordinateChannel, tweetsCoordinates);
})
.catch(console.error);
};
@@ -106,7 +116,14 @@ Searches.prototype.calculator = function (tweetData) {
totals.retweets += tweet.retweets;
totals.favourites += tweet.favourites;
})
- console.log(totals)
return totals;
}
+
+Searches.prototype.renderLoadingSpinner = function() {
+ const loadingOverlay = document.querySelector('#loading-overlay');
+ const loadingModal = document.querySelector('#loading-modal');
+ loadingOverlay.classList.remove('hidden');
+ loadingModal.classList.remove('hidden');
+};
+
module.exports = Searches;
diff --git a/client/src/views/chart_view.js b/client/src/views/chart_view.js
index 3cf1199..db70213 100644
--- a/client/src/views/chart_view.js
+++ b/client/src/views/chart_view.js
@@ -1,7 +1,7 @@
var echarts = require('echarts');
const PubSub = require('../helpers/pub_sub.js');
-const ViewChart = function(chartHtmlElement){
+const ViewChart = function (chartHtmlElement) {
this.myChart_1 = null;
this.myChart_2 = null;
this.chartHtmlElement = chartHtmlElement;
@@ -10,29 +10,50 @@ const ViewChart = function(chartHtmlElement){
ViewChart.prototype.bindEvents = function () {
-
- this.myChart_1 = echarts.init(document.getElementById('chart_1'))
- this.myChart_2 = echarts.init(document.getElementById('chart_2'))
- this.retweetsValues = [];
- this.favouritesValues = [];
+ this.retweetsValues = {};
+ this.favouritesValues = {};
+
+
PubSub.subscribe('Searches:happy-totals-calulated', (event) => {
- this.retweetsValues.push(event.detail.retweets);
+ const chart1 = document.querySelector('#chart_1');
+ this.chartHtmlElement.classList.remove('hidden');
+ chart1.classList.add('chart');
+ this.myChart_1 = echarts.init(document.getElementById('chart_1'))
+ this.myChart_2 = echarts.init(document.getElementById('chart_2'))
+ this.retweetsValues.happyRetweets = event.detail.retweets;
+ this.favouritesValues.happyFavourites = event.detail.favourites;
+ // this.retweetsValues.push(event.detail.retweets);
console.log('happy retweets from event: ', event.detail.retweets);
- if (this.retweetsValues.length === 2){
- console.log('both values are ready from happy listener: ', this.retweetsValues);
- this.renderChart(this.retweetsValues[0], this.retweetsValues[1], 'Positive vs Negative Retweets', this.myChart_1);
- this.retweetsValues = [];
+ console.log('happy retweets from object: ', this.retweetsValues);
+ if (this.retweetsValues.happyRetweets && this.retweetsValues.sadRetweets) {
+ console.log('both values are ready from happy listener: ', this.retweetsValues);
+ this.renderChart(this.retweetsValues.happyRetweets, this.retweetsValues.sadRetweets, 'Positive vs Negative Retweets', this.myChart_1);
+ //this.retweetsValues = [];
+ }
+ if (this.favouritesValues.happyFavourites && this.favouritesValues.sadFavourites) {
+ console.log('both values are ready from happy listener: ', this.favouritesValues);
+ this.renderChart(this.favouritesValues.happyFavourites, this.favouritesValues.sadFavourites, 'Positive vs Negative Retweets', this.myChart_2);
+ //this.retweetsValues = [];
}
//this.renderChart(event.detail.favourites, event.detail.favourites, 'Positive vs Negative Likes', this.myChart_2);
})
PubSub.subscribe('Searches:sad-totals-calulated', (event) => {
- this.retweetsValues.push(event.detail.retweets);
+ const chart2 = document.querySelector('#chart_2');
+ this.chartHtmlElement.classList.remove('hidden');
+ chart2.classList.add('chart');
+ this.retweetsValues.sadRetweets = event.detail.retweets;
+ this.favouritesValues.sadFavourites = event.detail.favourites;
console.log('sad retweets from event: ', event.detail.retweets);
- if (this.retweetsValues.length === 2){
+ if (this.retweetsValues.length === 2) {
console.log('both values are ready from sad listener: ', this.retweetsValues);
- this.renderChart(this.retweetsValues[0], this.retweetsValues[1], 'Positive vs Negative Retweets', this.myChart_1);
- this.retweetsValues = [];
+ this.renderChart(this.retweetsValues.happyRetweets, this.retweetsValues.sadRetweets, 'Positive vs Negative Retweets', this.myChart_1);
+ //this.retweetsValues = [];
+ }
+ if (this.favouritesValues.happyFavourites && this.favouritesValues.sadFavourites) {
+ console.log('both values are ready from happy listener: ', this.favouritesValues);
+ this.renderChart(this.favouritesValues.happyFavourites, this.favouritesValues.sadFavourites, 'Positive vs Negative Retweets', this.myChart_2);
+ //this.retweetsValues = [];
}
//this.renderChart(event.detail.favourites, event.detail.favourites, 'Positive vs Negative Likes', this.myChart_2);
})
@@ -40,17 +61,34 @@ ViewChart.prototype.bindEvents = function () {
ViewChart.prototype.renderChart = function (chartData_positive, chartData_negative, title, chart_x) {
chart_x.setOption({
- title: {
- text: title
+ // title: {
+ // text: title
+ // },
+ color: ['#c4114b','#15A5CC'],
+ grid: {
+ left: 0,
+ top: 0,
+ right: 0,
+ bottom: 0
},
tooltip: {},
series: [{
- radius: '50%',
+ radius: '70%',
type: 'pie',
data: [
{ value: chartData_positive, name: `positive: ${chartData_positive}` },
{ value: chartData_negative, name: `negative: ${chartData_negative}` },
- ]
+ ],
+ itemStyle: {
+ normal: {
+ label: {
+ show: false
+ },
+ labelLine: {
+ show: false
+ }
+ }
+ }
}]
});
}
diff --git a/client/src/views/list_view.js b/client/src/views/list_view.js
index 8791d3d..78bedc4 100644
--- a/client/src/views/list_view.js
+++ b/client/src/views/list_view.js
@@ -8,6 +8,14 @@ const ListView = function (listElement, searchResultsListElement) {
}
ListView.prototype.bindEvents = function () {
+ let allTweets = [];
+ let eventCounter = 0;
+
+ PubSub.subscribe('FormView:search-term-submitted', (event) => {
+ eventCounter = 0;
+ allTweets = [];
+ });
+
PubSub.subscribe('Searches:searches-data-loaded', (event) => {
this.render(event.detail);
})
@@ -16,12 +24,22 @@ ListView.prototype.bindEvents = function () {
// this.renderSearchResultsList(event.detail);
// });
PubSub.subscribe('Searches:happy-tweet-data-loaded', (event) => {
+ eventCounter += 1;
+ allTweets = allTweets.concat(event.detail, allTweets);
+ if (eventCounter === 2) {
+ this.renderTop5Tweets(allTweets);
+ }
console.log('got happy tweets')
- this.renderSearchResultsList(event.detail);
+ //this.renderSearchResultsList(event.detail);
});
PubSub.subscribe('Searches:sad-tweet-data-loaded', (event) => {
+ eventCounter += 1;
+ allTweets = allTweets.concat(event.detail, allTweets);
+ if (eventCounter === 2) {
+ this.renderTop5Tweets(allTweets);
+ }
console.log('got sad tweets')
- this.renderSearchResultsList(event.detail);
+ //this.renderSearchResultsList(event.detail);
});
};
@@ -35,7 +53,17 @@ ListView.prototype.renderSearchResultsList = function (searchResults) {
ListView.prototype.renderOneSearchResult = function (searchResult) {
const searchResultItem = document.createElement('li');
searchResultItem.textContent = searchResult.text;
+ searchResultItem.id = 'tweet' + searchResult.id;
+ //console.log(searchResult.retweets);
this.searchResultsListElement.appendChild(searchResultItem);
+ twttr.widgets.createTweet(
+ ' ' + searchResult.id,
+ document.getElementById('tweet' + searchResult.id),
+ console.log(searchResult.id),
+ {
+ theme: 'light'
+ }
+ );
}
ListView.prototype.render = function (searches) {
@@ -61,14 +89,27 @@ ListView.prototype.createDeleteButton = function (listItemId) {
PubSub.publish('ListView:delete-clicked', listItemId);
})
- const historyButtons = document.querySelectorAll('#list button');
- const list = document.querySelector('#list');
- const buttonCount = historyButtons.length;
- if (buttonCount >= 4) {
- list.lastChild.remove();
- };
+ // const historyButtons = document.querySelectorAll('#list button');
+ // const list = document.querySelector('#list');
+ // const buttonCount = historyButtons.length;
+ // if (buttonCount >= 6) {
+ // list.lastChild.remove();
+ // };
return deleteButton
}
+ListView.prototype.renderTop5Tweets = function (tweets) {
+ tweets.sort((a, b) => {
+ return b.retweets - a.retweets;
+ });
+ const top5Tweets = tweets.slice(0, 5);
+ const container = document.querySelector('#result-container');
+ container.classList.remove('hidden');
+ this.searchResultsListElement.innerHTML = "";
+ top5Tweets.forEach((tweet) => {
+ this.renderOneSearchResult(tweet);
+ });
+};
+
-module.exports = ListView;
\ No newline at end of file
+module.exports = ListView;
diff --git a/client/src/views/map_view.js b/client/src/views/map_view.js
index 7e0940f..7d6959b 100644
--- a/client/src/views/map_view.js
+++ b/client/src/views/map_view.js
@@ -7,10 +7,39 @@ const MapView = function(){
const geoJsonTest = new GeoJsonTest();
MapView.prototype.bindEvents = function () {
+ let eventCounter = 0;
+ let allCoordinates = [];
+
+ // load empty map on start
this.renderMap();
- PubSub.subscribe('Searches:tweet-coordinates-loaded', (event) => {
- // console.log('event', event);
- this.renderMap(event.detail);
+
+ PubSub.subscribe('FormView:search-term-submitted', (event) => {
+ eventCounter = 0;
+ allCoordinates = [];
+ });
+
+ PubSub.subscribe('Searches:happy-tweet-coordinates-loaded', (event) => {
+ eventCounter += 1;
+ allCoordinates = allCoordinates.concat(event.detail, allCoordinates);
+ if (eventCounter === 2){
+ console.log('all coords happy', allCoordinates);
+ this.renderMap(allCoordinates);
+ this.removeLoadingSpinner();
+ } else {
+ console.log('all coords sad was resolved first', allCoordinates);
+ }
+ });
+
+ PubSub.subscribe('Searches:sad-tweet-coordinates-loaded', (event) => {
+ eventCounter += 1;
+ allCoordinates = allCoordinates.concat(event.detail, allCoordinates);
+ if (eventCounter === 2){
+ console.log('all coords sad', allCoordinates);
+ this.renderMap(allCoordinates);
+ this.removeLoadingSpinner();
+ } else {
+ console.log('all coords happy was resolved first', allCoordinates);
+ }
});
};
@@ -35,7 +64,7 @@ const json = {
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
- center: [-20, 20],
+ center: [0, 45],
zoom: 0.65
});
@@ -167,4 +196,11 @@ const json = {
}
+MapView.prototype.removeLoadingSpinner = function(){
+ const loadingOverlay = document.querySelector('#loading-overlay');
+ const loadingModal = document.querySelector('#loading-modal');
+ loadingOverlay.classList.add('hidden');
+ loadingModal.classList.add('hidden');
+}
+
module.exports = MapView;
diff --git a/client/src/views/totals_view.js b/client/src/views/totals_view.js
index 946d8f1..f591514 100644
--- a/client/src/views/totals_view.js
+++ b/client/src/views/totals_view.js
@@ -1,31 +1,62 @@
const PubSub = require('../helpers/pub_sub.js');
-const TotalsView = function (formElement) {
- this.formElement = formElement;
+const TotalsView = function (positiveGrid, negativeGrid) {
+ this.positiveGrid = positiveGrid;
+ this.negativeGrid = negativeGrid;
+ this.happyTweets = 0;
+ this.sadTweets = 0;
+ this.sadRetweets = 0;
+ this.happyRetweets = 0;
+ this.happyFavourites = 0;
+ this.sadFavourites = 0;
}
TotalsView.prototype.bindEvents = function () {
- PubSub.subscribe('Searches:totals-calulated', (event) => {
- this.renderTotals(event.detail);
+ PubSub.subscribe('Searches:happy-totals-calulated', (event) => {
+ //this.happyTweets = event.detail.totalTweets;
+ this.happyRetweets = event.detail.retweets;
+ this.happyFavourites = event.detail.favourites;
+
+ this.renderTotals()
+ this.positiveGrid.classList.remove('hidden');
+
})
+ PubSub.subscribe('Searches:sad-totals-calulated', (event) => {
+ //this.sadTweets = event.detail.totalTweets;
+ this.sadRetweets = event.detail.retweets;
+ this.sadFavourites = event.detail.favourites;
+
+ this.renderTotals()
+ this.negativeGrid.classList.remove('hidden');
+ })
}
-TotalsView.prototype.renderTotals = function (totalData) {
+TotalsView.prototype.renderTotals = function () {
const firstContentElement = document.querySelector('#grid-item-5');
- firstContentElement.textContent = totalData.totalTweets;
+ firstContentElement.textContent = this.happyRetweets;
const secondContentElement = document.querySelector('#grid-item-6');
- secondContentElement.textContent = totalData.favourites;
+ secondContentElement.textContent = this.sadRetweets;
const thirdContentElement = document.querySelector('#grid-item-7');
- thirdContentElement.textContent = totalData.retweets;
+ thirdContentElement.textContent = this.happyFavourites;
const fourthContentElement = document.querySelector('#grid-item-8');
- // fourthContentElement.textContent = totalData.XXXX;
+ fourthContentElement.textContent = this.sadFavourites;
+ //const sentiment = this.determineSentiment();
+ //fourthContentElement.textContent = sentiment;
+}
+TotalsView.prototype.determineSentiment = function() {
+ if (this.sadRetweets + this.sadFavourites <= this.happyRetweets + this.happyFavourites){
+ return ':)'
+ }
+ return ':('
}
+
+
module.exports = TotalsView;
\ No newline at end of file
diff --git a/server/helpers/twitter.js b/server/helpers/twitter.js
index 5b01eaf..7906ea3 100644
--- a/server/helpers/twitter.js
+++ b/server/helpers/twitter.js
@@ -24,7 +24,7 @@ Twitter.prototype.getAllSearchResultsFromLast7DaysForSearchTerm = function (sear
return new Promise((resolve, reject) => {
let nextResultsQuery = null;
let currentPageNumber = 1;
- const maxPages = 1;
+ const maxPages = 2;
let allResults = [];
this.getSinglePageOfResultsFromLast7Days(searchTerm, nextResultsQuery, currentPageNumber, maxPages, allResults, resolve, reject);
@@ -39,7 +39,7 @@ Twitter.prototype.getSinglePageOfResultsFromLast7Days = function (searchTerm, ne
// and the we're definately not on the last page
// add the data so far to allResults
const tweets = data.statuses.map(tweet => {
- return { text: tweet.full_text, location: tweet.user.location, retweets: tweet.retweet_count, favourites: tweet.favorite_count }
+ return { text: tweet.full_text, location: tweet.user.location, retweets: tweet.retweet_count, favourites: tweet.favorite_count, id: tweet.id }
});
tweets.forEach((tweet) => {
@@ -47,11 +47,11 @@ Twitter.prototype.getSinglePageOfResultsFromLast7Days = function (searchTerm, ne
});
// allResults.push(tweets);
- console.log("next results = ", data.search_metadata.next_results);
if (data.search_metadata.next_results && (currentPageNumber < maxPages)) {
// set nextResults to the value from the metadata
nextResultsQuery = data.search_metadata.next_results;
let nextPageNumber = currentPageNumber + 1;
+ console.log('Fetched a page', currentPageNumber, searchTerm, nextResultsQuery);
// call this method recursively with the new values from the first call
this.getSinglePageOfResultsFromLast7Days(searchTerm, nextResultsQuery, nextPageNumber, maxPages, allResults, resolve, reject);
// otherwise, exit now and resolve with the data