Skip to content

Commit

Permalink
Dynamic time frame
Browse files Browse the repository at this point in the history
  • Loading branch information
aunefyren committed Dec 13, 2023
1 parent 1b66ddd commit b0c575c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 49 deletions.
2 changes: 2 additions & 0 deletions files/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func CreateConfigFile() error {
config.CreateShareLinks = true
config.WinterTheme = true
config.BasicAuth = false
config.WrappedDynamic = false
config.WrappedDynamicDays = 0
config.WrapperrCustomize.StatsTopListLength = 10
config.WrapperrCustomize.ObfuscateOtherUsers = true
config.WrapperrCustomize.StatsOrderByDuration = true
Expand Down
64 changes: 34 additions & 30 deletions models/config.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package models

type WrapperrConfig struct {
TautulliConfig []TautulliConfig `json:"tautulli_config"`
WrapperrCustomize WrapperrCustomize `json:"wrapperr_customize"`
WrapperrVersion string `json:"wrapperr_version"`
Timezone string `json:"timezone"`
ApplicationName string `json:"application_name"`
ApplicationURL string `json:"application_url"`
UseCache bool `json:"use_cache"`
UseLogs bool `json:"use_logs"`
ClientKey string `json:"client_key"`
WrapperrRoot string `json:"wrapperr_root"`
PrivateKey string `json:"private_key"`
CreateShareLinks bool `json:"create_share_links"`
WrappedStart int `json:"wrapped_start"`
WrappedEnd int `json:"wrapped_end"`
WrapperrPort int `json:"wrapperr_port"`
PlexAuth bool `json:"plex_auth"`
BasicAuth bool `json:"basic_auth"`
WinterTheme bool `json:"winter_theme"`
TautulliConfig []TautulliConfig `json:"tautulli_config"`
WrapperrCustomize WrapperrCustomize `json:"wrapperr_customize"`
WrapperrVersion string `json:"wrapperr_version"`
Timezone string `json:"timezone"`
ApplicationName string `json:"application_name"`
ApplicationURL string `json:"application_url"`
UseCache bool `json:"use_cache"`
UseLogs bool `json:"use_logs"`
ClientKey string `json:"client_key"`
WrapperrRoot string `json:"wrapperr_root"`
PrivateKey string `json:"private_key"`
CreateShareLinks bool `json:"create_share_links"`
WrappedStart int `json:"wrapped_start"`
WrappedEnd int `json:"wrapped_end"`
WrappedDynamic bool `json:"wrapped_dynamic"`
WrappedDynamicDays int `json:"wrapped_dynamic_days"`
WrapperrPort int `json:"wrapperr_port"`
PlexAuth bool `json:"plex_auth"`
BasicAuth bool `json:"basic_auth"`
WinterTheme bool `json:"winter_theme"`
}

type WrapperrConfigLegacy struct {
Expand Down Expand Up @@ -47,17 +49,19 @@ type SetWrapperrConfig struct {
TautulliConfig []TautulliConfig `json:"tautulli_config"`
WrapperrCustomize WrapperrCustomize `json:"wrapperr_customize"`
WrapperrData struct {
UseCache bool `json:"use_cache"`
UseLogs bool `json:"use_logs"`
PlexAuth bool `json:"plex_auth"`
BasicAuth bool `json:"basic_auth"`
WrapperrRoot string `json:"wrapperr_root"`
CreateShareLinks bool `json:"create_share_links"`
Timezone string `json:"timezone"`
ApplicationName string `json:"application_name"`
ApplicationURL string `json:"application_url"`
WrappedStart int `json:"wrapped_start"`
WrappedEnd int `json:"wrapped_end"`
WinterTheme bool `json:"winter_theme"`
UseCache bool `json:"use_cache"`
UseLogs bool `json:"use_logs"`
PlexAuth bool `json:"plex_auth"`
BasicAuth bool `json:"basic_auth"`
WrapperrRoot string `json:"wrapperr_root"`
CreateShareLinks bool `json:"create_share_links"`
Timezone string `json:"timezone"`
ApplicationName string `json:"application_name"`
ApplicationURL string `json:"application_url"`
WrappedStart int `json:"wrapped_start"`
WrappedEnd int `json:"wrapped_end"`
WinterTheme bool `json:"winter_theme"`
WrappedDynamic bool `json:"wrapped_dynamic"`
WrappedDynamicDays int `json:"wrapped_dynamic_days"`
} `json:"wrapperr_data"`
}
6 changes: 6 additions & 0 deletions modules/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ func GetWrapperStatistics(user_name string, user_friendlyname string, user_id in

log.Println("Cache stage completed for " + user_name + ".")

// Change wrapped start & end dates if dynamic setting is configured
if config.WrappedDynamic {
config.WrappedEnd = int(time.Now().Unix())
config.WrappedStart = int(time.Now().AddDate(0, 0, -config.WrappedDynamicDays).Unix())
}

// Download/refresh data-set from Tautulli
wrapperr_data, wrapperr_data_complete, err := WrapperrDownloadDays(user_id, wrapperr_data, cacheLimit, config)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions routes/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func ApiSetConfig(context *gin.Context) {
config.ApplicationURL = config_payload.WrapperrData.ApplicationURL
config.WrappedEnd = config_payload.WrapperrData.WrappedEnd
config.WrappedStart = config_payload.WrapperrData.WrappedStart
config.WrappedDynamic = config_payload.WrapperrData.WrappedDynamic
config.WrappedDynamicDays = config_payload.WrapperrData.WrappedDynamicDays
config.WinterTheme = config_payload.WrapperrData.WinterTheme

err = files.SaveConfig(config)
Expand Down
8 changes: 8 additions & 0 deletions web/assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -518,4 +518,12 @@ a {
align-content: center;
justify-content: center;
align-items: center;
}

.dynamic-enabled, .static-enabled {
display: flex;
}

.dynamic-disabled, .static-disabled {
display: none;
}
2 changes: 2 additions & 0 deletions web/html/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ <h1 id='application_name'>Wrapperr Setup</h1>

var wrapped_start = '';
var wrapped_end = '';
var wrapped_dynamic = false;
var wrapped_dynamic_days = 0;
var create_share_links = '';
var obfuscate_other_users = false;

Expand Down
3 changes: 3 additions & 0 deletions web/js/adminFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ function get_config(cookie) {
wrapped_end = new Date(0);
wrapped_end.setUTCSeconds(result.data.wrapped_end);

wrapped_dynamic = result.data.wrapped_dynamic
wrapped_dynamic_days = result.data.wrapped_dynamic_days

stats_order_by_plays = result.data.wrapperr_customize.stats_order_by_plays;
stats_order_by_duration = result.data.wrapperr_customize.stats_order_by_duration;

Expand Down
96 changes: 77 additions & 19 deletions web/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ function loadAdminPage() {
html += '<div class="warning">!<br>The more unique days in the wrapped period, the more Tautulli API calls. It is recommended to enable "Cache results for later use" to prevent long load times.</div>';
html += '</div>';

html += '<div class="form-group">';
html += '<label for="wrapped_dynamic" title="Whether the time frame is static or dynamic.">Dynamic time frame:<br>';
html += '<input type="checkbox" class="form-control" id="wrapped_dynamic" onclick="toggleWrappedPeriod();" ';
if(wrapped_dynamic) {
html += 'checked="' + wrapped_dynamic + '" ';
}
html += '/><br>';
html += '</div>';

html += '<div class="form-group newline">';
html += '</div>';

html += '<div id="wrapped-static" class="wrapped-static static-enabled">';

html += '<div class="form-group">';
html += '<label for="wrapped_start" title="The start of the period you want wrapped.">Start of wrapped period:<br>';
html += '<input type="datetime-local" class="form-control" id="wrapped_start" value="' + temp_date + '" required /><br>';
Expand All @@ -45,6 +59,17 @@ function loadAdminPage() {
html += '<input type="datetime-local" class="form-control" id="wrapped_end" value="' + temp_date + '" required /></label>';
html += '</div>';

html += '</div>';

html += '<div id="wrapped-dynamic" class="wrapped-dynamic dynamic-disabled">';

html += '<div class="form-group">';
html += '<label for="wrapped_dynamic_days" title="How many days into the past should be included in the time period?">Days in the past to include:<br>';
html += '<input type="number" class="form-control" id="wrapped_dynamic_days" value="' + wrapped_dynamic_days + '" required /></label>';
html += '</div>';

html += '</div>';

html += '<div class="form-group newline">';
html += '<hr>';
html += '</div>';
Expand Down Expand Up @@ -153,6 +178,10 @@ function loadAdminPage() {
document.getElementById("setup").innerHTML = html;

getTimezones();

if(wrapped_dynamic) {
toggleWrappedPeriod();
}
}

function set_wrapperr_settings_call() {
Expand All @@ -175,6 +204,8 @@ function set_wrapperr_settings_call() {
timezone = document.getElementById('timezone').value;
clear_cache = document.getElementById('clear_cache').checked;
winter_theme = document.getElementById('winter_theme').checked;
wrapped_dynamic = document.getElementById('wrapped_dynamic').checked;
wrapped_dynamic_days = document.getElementById('wrapped_dynamic_days').value;

if(wrapperr_root_original !== wrapperr_root) {
if(!confirm("You have changed the Wrapperr root/base URL. Wrapperr will attempt to restart and the interface to access this webpage will change. If you are using an URL base you must use a trailing '/' at the end of your new URL.")){
Expand Down Expand Up @@ -228,25 +259,27 @@ function set_wrapperr_settings_call() {
}

wrapperr_settings_form = {
"clear_cache" : clear_cache,
"data_type" : "wrapperr_data",
"tautulli_config" : [],
"wrapperr_customize" : {},
"wrapperr_data" : {
"use_cache" : use_cache,
"use_logs" : use_logs,
"plex_auth" : plex_auth,
"basic_auth" : basic_auth,
"wrapperr_root" : wrapperr_root,
"create_share_links" : create_share_links,
"timezone" : timezone,
"application_name" : application_name_str,
"application_url" : application_url_str,
"wrapped_start" : Math.round(wrapped_start.getTime() / 1000),
"wrapped_end" : Math.round(wrapped_end.getTime() / 1000),
"winter_theme" : winter_theme
}
};
"clear_cache" : clear_cache,
"data_type" : "wrapperr_data",
"tautulli_config" : [],
"wrapperr_customize" : {},
"wrapperr_data" : {
"use_cache" : use_cache,
"use_logs" : use_logs,
"plex_auth" : plex_auth,
"basic_auth" : basic_auth,
"wrapperr_root" : wrapperr_root,
"create_share_links" : create_share_links,
"timezone" : timezone,
"application_name" : application_name_str,
"application_url" : application_url_str,
"wrapped_start" : Math.round(wrapped_start.getTime() / 1000),
"wrapped_end" : Math.round(wrapped_end.getTime() / 1000),
"wrapped_dynamic" : wrapped_dynamic,
"wrapped_dynamic_days" : parseInt(wrapped_dynamic_days),
"winter_theme" : winter_theme
}
};

var wrapperr_settings_data = JSON.stringify(wrapperr_settings_form);

Expand Down Expand Up @@ -320,4 +353,29 @@ function placeTimezones(timezoneArray) {
option.name = item;
dataList.appendChild(option);
});
}

function toggleWrappedPeriod() {
wrapped_dynamic = document.getElementById("wrapped-dynamic")
wrapped_static = document.getElementById("wrapped-static")

if(wrapped_dynamic.classList.contains("dynamic-enabled")) {
wrapped_dynamic.classList.remove("dynamic-enabled")
wrapped_dynamic.classList.add("dynamic-disabled")
} else if(wrapped_dynamic.classList.contains("dynamic-disabled")) {
wrapped_dynamic.classList.add("dynamic-enabled")
wrapped_dynamic.classList.remove("dynamic-disabled")
} else {
wrapped_dynamic.classList.add("dynamic-disabled")
}

if(wrapped_static.classList.contains("static-enabled")) {
wrapped_static.classList.remove("static-enabled")
wrapped_static.classList.add("static-disabled")
} else if(wrapped_static.classList.contains("static-disabled")) {
wrapped_static.classList.add("static-enabled")
wrapped_static.classList.remove("static-disabled")
} else {
wrapped_static.classList.add("static-enabled")
}
}

0 comments on commit b0c575c

Please sign in to comment.