UNPKG

columns-sdk

Version:

build, publish and share a data visualization on Columns

203 lines 6.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Columns = void 0; const fs_1 = __importDefault(require("fs")); const pako_1 = require("pako"); const axios_1 = __importDefault(require("axios")); const columns_graph_model_1 = require("columns-graph-model"); const compression = { compress: (text) => { if (!text) { return null; } return Buffer.from((0, pako_1.gzip)(text)).toString('base64'); }, decompress: (text) => { if (!text) { return null; } return (0, pako_1.ungzip)(Buffer.from(text, 'base64'), { to: 'string' }); }, }; const baseGraph = (data, fontColor, tooltipColor, title = null, margin = null) => { const left = (0, columns_graph_model_1.axis)(5, fontColor); left.tickProps.textAnchor = 'end'; left.modByUser = true; const bottom = (0, columns_graph_model_1.axis)(4, fontColor); bottom.tickProps.font.angle = -30; bottom.tickProps.textAnchor = 'end'; bottom.modByUser = true; if (!margin) { margin = { left: 100, right: 20, top: 100, bottom: 100 }; } const annotations = []; if (title) { annotations.push({ id: columns_graph_model_1.TITLE_ANNOTATION_ID, text: title, image: null, font: (0, columns_graph_model_1.defaultFont)(fontColor, 24), imageSize: null, anchor: 'middle', link: '', width: 0, byUser: true, }); } return { id: (0, columns_graph_model_1.shortId)(), meta: 'columns graph', data, forecasts: {}, type: columns_graph_model_1.ChartType.COLUMN, top: 0, sort: [], options: {}, annotations, arrows: [], chats: [], widgets: [], shapes: {}, settings: { general: { sketch: { enabled: false }, distribute: 'metric', font: (0, columns_graph_model_1.defaultFont)(fontColor), baseline: 'Raw', policy: 'Fill-Zero', valueOption: 'full', precision: 0, playback: 0, looping: false, autotitle: true, tooltip: { color: tooltipColor, background: '#000000DD', affix: { prefix: '', suffix: '' }, }, margins: { [columns_graph_model_1.ChartType.COLUMN]: margin }, background: 'transparent', gradient: null, palette: null, hideLegend: false, hideTooltip: false, filter: null, legend: 'dot', maxKeyLength: 0, percentage: false, logScale: false, hideBacklink: false, accumulated: false, gridLine: { type: 'none', color: '', }, secondAxis: [], targets: [], }, metrics: null, axes: { keyLeft: true, left, bottom, right: null, top: null, }, bar: null, }, version: 0, dataId: null, viewbox: null, cv: columns_graph_model_1.ColumnsVersion, summary: [], conditions: [], filter: { metrics: null, range: null, keys: null, origin: data }, }; }; const baseURL = 'https://columns.ai/api'; class Columns { constructor(apiKey) { this.apiKey = apiKey; this.version = '1.0.0'; this.client = null; this.client = axios_1.default.create({ baseURL }); } data(keys, metrics, rows) { if (!rows || rows.length === 0) { throw new Error('No data to graph'); } const row = rows[0]; keys.forEach((key) => { if (!(key in row)) throw new Error(`Key ${key} is not in data`); }); metrics.forEach((metric) => { if (!(metric in row)) throw new Error(`Metric ${metric} is not in data`); }); return { keys, metrics, data: rows, timestamp: Date.now() }; } graph(data, margin = { left: 100, right: 50, top: 50, bottom: 50 }) { if (!data) { throw new Error('No data to graph'); } return baseGraph(data, '#000', '#DDD', null, margin); } template(visualId) { if (!visualId) { return null; } return new Promise((resolve, reject) => { this.client.post('/snapshot/visual', { id: visualId }, {}) .then((data) => { const v = data.data; if (v) { const gd = JSON.parse(compression.decompress(v.graph)); gd.filter = null; resolve(gd); return; } resolve(null); }) .catch((e) => { console.error(e); reject('Failed to read specified visual'); }); }); } publish(name, graph) { const id = (0, columns_graph_model_1.shortId)(); const key = this.apiKey; const req = { id, name, graph }; return new Promise((resolve) => { this.client.post('/sdk/graph', req, { headers: { 'x-api-key': key } }) .then((res) => { if (res.data) { resolve(res.data); } resolve(baseURL); }) .catch((e) => { console.error(e); resolve('Failed to publish graph'); }); }); } download(path, id) { const key = this.apiKey; return new Promise((resolve) => { this.client.get(`/sdk/image/${id}`, { headers: { 'x-api-key': key }, responseType: 'stream' }) .then((res) => { const fileStream = fs_1.default.createWriteStream(path); res.data.pipe(fileStream); resolve(''); }) .catch((e) => { console.error(e); resolve('Failed to download graph'); }); }); } } exports.Columns = Columns; ; //# sourceMappingURL=index.js.map