Skip to content

Commit

Permalink
Record ticket daily counts when opening tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 9, 2022
1 parent a1abeb4 commit 114e510
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
4 changes: 3 additions & 1 deletion core/handlers/ticket_opened_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ func TestTicketOpened(t *testing.T) {
},
}))

oa := testdata.Org1.Load(rt)

// an existing ticket
cathyTicket := models.NewTicket(flows.TicketUUID(uuids.New()), testdata.Org1.ID, testdata.Cathy.ID, testdata.Mailgun.ID, "748363", testdata.DefaultTopic.ID, "Who?", models.NilUserID, nil)
err := models.InsertTickets(ctx, db, []*models.Ticket{cathyTicket})
err := models.InsertTickets(ctx, db, oa, []*models.Ticket{cathyTicket})
require.NoError(t, err)

tcs := []handlers.TestCase{
Expand Down
2 changes: 1 addition & 1 deletion core/hooks/insert_tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (h *insertTicketsHook) Apply(ctx context.Context, rt *runtime.Runtime, tx *
}

// insert the tickets
err := models.InsertTickets(ctx, tx, tickets)
err := models.InsertTickets(ctx, tx, oa, tickets)
if err != nil {
return errors.Wrapf(err, "error inserting tickets")
}
Expand Down
43 changes: 41 additions & 2 deletions core/models/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -51,10 +52,15 @@ func (i *TicketID) Scan(value interface{}) error {

type TicketerID null.Int
type TicketStatus string
type TicketDailyCountType string

const (
TicketStatusOpen = TicketStatus("O")
TicketStatusClosed = TicketStatus("C")

TicketDailyCountOpening = TicketDailyCountType("O")
TicketDailyCountAssignment = TicketDailyCountType("A")
TicketDailyCountReply = TicketDailyCountType("R")
)

// Register a ticket service factory with the engine
Expand Down Expand Up @@ -334,7 +340,7 @@ RETURNING
`

// InsertTickets inserts the passed in tickets returning any errors encountered
func InsertTickets(ctx context.Context, tx Queryer, tickets []*Ticket) error {
func InsertTickets(ctx context.Context, tx Queryer, oa *OrgAssets, tickets []*Ticket) error {
if len(tickets) == 0 {
return nil
}
Expand All @@ -344,7 +350,40 @@ func InsertTickets(ctx context.Context, tx Queryer, tickets []*Ticket) error {
ts[i] = &tickets[i].t
}

return BulkQuery(ctx, "inserted tickets", tx, sqlInsertTicket, ts)
if err := BulkQuery(ctx, "inserted tickets", tx, sqlInsertTicket, ts); err != nil {
return err
}

return insertTicketDailyCounts(ctx, tx, TicketDailyCountOpening, oa.Org().Timezone(), map[string]int{
fmt.Sprintf("o:%d", oa.OrgID()): len(tickets),
})
}

const sqlInsertTicketDailyCount = `
INSERT INTO tickets_ticketdailycount(count_type, scope, count, day, is_squashed)
VALUES(:count_type, :scope, :count, :day, FALSE)`

func insertTicketDailyCounts(ctx context.Context, tx Queryer, countType TicketDailyCountType, tz *time.Location, scopeCounts map[string]int) error {
type dailyCount struct {
CountType TicketDailyCountType `db:"count_type"`
Scope string `db:"scope"`
Count int `db:"count"`
Day dates.Date `db:"day"`
}

day := dates.ExtractDate(dates.Now().In(tz))

counts := make([]*dailyCount, 0, len(scopeCounts))
for scope, count := range scopeCounts {
counts = append(counts, &dailyCount{
CountType: countType,
Scope: scope,
Count: count,
Day: day,
})
}

return BulkQuery(ctx, "inserted ticket daily counts", tx, sqlInsertTicketDailyCount, counts)
}

// UpdateTicketExternalID updates the external ID of the given ticket
Expand Down
7 changes: 6 additions & 1 deletion core/models/tickets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestTickets(t *testing.T) {

defer testsuite.Reset(testsuite.ResetData)

oa := testdata.Org1.Load(rt)

ticket1 := models.NewTicket(
"2ef57efc-d85f-4291-b330-e4afe68af5fe",
testdata.Org1.ID,
Expand Down Expand Up @@ -108,12 +110,15 @@ func TestTickets(t *testing.T) {
assert.Equal(t, testdata.Admin.ID, ticket1.AssigneeID())
assert.Equal(t, "", ticket1.Config("xyz"))

err := models.InsertTickets(ctx, db, []*models.Ticket{ticket1, ticket2, ticket3})
err := models.InsertTickets(ctx, db, oa, []*models.Ticket{ticket1, ticket2, ticket3})
assert.NoError(t, err)

// check all tickets were created
assertdb.Query(t, db, `SELECT count(*) FROM tickets_ticket WHERE status = 'O' AND closed_on IS NULL`).Returns(3)

// check the opened count was added
assertdb.Query(t, db, `SELECT SUM(count) FROM tickets_ticketdailycount WHERE count_type = 'O' AND scope = CONCAT('o:', $1::text)`, testdata.Org1.ID).Returns(3)

// can lookup a ticket by UUID
tk1, err := models.LookupTicketByUUID(ctx, db, "2ef57efc-d85f-4291-b330-e4afe68af5fe")
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.10.4
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.19.1
github.com/nyaruka/gocommon v1.20.0
github.com/nyaruka/goflow v0.158.1
github.com/nyaruka/librato v1.0.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
github.com/naoina/toml v0.1.1/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.19.1 h1:miVSFCqSEe1pKAID/PWqwRBtdvPnw6kQZC/Bz9tYVgQ=
github.com/nyaruka/gocommon v1.19.1/go.mod h1:JrQSLAPo9ezSy1AzsJ1zDr1HW0/eu+aipICJkN/+kpg=
github.com/nyaruka/gocommon v1.20.0 h1:qbxRsBBPvpfGbuBq08jlQGxa5t+UZX/YGV7+kR+/moM=
github.com/nyaruka/gocommon v1.20.0/go.mod h1:JrQSLAPo9ezSy1AzsJ1zDr1HW0/eu+aipICJkN/+kpg=
github.com/nyaruka/goflow v0.158.1 h1:Q6zrf55f8XqsC3WFRD/Gcpp/RwLXnHdY7UslT32TI7o=
github.com/nyaruka/goflow v0.158.1/go.mod h1:J+FJ0iw1cjivEziBGpVPtTl9fuOz+ib558MCBdKLC8M=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
Expand Down
7 changes: 3 additions & 4 deletions services/tickets/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func TestCloseTicket(t *testing.T) {
},
}))

oa := testdata.Org1.Load(rt)

// create an open ticket
ticket1 := models.NewTicket(
"2ef57efc-d85f-4291-b330-e4afe68af5fe",
Expand All @@ -172,15 +174,12 @@ func TestCloseTicket(t *testing.T) {
"contact-display": "Cathy",
},
)
err := models.InsertTickets(ctx, db, []*models.Ticket{ticket1})
err := models.InsertTickets(ctx, db, oa, []*models.Ticket{ticket1})
require.NoError(t, err)

// create a close ticket trigger
testdata.InsertTicketClosedTrigger(db, testdata.Org1, testdata.Favorites)

oa, err := models.GetOrgAssets(ctx, rt, testdata.Org1.ID)
require.NoError(t, err)

logger := &models.HTTPLogger{}

err = tickets.Close(ctx, rt, oa, ticket1, true, logger)
Expand Down
1 change: 1 addition & 0 deletions testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ UPDATE contacts_contact SET current_flow_id = NULL;
DELETE FROM notifications_notification;
DELETE FROM notifications_incident;
DELETE FROM request_logs_httplog;
DELETE FROM tickets_ticketdailycount;
DELETE FROM tickets_ticketevent;
DELETE FROM tickets_ticket;
DELETE FROM triggers_trigger_contacts WHERE trigger_id >= 30000;
Expand Down

0 comments on commit 114e510

Please sign in to comment.