Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kris-watts-gravwell committed Nov 8, 2019
1 parent 33802e0 commit 0cb89ee
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 0 deletions.
76 changes: 76 additions & 0 deletions email/htmlEmail.ank
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Module to send HTML formatted Emails
# Example use:
# em = htmlEmail
# em.SetTitle("alert stuff"
# em.AddSubTitle("you should care about this")
# em.AddBodyData("hi")
# em.AddEntsTable(ents, ["foo", "bar", "baz"])
# em.AddQueryInfo(query, time.Now().Add(-1 * time.Hour), time.Now())
# em.SendEmail("[email protected]", "[email protected]", "heeeyooo")

module htmlEmail {
title = ""
body = ""
queryinfo = ""
var time = import("time")

func AddEntsTable(ents, cols) {
ret = `<table><tr>`
for col in cols {
ret += `<td>` + toString(col) + `</td>`
}
ret += `</tr>`
for ent in ents {
row = `<tr>`
for col in cols {
v, err = getEntryEnum(ent, col)
if err != nil {
v = ``
}
row += `<td>` + toString(v) + `</td>`
}
row += `</tr>`
ret += row
}
ret += `</table>`
body += ret
}

func AddBodyData(v) {
body += `<div>` + v + `</div>`
}

func SetTitle(v) {
title = v
}

func AddSubTitle(v) {
body += `<h3>` + v + `</h3>`
}

func AddQueryInfo(query, start, end) {
ret = `<div>Query In Use:<br><i>` + query + `</i></div><br>`
ret += `Query Timerange:<br>`
ret += `Start: ` + start.Format(time.RFC822) + `<br>`
ret += `End: ` + end.Format(time.RFC822) + `<br>`
ret += `Duration: ` + end.Sub(start).String() + `<br>`
if queryinfo != "" {
queryinfo += "<br>" + ret
} else {
queryinfo = ret
}
}

func SendEmail(from, to, subj) {
data = `<html>`
if title != "" {
data += `<h2>` + title + `</h2>`
}
data += body
if queryinfo != "" {
data += `<br><br>` + queryinfo
}
data += `</html>`
return email(from, to, subj, data)
}
}
39 changes: 39 additions & 0 deletions time/timemap.ank
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Module to stash and retrieve timestamps by name
# Example use:
# tm = timemap
# tm.Set("foo", time.Now())
# tm.Get("foo") # returns the previously set time
# tm.Get("bar") # returns the zero time


module timemap {
time = import("time")
zero, _ = time.Parse("Jan 2 15:04:05 MST 2006", "Jan 1 00:00:00 UTC 0000")
mapname = "timemap"

# Note that if you're going to call this, call it FIRST
# before you set anything
func SetMapName(n) {
mapname = n
}

func GetMapName() {
return mapname
}

func Set(k, v) {
setPersistentMap(mapname, k, toString(v.Unix()))
}

func Get(k) {
v = getPersistentMap(mapname, k)
if v == nil {
return zero
}
return time.Unix(toInt(v), 0)
}

func Del(k) {
delPersistentMap(mapname, k)
}
}
33 changes: 33 additions & 0 deletions utils/links.ank
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Module to stash and retrieve timestamps by name
# Example use:
# tm = timemap

module link {
url = import ("net/url")
u, _ = url.Parse("http://")
q = u.Query()
func SetHost(v) {
u.Host = v
}

func SetScheme(v) {
u.Scheme = v
}

func SetPath(v) {
u.Path = v
}

func AddParam(name, value) {
q.Set(name, value)
u.RawQuery = q.Encode()
}

func Encode() {
return u.String()
}

func String() {
return u.String()
}
}
60 changes: 60 additions & 0 deletions utils/throttle.ank
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Module to stash and retrieve timestamps by name
# Example use:
# tr = throttle
# tr.SetInterval(24*time.Hour)
# if !tr.ShouldThrottle(`foo`, nil) {
# DoStuff()
tm.Mark("foo", time.Now())
}


module throttle {
time = import("time")
mapname = "throttle"
interval = time.Hour
zero, _ = time.Parse("Jan 2 15:04:05 MST 2006", "Jan 1 00:00:00 UTC 0000")

# Note that if you're going to call this, call it FIRST
# before you set anything
func SetMapName(n) {
mapname = n
}

func SetInterval(dur) {
interval = dur
}

func Mark(k, ts) {
if ts == nil {
ts = time.Now()
}
setPersistentMap(mapname, k, toString(ts.Unix()))
}

func ShouldThrottle(k, ts) {
if ts == nil {
ts = time.Now()
}
#if any of the internal values are bad, just return true, don't throttle
last = getPersistentMap(mapname, k)
if last == nil {
return false
} else if interval == nil {
return false
}
lastts = time.Unix(toInt(last), 0) #last time the event occurred
sb = interval
if sb < 0 {
sb = -1 * interval
}
return time.Now().Before(lastts.Add(sb))
}

func LastEvent(k) {
last = getPersistentMap(mapname, k)
if last == nil {
return zero
}
return time.Unix(toInt(last), 0)
}
}

0 comments on commit 0cb89ee

Please sign in to comment.