Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions code/__DEFINES/elastic.dm
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@
#define ELASCAT_BALANCE "balance"
#define ELASCAT_MEDICAL "medical"
#define ELASCAT_ENCHANTING "enchanting"
#define ELASCAT_HEARTBEAT "heartbeat"
#define ELASCAT_ROUND "round"

/* Abstract Data */
/* Combat */
/// A mob has had its head dismembered.
#define ELASDATA_DECAPITATIONS "decapitations"
/// An animal mob has eaten a corpse.
#define ELASDATA_EATEN_BODIES "eaten_bodies"
/* Underworld */
/// An underworld spirit has been revived with a Toll.
#define ELASDATA_COIN_REVIVES "coin_revive"
/// An underworld spirit has won a pit fight.
#define ELASDATA_FIGHT_REVIVES "fight_revives"
/* Economy */
#define ELASDATA_MAMMONS_GAINED "mammons_gained"
#define ELASDATA_MAMMONS_SPENT "mammons_spent"
#define ELASDATA_TAXES_EVADED "taxes_evaded"
#define ELASDATA_TAXES_COLLECTED "taxes_collected"
#define ELASDATA_WAGES_PAID "wages_paid"
#define ELASDATA_FINE_INCOME "fine_income"
#define ELASDATA_IMPORT_VALUE "import_value"
#define ELASDATA_EXPORT_VALUE "export_value"
#define ELASDATA_GOLDFACE_SPENT "goldface_spent"
#define ELASDATA_NOBLE_INCOME "noble_income"
#define ELASDATA_TRIUMPH_AWARDED "triumph_awarded"
#define ELASDATA_TRIUMPH_SPENT "triumph_spent"

/* Medical */
#define ELASDATA_ANASTASIS_REVIVE "anastasis"
Expand All @@ -39,3 +46,8 @@
#define ELASDATA_LUX_REVIVE "lux_revive"
#define ELASDATA_LUX_EXTRACT "lux_extract_npc"
#define ELASDATA_LUX_EXTRACT_PLAYER "lux_extract_player"
/// An underworld spirit has been revived with a Toll.
#define ELASDATA_COIN_REVIVES "coin_revive"
/// An underworld spirit has won a pit fight.
#define ELASDATA_FIGHT_REVIVES "fight_revives"
#define ELASDATA_DEATH "deaths"
2 changes: 1 addition & 1 deletion code/controllers/subsystem/death_arena.dm
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ SUBSYSTEM_DEF(death_arena)
/obj/structure/table/wood/fine/altar/after_added_effects(obj/item/item, mob/user)
if(!istype(item, /obj/item/bodypart/head))
return
add_abstract_elastic_data(ELASCAT_COMBAT, ELASDATA_FIGHT_REVIVES, 1)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_FIGHT_REVIVES, 1)
record_round_statistic(STATS_UNDERWORLD_DUELS)
SSdeath_arena.process_fight_end(item, user)

Expand Down
141 changes: 0 additions & 141 deletions code/controllers/subsystem/elastic.dm

This file was deleted.

47 changes: 47 additions & 0 deletions code/controllers/subsystem/elastic/_helpers.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/proc/add_elastic_data(main_cat, list/assoc_data)
if(!main_cat || !length(assoc_data))
return
var/datum/elastic_shard/S = SSelastic.get_shard(main_cat)
if(!S)
return
S.add_list_data(assoc_data)
return TRUE

/proc/add_elastic_data_immediate(main_cat, list/assoc_data)
if(!main_cat || !length(assoc_data))
return
var/datum/elastic_shard/S = SSelastic.get_shard(main_cat)
if(!S)
return
S.add_list_data(assoc_data)
var/compiled = S.get_compiled_data(SSelastic)
if(compiled)
SSelastic.dispatch_request(compiled)
S.reset()
return TRUE

/proc/add_abstract_elastic_data(main_cat, abstract_name, abstract_value, maximum)
if(!main_cat || !isnum(abstract_value))
return
var/datum/elastic_shard/S = SSelastic.get_shard(main_cat)
if(!S)
return
S.add_abstract_data(abstract_name, abstract_value, maximum)
return TRUE

/proc/init_abstract_zeros()
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_FIGHT_REVIVES, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_COIN_REVIVES, 0)
add_abstract_elastic_data(ELASCAT_COMBAT, ELASDATA_EATEN_BODIES, 0)
add_abstract_elastic_data(ELASCAT_COMBAT, ELASDATA_DECAPITATIONS, 0)
add_abstract_elastic_data(ELASCAT_ECONOMY, ELASDATA_MAMMONS_GAINED, 0)
add_abstract_elastic_data(ELASCAT_ECONOMY, ELASDATA_MAMMONS_SPENT, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_ANASTASIS_REVIVE, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_CPR_REVIVE, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_ABSOLVE_REVIVE, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_ULTIMATE_REVIVE, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_LUX_REVIVE, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_LUX_EXTRACT, 0)
add_abstract_elastic_data(ELASCAT_MEDICAL, ELASDATA_LUX_EXTRACT_PLAYER, 0)
add_abstract_elastic_data(ELASCAT_ECONOMY, ELASDATA_TRIUMPH_SPENT, 0)
add_abstract_elastic_data(ELASCAT_ECONOMY, ELASDATA_TRIUMPH_AWARDED, 0)
31 changes: 31 additions & 0 deletions code/controllers/subsystem/elastic/config.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/datum/config_entry/flag/elastic_middleware_enabled

/datum/config_entry/string/elastic_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/combat_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/economy_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/medical_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/crafting_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/enchanting_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/round_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/storyteller_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/heartbeat_endpoint
protection = CONFIG_ENTRY_HIDDEN

/datum/config_entry/string/metrics_api_token
protection = CONFIG_ENTRY_HIDDEN
50 changes: 50 additions & 0 deletions code/controllers/subsystem/elastic/elastic.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
SUBSYSTEM_DEF(elastic)
name = "Elastic Middleware"
wait = 5 SECONDS
runlevels = RUNLEVEL_LOBBY | RUNLEVEL_SETUP | RUNLEVEL_GAME | RUNLEVEL_POSTGAME
flags = SS_KEEP_TIMING
var/world_init_time = 0
var/list/active_requests = list()
var/list/shards = list()
var/shutting_down = FALSE

/datum/controller/subsystem/elastic/Initialize(start_timeofday)
if(!CONFIG_GET(flag/elastic_middleware_enabled))
flags |= SS_NO_FIRE
return ..()
register_shards()
init_abstract_zeros()
return ..()

/datum/controller/subsystem/elastic/proc/register_shards()
for(var/datum/elastic_shard/shard as anything in subtypesof(/datum/elastic_shard))
shards += new shard()

/datum/controller/subsystem/elastic/proc/get_shard(shard_category)
for(var/datum/elastic_shard/S as anything in shards)
if(S.shard_category == shard_category)
return S

/datum/controller/subsystem/elastic/fire(resumed)
for(var/datum/elastic_shard/S as anything in shards)
if(S.should_fire())
S.fire(src)
// Clean up completed requests
for(var/datum/http_request/request as anything in active_requests)
if(request.is_complete())
active_requests -= request
qdel(request)

/datum/controller/subsystem/elastic/proc/dispatch_request(datum/elastic_shard/shard, json_body)
if(shutting_down)
return
var/endpoint = shard.get_endpoint()
if(!endpoint)
return
var/datum/http_request/request = new()
request.prepare(RUSTG_HTTP_METHOD_POST, endpoint, json_body, list(
"Authorization" = "ApiKey [CONFIG_GET(string/metrics_api_token)]",
"Content-Type" = "application/json"
))
request.begin_async()
active_requests += request
57 changes: 57 additions & 0 deletions code/controllers/subsystem/elastic/shards/_base_shard.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/datum/elastic_shard
/// Display name for debugging
var/name = "Generic Shard"
/// How often this shard fires, in real seconds
var/upload_frequency = 30 SECONDS
/// Elasticsearch index/category this shard posts to (can be a different endpoint suffix)
var/shard_category = ELASCAT_GENERIC
/// Tracks the last REALTIMEOFDAY this shard fired
var/last_fired = 0
/// Accumulated assoc data for this shard
var/list/assoc_list_data = list()
/// Abstract (cumulative numeric) data, keyed by ELASDATA_ defines
var/list/abstract_information = list()
///should we keep things dated? (send on loop if its being triggered once)
var/should_keep_dated = FALSE

/datum/elastic_shard/proc/should_fire()
return (REALTIMEOFDAY - last_fired) >= upload_frequency

/datum/elastic_shard/proc/get_endpoint()
return CONFIG_GET(string/elastic_endpoint)

/datum/elastic_shard/proc/fire(datum/controller/subsystem/elastic/SS)
last_fired = REALTIMEOFDAY
var/compiled = get_compiled_data(SS)
if(!compiled)
return
SS.dispatch_request(src, compiled)
reset()

/datum/elastic_shard/proc/get_compiled_data(datum/controller/subsystem/elastic/SS)
if(!length(assoc_list_data) && (should_keep_dated && !length(abstract_information)))
return null
var/list/compiled = list()
compiled["@timestamp"] = time_stamp_metric()
compiled["shard"] = shard_category
compiled["round_id"] = GLOB.rogue_round_id
compiled["elapsed_real_time"] = (REALTIMEOFDAY - SS.world_init_time)
compiled |= assoc_list_data
return json_encode(compiled)

/datum/elastic_shard/proc/reset()
assoc_list_data = list()
// Note: abstract_information is NOT reset - it's cumulative

/datum/elastic_shard/proc/add_list_data(list/assoc_data)
assoc_list_data |= assoc_data

/datum/elastic_shard/proc/add_abstract_data(abstract_name, abstract_value, maximum)
if(!isnum(abstract_value))
return
abstract_information |= abstract_name
abstract_information[abstract_name] += abstract_value
if(maximum)
abstract_information[abstract_name] = min(maximum, abstract_information[abstract_name])
// Mirror into assoc so it gets compiled
assoc_list_data["[abstract_name]"] = abstract_information[abstract_name]
7 changes: 7 additions & 0 deletions code/controllers/subsystem/elastic/shards/combat.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/datum/elastic_shard/combat
name = "Combat"
upload_frequency = 2 MINUTES
shard_category = ELASCAT_COMBAT

/datum/elastic_shard/combat/get_endpoint()
return CONFIG_GET(string/combat_endpoint)
7 changes: 7 additions & 0 deletions code/controllers/subsystem/elastic/shards/crafting.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/datum/elastic_shard/crafting
name = "Crafting"
upload_frequency = 5 MINUTES
shard_category = ELASCAT_CRAFTING

/datum/elastic_shard/crafting/get_endpoint()
return CONFIG_GET(string/crafting_endpoint)
Loading
Loading