diff --git a/history.lua b/history.lua index 205ae34..e19b4d5 100644 --- a/history.lua +++ b/history.lua @@ -38,7 +38,7 @@ function private.read_record(item_key) record = private.new_record() end - if record.next_push < time() then + if record.next_push <= time() then private.push_record(record) private.write_record(item_key, record) end @@ -80,9 +80,10 @@ function public.price_data(item_key) end function public.value(item_key) - if not private.value_cache[item_key] then + if not private.value_cache[item_key] or private.value_cache[item_key].next_push <= time() then local item_record = private.read_record(item_key) + local value, unreliable if getn(item_record.data_points) > 0 then local weighted_values = {} local total_weight = 0 @@ -95,13 +96,18 @@ function public.value(item_key) weighted_value.weight = weighted_value.weight / total_weight end - private.value_cache[item_key] = private.weighted_median(weighted_values) + value = private.weighted_median(weighted_values) + -- not seen in the last week or not ignoring at least two high and low outliers + unreliable = item_record.data_points[1].time < time() - 60 * 60 * 24 * 7 or weighted_values[1].weight + (weighted_values[2] and weighted_values[2].weight or 0) >= 0.5 else - private.value_cache[item_key] = private.market_value(item_record) + value = private.market_value(item_record) + unreliable = true end + + private.value_cache[item_key] = {value=value, unreliable=unreliable, next_push=item_record.next_push} end - return private.value_cache[item_key] + return private.value_cache[item_key].value, private.value_cache[item_key].unreliable end function public.market_value(item_key) diff --git a/tooltip.lua b/tooltip.lua index 25a51fe..852f0a6 100644 --- a/tooltip.lua +++ b/tooltip.lua @@ -71,7 +71,7 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local item_key = (item_id or 0)..':'..(suffix_id or 0) - local value = Aux.history.value(item_key) + local value, unreliable = Aux.history.value(item_key) local value_line = 'Value: ' @@ -79,7 +79,7 @@ function private.extend_tooltip(tooltip, hyperlink, quantity) local _, _, data_points = Aux.history.price_data(item_key) -- mark as unreliable if less than 5 data points in the last month - if value and (not data_points[5] or data_points[5].time < time() - 60 * 60 * 24 * 30) then + if value and unreliable then value_line = value_line..' (?)' end