zitadel/internal/view/repository/sequence.go
Fabi 6b3f5b984c
feat: metrics (#1024)
* refactor: switch from opencensus to opentelemetry

* tempo works as designed nooooot

* fix: log traceids

* with grafana agent

* fix: http tracing

* fix: cleanup files

* chore: remove todo

* fix: bad test

* fix: ignore methods in grpc interceptors

* fix: remove test log

* clean up

* typo

* fix(config): configure tracing endpoint

* fix(span): add error id to span

* feat: metrics package

* feat: metrics package

* fix: counter

* fix: metric

* try metrics

* fix: coutner metrics

* fix: active sessin counter

* fix: active sessin counter

* fix: change current Sequence table

* fix: change current Sequence table

* fix: current sequences

* fix: spooler div metrics

* fix: console view

* fix: merge master

* fix: Last spool run on search result instead of eventtimestamp

* fix: go mod

* Update console/src/assets/i18n/de.json

Co-authored-by: Livio Amstutz <livio.a@gmail.com>

* fix: pr review

* fix: map

* update oidc pkg

* fix: handlers

* fix: value observer

* fix: remove fmt

* fix: handlers

* fix: tests

* fix: handler minimum cycle duration 1s

* fix(spooler): handler channel buffer

* fix interceptors

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-12-02 08:50:59 +01:00

94 lines
2.6 KiB
Go

package repository
import (
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/view/model"
"github.com/jinzhu/gorm"
"strings"
"time"
)
type CurrentSequence struct {
ViewName string `gorm:"column:view_name;primary_key"`
CurrentSequence uint64 `gorm:"column:current_sequence"`
EventTimestamp time.Time `gorm:"column:event_timestamp"`
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
}
type SequenceSearchKey int32
const (
SequenceSearchKeyUndefined SequenceSearchKey = iota
SequenceSearchKeyViewName
)
type sequenceSearchKey SequenceSearchKey
func (key sequenceSearchKey) ToColumnName() string {
switch SequenceSearchKey(key) {
case SequenceSearchKeyViewName:
return "view_name"
default:
return ""
}
}
func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
dbView := strings.Split(sequence.ViewName, ".")
return &model.View{
Database: dbView[0],
ViewName: dbView[1],
CurrentSequence: sequence.CurrentSequence,
EventTimestamp: sequence.EventTimestamp,
LastSuccessfulSpoolerRun: sequence.LastSuccessfulSpoolerRun,
}
}
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now()})
}
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) error {
save := PrepareSave(table)
err := save(db, currentSequence)
if err != nil {
return caos_errs.ThrowInternal(err, "VIEW-5kOhP", "unable to updated processed sequence")
}
return nil
}
func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, error) {
sequence := new(CurrentSequence)
query := PrepareGetByKey(table, sequenceSearchKey(SequenceSearchKeyViewName), viewName)
err := query(db, sequence)
if err == nil {
return sequence, nil
}
if caos_errs.IsNotFound(err) {
return sequence, nil
}
return nil, caos_errs.ThrowInternalf(err, "VIEW-9LyCB", "unable to get latest sequence of %s", viewName)
}
func AllCurrentSequences(db *gorm.DB, table string) ([]*CurrentSequence, error) {
sequences := make([]*CurrentSequence, 0)
query := PrepareSearchQuery(table, GeneralSearchRequest{})
_, err := query(db, &sequences)
if err != nil {
return nil, err
}
return sequences, nil
}
func ClearView(db *gorm.DB, truncateView, sequenceTable string) error {
truncate := PrepareTruncate(truncateView)
err := truncate(db)
if err != nil {
return err
}
return SaveCurrentSequence(db, sequenceTable, truncateView, 0, time.Now())
}