Skip to content

Commit

Permalink
fix(all): CHART and IMAGE from GET HTTP calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jul 6, 2022
1 parent 55ff686 commit cf62b10
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 24,410 deletions.
2 changes: 1 addition & 1 deletion packages/analytics.gblib/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class GuaribasConversation extends Model<GuaribasConversation> {
public text: string;

@ForeignKey(() => GuaribasUser)
@Column(DataType.STRING(255))
@Column(DataType.INTEGER)
public startedByUserId: number;

@BelongsTo(() => GuaribasUser)
Expand Down
59 changes: 47 additions & 12 deletions packages/basic.gblib/services/DialogKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class DialogKeywords {
"--disable-accelerated-2d-canvas",
"--disable-gpu"],
ignoreHTTPSErrors: true,
headless: false,
headless: true,
});
}
const page = await this.browser.newPage();
Expand All @@ -137,30 +137,65 @@ export class DialogKeywords {
* @param legends
* @see https://www.npmjs.com/package/plot
*/
public async chart(step, type, data, legends) {
public async chart(step, type, data, legends, transpose) {

const columns = [[]];
const legends_ = legends.split(';');
let table = [[]];

for (let i = 0; i < legends_.length; i++) {
columns[i] = [legends_[i]];
columns[i] = columns[i].concat(data);
if (legends) {

const legends_ = legends.split(';');

// Columns and data are merged like:
// columns: [
// ['data1', 30, 200, 100, 400, 150, 250],
// ['data2', 50, 20, 10, 40, 15, 25]
// ]

for (let i = 0; i < legends_.length; i++) {
table[i] = [legends_[i]];
table[i] = table[i].concat(data);
}
}
else {
table = SystemKeywords.JSONAsGBTable(data, false);
table.shift();
}

const definition = {
if (transpose) {
const transpose = (array) => {
return array.reduce((prev, next) => next.map((item, i) =>
(prev[i] || []).concat(next[i])
), []);
}
table = transpose(table);
}


let definition = {
size: {
"height": 600,
"width": 1200
"height": 420,
"width": 680
},
data: {
columns: columns,
columns: table,
type: type
},
bar: {
width: 200
ratio: 0.5
}
};

// TODO: https://c3js.org/samples/timeseries.html

if (type === 'timeseries') {
definition['axis'][table[0]] = {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
}

const gbaiName = `${this.min.botId}.gbai`;
const localName = Path.join('work', gbaiName, 'cache', `img${GBAdminService.getRndReadableIdentifier()}.jpg`);

Expand Down
5 changes: 5 additions & 0 deletions packages/basic.gblib/services/GBVMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ export class GBVMService extends GBService {
return `sys().convert(${$3})\n`;
});

// TODO: AS CHART.
// code = code.replace(/(\w+)\s*\=\s*(.*)\s*as chart/gi, ($0, $1, $2) => {
// return `${$1} = sys().asImage(${$2})\n`;
// });

code = code.replace(/(\w+)\s*\=\s*(.*)\s*as image/gi, ($0, $1, $2) => {
return `${$1} = sys().asImage(${$2})\n`;
});
Expand Down
87 changes: 74 additions & 13 deletions packages/basic.gblib/services/SystemKeywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { DialogKeywords } from './DialogKeywords';
import { Tabulator } from 'tabulator-tables';
import { GBServer } from '../../../src/app';
import * as fs from 'fs';
import { jar } from 'request-promise';
const Fs = require('fs');
const Excel = require('exceljs');

Expand Down Expand Up @@ -181,6 +182,51 @@ export class SystemKeywords {
}
}

public static JSONAsGBTable(data, headers) {
try {
let output = [];
let isObject = false;

if (Array.isArray(data)) {
isObject = Object.keys(data[0]) !== null;
}

if (isObject || JSON.parse(data) !== null) {

let keys = Object.keys(data[0]);


if (headers) {
output[0] = [];
// Copies headers as the first element.

for (let i = 0; i < keys.length; i++) {

output[0][i] = keys[i];
}
}
else
{
output.push({ 'gbarray': '0' }); ;
}

// Copies data from JSON format into simple array.

for (let i = 0; i < data.length; i++) {
output[i + 1] = [];
for (let j = 0; j < keys.length; j++) {
output[i + 1][j] = data[i][keys[j]];
}
}

return output;
}
} catch (error) {
GBLog.error(error);
return data;
}
}

/**
*
* @param data
Expand All @@ -192,11 +238,19 @@ export class SystemKeywords {
* @see puppeteer.
*/
private async renderTable(data, renderPDF, renderImage) {

if (!data[1]) {
return null;
}

data = SystemKeywords.JSONAsGBTable(data, true);

// Detects if it is a collection with repeated
// headers.


const gbaiName = `${this.min.botId}.gbai`;
const browser = await puppeteer.launch({ headless: false });
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();

// Includes the associated CSS related to current theme.
Expand Down Expand Up @@ -285,19 +339,30 @@ export class SystemKeywords {

public async asPDF(data, filename) {
let file = await this.renderTable(data, true, false);
return file['url'];
return file[0];
}

public async asImage(data, filename) {
let file = await this.renderTable(data, false, true);
return file['url'];
return file[0];

}

public async executeSQL(data, sql, tableName) {
const first = data.shift();

let objectMode = false;
if (Object.keys(data[0])) {
objectMode = true;
}

let first;
if (!objectMode) {
first = data.shift();
}
data = alasql(sql, [data]);
data.unshift(first);
if (!objectMode) {
data.unshift(first);
}
return data;
}

Expand Down Expand Up @@ -1290,21 +1355,17 @@ export class SystemKeywords {
options['responseType'] = 'stream';
options['encoding'] = null;
}
const isAO = (val) => {
return val instanceof Array || val instanceof Object ? true : false;
}
let result = await request.get(options);

try {

if (isAO(result) && !streaming) {
GBLog.info(`[GET]: ${url} : ${result}`);
return JSON.parse(result);
}
else {

} catch (error) {
GBLog.info(`[GET]: OK.`);

return result;
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ export class GBDeployer implements IGBDeployer {
GBLog.info(`Local is up to date: ${itemPath}...`);
}
}
});
});
}
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core.gbapp/services/GBSSR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function ssr(url: string, useCache: boolean, cacheRefreshRate: number) {
}
}
const browser = await puppeteer.launch({
headless: false,
headless: true,
args: ["--single-process", "--no-zygote", "--no-sandbox", "--disable-features=site-per-process"]
});
// browserWSEndpoint = await browserT.wsEndpoint();
Expand Down

0 comments on commit cf62b10

Please sign in to comment.