cacert-boardvoting/internal/models/motions.go
Jan Dittberner ff93acb65c Refactorings
- fix typo in nav.html and template functions
- implement template cache and render function
- refactor motion list methods to reduce cyclomatic complexity
2022-05-21 20:49:35 +02:00

628 lines
14 KiB
Go

/*
Copyright 2017-2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package models
import (
"context"
"database/sql"
"errors"
"fmt"
"log"
"time"
"github.com/jmoiron/sqlx"
)
type VoteType uint8
const unknownVariant = "unknown"
const (
VoteTypeMotion VoteType = 0
VoteTypeVeto VoteType = 1
)
var voteTypeLabels = map[VoteType]string{
VoteTypeMotion: "motion",
VoteTypeVeto: "veto",
}
func (v VoteType) String() string {
if label, ok := voteTypeLabels[v]; ok {
return label
}
return unknownVariant
}
func (v VoteType) QuorumAndMajority() (int, float32) {
const (
majorityDefault = 0.99
majorityMotion = 0.50
quorumDefault = 1
quorumMotion = 3
)
if v == VoteTypeMotion {
return quorumMotion, majorityMotion
}
return quorumDefault, majorityDefault
}
type VoteStatus int8
const (
voteStatusDeclined VoteStatus = -1
voteStatusPending VoteStatus = 0
voteStatusApproved VoteStatus = 1
voteStatusWithdrawn VoteStatus = -2
)
var voteStatusLabels = map[VoteStatus]string{
voteStatusDeclined: "declined",
voteStatusPending: "pending",
voteStatusApproved: "approved",
voteStatusWithdrawn: "withdrawn",
}
func (v VoteStatus) String() string {
if label, ok := voteStatusLabels[v]; ok {
return label
}
return unknownVariant
}
type VoteChoice int
const (
voteAye VoteChoice = 1
voteNaye VoteChoice = -1
voteAbstain VoteChoice = 0
)
var voteChoiceLabels = map[VoteChoice]string{
voteAye: "aye",
voteNaye: "naye",
voteAbstain: "abstain",
}
func (v VoteChoice) String() string {
if label, ok := voteChoiceLabels[v]; ok {
return label
}
return unknownVariant
}
type VoteSums struct {
Ayes, Nayes, Abstains int
}
func (v *VoteSums) VoteCount() int {
return v.Ayes + v.Nayes + v.Abstains
}
func (v *VoteSums) TotalVotes() int {
return v.Ayes + v.Nayes
}
func (v *VoteSums) CalculateResult(quorum int, majority float32) (VoteStatus, string) {
if v.VoteCount() < quorum {
return voteStatusDeclined, fmt.Sprintf("Needed quorum of %d has not been reached.", quorum)
}
if (float32(v.Ayes) / float32(v.TotalVotes())) < majority {
return voteStatusDeclined, fmt.Sprintf("Needed majority of %0.2f%% has not been reached.", majority)
}
return voteStatusApproved, "Quorum and majority have been reached"
}
type Motion struct {
ID int64 `db:"id"`
Proposed time.Time
Proponent int64 `db:"proponent"`
Title string
Content string
Quorum int
Majority int
Status VoteStatus
Due time.Time
Modified time.Time
Tag string
VoteType VoteType
}
type ClosedMotion struct {
Decision *Motion
VoteSums *VoteSums
Reasoning string
}
type MotionModel struct {
DB *sqlx.DB
InfoLog *log.Logger
}
// Create a new decision.
func (m *MotionModel) Create(
ctx context.Context,
proponent *Voter,
voteType VoteType,
title, content string,
proposed, due time.Time,
) (int64, error) {
d := &Motion{
Proposed: proposed.UTC(),
Proponent: proponent.ID,
Title: title,
Content: content,
Due: due.UTC(),
VoteType: voteType,
}
result, err := m.DB.NamedExecContext(
ctx,
`INSERT INTO decisions
(proposed, proponent, title, content, votetype, status, due, modified, tag)
VALUES (:proposed, :proponent, :title, :content, :votetype, :status, :due, :proposed,
'm' || STRFTIME('%Y%m%d', :proposed) || '.' || (
SELECT COUNT(*)+1 AS num
FROM decisions
WHERE proposed BETWEEN DATE(:proposed) AND DATE(:proposed, '1 day')
))`,
d,
)
if err != nil {
return 0, fmt.Errorf("creating motion failed: %w", err)
}
id, err := result.LastInsertId()
if err != nil {
return 0, fmt.Errorf("could not get inserted decision id: %w", err)
}
return id, nil
}
func (m *MotionModel) CloseDecisions(ctx context.Context) ([]*ClosedMotion, error) {
tx, err := m.DB.BeginTxx(ctx, nil)
if err != nil {
return nil, fmt.Errorf("could not start transaction: %w", err)
}
defer func(tx *sqlx.Tx) {
_ = tx.Rollback()
}(tx)
rows, err := tx.NamedQuery(`
SELECT decisions.id, decisions.tag, decisions.proponent, decisions.proposed, decisions.title, decisions.content,
decisions.votetype, decisions.status, decisions.due, decisions.modified
FROM decisions
WHERE decisions.status=0 AND :now > due`, struct{ Now time.Time }{Now: time.Now().UTC()})
if err != nil {
return nil, fmt.Errorf("fetching closable decisions failed: %w", err)
}
defer func(rows *sqlx.Rows) {
_ = rows.Close()
}(rows)
decisions := make([]*Motion, 0)
for rows.Next() {
decision := &Motion{}
if err = rows.StructScan(decision); err != nil {
return nil, fmt.Errorf("scanning row failed: %w", err)
}
if rows.Err() != nil {
return nil, fmt.Errorf("row error: %w", err)
}
decisions = append(decisions, decision)
}
results := make([]*ClosedMotion, 0, len(decisions))
var decisionResult *ClosedMotion
for _, decision := range decisions {
m.InfoLog.Printf("found closable decision %s", decision.Tag)
if decisionResult, err = m.CloseDecision(ctx, tx, decision); err != nil {
return nil, fmt.Errorf("closing decision %s failed: %w", decision.Tag, err)
}
results = append(results, decisionResult)
}
if err = tx.Commit(); err != nil {
return nil, fmt.Errorf("could not commit transaction: %w", err)
}
return results, nil
}
func (m *MotionModel) CloseDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*ClosedMotion, error) {
quorum, majority := d.VoteType.QuorumAndMajority()
var (
voteSums *VoteSums
err error
reasoning string
)
if voteSums, err = m.SumsForDecision(ctx, tx, d); err != nil {
return nil, fmt.Errorf("getting vote sums failed: %w", err)
}
d.Status, reasoning = voteSums.CalculateResult(quorum, majority)
result, err := m.DB.NamedExecContext(
ctx,
`UPDATE decisions SET status=:status, modified=CURRENT_TIMESTAMP WHERE id=:id`,
d,
)
if err != nil {
return nil, fmt.Errorf("could not execute update query: %w", err)
}
affectedRows, err := result.RowsAffected()
if err != nil {
return nil, fmt.Errorf("could not get affected rows count: %w", err)
}
if affectedRows != 1 {
return nil, fmt.Errorf("unexpected number of rows %d instead of 1", affectedRows)
}
m.InfoLog.Printf("decision %s closed with result %s: reasoning '%s'", d.Tag, d.Status, reasoning)
return &ClosedMotion{d, voteSums, reasoning}, nil
}
func (m *MotionModel) UnVotedDecisionsForVoter(_ context.Context, _ *Voter) ([]Motion, error) {
panic("not implemented")
}
func (m *MotionModel) SumsForDecision(ctx context.Context, tx *sqlx.Tx, d *Motion) (*VoteSums, error) {
voteRows, err := tx.QueryxContext(
ctx,
`SELECT vote, COUNT(vote) FROM votes WHERE decision=$1 GROUP BY vote`,
d.ID,
)
if err != nil {
return nil, fmt.Errorf("fetching vote sums for motion %s failed: %w", d.Tag, err)
}
defer func(voteRows *sqlx.Rows) {
_ = voteRows.Close()
}(voteRows)
sums := &VoteSums{}
for voteRows.Next() {
var (
vote VoteChoice
count int
)
if err = voteRows.Err(); err != nil {
return nil, fmt.Errorf("could not fetch vote sums for motion %s: %w", d.Tag, err)
}
if err = voteRows.Scan(&vote, &count); err != nil {
return nil, fmt.Errorf("could not parse row for vote sums of motion %s: %w", d.Tag, err)
}
switch vote {
case voteAye:
sums.Ayes = count
case voteNaye:
sums.Nayes = count
case voteAbstain:
sums.Abstains = count
}
}
return sums, nil
}
func (m *MotionModel) NextPendingDecisionDue(ctx context.Context) (*time.Time, error) {
row := m.DB.QueryRowContext(
ctx,
`SELECT due FROM decisions WHERE status=0 ORDER BY due LIMIT 1`,
nil,
)
if row == nil {
return nil, errors.New("no row returned")
}
if err := row.Err(); err != nil {
return nil, fmt.Errorf("could not retrieve row for next pending decision: %w", err)
}
var due time.Time
if err := row.Scan(&due); err != nil {
if errors.Is(err, sql.ErrNoRows) {
m.InfoLog.Print("no pending decisions")
return nil, nil
}
return nil, fmt.Errorf("parsing result failed: %w", err)
}
return &due, nil
}
type MotionForDisplay struct {
ID int64 `db:"id"`
Tag string
Proponent int64
Proposer string
Proposed time.Time
Title string
Content string
Type VoteType `db:"votetype"`
Status VoteStatus
Due time.Time
Modified time.Time
Sums VoteSums
Votes []VoteForDisplay
}
type VoteForDisplay struct {
Name string
Vote VoteChoice
}
type MotionListOptions struct {
Limit int
Before, After *time.Time
}
func (m *MotionModel) TimestampRange(ctx context.Context) (*time.Time, *time.Time, error) {
row := m.DB.QueryRowxContext(ctx, `SELECT MIN(proposed), MAX(proposed) FROM decisions`)
if err := row.Err(); err != nil {
return nil, nil, fmt.Errorf("could not query for motion timestamps: %w", err)
}
var (
first, last sql.NullString
firstTs, lastTs *time.Time
err error
)
if err := row.Scan(&first, &last); err != nil {
return nil, nil, fmt.Errorf("could not scan timestamps: %w", err)
}
if !first.Valid || !last.Valid {
return nil, nil, nil
}
if firstTs, err = parseSqlite3TimeStamp(first.String); err != nil {
return nil, nil, err
}
if lastTs, err = parseSqlite3TimeStamp(last.String); err != nil {
return nil, nil, err
}
return firstTs, lastTs, nil
}
func (m *MotionModel) GetMotions(ctx context.Context, options *MotionListOptions) ([]*MotionForDisplay, error) {
var (
rows *sqlx.Rows
err error
)
switch {
case options.Before != nil:
rows, err = m.GetMotionRowsBefore(ctx, options)
case options.After != nil:
rows, err = m.GetMotionRowsAfter(ctx, options)
default:
rows, err = m.GetFirstMotionRows(ctx, options)
}
if err != nil {
return nil, err
}
defer func(rows *sqlx.Rows) {
_ = rows.Close()
}(rows)
motions := make([]*MotionForDisplay, 0, options.Limit)
for rows.Next() {
var decision MotionForDisplay
if err = rows.Err(); err != nil {
return nil, fmt.Errorf("could not fetch row: %w", err)
}
if err = rows.StructScan(&decision); err != nil {
return nil, fmt.Errorf("could not scan result: %w", err)
}
motions = append(motions, &decision)
}
if len(motions) > 0 {
err = m.FillVoteSums(ctx, motions)
if err != nil {
return nil, err
}
}
return motions, nil
}
func (m *MotionModel) FillVoteSums(ctx context.Context, decisions []*MotionForDisplay) error {
decisionIds := make([]int64, len(decisions))
decisionMap := make(map[int64]*MotionForDisplay, len(decisions))
for idx, decision := range decisions {
decisionIds[idx] = decision.ID
decisionMap[decision.ID] = decision
}
query, args, err := sqlx.In(
`SELECT v.decision, v.vote, COUNT(*) FROM votes v WHERE v.decision IN (?) GROUP BY v.decision, v.vote`,
decisionIds,
)
if err != nil {
return fmt.Errorf("could not create IN query: %w", err)
}
rows, err := m.DB.QueryContext(ctx, query, args...)
if err != nil {
return fmt.Errorf("could not execute query: %w", err)
}
defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)
for rows.Next() {
if err = rows.Err(); err != nil {
return fmt.Errorf("could not fetch row: %w", err)
}
var (
decisionID int64
vote VoteChoice
count int
)
err = rows.Scan(&decisionID, &vote, &count)
if err != nil {
return fmt.Errorf("could not scan row: %w", err)
}
switch vote {
case voteAye:
decisionMap[decisionID].Sums.Ayes = count
case voteNaye:
decisionMap[decisionID].Sums.Nayes = count
case voteAbstain:
decisionMap[decisionID].Sums.Abstains = count
}
}
return nil
}
func (m *MotionModel) GetMotionRowsBefore(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
rows, err := m.DB.QueryxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE decisions.proposed < $1
ORDER BY proposed DESC
LIMIT $2`,
options.Before,
options.Limit,
)
if err != nil {
return nil, fmt.Errorf("could not query motions before %s: %w", options.Before, err)
}
return rows, nil
}
func (m *MotionModel) GetMotionRowsAfter(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
rows, err := m.DB.QueryxContext(
ctx,
`WITH display_decision AS (SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
WHERE decisions.proposed > $1
ORDER BY proposed
LIMIT $2)
SELECT *
FROM display_decision
ORDER BY proposed DESC`,
options.After,
options.Limit,
)
if err != nil {
return nil, fmt.Errorf("could not query motions after %s: %w", options.After, err)
}
return rows, nil
}
func (m *MotionModel) GetFirstMotionRows(ctx context.Context, options *MotionListOptions) (*sqlx.Rows, error) {
rows, err := m.DB.QueryxContext(
ctx,
`SELECT decisions.id,
decisions.tag,
decisions.proponent,
voters.name AS proposer,
decisions.proposed,
decisions.title,
decisions.content,
decisions.votetype,
decisions.status,
decisions.due,
decisions.modified
FROM decisions
JOIN voters ON decisions.proponent = voters.id
ORDER BY proposed DESC
LIMIT $1`,
options.Limit,
)
if err != nil {
return nil, fmt.Errorf("could not query motions: %w", err)
}
return rows, nil
}