Skip to content

Commit bc56eae

Browse files
committed
precompute template result
1 parent f551c6a commit bc56eae

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

microservices/playground-ws/scripts/init.sql

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ CREATE TABLE templates (
22
id VARCHAR(255) PRIMARY KEY,
33
title VARCHAR(255),
44
description TEXT,
5-
program TEXT
5+
program TEXT,
6+
result VARCHAR(255)
67
);
78

8-
INSERT INTO templates VALUES('variableBinding', 'Variable binding', 'How variable binding works in Monkey language', '');
9+
INSERT INTO templates VALUES('variableBinding', 'Variable binding', 'How variable binding works in Monkey language', '', '15');
910
UPDATE templates
1011
SET program = (
1112
SELECT program
@@ -16,7 +17,7 @@ SET program = (
1617
)
1718
WHERE id = 'variableBinding';
1819

19-
INSERT INTO templates VALUES('controlFlow', 'Control flow', 'Control the execution flow of a Monkey program', '');
20+
INSERT INTO templates VALUES('controlFlow', 'Control flow', 'Control the execution flow of a Monkey program', '', 'false');
2021
UPDATE templates
2122
SET program = (
2223
SELECT program
@@ -27,7 +28,7 @@ SET program = (
2728
)
2829
WHERE id = 'controlFlow';
2930

30-
INSERT INTO templates VALUES('arrayBuiltinFn', 'Arrays and built in functions', 'Arrays data structure as well as built in functions to work with arrays', '');
31+
INSERT INTO templates VALUES('arrayBuiltinFn', 'Arrays and built in functions', 'Arrays data structure as well as built in functions to work with arrays', '', '[4, false, 1, 4, [99, 98, 97], 4, 25]');
3132
UPDATE templates
3233
SET program = (
3334
SELECT program
@@ -38,7 +39,7 @@ SET program = (
3839
)
3940
WHERE id = 'arrayBuiltinFn';
4041

41-
INSERT INTO templates VALUES('dictionary', 'Dictionary data structure', 'Working with dictionaries', '');
42+
INSERT INTO templates VALUES('dictionary', 'Dictionary data structure', 'Working with dictionaries', '', '20');
4243
UPDATE templates
4344
SET program = (
4445
SELECT program
@@ -49,7 +50,7 @@ SET program = (
4950
)
5051
WHERE id = 'dictionary';
5152

52-
INSERT INTO templates VALUES('closures', 'Closures', 'Closures in Monkey language', '');
53+
INSERT INTO templates VALUES('closures', 'Closures', 'Closures in Monkey language', '', '29');
5354
UPDATE templates
5455
SET program = (
5556
SELECT program

microservices/playground-ws/src/main/java/org/playground/ws/dao/TemplateDao.java

+11-9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ public class TemplateDao implements Serializable {
2424
@Column(name = "program")
2525
String program;
2626

27-
public static TemplateDao of(String title, String description, String program) {
27+
@Column(name="result")
28+
String result;
29+
30+
public static TemplateDao of(final String title, final String description, final String program, final String result) {
2831
TemplateDao template = new TemplateDao();
2932
template.setTitle(title);
3033
template.setDescription(description);
3134
template.setProgram(program);
35+
template.setResult(result);
3236

3337
return template;
3438
}
@@ -65,13 +69,11 @@ public void setProgram(String program) {
6569
this.program = program;
6670
}
6771

68-
@Override
69-
public String toString() {
70-
return "TemplateDao{" +
71-
"id='" + id + '\'' +
72-
", title='" + title + '\'' +
73-
", description='" + description + '\'' +
74-
", program=" + program +
75-
'}';
72+
public String getResult() {
73+
return result;
74+
}
75+
76+
public void setResult(String result) {
77+
this.result = result;
7678
}
7779
}

microservices/playground-ws/src/main/java/org/playground/ws/dto/PlaygroundDto.java

+4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public ArrayList<PlaygroundHistoryDto> getHistory() {
3939
return history;
4040
}
4141

42+
public void setHistory(ArrayList<PlaygroundHistoryDto> history) {
43+
this.history = history;
44+
}
45+
4246
public void addHistoryResult(final PlaygroundHistoryDto result) {
4347
this.history.add(result);
4448
}

microservices/playground-ws/src/main/java/org/playground/ws/factory/PlaygroundFactory.java

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public PlaygroundDto getPlayground(final CreatePlaygroundDto createPlaygroundDto
5252
// if the user has selected a template, init the playground with the corresponding code
5353
if (templateDao.isPresent()) {
5454
playground.setProgram(templateDao.get().getProgram());
55+
final ArrayList<PlaygroundHistoryDto> playgroundHistory = new ArrayList<>();
56+
playgroundHistory.add(new PlaygroundHistoryDto(LocalDateTime.now(), templateDao.get().getResult()));
57+
playground.setHistory(playgroundHistory);
5558
}
5659

5760
// save the object in the cache, so we can start storing eval results

microservices/playground-ws/src/test/java/org/playground/ws/factory/PlaygroundFactoryTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testNewPlaygroundFromTemplate() {
7070

7171
final String MOCKED_PROGRAM = "let a = 1; a;";
7272
final Optional<TemplateDao> mockedFindByResponse = Optional.of(
73-
TemplateDao.of("Mocked title", "Mocked description", MOCKED_PROGRAM)
73+
TemplateDao.of("Mocked title", "Mocked description", MOCKED_PROGRAM, "1")
7474
);
7575
doReturn(mockedFindByResponse).when(this.templateRepository).findById(any());
7676
final PlaygroundDto playground = this.playgroundFactory

web/playground/src/screens/playground/Playground.js

-5
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ function Playground() {
3939
const { program = "", history = [] } = await getPlaygroundHistory(playgroundId);
4040
setProgram(program);
4141
setHistory(history);
42-
43-
if (program && !history.length) {
44-
// TODO: This should already come in the history array. We shouldn't need an extra computation
45-
handleEditorChanged(program);
46-
}
4742
} catch (error) {
4843
setPlaygroundNotFound(true);
4944
return;

0 commit comments

Comments
 (0)