am-i-alive-go

Make it easy for people to check if you are alive or not.
git clone http://git.hanabi.in/repos/am-i-alive-go.git
Log | Files | Refs | README | LICENSE

get-home-page-utils.go (3154B)


      1 package handlers
      2 
      3 import (
      4 	"database/sql"
      5 	"encoding/json"
      6 	"fmt"
      7 	"net/http"
      8 	"time"
      9 
     10 	"git.hanabi.in/dev/am-i-alive/config"
     11 	"git.hanabi.in/dev/am-i-alive/consts"
     12 	JsonStructs "git.hanabi.in/dev/am-i-alive/json-structs"
     13 )
     14 
     15 func getRespForHomePage(r *http.Request) (header int, resp []byte) {
     16 	if r.Method != http.MethodGet {
     17 		header = http.StatusMethodNotAllowed
     18 		err_msg := "Method not allowed."
     19 		resp, _ = json.Marshal(JsonStructs.ErrJson{Err: err_msg})
     20 	} else {
     21 		if msg, lastseen, unix, err := fetchUpdatedAtMsg(); err != nil {
     22 			header = http.StatusInternalServerError
     23 			resp, _ = json.Marshal(JsonStructs.ErrJson{Err: err.Error()})
     24 		} else {
     25 			header = http.StatusOK
     26 			if lastseen == "" {
     27 				resp, _ = json.Marshal(JsonStructs.NotStartedJson{Message: msg})
     28 			} else {
     29 				resp, _ = json.Marshal(JsonStructs.ResJson{Message: msg, Lastseen: lastseen, Unix: unix})
     30 			}
     31 		}
     32 	}
     33 	return header, resp
     34 }
     35 
     36 func fetchUpdatedAtMsg() (msg string, lastseen string, unix int64, err error) {
     37 	db, err := open_db()
     38 	defer db.Close()
     39 
     40 	if err == nil {
     41 		if updated_at, err := queryUpdatedAt(db); err == nil {
     42 			if updated_at == 0 {
     43 				msg = fmt.Sprintf("%s has not started using this tool yet.", config.Name)
     44 			} else {
     45 				lastseen, unix = getLastSeen(updated_at)
     46 				msg = getMsgBasedOn(updated_at)
     47 			}
     48 		}
     49 	}
     50 	return msg, lastseen, unix, err
     51 }
     52 
     53 // Get seconds between RIGHT NOW, and when was the last update.
     54 func getDiff(updated_at int64) (diff int64) {
     55 	now := time.Now().Unix() - consts.Offset // since updated_at is decreased by offset.
     56 	diff = now - updated_at
     57 	return diff
     58 }
     59 
     60 // Get the last updated_at (UNIX timestamp - offset) for the userid from DB.
     61 func queryUpdatedAt(db *sql.DB) (res int64, err error) {
     62 	query_str := "SELECT updatedat FROM alive WHERE id=0"
     63 	row := db.QueryRow(query_str)
     64 	err = row.Scan(&res)
     65 	return res, err
     66 }
     67 
     68 // Get lastseen string and UNIX (milli) timestamp based on last updated_at (UNIX timestamp - offset).
     69 func getLastSeen(updated_at int64) (lastseen string, unix int64) {
     70 	secs := (updated_at + consts.Offset)
     71 	unix = secs * consts.Ms
     72 	datetime := time.Unix(secs, 0).UTC().Format(time.RFC1123)
     73 	lastseen = fmt.Sprintf("%s was last active around %s+0000 (Coordinated Universal Time).", config.Name, datetime)
     74 	return lastseen, unix
     75 }
     76 
     77 // depending on diff in seconds from last updated and now, get a formatted message.
     78 func getMsgBasedOn(updated_at int64) (msg string) {
     79 	diff := getDiff(updated_at)
     80 	if diff >= 0 && diff <= consts.T_4hr {
     81 		msg = fmt.Sprintf("%s is most certainly alive.", config.Name)
     82 	} else if diff > consts.T_4hr && diff <= consts.T_18hr {
     83 		msg = fmt.Sprintf("%s is most certainly AFK.", config.Name)
     84 	} else if diff > consts.T_18hr && diff <= consts.T_60hr {
     85 		msg = fmt.Sprintf("%s has been away for quite some time now.  You might want to check on him.", config.Name)
     86 	} else if diff > consts.T_60hr && diff <= consts.T_5d {
     87 		msg = fmt.Sprintf("%s has been away for rather too long.  If %s didn't tell you he will be gone for so long, *please* check on him!", config.Name, config.Name)
     88 	} else {
     89 		msg = fmt.Sprintf("%s is most certainly dead.", config.Name)
     90 	}
     91 	return msg
     92 }