fix(all): CHART and IMAGE from GET HTTP calls.

This commit is contained in:
Rodrigo Rodriguez 2022-07-06 12:36:51 -03:00
parent 55ff686a3f
commit cf62b10196
8 changed files with 175 additions and 24410 deletions

View file

@ -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)

View file

@ -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();
@ -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`);

View file

@ -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`;
});

View file

@ -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');
@ -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
@ -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.
@ -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;
}
@ -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;
}
}
/**

View file

@ -521,7 +521,7 @@ export class GBDeployer implements IGBDeployer {
GBLog.info(`Local is up to date: ${itemPath}...`);
}
}
});
});
}
}
/**

View file

@ -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();

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,7 @@
"botframework-directlinejs": "0.14.1",
"botframework-webchat": "^4.13.0",
"deep-extend": "0.6.0",
"eslint": "7.11.0",
"fetch": "1.1.0",
"msal": "^1.4.11",
"powerbi-client": "2.18.0",
@ -25,8 +26,7 @@
"react-transition-group": "^4.4.2",
"rxjs": "^7.1.0",
"url-join": "4.0.1",
"webpack": "4.44.2",
"eslint": "7.11.0"
"webpack": "4.44.2"
},
"scripts": {
"start": "react-scripts start",