-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mediumroast/alpha
Alpha - The big commit
- Loading branch information
Showing
25 changed files
with
10,224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules/ | ||
.DS_Store | ||
*.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[DEFAULT] | ||
rest_server=http://cherokee.from-ca.com:46767 | ||
user=rflores | ||
secret=password | ||
api_key=Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJmbG9yZXMiLCJjb21wYW55IjoieCIsImlhdCI6MTY1NTAwNDM2NH0.znocDyjS4VSS9tu_ND-pUKw76yNgseUUHYpJ1Tq87do | ||
server_type=mr | ||
working_dir=/tmp | ||
|
||
[s3_settings] | ||
user=medium_roast_io | ||
api_key=b7d1ac5ec5c2193a7d6dd61e7a8a76451885da5bd754b2b776632afd413d53e7 | ||
server=http://cherokee.from-ca.com:9000 | ||
region=leo-dc | ||
source=openvault | ||
|
||
[document_settings] | ||
font_type=Avenir Next | ||
font_size=10 | ||
title_font_color=#41a6ce | ||
title_font_size=14 | ||
company=Mediumroast, Inc. | ||
copyright=Copyright 2022, Mediumroast. All rights reserved. | ||
output_dir=Documents |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* A CLI utility used for accessing and reporting on mediumroast.io company objects | ||
* @author Michael Hay <michael.hay@mediumroast.io> | ||
* @file company.js | ||
* @copyright 2022 Mediumroast, Inc. All rights reserved. | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
// Import required modules | ||
import { Auth, Companies, Interactions } from '../src/api/mrServer.js' | ||
import { CLI } from '../src/helpers.js' | ||
import { CompanyStandalone } from '../src/report/companies.js' | ||
|
||
// Globals | ||
const objectType = 'Companies' | ||
|
||
// Construct the CLI object | ||
const myCLI = new CLI ( | ||
'2.0', | ||
'company', | ||
'Command line interface for mediumroast.io Company objects.', | ||
objectType | ||
) | ||
|
||
// Create the environmental settings | ||
const myArgs = myCLI.parseCLIArgs() | ||
const myConfig = myCLI.getConfig(myArgs.conf_file) | ||
const myEnv = myCLI.getEnv(myArgs, myConfig) | ||
|
||
// Generate the credential & construct the API Controller | ||
const myAuth = new Auth( | ||
myEnv.restServer, | ||
myEnv.apiKey, | ||
myEnv.user, | ||
myEnv.secret | ||
) | ||
const myCredential = myAuth.login() | ||
const apiController = new Companies(myCredential) | ||
const interactionController = new Interactions(myCredential) | ||
|
||
// Predefine the results variable | ||
let [success, stat, results] = [null, null, null] | ||
|
||
// Process the cli options | ||
if (myArgs.report) { | ||
// Retrive the interaction by Id | ||
const [comp_success, comp_stat, comp_results] = await apiController.findById(myArgs.report) | ||
// Retrive the company by Name | ||
const interactionNames = Object.keys(comp_results[0].linked_interactions) | ||
let interactions = [] | ||
for (const interactionName in interactionNames) { | ||
const [mySuccess, myStat, myInteraction] = await interactionController.findByName( | ||
interactionNames[interactionName] | ||
) | ||
interactions.push(myInteraction[0]) | ||
} | ||
// Set the root name to be used for file and directory names in case of packaging | ||
const baseName = comp_results[0].name.replace(/ /g,"_") | ||
// Set the directory name for the package | ||
const baseDir = myEnv.workDir + '/' + baseName | ||
// Define location and name of the report output, depending upon the package switch this will change | ||
let fileName = process.env.HOME + '/Documents/' + comp_results[0].name.replace(/ /g,"_") + '.docx' | ||
|
||
// Set up the document controller | ||
const docController = new CompanyStandalone( | ||
comp_results[0], // Company to report on | ||
interactions, // The interactions associated to the company | ||
'mediumroast.io barrista robot', // The author | ||
'Mediumroast, Inc.' // The authoring company/org | ||
) | ||
|
||
|
||
if(myArgs.package) { | ||
// Create the working directory | ||
const [dir_success, dir_msg, dir_res] = myCLI.safeMakedir(baseDir + '/interactions') | ||
|
||
// If the directory creations was successful download the interaction | ||
if(dir_success) { | ||
fileName = baseDir + '/' + baseName + '_report.docx' | ||
/* | ||
TODO the below only assumes we're storing data in S3, this is intentionally naive. | ||
In the future we will need to be led by the URL string to determine where and what | ||
to download from. Today we only support S3, but this could be Sharepoint, | ||
a local file system, OneDrive, GDrive, etc. There might be an initial less naive | ||
implementation that looks at OneDrive, GDrive, DropBox, etc. as local file system | ||
access points, but the tradeoff would be that caffeine would need to run on a | ||
system with file system access to these objects. | ||
*/ | ||
await myCLI.s3DownloadObjs(interactions, myEnv, baseDir + '/interactions') | ||
// Else error out and exit | ||
} else { | ||
console.error('ERROR (%d): ' + dir_msg, -1) | ||
process.exit(-1) | ||
} | ||
|
||
} | ||
// Create the document | ||
const [report_success, report_stat, report_result] = await docController.makeDocx(fileName, myArgs.package) | ||
|
||
// Create the package and cleanup as needed | ||
if (myArgs.package) { | ||
const [package_success, package_stat, package_result] = await myCLI.createZIPArchive( | ||
myEnv.outputDir + '/' + baseName + '.zip', | ||
baseDir | ||
) | ||
if (package_success) { | ||
console.log(package_stat) | ||
myCLI.rmDir(baseDir) | ||
process.exit(0) | ||
} else { | ||
console.error(package_stat, -1) | ||
process.exit(-1) | ||
} | ||
|
||
} | ||
|
||
// This is the fallback case if we were just creating the report | ||
if (report_success) { | ||
console.log(report_stat) | ||
process.exit(0) | ||
} else { | ||
console.error(report_stat, -1) | ||
process.exit(-1) | ||
} | ||
} else if (myArgs.find_by_id) { | ||
[success, stat, results] = await apiController.findById(myArgs.find_by_id) | ||
} else if (myArgs.find_by_name) { | ||
[success, stat, results] = await apiController.findByName(myArgs.find_by_name) | ||
} else if (myArgs.find_by_x) { | ||
const myCLIObj = JSON.parse(myArgs.find_by_x) | ||
const toFind = Object.entries(myCLIObj)[0] | ||
[success, stat, results] = await apiController.findByX(toFind[0], toFind[1]) | ||
} else if (myArgs.create) { | ||
const [success, msg, rawData] = myCLI.readTextFile(myArgs.create) | ||
if (success) { | ||
const jsonData = JSON.parse(rawData) | ||
const toRegister = jsonData.map(async element => { | ||
const [success, stat, resp] = await apiController.createObj(element) | ||
if (await stat.status_code == 200) { | ||
console.log(`SUCCESS: Created new [${objectType}] object in the mediumroast.io backend.`) | ||
} else { | ||
console.error('ERROR (%d): ' + stat.status_msg, stat.status_code) | ||
} | ||
}) | ||
const registered = await Promise.all(toRegister) | ||
console.log(`SUCCESS: Loaded [${jsonData.length}] objects from file [${myArgs.create}].`) | ||
process.exit(0) | ||
} else { | ||
console.error("ERROR (%d): " + msg, -1) | ||
process.exit(-1) | ||
} | ||
} else if (myArgs.delete) { | ||
console.error('ERROR (%d): Delete not implemented on the backend.', -1) | ||
process.exit(-1) | ||
//results = await apiController.delete(myArgs.delete) | ||
} else { | ||
[success, stat, results] = await apiController.getAll() | ||
} | ||
|
||
// Emit the output | ||
myCLI.outputCLI(myArgs.output, results, myEnv, objectType) |
Oops, something went wrong.