Removed mongo integration

This commit is contained in:
NunoSempere 2021-09-22 22:59:06 +02:00
parent 9d78a1800f
commit 8ffa27b738
5 changed files with 48 additions and 2705 deletions

View File

@ -1,224 +0,0 @@
import { hashString } from "./utils.js"
import { MongoClient } from 'mongodb'
const uri = process.env.MONGODB_URI
const options = {
useUnifiedTopology: true,
useNewUrlParser: true,
}
let client
let clientPromise
if (!process.env.MONGODB_URI) {
throw new Error('Please add your Mongo URI to .env.local')
}
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options)
global._mongoClientPromise = client.connect()
}
clientPromise = global._mongoClientPromise
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options)
clientPromise = client.connect()
}
// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
// export default clientPromise
function roughSizeOfObject(object) {
var objectList = [];
var stack = [object];
var bytes = 0;
while (stack.length) {
var value = stack.pop();
if (typeof value === 'boolean') {
bytes += 4;
}
else if (typeof value === 'string') {
bytes += value.length * 2;
}
else if (typeof value === 'number') {
bytes += 8;
}
else if
(
typeof value === 'object'
&& objectList.indexOf(value) === -1
) {
objectList.push(value);
for (var i in value) {
stack.push(value[i]);
}
}
}
let megaBytes = bytes / (1024) ** 2
let megaBytesRounded = Math.round(megaBytes * 10) / 10
return megaBytesRounded;
}
export async function upsert(contents, documentName, collectionName = "utitlityFunctionCollection", databaseName = "utilityFunctionExtractorDatabase") {
const url = process.env.MONGODB_URL
// const client = new MongoClient(url);
try {
await client.connect();
console.log("Connected correctly to server");
const db = client.db(databaseName);
// Use the collection "data"
const collection = db.collection(collectionName);
// Construct a document
let document = ({
"name": documentName,
"timestamp": new Date().toISOString(),
"contentsArray": contents
})
// Create a filter
const filter = { "name": documentName };
// Insert a single document, wait for promise so we can read it back
// const p = await collection.insertOne(metaforecastDocument);
await collection.replaceOne(filter, document, { upsert: true });
console.log(`Pushed document ${documentName} in collection ${collectionName} in database ${databaseName} with approximate size ${roughSizeOfObject(document)} MB`)
// Find one document
const myDocument = await collection.findOne(filter);
// Print to the console
console.log(`Received document ${documentName} in collection ${collectionName} in database ${databaseName} with approximate size ${roughSizeOfObject(contents)} MB`)
console.log("Sample: ")
console.log(JSON.stringify(myDocument.contentsArray.slice(0, 1), null, 4));
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
}
export async function pushToMongo(jsObject) {
let documentName = hashString(JSON.stringify(jsObject)) + "-test"
upsert(jsonObject, documentName)
}
export async function mongoRead(documentName, collectionName = "utitlityFunctionCollection", databaseName = "utilityFunctionExtractorDatabase") {
const url = process.env.MONGODB_URL
/*const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
*/
let documentContents
try {
await client.connect();
console.log(`Connected correctly to server to read ${documentName}`);
const db = client.db(databaseName);
// Use the collection "data"
const collection = db.collection(collectionName);
// Search options
const query = { "name": documentName };
const options = {
// sort matched documents in descending order by rating
sort: { rating: -1 },
};
// Insert a single document, wait for promise so we can read it back
// const p = await collection.insertOne(metaforecastDocument);
const document = await collection.findOne(query, options);
documentContents = document.contentsArray
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
console.log(documentContents.slice(0, 1));
return documentContents
}
export async function mongoReadWithReadCredentials(documentName, collectionName = "utitlityFunctionCollection", databaseName = "utilityFunctionExtractorDatabase") {
const url = "mongodb+srv://metaforecast-frontend:hJr5c9kDhbutBtF1@metaforecastdatabaseclu.wgk8a.mongodb.net/?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true"; // This user only has read permissions, so I'm not excessively worried, and would even be pleased, if someone read this and decided to do something cool with the database.
/*const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});*/
let documentContents
try {
await client.connect();
// console.log(`Connected correctly to server to read ${documentName}`);
const db = client.db(databaseName);
// Use the collection "data"
const collection = db.collection(collectionName);
// Search options
const query = { "name": documentName };
const options = {
// sort matched documents in descending order by rating
sort: { rating: -1 },
};
// Insert a single document, wait for promise so we can read it back
// const p = await collection.insertOne(metaforecastDocument);
const document = await collection.findOne(query, options);
documentContents = document.contentsArray
} catch (err) {
console.log(err.stack);
}
finally {
await client.close();
}
// console.log(documentContents.slice(0,1));
return documentContents
}
export async function mongoGetAllElements(databaseName = "utilityFunctionExtractorDatabase", collectionName = "utitlityFunctionCollection") {
const url = process.env.MONGODB_URL
/*const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});*/
try {
await client.connect();
console.log(`Connected correctly to server`);
const db = client.db(databaseName);
// Use the collection "data"
const collection = db.collection(collectionName);
// Search options
const query = ({});
const options = ({});
// Insert a single document, wait for promise so we can read it back
// const p = await collection.insertOne(metaforecastDocument);
const documents = await collection.find().toArray()
let documentNames = documents.map(document => ({ name: document.name, roughSizeMBs: roughSizeOfObject(document) }));
console.log(documentNames)
} catch (error) {
console.log(error)
}
finally {
await client.close();
}
}
//mongoGetAllElements()
//mongoGetAllElements("utilityFunctionExtractorDatabase", "metaforecastHistory")

8
lib/pushToMongo.js Normal file
View File

@ -0,0 +1,8 @@
import axios from "axios"
export async function pushToMongo(data){
let response = await axios.post('https://metaforecast-twitter-bot.herokuapp.com/utility-function-extractor', {
data: data
})
console.log(response)
}

2510
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -8,18 +8,15 @@
"start": "next start"
},
"dependencies": {
"axios": "^0.21.4",
"d3": "^6.7.0",
"dns": "^0.1.2",
"mongodb": "^4.1.2",
"net": "^1.0.2",
"next": "latest",
"path": "^0.12.7",
"react": "^17.0.1",
"react-compound-slider": "^3.3.1",
"react-dom": "^17.0.1",
"react-markdown": "^6.0.2",
"remark-gfm": "^1.0.0",
"tls": "^0.0.1"
"remark-gfm": "^1.0.0"
},
"devDependencies": {
"autoprefixer": "^10.0.4",

View File

@ -8,8 +8,7 @@ import { DisplayElement } from '../lib/displayElement'
import { DisplayAsMarkdown } from '../lib/displayAsMarkdown'
import { CreateTableWithDistances } from '../lib/findPaths'
import { TextAreaForJson } from "../lib/textAreaForJson"
import { pushToMongo } from "../lib/mongo-wrapper.js"
// Utilities
import { pushToMongo } from "./lib/pushToMongo.js"
let increasingList = (n) => Array.from(Array(n).keys())
let buildLinks = quantitativeComparisons => quantitativeComparisons.map(([element1, element2, distance]) => ({ source: element1, target: element2, distance: distance }))
@ -226,6 +225,7 @@ export default function Home({ listOfElementsDefault }) {
if (successStatus) {
let jsObject = nicelyFormatLinks(quantitativeComparisons, listOfElements)
pushToMongo(jsObject)
console.log(jsObject)
}
}