Skip to content

Commit

Permalink
feat: add dynamic time limit conf from api
Browse files Browse the repository at this point in the history
  • Loading branch information
abidibo committed Jan 15, 2024
1 parent c8cd207 commit d624123
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Create the file `/etc/gomonitor.json`. Yes, it's a json file. Yes GoMonitor does
"screenTimeLimitMinutes": {
"USER": 120,
},
"screenTimeLimitApiMinutes": {
"OTHER_USER": "https://myapi/gomonitor",
},
"logIntervalMinutes": 10,
"retainPeriodDays": 10
}
Expand All @@ -63,6 +66,14 @@ GoMonitor keeps data and logs for `retainPeriodDays` days

Replace `USER` with the user you want to monitor.

You can set a static limit for `screenTimeLimitMinutes` or use an API endpoint for `screenTimeLimitApiMinutes`, in such case you must return the following json response:

``` json
{
"limitMin": 120
}
```

### Usage

GoMonitor behaves differently when running as root or as normal user. Fact.
Expand Down
3 changes: 3 additions & 0 deletions core/monitor/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package monitor

import (
"fmt"
"math"
"os"
"path/filepath"
Expand All @@ -21,6 +22,7 @@ type ProcessLog struct {
}

func RunAsRoot() {
fmt.Println("Retrieving current user processes")
// now
startTime := time.Now()
// every logIntervalMinutes minutes we log
Expand All @@ -41,6 +43,7 @@ func RunAsRoot() {
}

timeScreenLimit, err := utils.GetScreenTimeLimitMinutes(currentUser)
logger.ZapLog.Debug("Current screen time limit ", timeScreenLimit)

logger.ZapLog.Debug("Retrieving current user processes")
logProcesses := make([]ProcessLog, 0)
Expand Down
2 changes: 2 additions & 0 deletions core/monitor/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ func notificationsThread() {
}

for {
// if taken from api, we need to refresh it
timeScreenLimit, err := utils.GetScreenTimeLimitMinutes(currentUser)
// get sum of partial_time_minutes for current day and user
totalMinutes, err := utils.GetTotalTodayTimeMinutes(currentUser)
if err == nil {
Expand Down
36 changes: 34 additions & 2 deletions core/utils/settings.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,47 @@
package utils

import "github.com/spf13/viper"
import (
"encoding/json"
"io"
"net/http"

"github.com/spf13/viper"
)

type ApiLimitBody struct {
LimitMin int `json:"limitMin"`
}

func GetScreenTimeLimitMinutes(user string) (int, error) {
apiLimits := make(map[string]string)
err := viper.UnmarshalKey("app.screenTimeLimitApiMinutes", &apiLimits)
timeScreenLimitApi, okTimeScreenLimitApi := apiLimits[user]

if err == nil && okTimeScreenLimitApi {
res, err := http.Get(timeScreenLimitApi)
if err == nil {
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err == nil {
var apiLimitBody ApiLimitBody
err = json.Unmarshal(body, &apiLimitBody)
if err == nil {
return apiLimitBody.LimitMin * 100, err
}
}
}
}

limits := make(map[string]int)
err := viper.UnmarshalKey("app.screenTimeLimitMinutes", &limits)
err = viper.UnmarshalKey("app.screenTimeLimitMinutes", &limits)
timeScreenLimit, okTimeScreenLimit := limits[user]

if err == nil && okTimeScreenLimit {
return timeScreenLimit, nil
} else {
// try api limimts
apiLimits := make(map[string]string)
err := viper.UnmarshalKey("app.screenTimeLimitMinutes", &apiLimits)
return 0, err
}
}
Binary file modified gomonitor
Binary file not shown.

0 comments on commit d624123

Please sign in to comment.