-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33802e0
commit 0cb89ee
Showing
4 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |