Skip to content

Commit

Permalink
feat(sql-checker): make distance disableable
Browse files Browse the repository at this point in the history
  • Loading branch information
Zitrone44 committed Dec 11, 2024
1 parent 3957561 commit 403d713
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class CheckerConfigurationController {
case (Some(checkerType), Some(ord), Some(checkerTypeInformation)) =>
(checkerTypeInformation.retrive("showHints").asBool(),
checkerTypeInformation.retrive("showHintsAt").asInt(), checkerTypeInformation.retrive("showExtendedHints").asBool(),
checkerTypeInformation.retrive("showExtendedHintsAt").asInt()) match {
case (Some(showHints), Some(showHintsAt), Some(showExtendedHints), Some(showExtendedHintsAt)) =>
checkerTypeInformation.retrive("showExtendedHintsAt").asInt(), checkerTypeInformation.retrive("disableDistance").asBool()) match {
case (Some(showHints), Some(showHintsAt), Some(showExtendedHints), Some(showExtendedHintsAt), Some(disableDistance)) =>
val cc = CheckrunnerConfiguration(checkerType, ord, checkerTypeInformation =
Some(SqlCheckerInformation("", showHints, showHintsAt, showExtendedHints, showExtendedHintsAt)))
Some(SqlCheckerInformation("", showHints, showHintsAt, showExtendedHints, showExtendedHintsAt, disableDistance)))
notifyChecker(tid, cc)
val ccc = this.ccs.create(cid, tid, cc)
notifyChecker(tid, ccc)
Expand Down Expand Up @@ -139,10 +139,10 @@ class CheckerConfigurationController {
case (Some(checkerType), Some(ord), Some(checkerTypeInformation)) =>
(checkerTypeInformation.retrive("showHints").asBool(),
checkerTypeInformation.retrive("showHintsAt").asInt(), checkerTypeInformation.retrive("showExtendedHints").asBool(),
checkerTypeInformation.retrive("showExtendedHintsAt").asInt()) match {
case (Some(showHints), Some(showHintsAt), Some(showExtendedHints), Some(showExtendedHintsAt)) =>
checkerTypeInformation.retrive("showExtendedHintsAt").asInt(), checkerTypeInformation.retrive("disableDistance").asBool()) match {
case (Some(showHints), Some(showHintsAt), Some(showExtendedHints), Some(showExtendedHintsAt), Some(disableDistance)) =>
val cc = CheckrunnerConfiguration(checkerType, ord, checkerTypeInformation =
Some(SqlCheckerInformation("", showHints, showHintsAt, showExtendedHints, showExtendedHintsAt)))
Some(SqlCheckerInformation("", showHints, showHintsAt, showExtendedHints, showExtendedHintsAt, disableDistance)))
notifyChecker(tid, cc)
this.ccs.update(cid, tid, ccid, cc)
case _ => throw new BadRequestException("Malformed checker type information")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object CheckerTypeInformation {
val obj = new JSONObject(json)
obj.getString("type") match {
case "sqlCheckerInformation" => SqlCheckerInformation (obj.getString ("solution"), obj.getBoolean ("showHints"),
obj.getInt ("showHintsAt"), obj.getBoolean ("showExtendedHints"), obj.getInt ("showExtendedHintsAt") )
obj.getInt ("showHintsAt"), obj.getBoolean ("showExtendedHints"), obj.getInt ("showExtendedHintsAt"), obj.getBoolean("disableDistance") )
case _ => throw new IllegalArgumentException()
}
}
Expand All @@ -32,6 +32,7 @@ object CheckerTypeInformation {
.put("showHintsAt", sobj.showHintsAt)
.put("showExtendedHints", sobj.showExtendedHints)
.put("showExtendedHintsAt", sobj.showExtendedHintsAt)
.put("disableDistance", sobj.disableDistance)
.toString
case _ =>
throw new IllegalArgumentException()
Expand All @@ -57,5 +58,6 @@ case class SqlCheckerInformation(
showHints: Boolean,
showHintsAt: Int,
showExtendedHints: Boolean,
showExtendedHintsAt: Int
showExtendedHintsAt: Int,
disableDistance: Boolean,
) extends CheckerTypeInformation
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class SqlCheckerRemoteCheckerService(@Value("${services.masterRunner.insecure}")
formatLegacy(hints, query)
}
}
if (query.distance.isPresent) {
if (!sci.disableDistance && query.distance.isPresent) {
val steps = Math.round(query.distance.get / 50)
if (steps == 0) {
hints ++= "Du bist ganz nah an der Lösung, es sind nur noch kleine Änderung notwendig.\n"
Expand Down Expand Up @@ -192,7 +192,7 @@ class SqlCheckerRemoteCheckerService(@Value("${services.masterRunner.insecure}")
val passed = submission.results.headOption.exists(result => result.exitCode == 0)
new ObjectMapper().createObjectNode()
.put("passed", passed)
.put("isSol", false)
.put("isSol", !checker.checkerTypeInformation.get.asInstanceOf[SqlCheckerInformation].disableDistance)
.put("userId", submission.userID.get)
.put("cid", task.courseID)
.put("tid", checker.taskId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ <h4>
</mat-form-field>
</div>
</div>
<div>
<mat-checkbox
formControlName="disableDistance"
value="false"
(change)="showHintsEvent(checkerForm.value)"
>{{
"dialog.checker.new.disable-distance" | i18nextEager
}}</mat-checkbox
>
</div>
</div>
</form>
<mat-dialog-actions class="actions">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class NewCheckerDialogComponent implements OnInit {
showHintsAt: new UntypedFormControl(0),
showExtendedHints: new UntypedFormControl(false),
showExtendedHintsAt: new UntypedFormControl(0),
disableDistance: new UntypedFormControl(false),
});
choosedSQLChecker;
mainFile: File[] = [];
Expand All @@ -38,13 +39,15 @@ export class NewCheckerDialogComponent implements OnInit {
showExtendedHintsAt: 0,
showHints: false,
showHintsAt: 0,
disableDistance: false,
},
checkerType: "",
ord: 0,
};
checkerCount: Observable<CheckerConfig[]> = of();
showHintsConfig;
showExtendedHintsConfig;
disableDistance;

constructor(
public dialogRef: MatDialogRef<NewCheckerDialogComponent>,
Expand Down Expand Up @@ -77,6 +80,9 @@ export class NewCheckerDialogComponent implements OnInit {
this.checkerForm.controls["showHintsAt"].setValue(
this.checker.checkerTypeInformation.showHintsAt
);
this.checkerForm.controls["disableDistance"].setValue(
this.checker.checkerTypeInformation.disableDistance
);
}

if (this.checker.mainFileUploaded || this.checker.secondaryFileUploaded) {
Expand Down Expand Up @@ -146,6 +152,7 @@ export class NewCheckerDialogComponent implements OnInit {
this.checker.checkerType = value.checkerType;
this.checker.checkerTypeInformation.showHints = value.showHints;
this.checker.checkerTypeInformation.showHintsAt = value.showHintsAt;
this.checker.checkerTypeInformation.disableDistance = value.disableDistance;
this.checker.checkerTypeInformation.showExtendedHints =
value.showExtendedHints;
this.checker.checkerTypeInformation.showExtendedHintsAt =
Expand Down Expand Up @@ -211,6 +218,7 @@ export class NewCheckerDialogComponent implements OnInit {
this.checker.checkerType = value.checkerType;
this.checker.checkerTypeInformation.showHints = value.showHints;
this.checker.checkerTypeInformation.showHintsAt = value.showHintsAt;
this.checker.checkerTypeInformation.disableDistance = value.disableDistance;
this.checker.checkerTypeInformation.showExtendedHints =
value.showExtendedHints;
this.checker.checkerTypeInformation.showExtendedHintsAt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class TaskNewDialogComponent implements OnInit {
showExtendedHintsAt: 0,
showHints: false,
showHintsAt: 0,
disableDistance: false,
},
};
const infoFile = new File(
Expand Down
1 change: 1 addition & 0 deletions modules/fbs-core/web/src/app/model/CheckerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ export interface CheckerConfig {
showHintsAt: number;
showExtendedHints: boolean;
showExtendedHintsAt: number;
disableDistance: boolean;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@
(update)="updateSubmissionContent($event)"
></app-highlighted-input>
</div>
<p
<div
class="error-message"
*ngIf="activeTab.error && activeTab.isSubmitMode"
>
<strong>{{
"sql-playground.input.label.errorMessage" | i18nextEager
}}</strong>
{{ activeTab.errorMsg }}
</p>
<pre>{{ activeTab.errorMsg }}</pre>
</div>
</mat-tab>
</ng-container>
</mat-tab-group>
Expand Down
6 changes: 4 additions & 2 deletions modules/fbs-sql-checker/api/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Result:
statement: str = ""
time: str = ""
passed: bool = False
is_solution: bool = False
parsable: bool = False
closest_solution: str = ""
min_distance: int = 0
Expand All @@ -92,6 +93,7 @@ def from_submission(cls, submission: Submission) -> Self:
attempt=submission.attempt,
statement=submission.submission,
time=datetime.now(UTC).isoformat(),
is_solution=submission.is_solution and submission.passed,
)

@classmethod
Expand Down Expand Up @@ -123,6 +125,7 @@ def to_db_dict(self):
"statement": self.statement,
"passed": self.passed,
"parsable": self.parsable,
"isSolution": self.is_solution,
"time": self.time,
"usedSolutionId": self.closest_solution,
"distance": self.min_distance,
Expand All @@ -134,7 +137,6 @@ def to_db_dict(self):
@typechecked
class LegacyResult(Result):
task_nr: int = 0
is_sol: bool = False
tables_right: bool = False
sel_attributes_right: bool = False
pro_attributes_right: bool = False
Expand All @@ -154,7 +156,7 @@ def to_db_dict(self):
"statement": self.statement,
"queryRight": self.passed,
"parsable": self.parsable,
"isSolution": self.is_sol,
"isSolution": self.is_solution,
"tablesRight": self.tables_right,
"selAttributesRight": self.sel_attributes_right,
"proAttributesRight": self.pro_attributes_right,
Expand Down
9 changes: 6 additions & 3 deletions modules/fbs-sql-checker/api/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# main.py

"""System module."""
import json
import sys # pylint: disable=W0611

import requests # pylint: disable=W0611
Expand All @@ -10,7 +11,7 @@
from api.distance.distance_calc import GetDistanceCalculator
from api.query_processor import QueryProcessorV2
from api.datatypes import Submission
from api.solution_fetcher import NearestSolutionFetcher
from api.solution_fetcher import NearestSolutionFetcher, FirstSolutionFetcher

# The following Code is for productive purposes

Expand All @@ -21,12 +22,14 @@
"zu analysierende JSON aufgerufen werden soll."
)
else:
answer = requests.get(sys.argv[1], verify=False, timeout=60)
submission = Submission.from_dict(answer.json())
storage = MongoDataStore.connect(sys.argv[2])
qp = QueryProcessorV2(
SqlparseComparator(),
NearestSolutionFetcher(storage, GetDistanceCalculator()),
)
answer = requests.get(sys.argv[1], verify=False, timeout=60)
result = qp.process(Submission.from_dict(answer.json()))
result = qp.process(submission)

print("storing", result.to_db_dict())
storage.store("Queries", result.to_db_dict())
37 changes: 17 additions & 20 deletions modules/fbs-sql-checker/api/query_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,24 @@ def __init__(

def process(self, submission: Submission) -> Result:
result = ResultV2.from_submission(submission)
if not (submission.is_solution or submission.passed):
solution, distance = self._solution_fetcher.fetch_solution(
submission.task_id, submission
solution, distance = self._solution_fetcher.fetch_solution(
submission.task_id, submission
)
if solution is None and result.is_solution:
sqlparse.parse(submission.submission)
result.passed = True
result.parsable = True
return result
try:
result.errors = self._comparator.compare(
solution.statement, submission.submission
)
try:
result.errors = self._comparator.compare(
solution.statement, submission.submission
)
result.passed = len(result.errors) == 0
result.closest_solution = solution.uuid
result.min_distance = distance
result.parsable = True
except Exception as e:
logger.exception(e)
else:
try:
sqlparse.parse(submission.submission)
result.parsable = True
except Exception as e:
pass
result.passed = result.parsable
result.passed = len(result.errors) == 0
result.closest_solution = solution.uuid
result.min_distance = distance
result.parsable = True
except Exception as e:
logger.exception(e)

return result

Expand Down
15 changes: 8 additions & 7 deletions modules/fbs-sql-checker/api/solution_fetcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
from abc import ABCMeta, abstractmethod
from typing import Tuple
from typing import Tuple, Optional

from api.data_store import DataStore
from api.distance.distance_calc import DistanceCalculator
Expand All @@ -11,7 +11,7 @@ class SolutionFetcher(metaclass=ABCMeta):
@abstractmethod
def fetch_solution(
self, task_id: int, submission: Submission
) -> Tuple[Result, float]:
) -> Tuple[Optional[Result], float]:
pass


Expand All @@ -21,11 +21,12 @@ def __init__(self, datastore: DataStore):

def fetch_solution(
self, task_id: int, submission: Submission
) -> Tuple[Result, float]:
) -> Tuple[Optional[Result], float]:
solution = self._datastore.query("Queries", {"taskId": task_id, "isSolution": True})
if len(solution) == 0:
return None, 0.0
return (
Result.from_db_dict(
self._datastore.query("Queries", {"taskId": task_id, "passed": True})
),
Result.from_db_dict(solution[0]),
1.0,
)

Expand All @@ -39,7 +40,7 @@ def fetch_solution(
self, task_id: int, submission: Submission
) -> Tuple[Result, float]:
solutions = self._datastore.query(
"Queries", {"taskId": task_id, "passed": True}
"Queries", {"taskId": task_id, "isSolution": True}
)
solutions = [Result.from_db_dict(solution) for solution in solutions]
min_distance = math.inf
Expand Down

0 comments on commit 403d713

Please sign in to comment.