108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package dobotaiops
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// Actor keys are internal join material, never fields of the browser response.
|
|
type ActorIdentity struct {
|
|
ActorID string `json:"actor_id"`
|
|
StaffID string `json:"staff_id"`
|
|
UnionID string `json:"union_id"`
|
|
}
|
|
type OrganizationVersion struct {
|
|
RegionNames []string `json:"region_names"`
|
|
State string `json:"state"`
|
|
DirectoryID string `json:"directory_id"`
|
|
DirectorySource string `json:"directory_source"`
|
|
SyncedAt *time.Time `json:"synced_at"`
|
|
RevisionID string `json:"revision_id"`
|
|
Basis string `json:"basis"`
|
|
}
|
|
type RecordOrganization struct {
|
|
Version OrganizationVersion `json:"version"`
|
|
State string `json:"state"`
|
|
Departments []string `json:"departments"`
|
|
Region string `json:"region"`
|
|
SourceDepartment string `json:"source_department"`
|
|
SourceRegion string `json:"source_region"`
|
|
DepartmentOrigin string `json:"department_origin"`
|
|
RegionOrigin string `json:"region_origin"`
|
|
}
|
|
type UsageGroup struct {
|
|
Identity ActorIdentity `json:"-"`
|
|
Total int64
|
|
Succeeded int64
|
|
Failed int64
|
|
}
|
|
type UsageGroups struct {
|
|
Groups []UsageGroup `json:"-"`
|
|
AsOf time.Time
|
|
}
|
|
|
|
func decodeRecord(raw string) (Record, error) {
|
|
var wire struct {
|
|
Record
|
|
Identity ActorIdentity `json:"_identity"`
|
|
QualityHash string `json:"_quality_hash"`
|
|
QualityObservedAt time.Time `json:"_quality_observed_at"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &wire); err != nil {
|
|
return Record{}, err
|
|
}
|
|
wire.Record.Identity = wire.Identity
|
|
wire.Record.QualityInputHash = wire.QualityHash
|
|
wire.Record.QualityObservedAt = wire.QualityObservedAt
|
|
wire.Record.Coverage = CheckAnswerCoverage(wire.Record.Answer)
|
|
return wire.Record, nil
|
|
}
|
|
func (c *RecordsRepository) ReadUsageGroups(ctx context.Context, query RecordQuery) (UsageGroups, error) {
|
|
if err := c.validateQueryWindow(query.Window); err != nil {
|
|
return UsageGroups{}, err
|
|
}
|
|
if query.Cursor != "" {
|
|
return UsageGroups{}, ErrInvalidQuery
|
|
}
|
|
pattern, patternErr := recordKeywordPattern(query.Keyword)
|
|
if patternErr != nil {
|
|
return UsageGroups{}, patternErr
|
|
}
|
|
result := UsageGroups{Groups: []UsageGroup{}}
|
|
err := c.transact(ctx, func(ctx context.Context, q querier) error {
|
|
if c.readAsOf(ctx, q, &result.AsOf) != nil {
|
|
return ErrUnavailable
|
|
}
|
|
rows, err := q.Query(ctx, c.baseSQL+` SELECT identity::text,count(*),count(*) FILTER(WHERE status='succeeded'),count(*) FILTER(WHERE status='failed') FROM records WHERE `+recordFilterSQL+` GROUP BY identity`, query.Window.TenantKey, query.Window.Start, query.Window.End, pattern)
|
|
if err != nil {
|
|
return ErrUnavailable
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var raw string
|
|
var group UsageGroup
|
|
if rows.Scan(&raw, &group.Total, &group.Succeeded, &group.Failed) != nil || json.Unmarshal([]byte(raw), &group.Identity) != nil {
|
|
return ErrUnavailable
|
|
}
|
|
result.Groups = append(result.Groups, group)
|
|
}
|
|
return rows.Err()
|
|
})
|
|
return result, err
|
|
}
|
|
|
|
// Keep the agreed field ordering after governance enriches an initially empty column.
|
|
func AddRecordColumns(page *RecordPage, extra ...string) {
|
|
all := map[string]bool{}
|
|
for _, col := range append(page.Columns, extra...) {
|
|
all[col] = true
|
|
}
|
|
page.Columns = []string{}
|
|
for _, col := range recordColumnOrder {
|
|
if all[col] {
|
|
page.Columns = append(page.Columns, col)
|
|
}
|
|
}
|
|
}
|