Skip to content

Commit

Permalink
Merge pull request #79 from oslabs-beta/master
Browse files Browse the repository at this point in the history
SeeQR 8.0
  • Loading branch information
Frynoceros authored Aug 4, 2022
2 parents 70f5ac2 + c6aacc7 commit 1b27c51
Show file tree
Hide file tree
Showing 39 changed files with 44,780 additions and 11,700 deletions.
104 changes: 46 additions & 58 deletions README.md

Large diffs are not rendered by default.

Binary file added assets/readmeImages/config_demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/readmeImages/config_demo2.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/readmeImages/gifs/create_db.gif
Binary file not shown.
Binary file added assets/readmeImages/gifs/create_db3.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions backend/BE_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ColumnObj {
export interface dbDetails {
db_name: string;
db_size: string;
db_type: DBType;
}
export interface TableDetails {
table_catalog: string;
Expand All @@ -26,6 +27,7 @@ export interface TableDetails {
columns?: ColumnObj[];
}
export interface DBList {
databaseConnected: boolean[];
databaseList: dbDetails[];
tableList: TableDetails[];
}
Expand All @@ -36,3 +38,26 @@ export type BackendObjType = {
database: string;
updates: UpdatesObjType;
};

export enum DBType {
Postgres = 'pg',
MySQL = 'mysql'
}

export enum LogType {
SUCCESS = 'SUCCESS',
ERROR = 'ERROR',
WARNING = 'WARNING',
NORMAL = 'NORMAL',
SEND = 'SEND',
RECEIVE = 'RECEIVE'
}

export interface DocConfigFile {
mysql_user: string,
mysql_pass: string,
mysql_port: number | string,
pg_user: string,
pg_pass: string,
pg_port: number | string
}
23 changes: 14 additions & 9 deletions backend/DummyD/dummyDataMain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import faker from 'faker';
import { ColumnObj, DummyRecords } from '../BE_types';
import { ColumnObj, DummyRecords, LogType } from '../BE_types';
import logger from '../Logging/masterlog';

const db = require('../models');

Expand Down Expand Up @@ -70,11 +71,11 @@ type GenerateDummyData = (tableInfo: ColumnObj[], numRows: number) => Promise<Du

const generateDummyData: GenerateDummyData = async (tableInfo: ColumnObj[], numRows: number) => {
// assuming primary key is serial, get all the column names except for the column with the primary key
const columnNames = tableInfo.reduce((acc: string[], curr: ColumnObj) => {
// if (curr.constraint_type !== 'PRIMARY KEY')
acc.push(curr.column_name);
return acc;
}, []);
const columnNames: Array<string> = [];
for(let i = 0; i < tableInfo.length; i++) {
columnNames.push(tableInfo[i].column_name);
}

const dummyRecords: DummyRecords = [columnNames];

// generate dummy records for each row
Expand Down Expand Up @@ -104,9 +105,13 @@ const generateDummyData: GenerateDummyData = async (tableInfo: ColumnObj[], numR
if (typeof chosenPrimaryValue === 'string') row.push(`'${chosenPrimaryValue}'`);
else row.push(chosenPrimaryValue);
}
else return new Error('There was an error while retrieving a valid foreign key.');
} catch(err) {
return err;
else{
logger('There was an error while retrieving a valid foreign key while generating dummy data.', LogType.ERROR);
throw new Error('There was an error while retrieving a valid foreign key.');
}
}
catch(err) {
throw err;
}
}
}
Expand Down
72 changes: 72 additions & 0 deletions backend/Logging/masterlog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable func-names */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { LogType } from '../BE_types';

interface Logger {
log: (message: string, logType?: LogType, opt1?: any, opt2?: any) => void;
}

// Helper functions!
const saveLogMessage = (message) => {
const time = new Date().toLocaleDateString('en-US', { timeZone: 'UTC' });
};

// Export
const logger = function (
message: string,
logType: LogType = LogType.NORMAL,
opt1?,
opt2?
) {
// Code for the log color
let colorCode = 0;

// Prefix for the message
let logString: any = logType;

switch (logType) {
case LogType.ERROR: // RED
colorCode = 31;
break;

case LogType.SUCCESS: // GREEN
colorCode = 32;
break;

case LogType.WARNING: // YELLOW
colorCode = 33;
break;

case LogType.RECEIVE: // BLUE
colorCode = 34;
logString = 'SERVER_IPC_RECEIVE';
break;

case LogType.SEND: // MAGENTA
colorCode = 35;
logString = 'SERVER_IPC_SEND';
break;

case LogType.NORMAL: // WHITE (And change logType string)
colorCode = 0;
logString = 'LOG';
break;

default:
colorCode = 0;
break;
}

let moreText = '';

if (opt1) moreText += JSON.stringify(opt1);
if (opt2) moreText += JSON.stringify(opt2);

console.log(
`\u001b[1;${colorCode}m ${`[${logType}] ${message + moreText}`}` +
`\u001b[1;0m`
);
saveLogMessage(`[${logType}] ${message}`);
};

export default logger;
112 changes: 112 additions & 0 deletions backend/_documentsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* eslint-disable no-throw-literal */
/* eslint-disable no-shadow */
/* eslint-disable object-shorthand */
// import path from 'path';
import fs from 'fs';
import os from 'os';
import { DBType, DocConfigFile, LogType } from './BE_types';
import logger from './Logging/masterlog';

const home = `${os.homedir()}/Documents/SeeQR`;
const configFile = `config.json`;
const configPath = `${home}/${configFile}`;

const writeConfigDefault = function (): DocConfigFile {
logger('Could not find config file. Creating default', LogType.WARNING);

const defaultFile: DocConfigFile = {
mysql_user: 'mysql',
mysql_pass: 'mysql',
mysql_port: 3306,
pg_user: 'postgres',
pg_pass: 'postgres',
pg_port: 5432
};

fs.writeFileSync(configPath, JSON.stringify(defaultFile));

return defaultFile;
};

const readConfigFile = function (): DocConfigFile {
if (fs.existsSync(configPath)) {
try {
const text = fs.readFileSync(configPath, 'utf-8');
return JSON.parse(text) as DocConfigFile;
} catch (err: any) {
throw `Error parsing config file: ${err.message}`;
}
} else {
return writeConfigDefault();
}
};

interface DocConfig {
getConfigFolder: () => string;
getCredentials: (dbType: DBType) => { user: string; pass: string, port: number | string };
getFullConfig: () => Object;
saveConfig: (config: Object) => void;
}

const docConfig: DocConfig = {
getConfigFolder: function () {
if (fs.existsSync(home)) {
logger(`Found documents directory: ${home}`, LogType.SUCCESS);
} else {
logger(
`Could not find documents directory. Creating at: ${home}`,
LogType.WARNING
);
fs.mkdirSync(home);
}
return home;
},

getCredentials: function (dbType: DBType) {
this.getConfigFolder();

let configFile: DocConfigFile;
try {
configFile = readConfigFile();
} catch (err: any) {
logger(err.message, LogType.WARNING);
return { user: 'none', pass: 'none', port: 1 };
}

if (dbType === DBType.Postgres) {
return { user: configFile.pg_user, pass: configFile.pg_pass, port: configFile.pg_port };
}
if (dbType === DBType.MySQL) {
return { user: configFile.mysql_user, pass: configFile.mysql_pass, port: configFile.mysql_port };
}
logger('Could not get credentials of DBType: ', LogType.ERROR, dbType);
return { user: 'none', pass: 'none', port: 1 };
},

getFullConfig: function() {
this.getConfigFolder();
let configFile: DocConfigFile;
try {
configFile = readConfigFile();
return configFile;
} catch (err: any) {
logger(err.message, LogType.WARNING);
return {
mysql_user: 'Failed to retrieve data.', mysql_pass: 'Failed to retrieve data.', mysql_port: 'Failed to retrieve data.',
pg_user: 'Failed to retrieve data.', pg_pass: 'Failed to retrieve data.', pg_port: 'Failed to retrieve data.'
};
}
},

saveConfig: function(config: Object) {
try {
fs.writeFileSync(configPath, JSON.stringify(config));
logger('Saved new config: ', LogType.NORMAL, config);
} catch(err: any) {
logger(err.message, LogType.WARNING);
}

}
};

module.exports = docConfig;
Loading

0 comments on commit 1b27c51

Please sign in to comment.