Skip to content

Commit e233e4f

Browse files
committed
Track Format Preview usage
Track the usage of Format Preview, but only one DB entry per page unlike WordCheck where we track every usage along with any suggestions.
1 parent 84b139f commit e233e4f

21 files changed

Lines changed: 338 additions & 15 deletions

SETUP/ARCHIVING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
After projects are completed, they can be archived. This includes moving the
44
project files into a separate archive directory, moving the project tables into
5-
a separate archive database, and moving a project's page_events and
6-
wordcheck_events into the archive database.
5+
a separate archive database, and moving a project's `page_events`,
6+
`wordcheck_events`, and `format_preview_events` into the archive database.
77

88
The database instructions in this file resemble those for the main database. See
99
[INSTALL.md](INSTALL.md) for more information on how to run database commands.
@@ -31,8 +31,8 @@ project directory.
3131
GRANT ALL ON dp_archive.* TO dp_user@localhost IDENTIFIED BY 'dp_password';
3232
```
3333

34-
4. Create the `page_events` and `wordcheck_events` table in the archive
35-
database. Find the 'CREATE TABLE' commands for those 2 tables in
34+
4. Create the `page_events`, `wordcheck_events`, and `format_preview_events` tables
35+
in the archive database. Find the 'CREATE TABLE' commands for those 2 tables in
3636
`db_schema.sql`. These commands create those tables in the main database;
3737
the corresponding tables in the archive database have exactly the same
3838
structure. So to create them, make the archive db the current database

SETUP/db_schema.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ CREATE TABLE `charsuites` (
5555
INSERT INTO charsuites
5656
SET name='basic-latin';
5757

58+
--
59+
-- Table structure for table `format_preview_events`
60+
--
61+
62+
CREATE TABLE `format_preview_events` (
63+
`event_id` int unsigned NOT NULL AUTO_INCREMENT,
64+
`projectid` varchar(22) NOT NULL,
65+
`image` varchar(12) NOT NULL,
66+
`round_id` char(2) NOT NULL,
67+
`username` varchar(25) NOT NULL,
68+
`timestamp` int unsigned NOT NULL,
69+
PRIMARY KEY (`event_id`),
70+
UNIQUE KEY `pc_compound` (`projectid`,`image`,`round_id`,`username`)
71+
);
72+
5873
--
5974
-- Table structure for table `image_sources`
6075
--

SETUP/site_admin_notes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ to allow a three-character round-id, you'd need to do:
2121
ALTER TABLE page_events MODIFY COLUMN round_id CHAR(3);
2222
ALTER TABLE queue_defns MODIFY COLUMN round_id CHAR(3);
2323
ALTER TABLE wordcheck_events MODIFY COLUMN round_id CHAR(3);
24+
ALTER TABLE format_preview_events MODIFY COLUMN round_id CHAR(3);
2425
ALTER TABLE best_tally_rank MODIFY COLUMN tally_name CHAR(3);
2526
ALTER TABLE current_tallies MODIFY COLUMN tally_name CHAR(3);
2627
ALTER TABLE past_tallies MODIFY COLUMN tally_name CHAR(3);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
$relPath = '../../../pinc/';
3+
include_once($relPath.'base.inc');
4+
5+
header('Content-type: text/plain');
6+
7+
echo "Creating format_preview_events...\n";
8+
$sql = "
9+
CREATE TABLE `format_preview_events` (
10+
`event_id` int unsigned NOT NULL AUTO_INCREMENT,
11+
`projectid` varchar(22) NOT NULL,
12+
`image` varchar(12) NOT NULL,
13+
`round_id` char(2) NOT NULL,
14+
`username` varchar(25) NOT NULL,
15+
`timestamp` int unsigned NOT NULL,
16+
PRIMARY KEY (`event_id`),
17+
UNIQUE KEY `pc_compound` (`projectid`,`image`,`round_id`,`username`)
18+
)
19+
";
20+
21+
mysqli_query(DPDatabase::get_connection(), $sql) or die(mysqli_error(DPDatabase::get_connection()));
22+
23+
echo "\nDone!\n";

api/dp-openapi.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,11 +1210,35 @@ paths:
12101210
default:
12111211
$ref: '#/components/responses/UnexpectedError'
12121212

1213+
/projects/{projectid}/pages/{pagename}/formatpreview:
1214+
put:
1215+
tags:
1216+
- project
1217+
description: Report Format Preview usage
1218+
parameters:
1219+
- $ref: '#/components/parameters/projectid'
1220+
- $ref: '#/components/parameters/pagename'
1221+
responses:
1222+
200:
1223+
description: OK
1224+
'400':
1225+
$ref: '#/components/responses/InvalidValue'
1226+
'401':
1227+
$ref: '#/components/responses/Unauthorized'
1228+
'403':
1229+
$ref: '#/components/responses/Forbidden'
1230+
'404':
1231+
$ref: '#/components/responses/NotFound'
1232+
'429':
1233+
$ref: '#/components/responses/RateLimitExceeded'
1234+
default:
1235+
$ref: '#/components/responses/UnexpectedError'
1236+
12131237
/projects/{projectid}/pages/{pagename}/wordcheck:
12141238
put:
12151239
tags:
12161240
- project
1217-
description: Report wordcheck reults
1241+
description: Report WordCheck usage and results
12181242
parameters:
12191243
- $ref: '#/components/parameters/projectid'
12201244
- $ref: '#/components/parameters/pagename'

api/v1.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ $router->add_route("GET", "v1/projects/:projectid/pagedetails", "api_v1_project_
4848
$router->add_route("GET", "v1/projects/:projectid/pages", "api_v1_project_pages");
4949
$router->add_route("PUT", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page");
5050
$router->add_route("GET", "v1/projects/:projectid/pages/:pagename", "api_v1_project_page");
51+
$router->add_route("PUT", "v1/projects/:projectid/pages/:pagename/formatpreview", "api_v1_project_page_format_preview");
5152
$router->add_route("PUT", "v1/projects/:projectid/pages/:pagename/reportbad", "api_v1_project_page_report_bad");
5253
$router->add_route("PUT", "v1/projects/:projectid/pages/:pagename/wordcheck", "api_v1_project_page_wordcheck");
5354
$router->add_route("GET", "v1/projects/:projectid/pages/:pagename/pagerounds/:pageroundid", "api_v1_project_page_round");

api/v1_projects.inc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,24 @@ function api_v1_project_page(string $method, array $data, array $query_params)
943943
}
944944
}
945945

946+
/** @param array<string,string|string[]> $query_params */
947+
function api_v1_project_page_format_preview(string $method, array $data, array $query_params): void
948+
{
949+
try {
950+
$project = $data[":projectid"];
951+
$proof_project = new ProofProject($project);
952+
953+
$project_page = $data[":pagename"];
954+
$proof_project_page = new ProofProjectPage($proof_project, $project_page);
955+
956+
$proof_project_page->fp_report();
957+
} catch (ProjectException $exception) {
958+
throw new NotFoundError($exception->getMessage(), $exception->getCode());
959+
} catch (UserAccessException $exception) {
960+
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
961+
}
962+
}
963+
946964
/** @param array<string, string|string[]> $query_params */
947965
function api_v1_project_page_report_bad(string $method, array $data, array $query_params): void
948966
{

crontab/PrunePageData.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ class PrunePageData extends BackgroundJob
4444
);
4545
DPDatabase::query($sql);
4646

47+
// then format_preview_events
48+
$sql = sprintf(
49+
"
50+
DELETE FROM $database.format_preview_events
51+
WHERE projectid = '%s'
52+
",
53+
DPDatabase::escape($project_id)
54+
);
55+
DPDatabase::query($sql);
56+
4757
// then page_events
4858
$sql = sprintf(
4959
"

pinc/FormatPreview.inc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
class FormatPreview
4+
{
5+
/**
6+
* Save a Format Preview event.
7+
*/
8+
public static function save_event(string $projectid, string $round, string $page, string $proofer): void
9+
{
10+
$setters = join(", ", [
11+
set_col_str("projectid", $projectid),
12+
set_col_str("image", $page),
13+
set_col_str("round_id", $round),
14+
set_col_str("username", $proofer),
15+
set_col_num("timestamp", time()),
16+
]);
17+
$sql = sprintf(
18+
"
19+
INSERT INTO format_preview_events
20+
SET $setters
21+
ON DUPLICATE KEY UPDATE
22+
timestamp = %d
23+
",
24+
time()
25+
);
26+
DPDatabase::query($sql);
27+
}
28+
29+
/**
30+
* Delete any format_preview_events for this project.
31+
*
32+
* This is called when deleting a project.
33+
*/
34+
public static function delete_project_events(string $projectid): void
35+
{
36+
$sql = sprintf(
37+
"
38+
DELETE FROM format_preview_events
39+
WHERE projectid='%s'
40+
",
41+
DPDatabase::escape($projectid)
42+
);
43+
DPDatabase::query($sql);
44+
}
45+
46+
/**
47+
* Copy format_preview_events from one project to another.
48+
*
49+
* This is called when copying pages into a project.
50+
*
51+
* @param string[] $images
52+
*/
53+
public static function copy_project_events(string $src_projectid, string $dest_projectid, array $images = []): void
54+
{
55+
if ($images) {
56+
$images_where = " AND image in (" . surround_and_join(array_map("DPDatabase::escape", $images), '"', '"', ",") . ")";
57+
} else {
58+
$images_where = "";
59+
}
60+
61+
$sql = sprintf(
62+
"
63+
INSERT INTO format_preview_events
64+
(projectid, image, round_id, username, timestamp)
65+
SELECT '%s', image, round_id, username, timestamp
66+
FROM format_preview_events
67+
WHERE projectid = '%s' $images_where
68+
",
69+
DPDatabase::escape($dest_projectid),
70+
DPDatabase::escape($src_projectid)
71+
);
72+
DPDatabase::query($sql);
73+
}
74+
}

pinc/Project.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ class Project
665665
}
666666

667667
delete_project_wordcheck_events($this->projectid);
668+
FormatPreview::delete_project_events($this->projectid);
668669

669670
// Formerly, if the project was in the 'New' state, we would here
670671
// delete it from the projects table and log a 'deletion' event.

0 commit comments

Comments
 (0)