Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce number of Redmine API calls from backend #1010

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions backend/internal/database/priorityEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@ package database

import "fmt"

// The PriorityEntry type is a simple struct that encapsulates ai
// "priority entry", i.e. a Redmine issue ID and a Redmine activity ID
// The PriorityEntry type is a simple struct that encapsulates a
// "priority entry", i.e. a Redmine issue ID and subject with its
// corresponding Redmine project ID, and a Redmine activity ID and name
// together with a custom name to which a user has assigned the meaning
// "favorite" or "hidden" using the UI.
type PriorityEntry struct {
RedmineIssueId int
RedmineActivityId int
Name string
IsHidden bool
RedmineProjectId int
RedmineIssueId int
RedmineIssueSubject string
RedmineActivityId int
RedmineActivityName string
Name string
IsHidden bool
}

// GetAllUserPrioityEntries() returns a list of priority entries for
// a particular user.
func (db *Database) GetAllUserPrioityEntries(redmineUserId int) ([]PriorityEntry, error) {
selectStmt := `
SELECT
redmine_project_id,
redmine_issue_id,
redmine_issue_subject,
redmine_activity_id,
redmine_activity_name,
name,
is_hidden
FROM priority_entry
Expand All @@ -43,8 +50,11 @@ func (db *Database) GetAllUserPrioityEntries(redmineUserId int) ([]PriorityEntry
for rows.Next() {
var priorityEntry PriorityEntry

if err := rows.Scan(&priorityEntry.RedmineIssueId,
if err := rows.Scan(&priorityEntry.RedmineProjectId,
&priorityEntry.RedmineIssueId,
&priorityEntry.RedmineIssueSubject,
&priorityEntry.RedmineActivityId,
&priorityEntry.RedmineActivityName,
&priorityEntry.Name,
&priorityEntry.IsHidden); err != nil {
return nil, fmt.Errorf("sql.Scan() failed: %w", err)
Expand All @@ -59,8 +69,8 @@ func (db *Database) GetAllUserPrioityEntries(redmineUserId int) ([]PriorityEntry
return priorityEntries, nil
}

// SetAllUserPriorityEntries() replaces all stored priority entries for the given
// user by the ones provided in the list to this function.
// SetAllUserPriorityEntries() replaces all stored priority entries for
// the given user by the ones provided in the list to this function.
func (db *Database) SetAllUserPriorityEntries(redmineUserId int, favorites []PriorityEntry) error {
tx, err := db.handle().Begin()
if err != nil {
Expand Down Expand Up @@ -95,12 +105,15 @@ func (db *Database) SetAllUserPriorityEntries(redmineUserId int, favorites []Pri
insertStmt := `
INSERT INTO priority_entry (
redmine_user_id,
redmine_project_id,
redmine_issue_id,
redmine_issue_subject,
redmine_activity_id,
redmine_activity_name,
name,
is_hidden,
priority )
VALUES (?, ?, ?, ?, ?, ?)`
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`

stmt, err = tx.Prepare(insertStmt)
if err != nil {
Expand All @@ -110,8 +123,11 @@ func (db *Database) SetAllUserPriorityEntries(redmineUserId int, favorites []Pri

for priority, priorityEntry := range favorites {
if _, err := stmt.Exec(redmineUserId,
priorityEntry.RedmineProjectId,
priorityEntry.RedmineIssueId,
priorityEntry.RedmineIssueSubject,
priorityEntry.RedmineActivityId,
priorityEntry.RedmineActivityName,
priorityEntry.Name,
priorityEntry.IsHidden,
priority); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion backend/sql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- The relational database schema for Urdr, storing the data that is not
-- kept by Redmine by default but that we need to be persistent between
-- sessions.
-- sessions. We also store some data here for efficiency reasons.
--
-- We assume that this file is used to initialize a SQLite database.
-- The Go code does thes automatically, but you may also do so from the
Expand Down Expand Up @@ -64,12 +64,21 @@ CREATE TABLE user_setting (
-- a hidden entry. We also store a sorting priority, which determines
-- the relative positioning in the user interface (it's essentially a
-- sorting key).
--
-- For reasons of efficiency, we additionally store the Redmine project
-- ID and name, the Redmine issue subject, and the Redmine activity
-- name. This is not strictly necessary, but it makes it easier to
-- display the data in the user interface without having to query the
-- Redmine API for each entry.

DROP TABLE IF EXISTS priority_entry;
CREATE TABLE priority_entry (
redmine_user_id INTEGER NOT NULL,
redmine_project_id INTEGER NOT NULL,
redmine_issue_id INTEGER NOT NULL,
redmine_issue_subject TEXT,
redmine_activity_id INTEGER NOT NULL,
redmine_activity_name TEXT,
name TEXT,
is_hidden BOOLEAN,
priority INTEGER NOT NULL,
Expand Down
Loading