Skip to content

Commit

Permalink
add function to get the last synced timestamp for politeia (#208)
Browse files Browse the repository at this point in the history
* add function to get the last synced timestamp for politeia

* add time elapsed method

* choose format in which time is returned

* make GetLastSyncedTimeStamp() return a timestamp in int64

* -save lastsyncedtimestamp value to the database after sync has succesfully completed

* make abbreviation formats to be constants
remove redundant variable

* add config key for politeia last synced timestamp
  • Loading branch information
dreacot authored Oct 19, 2021
1 parent 05457b6 commit 3c6b257
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions politeia.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func newPoliteia(mwRef *MultiWallet, host string) (*Politeia, error) {
return p, nil
}

func (p *Politeia) saveLastSyncedTimestamp(lastSyncedTimestamp int64) {
p.mwRef.SetLongConfigValueForKey(PoliteiaLastSyncedTimestampConfigKey, lastSyncedTimestamp)
}

func (p *Politeia) getLastSyncedTimestamp() int64 {
return p.mwRef.ReadLongConfigValueForKey(PoliteiaLastSyncedTimestampConfigKey, 0)
}

func (p *Politeia) saveOrOverwiteProposal(proposal *Proposal) error {
var oldProposal Proposal
err := p.mwRef.db.One("Token", proposal.Token, &oldProposal)
Expand Down
2 changes: 2 additions & 0 deletions politeia_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (

ticketVoteApi = tkv1.APIRoute
proposalDetailsPath = "/proposals/"

PoliteiaLastSyncedTimestampConfigKey = "politeia_last_synced_timestamp"
)

var apiPath = www.PoliteiaWWWAPIRoute
Expand Down
5 changes: 5 additions & 0 deletions politeia_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ func (p *Politeia) Sync() error {
}

log.Info("Politeia sync: update complete")
p.saveLastSyncedTimestamp(time.Now().Unix())
p.publishSynced()
return nil
}
}

func (p *Politeia) GetLastSyncedTimeStamp() int64 {
return p.getLastSyncedTimestamp()
}

func (p *Politeia) IsSyncing() bool {
p.mu.RLock()
defer p.mu.RUnlock()
Expand Down
124 changes: 124 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const (
LegacyMainnetHDPath = "m / 44’ / 20’ / "

DefaultRequiredConfirmations = 2

LongAbbreviationFormat = "long"
ShortAbbreviationFormat = "short"
ShortestAbbreviationFormat = "shortest"
)

func (mw *MultiWallet) RequiredConfirmations() int32 {
Expand Down Expand Up @@ -322,3 +326,123 @@ func initWalletLoader(chainParams *chaincfg.Params, walletDataDir, walletDbDrive

return walletLoader
}

// makePlural is used with the TimeElapsed function. makePlural checks if the arguments passed is > 1,
// if true, it adds "s" after the given time to make it plural
func makePlural(x float64) string {
if int(x) == 1 {
return ""
}
return "s"
}

// TimeElapsed returns the formatted time diffrence between two times as a string.
// If the argument `fullTime` is set to true, then the full time available is returned e.g 3 hours, 2 minutes, 20 seconds ago,
// as opposed to 3 hours ago.
// If the argument `abbreviationFormat` is set to `long` the time format is e.g 2 minutes
// If the argument `abbreviationFormat` is set to `short` the time format is e.g 2 mins
// If the argument `abbreviationFormat` is set to `shortest` the time format is e.g 2 m
func TimeElapsed(now, then time.Time, abbreviationFormat string, fullTime bool) string {
var parts []string
var text string

year2, month2, day2 := now.Date()
hour2, minute2, second2 := now.Clock()

year1, month1, day1 := then.Date()
hour1, minute1, second1 := then.Clock()

year := math.Abs(float64(year2 - year1))
month := math.Abs(float64(month2 - month1))
day := math.Abs(float64(day2 - day1))
hour := math.Abs(float64(hour2 - hour1))
minute := math.Abs(float64(minute2 - minute1))
second := math.Abs(float64(second2 - second1))

week := math.Floor(day / 7)

if year > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" year"+makePlural(year))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" yr"+makePlural(year))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(year))+" y")
}
}

if month > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" month"+makePlural(month))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" mon"+makePlural(month))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(month))+" m")
}
}

if week > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" week"+makePlural(week))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" wk"+makePlural(week))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(week))+" w")
}
}

if day > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" day"+makePlural(day))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" dy"+makePlural(day))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(day))+" d")
}
}

if hour > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" hour"+makePlural(hour))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" hr"+makePlural(hour))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(hour))+" h")
}
}

if minute > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" minute"+makePlural(minute))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" min"+makePlural(minute))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(minute))+" mi")
}
}

if second > 0 {
if abbreviationFormat == LongAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" second"+makePlural(second))
} else if abbreviationFormat == ShortAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" sec"+makePlural(second))
} else if abbreviationFormat == ShortestAbbreviationFormat {
parts = append(parts, strconv.Itoa(int(second))+" s")
}
}

if now.After(then) {
text = " ago"
} else {
text = " after"
}

if len(parts) == 0 {
return "just now"
}

if fullTime {
return strings.Join(parts, ", ") + text
}
return parts[0] + text
}

0 comments on commit 3c6b257

Please sign in to comment.