Skip to content

Commit

Permalink
Merge pull request #4 from rodrigofegui/master
Browse files Browse the repository at this point in the history
Estilo do Instagram
  • Loading branch information
teogenesmoura authored Apr 5, 2018
2 parents 768ad6a + 00068e2 commit b5f2102
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 23 deletions.
Binary file added controllers/frentePopularInstagram_Rod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 38 additions & 12 deletions controllers/spreadsheets.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const { google } = require("googleapis");
const ChartjsNode = require("chartjs-node");
const instagram = require("../templates/instagram");
const logger = require("../config/logger");

const fileName = "frentePopularInstagram.png";
const fileName = "frentePopularInstagram_Rod.png";
const pathOfFile = `${__dirname}/${fileName}`;
const chartSize = 600;
const tweetsRow = 8;
const seguindoRow = 9;
const seguidoresRow = 10;
const curtidasRow = 11;


/**
* Contacts the Google API and generates a token. Returns the path of the
Expand Down Expand Up @@ -47,6 +54,7 @@ const authenticate = (client, req, res) => {
* @returns {Promise} collectivesPromise - Promise object that resolves when rows of Google
* Spreadsheet's data are collected and fails when the API returns an error
*/

const listCollectives = (auth) => {
const collectivesPromise = new Promise((resolve, reject) => {
const sheets = google.sheets("v4");
Expand Down Expand Up @@ -81,35 +89,53 @@ const listCollectives = (auth) => {
* the chart's image file is written to disk data are collected and fails when
* chartJSNode fails to do so.
*/

const generateCharts = async (collectives) => {
logger.trace("Generating graph from collectives");
const chartNode = new ChartjsNode(600, 600);
const chartNode = new ChartjsNode(chartSize, chartSize);
/* In sequence: Tweets, Seguindo, Seguidores, Curtidas */
const data = [collectives[2][8], collectives[2][9], collectives[2][10], collectives[2][11]];
const data = [
collectives[2][tweetsRow],
collectives[2][seguidoresRow],
collectives[2][seguidoresRow],
collectives[2][curtidasRow],
];
/* INSTAGRAM */
const label = collectives[0][16];
const label = collectives[0][instagram.label];
const labels = [
collectives[1][8], // Tweets
collectives[1][9], // Seguindo
collectives[1][10], // Seguidores
collectives[1][11], // Curtidas
collectives[1][tweetsRow], // Tweets
collectives[1][seguindoRow], // Seguindo
collectives[1][seguidoresRow], // Seguidores
collectives[1][curtidasRow], // Curtidas
];
const config = {
type: "pie",
type: "pie", // "doughnut"
data: {
datasets: [{
data: data,
backgroundColor: ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9"],
label: label,
backgroundColor: [
instagram.blue,
instagram.purpleViolet,
instagram.orange,
instagram.redOrange],
label: instagram.label,

}],
labels: labels,
},
options: {
responsive: true,
legend: {position: "bottom"},
cutoutPercentage: 25,
title: {
display: true,
text: instagram.label,
},
},
};
await chartNode.drawChart(config);
return chartNode.writeImageToFile("image/png", pathOfFile);
};

module.exports = { generateCharts, authenticate };
module.exports = {generateCharts, authenticate, fileName};

16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions templates/instagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const label = "Instagram";
const purpleViolet = "#8a3ab9",
blue = "#4c68d7",
maroon = "#cd486b",
orange = "#fbad50",
yellow = "#fccc63",
redViolet = "#bc2a8d",
redOrange = "#e95950";

module.exports = {label, purpleViolet, blue, maroon, orange, yellow, redViolet, redOrange};
7 changes: 4 additions & 3 deletions tests/spreadsheets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ const fs = require("fs");
* writing the file reflects the current state of the spreadsheet) and tests whether a chart is
* saved to the disk or not.
*/

describe("generateCharts method", () => {
it("frentePopularInstagram chart should be created", async (done) => {
const collectives = JSON.parse(fs.readFileSync("./tests/spreadsheets-collectives.json"));

// if image already exists, delete it so we can test its creation
if (fs.existsSync("./controllers/frentePopularInstagram.png")) {
fs.unlinkSync("./controllers/frentePopularInstagram.png");
if (fs.existsSync("./controllers/" + spreadsheets.fileName)) {
fs.unlinkSync("./controllers/" + spreadsheets.fileName);
}

// creates chart and writes it to a png file
await spreadsheets.generateCharts(collectives);

// loads the created image
const newImage = fs.readFileSync("./controllers/frentePopularInstagram.png");
const newImage = fs.readFileSync("./controllers/" + spreadsheets.fileName);
expect(newImage).toBeInstanceOf(Buffer);

done();
Expand Down

0 comments on commit b5f2102

Please sign in to comment.