From 7d7745a39bfbab0d2586296aeacb274e822caa75 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 22 Aug 2024 14:42:10 -0700 Subject: [PATCH] wip --- package.json | 2 + src/commands/analytics.ts | 104 +++++++++++++++++++++++++------------- 2 files changed, 70 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 8958631a..98336a04 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ "@socketsecurity/config": "^2.1.3", "@socketsecurity/sdk": "^1.2.0", "ansi-align": "^3.0.1", + "blessed": "^0.1.81", + "blessed-contrib": "^4.11.0", "chalk-table": "^1.0.2", "cli-boxes": "^3.0.0", "cli-spinners": "^2.9.2", diff --git a/src/commands/analytics.ts b/src/commands/analytics.ts index 84ecba43..3c5450db 100644 --- a/src/commands/analytics.ts +++ b/src/commands/analytics.ts @@ -1,8 +1,12 @@ +// @ts-nocheck +// @ts-ignore +import blessed from 'blessed' +import contrib from 'blessed-contrib' import chalk from 'chalk' -import meow from 'meow' -import ora from 'ora' // @ts-ignore import chalkTable from 'chalk-table' +import meow from 'meow' +import ora from 'ora' import { outputFlags, validationFlags } from '../flags' import { handleApiCall, handleUnsuccessfulApiResponse } from '../utils/api-helpers' @@ -108,43 +112,71 @@ async function fetchOrgAnalyticsData (time: string, spinner: Ora, apiKey: string spinner.stop() - const data = result.data.reduce((acc: { [key: string]: any }, current) => { - const formattedDate = new Date(current.created_at).toLocaleDateString() - - if (acc[formattedDate]) { - acc[formattedDate].total_critical_alerts += current.total_critical_alerts - acc[formattedDate].total_high_alerts += current.total_high_alerts - acc[formattedDate].total_critical_added += current.total_critical_added - acc[formattedDate].total_high_added += current.total_high_added - acc[formattedDate].total_critical_prevented += current.total_critical_prevented - acc[formattedDate].total_high_prevented += current.total_high_prevented - acc[formattedDate].total_medium_prevented += current.total_medium_prevented - acc[formattedDate].total_low_prevented += current.total_low_prevented - // acc[formattedDate].top_five_alert_types += current.top_five_alert_types - } else { - acc[formattedDate] = current - acc[formattedDate].created_at = formattedDate - } + // const data = result.data.reduce((acc: { [key: string]: any }, current) => { + // const formattedDate = new Date(current.created_at).toLocaleDateString() + + // if (acc[formattedDate]) { + // acc[formattedDate].total_critical_alerts += current.total_critical_alerts + // acc[formattedDate].total_high_alerts += current.total_high_alerts + // acc[formattedDate].total_critical_added += current.total_critical_added + // acc[formattedDate].total_high_added += current.total_high_added + // acc[formattedDate].total_critical_prevented += current.total_critical_prevented + // acc[formattedDate].total_high_prevented += current.total_high_prevented + // acc[formattedDate].total_medium_prevented += current.total_medium_prevented + // acc[formattedDate].total_low_prevented += current.total_low_prevented + // // acc[formattedDate].top_five_alert_types += current.top_five_alert_types + // } else { + // acc[formattedDate] = current + // acc[formattedDate].created_at = formattedDate + // } + + // return acc + // }, {}) + + + // const options = { + // columns: [ + // { field: 'created_at', name: chalk.cyan('Date') }, + // { field: 'total_critical_alerts', name: chalk.cyan('Critical alerts') }, + // { field: 'total_high_alerts', name: chalk.cyan('High alerts') }, + // { field: 'total_critical_added', name: chalk.cyan('Critical alerts added') }, + // { field: 'total_high_added', name: chalk.cyan('High alerts added') }, + // { field: 'total_critical_prevented', name: chalk.cyan('Critical alerts prevented') }, + // { field: 'total_medium_prevented', name: chalk.cyan('Medium alerts prevented') }, + // { field: 'total_low_prevented', name: chalk.cyan('Low alerts prevented') }, + // ] + // } + + // console.log(chalk.bgMagenta.white.bold(`\n Analytics data at the organization level over the last ${time} days (indicated in total amount): \n`)) + // console.log(`${chalkTable(options, Object.values(data))}\n`) + + const screen = blessed.screen() + // eslint-disable-next-line + const grid = new contrib.grid({rows: 1, cols: 2, screen}) + + const line = grid.set(0, 0, 1, 1, contrib.line, + { style: + { line: "yellow" + , text: "green" + , baseline: "black"} + , xLabelPadding: 3 + , xPadding: 5 + , label: 'Stocks'}) + + // const map = grid.set(0, 1, 1, 1, contrib.map, {label: 'Servers Location'}) + + const lineData = { + x: ['t1', 't2', 't3', 't4'], + y: [5, 1, 7, 5] + } - return acc - }, {}) + line.setData([lineData]) + screen.key(['escape', 'q', 'C-c'], function(ch, key) { + return process.exit(0); + }); - const options = { - columns: [ - { field: 'created_at', name: chalk.cyan('Date') }, - { field: 'total_critical_alerts', name: chalk.cyan('Critical alerts') }, - { field: 'total_high_alerts', name: chalk.cyan('High alerts') }, - { field: 'total_critical_added', name: chalk.cyan('Critical alerts added') }, - { field: 'total_high_added', name: chalk.cyan('High alerts added') }, - { field: 'total_critical_prevented', name: chalk.cyan('Critical alerts prevented') }, - { field: 'total_medium_prevented', name: chalk.cyan('Medium alerts prevented') }, - { field: 'total_low_prevented', name: chalk.cyan('Low alerts prevented') }, - ] - } - - console.log(chalk.bgMagenta.white.bold(`\n Analytics data at the organization level over the last ${time} days (indicated in total amount): \n`)) - console.log(`${chalkTable(options, Object.values(data))}\n`) + screen.render() } async function fetchRepoAnalyticsData (repo: string, time: string, spinner: Ora, apiKey: string): Promise {