diff --git a/backend/build.gradle b/backend/build.gradle index d767299b7..1beadd239 100644 --- a/backend/build.gradle +++ b/backend/build.gradle @@ -80,9 +80,6 @@ dependencies { // https://mvnrepository.com/artifact/org.json/json implementation group: 'org.json', name: 'json', version: '20240303' - // https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-sync - implementation group: 'org.mongodb', name: 'mongodb-driver-sync', version: '4.11.1' - // https://mvnrepository.com/artifact/com.auth0/java-jwt implementation group: 'com.auth0', name: 'java-jwt', version: '4.4.0' diff --git a/backend/src/main/java/org/fulib/webapp/mongo/Mongo.java b/backend/src/main/java/org/fulib/webapp/mongo/Mongo.java deleted file mode 100644 index 248b8eed1..000000000 --- a/backend/src/main/java/org/fulib/webapp/mongo/Mongo.java +++ /dev/null @@ -1,128 +0,0 @@ -package org.fulib.webapp.mongo; - -import com.mongodb.ConnectionString; -import com.mongodb.MongoClientSettings; -import com.mongodb.client.MongoClient; -import com.mongodb.client.MongoClients; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.ReplaceOptions; -import org.bson.Document; -import org.bson.codecs.configuration.CodecProvider; -import org.bson.codecs.configuration.CodecRegistry; -import org.bson.codecs.pojo.Convention; -import org.bson.codecs.pojo.Conventions; -import org.bson.codecs.pojo.PojoCodecProvider; -import org.fulib.webapp.Main; - -import javax.inject.Inject; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; - -import static org.bson.codecs.configuration.CodecRegistries.fromProviders; -import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; - -public class Mongo -{ - // =============== Constants =============== - - public static final String LOG_COLLECTION_NAME = "request-log"; - - private static final Document VERSIONS = new Document(); - - static - { - for (final Map.Entry entry : Main.VERSIONS.entrySet()) - { - String key = entry.getKey().toString(); - if ("fulib.org".equals(key)) - { - // for legacy reasons and also because "fulib.org" is not a valid Bson key - key = "webapp"; - } - VERSIONS.put(key, entry.getValue().toString()); - } - } - - // =============== Fields =============== - - private MongoClient mongoClient; - private MongoDatabase database; - - MongoCollection requestLog; - private final List conventions; - - { - this.conventions = new ArrayList<>(Conventions.DEFAULT_CONVENTIONS); - this.conventions.add(Conventions.USE_GETTERS_FOR_SETTERS); // to use get().add(...) instead of set() - } - - private final CodecProvider pojoCodecProvider = PojoCodecProvider.builder().conventions(this.conventions).build(); - - private final CodecRegistry pojoCodecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), - fromProviders(this.pojoCodecProvider)); - - // =============== Constructors =============== - - @Inject - public Mongo() - { - this(System.getenv("FULIB_MONGO_URL")); - } - - public Mongo(String url) - { - if (url == null) - { - return; - } - - final ConnectionString connString = new ConnectionString(url); - final MongoClientSettings settings = MongoClientSettings - .builder() - .applyConnectionString(connString) - .retryWrites(true) - .build(); - this.mongoClient = MongoClients.create(settings); - this.database = this.mongoClient.getDatabase(connString.getDatabase()); - this.requestLog = this.database.getCollection(LOG_COLLECTION_NAME); - } - - // =============== Methods =============== - - // --------------- Logging --------------- - - public void log(String ip, String userAgent, String request, String response) - { - if (this.requestLog == null) - { - return; - } - - final Document document = new Document(); - document.put("timestamp", new Date()); - document.put("ip", ip); - document.put("userAgent", userAgent); - document.put("versions", VERSIONS); - - document.put("request", Document.parse(request)); - document.put("response", Document.parse(response)); - - this.requestLog.insertOne(document); - } - - // --------------- Helpers --------------- - - private static void upsert(MongoCollection collection, Document doc, String idProperty) - { - upsert(collection, doc, idProperty, doc.getString(idProperty)); - } - - private static void upsert(MongoCollection collection, T doc, String idPropertyName, Object idPropertyValue) - { - collection.replaceOne(Filters.eq(idPropertyName, idPropertyValue), doc, new ReplaceOptions().upsert(true)); - } -} diff --git a/backend/src/main/java/org/fulib/webapp/tool/RunCodeGen.java b/backend/src/main/java/org/fulib/webapp/tool/RunCodeGen.java index c49ea3ae9..d8ead6561 100644 --- a/backend/src/main/java/org/fulib/webapp/tool/RunCodeGen.java +++ b/backend/src/main/java/org/fulib/webapp/tool/RunCodeGen.java @@ -1,7 +1,6 @@ package org.fulib.webapp.tool; import org.fulib.StrUtil; -import org.fulib.webapp.mongo.Mongo; import org.fulib.webapp.tool.model.CodeGenData; import org.fulib.webapp.tool.model.Diagram; import org.fulib.webapp.tool.model.Method; @@ -39,16 +38,14 @@ public class RunCodeGen // =============== Fields =============== - private final Mongo db; private final String tempDir = System.getProperty("java.io.tmpdir") + "/fulib.org/"; private final ScheduledExecutorService deleter = Executors.newScheduledThreadPool(1); // =============== Constructors =============== @Inject - public RunCodeGen(Mongo db) + public RunCodeGen() { - this.db = db; } // =============== Methods =============== @@ -71,13 +68,7 @@ public String handle(Request req, Response res) throws Exception res.type("application/json"); - final String resultBody = resultObj.toString(3); - - if (jsonObject.has("privacy") && "all".equals(jsonObject.get("privacy"))) - { - this.db.log(req.ip(), req.userAgent(), body, resultBody); - } - return resultBody; + return resultObj.toString(3); } // =============== Static Methods =============== diff --git a/backend/src/test/java/org/fulib/webapp/mongo/CollectErrors.java b/backend/src/test/java/org/fulib/webapp/mongo/CollectErrors.java deleted file mode 100644 index e3aae900d..000000000 --- a/backend/src/test/java/org/fulib/webapp/mongo/CollectErrors.java +++ /dev/null @@ -1,75 +0,0 @@ -package org.fulib.webapp.mongo; - -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.Projections; -import org.bson.Document; -import org.bson.conversions.Bson; - -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.Comparator; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class CollectErrors -{ - /** - * Counts all fulibScenario compiler error messages (like [foo.bar.baz]) from response outputs in the request log. - */ - public static void main(String[] args) - { - final String mongoUrl = args[0]; - final LocalDate start = LocalDate.parse(args[1]); - final LocalDateTime end = LocalDateTime.parse(args[2]); - - final Pattern pattern = Pattern.compile("\\[[\\w.]+]"); - final Map counts = new HashMap<>(); - - final Bson filter = Filters.and(Filters.gte("timestamp", start), Filters.lt("timestamp", end)); - - int totalCount = 0; - int totalFound = 0; - - for (final Document document : new Mongo(mongoUrl).requestLog - .find(filter) - .projection(Projections.include("response.output"))) - { - totalCount++; - - final Document response = document.get("response", Document.class); - final String output = response.getString("output"); - if (output == null) - { - continue; - } - - boolean found = false; - - final Matcher matcher = pattern.matcher(output); - while (matcher.find()) - { - final String match = matcher.group(); - counts.computeIfAbsent(match, k -> new AtomicInteger()).getAndIncrement(); - - found = true; - } - - if (found) { - totalFound++; - } - } - - counts.entrySet() - .stream() - .sorted(Map.Entry.comparingByValue(Comparator.comparing(AtomicInteger::longValue))) - .forEach(entry -> { - System.out.printf("%3d * %s%n", entry.getValue().get(), entry.getKey()); - }); - - System.out.println(totalCount + " total requests"); - System.out.println(totalFound + " requests with at least one marker"); - } -} diff --git a/backend/src/test/java/org/fulib/webapp/tool/RunCodeGenTest.java b/backend/src/test/java/org/fulib/webapp/tool/RunCodeGenTest.java index 7f66c6402..ab518025d 100644 --- a/backend/src/test/java/org/fulib/webapp/tool/RunCodeGenTest.java +++ b/backend/src/test/java/org/fulib/webapp/tool/RunCodeGenTest.java @@ -1,7 +1,6 @@ package org.fulib.webapp.tool; import org.fulib.StrUtil; -import org.fulib.webapp.mongo.Mongo; import org.fulib.webapp.tool.model.Result; import org.hamcrest.CoreMatchers; import org.json.JSONObject; @@ -26,8 +25,7 @@ public class RunCodeGenTest @Test public void handle() throws Exception { - final Mongo db = mock(Mongo.class); - final RunCodeGen codeGen = new RunCodeGen(db); + final RunCodeGen codeGen = new RunCodeGen(); final Request request = mock(Request.class); final String ip = "0.0.0.0"; @@ -46,7 +44,6 @@ public void handle() throws Exception final String responseBody = codeGen.handle(request, response); verify(response).type("application/json"); - verify(db).log(ip, userAgent, requestBody, responseBody); final JSONObject responseObj = new JSONObject(responseBody); assertThat(responseObj.getString("output"), equalTo(""));