Skip to content

Commit d0d12c2

Browse files
Rachel ChenRachel Chen
authored andcommitted
fixed params[]
1 parent 3dc3e12 commit d0d12c2

File tree

11 files changed

+148
-67
lines changed

11 files changed

+148
-67
lines changed

snuba/admin/notifications/slack/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def build_blocks(
3333
def build_allocation_policy_changed_text(
3434
data: Any, action: AuditLogAction
3535
) -> Optional[str]:
36+
# rachelhandlethis
3637
base = f"*Storage {data['storage']} Allocation Policy Changed:*"
3738

3839
if action == AuditLogAction.ALLOCATION_POLICY_DELETE:

snuba/admin/static/api_client.tsx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
CardinalityQueryResult,
3636
} from "SnubaAdmin/cardinality_analyzer/types";
3737

38-
import { AllocationPolicy, Configuration, Entity } from "SnubaAdmin/capacity_management/types";
38+
import { AllocationPolicy, ConfigurableComponent, Configuration, Entity } from "SnubaAdmin/capacity_management/types";
3939

4040
import { ReplayInstruction, Topic } from "SnubaAdmin/dead_letter_queue/types";
4141
import { AutoReplacementsBypassProjectsData } from "SnubaAdmin/auto_replacements_bypass_projects/types";
@@ -89,14 +89,14 @@ interface Client {
8989
getRoutingStrategyConfigs: (strategy_name: string) => Promise<Configuration[]>;
9090
setConfiguration: (
9191
entity: Entity,
92-
configurable_component_name: string,
92+
configurable_component: ConfigurableComponent,
9393
key: string,
9494
value: string,
9595
params: object,
9696
) => Promise<void>;
9797
deleteConfiguration: (
9898
entity: Entity,
99-
configurable_component_name: string,
99+
configurable_component: ConfigurableComponent,
100100
key: string,
101101
params: object,
102102
) => Promise<void>;
@@ -475,24 +475,25 @@ function Client(): Client {
475475
},
476476
setConfiguration: (
477477
entity: Entity,
478-
configurable_component_name: string,
478+
configurable_component: ConfigurableComponent,
479479
key: string,
480480
value: string,
481481
params: object,
482482
) => {
483-
console.log("dowenotheithere")
484483
let body: string;
485484
let url: string;
486485

487-
console.log("paramskdjlak", params)
488-
489-
if (entity.type === "storage") {
490-
body = JSON.stringify({ storage: entity.name, policy: configurable_component_name, key, value, params })
491-
url = baseUrl + "allocation_policy_config";
492-
} else {
493-
body = JSON.stringify({ strategy: configurable_component_name, key, value, params });
486+
if (configurable_component.type === "routing_strategy") {
487+
body = JSON.stringify({ strategy: configurable_component.name, key, value, params });
494488
url = baseUrl + "routing_strategy_config";
489+
} else if (entity.type === "strategy" && configurable_component.type === "allocation_policy") {
490+
body = JSON.stringify({ strategy: entity.name, policy: configurable_component.name, key, value, params })
491+
url = baseUrl + "allocation_policy_config_for_strategy";
492+
} else {
493+
body = JSON.stringify({ storage: entity.name, policy: configurable_component.name, key, value, params })
494+
url = baseUrl + "allocation_policy_config_for_storage";
495495
}
496+
496497
return fetch(url, {
497498
headers: { "Content-Type": "application/json" },
498499
method: "POST",
@@ -510,23 +511,21 @@ function Client(): Client {
510511
},
511512
deleteConfiguration: (
512513
entity: Entity,
513-
configurable_component_name: string,
514+
configurable_component: ConfigurableComponent,
514515
key: string,
515516
params: object,
516517
) => {
517518
let body: string;
518519
let url: string;
519520

520-
if (entity.type === "storage") {
521-
body = JSON.stringify({ storage: entity.name, policy: configurable_component_name, key, params })
521+
if (configurable_component.type === "allocation_policy") {
522+
body = JSON.stringify({ storage: entity.name, policy: configurable_component.name, key, params })
522523
url = baseUrl + "allocation_policy_config";
523524
} else {
524-
body = JSON.stringify({ strategy: configurable_component_name, key, params });
525+
body = JSON.stringify({ strategy: configurable_component.name, key, params });
525526
url = baseUrl + "routing_strategy_config";
526527
}
527528

528-
console.log("paramskdjlakdelete???", params)
529-
530529
return fetch(url, {
531530
headers: { "Content-Type": "application/json" },
532531
method: "DELETE",

snuba/admin/static/capacity_management/allocation_policy.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function Configurations(props: {
7575
api
7676
.deleteConfiguration(
7777
entity,
78-
configurable_component.name,
78+
configurable_component,
7979
toDelete.name,
8080
toDelete.params
8181
)
@@ -95,7 +95,7 @@ function Configurations(props: {
9595
api
9696
.setConfiguration(
9797
entity,
98-
configurable_component.name,
98+
configurable_component,
9999
config.name,
100100
config.value,
101101
config.params

snuba/admin/static/capacity_management/types.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { ReactNode } from "react";
22

33

4-
type ConfigurableComponent = {
4+
interface ConfigurableComponent {
5+
type: string;
56
name: string;
67
configs: Configuration[];
78
optional_config_definitions: OptionalConfigurationDefinition[];
89
};
910

10-
type AllocationPolicy = ConfigurableComponent & {
11+
interface AllocationPolicy extends ConfigurableComponent {
12+
type: "allocation_policy";
1113
query_type: string;
1214
};
1315

14-
type RoutingStrategy = ConfigurableComponent;
16+
interface RoutingStrategy extends ConfigurableComponent {
17+
type: "routing_strategy";
18+
}
1519

1620
type Configuration = {
1721
name: string;
@@ -78,5 +82,5 @@ export {
7882
isStorage,
7983
isStrategy,
8084
StorageEntity,
81-
StrategyEntity, ConfigurableComponent,
85+
StrategyEntity, ConfigurableComponent, RoutingStrategy
8286
};

snuba/admin/static/cbrs/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from "react";
22
import Client from "SnubaAdmin/api_client";
33
import { Configurations } from "SnubaAdmin/capacity_management/allocation_policy";
4-
import { AllocationPolicy, Configuration, ConfigurableComponent } from "SnubaAdmin/capacity_management/types";
4+
import { AllocationPolicy, Configuration, ConfigurableComponent, RoutingStrategy } from "SnubaAdmin/capacity_management/types";
55
import { CustomSelect, getParamFromStorage } from "SnubaAdmin/select";
66
import { COLORS } from "SnubaAdmin/theme";
77

@@ -61,7 +61,8 @@ function CapacityBasedRoutingSystem(props: { api: Client }) {
6161
return <p>No strategy configurations found.</p>;
6262
}
6363

64-
const strategyComponent: ConfigurableComponent = {
64+
const strategyComponent: RoutingStrategy = {
65+
type: "routing_strategy",
6566
name: selectedStrategy,
6667
configs: strategyConfigs,
6768
optional_config_definitions: [],

snuba/admin/static/tests/capacity_management/allocation_policies.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React from "react";
88

99
it("should populate configs table upon render", async () => {
1010
let allocationPolicy: AllocationPolicy = {
11+
type: "allocation_policy",
1112
name: "some_policy",
1213
configs: [
1314
{

snuba/admin/static/tests/capacity_management/index.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ it("should display allocation policy configs once a storage is selected", async
3232
let storages = ["storage1", "storage2"];
3333
let allocationPolicies: AllocationPolicy[] = [
3434
{
35+
type: "allocation_policy",
3536
name: "some_policy",
3637
configs: [
3738
{
@@ -46,6 +47,7 @@ it("should display allocation policy configs once a storage is selected", async
4647
query_type: "select",
4748
},
4849
{
50+
type: "allocation_policy",
4951
name: "some_other_policy",
5052
configs: [
5153
{

snuba/admin/views.py

Lines changed: 101 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@
9797
)
9898
from snuba.web.rpc import RPCEndpoint, list_all_endpoint_names, run_rpc_handler
9999
from snuba.web.rpc.storage_routing.routing_strategies.storage_routing import (
100+
CBRS_HASH,
100101
BaseRoutingStrategy,
101102
)
102103
from snuba.web.views import dataset_query
103-
from snuba.query.allocation_policies import CAPMAN_HASH
104104

105105
logger = structlog.get_logger().bind(module=__name__)
106106

@@ -990,7 +990,8 @@ def _add_policy_data(
990990
for policy in policies:
991991
data.append(
992992
{
993-
"policy_name": policy.config_key(),
993+
"type": "allocation_policy",
994+
"name": policy.config_key(),
994995
"configs": policy.get_current_configs(),
995996
"optional_config_definitions": policy.get_optional_config_definitions_json(),
996997
"query_type": query_type,
@@ -1024,7 +1025,7 @@ def get_allocation_policy_configs_of_routing_strategy(strategy_name: str) -> Res
10241025

10251026
policies = BaseRoutingStrategy.get_from_name(
10261027
strategy_name
1027-
).get_allocation_policies()
1028+
)().get_allocation_policies()
10281029

10291030
data: list[dict[str, Any]] = []
10301031
_add_policy_data(policies, "select", data)
@@ -1041,13 +1042,15 @@ def get_routing_strategy_configs(strategy_name: str) -> Response:
10411042

10421043
configs = BaseRoutingStrategy.get_from_name(strategy_name)().get_configurations()
10431044
serialized_configs = [
1044-
cast(RoutingStrategyConfig, config).to_definition_dict() for config in configs.values()
1045+
cast(RoutingStrategyConfig, config).to_config_dict()
1046+
for config in configs.values()
10451047
]
1046-
print("isithere")
1048+
print("isithereRACHELITSHERE", serialized_configs)
10471049
return Response(
10481050
json.dumps(serialized_configs), 200, {"Content-Type": "application/json"}
10491051
)
10501052

1053+
10511054
@application.route("/routing_strategy_config", methods=["POST", "DELETE"])
10521055
@check_tool_perms(tools=[AdminTools.CAPACITY_BASED_ROUTING_SYSTEM])
10531056
def set_routing_strategy_config() -> Response:
@@ -1056,7 +1059,7 @@ def set_routing_strategy_config() -> Response:
10561059
user = request.headers.get(USER_HEADER_KEY)
10571060

10581061
try:
1059-
print("datafsdfsdf", data)
1062+
print("datafsdfsdf", data, data["strategy"])
10601063
strategy, key = (data["strategy"], data["key"])
10611064
params = data.get("params", {})
10621065
print("params", params)
@@ -1068,10 +1071,12 @@ def set_routing_strategy_config() -> Response:
10681071
assert key != "", "Key cannot be empty string"
10691072

10701073
strategies = list(BaseRoutingStrategy.all_names())
1074+
print("strategies", strategies)
10711075
strategy = next(
10721076
(s for s in strategies if s == strategy),
10731077
None,
10741078
)
1079+
print("strategyyyy", strategy)
10751080
assert strategy is not None, "Strategy not found"
10761081

10771082
except (KeyError, AssertionError) as exc:
@@ -1087,9 +1092,94 @@ def set_routing_strategy_config() -> Response:
10871092
return Response("", 200)
10881093

10891094

1090-
@application.route("/allocation_policy_config", methods=["POST", "DELETE"])
1095+
@application.route("/allocation_policy_config_for_strategy", methods=["POST", "DELETE"])
1096+
@check_tool_perms(tools=[AdminTools.CAPACITY_MANAGEMENT])
1097+
def set_allocation_policy_config_for_strategy() -> Response:
1098+
data = json.loads(request.data)
1099+
user = request.headers.get(USER_HEADER_KEY)
1100+
1101+
try:
1102+
strategy, key, policy_name = (data["strategy"], data["key"], data["policy"])
1103+
print("dkfjlskjflks", data)
1104+
1105+
params = data.get("params", {})
1106+
1107+
assert isinstance(strategy, str), "Invalid strategy"
1108+
assert isinstance(key, str), "Invalid key"
1109+
assert isinstance(params, dict), "Invalid params"
1110+
assert key != "", "Key cannot be empty string"
1111+
assert isinstance(policy_name, str), "Invalid policy name"
1112+
strategy = BaseRoutingStrategy.get_from_name(strategy)
1113+
assert strategy is not None, "Strategy not found"
1114+
policies = (
1115+
strategy().get_allocation_policies()
1116+
+ strategy().get_delete_allocation_policies()
1117+
)
1118+
policy = next(
1119+
(p for p in policies if p.config_key() == policy_name),
1120+
None,
1121+
)
1122+
assert policy is not None, "Policy not found on strategy"
1123+
1124+
except (KeyError, AssertionError) as exc:
1125+
return Response(
1126+
json.dumps({"error": f"Invalid config: {str(exc)}"}),
1127+
400,
1128+
{"Content-Type": "application/json"},
1129+
)
1130+
1131+
if request.method == "DELETE":
1132+
policy.delete_config_value(
1133+
config_key=key, hash=CBRS_HASH, params=params, user=user
1134+
)
1135+
audit_log.record(
1136+
user or "",
1137+
AuditLogAction.ALLOCATION_POLICY_DELETE,
1138+
{
1139+
"storage": strategy,
1140+
"policy": policy.config_key(),
1141+
"key": key,
1142+
}, # todo: make audit log handle strategy in addition to storage
1143+
notify=True,
1144+
)
1145+
return Response("", 200)
1146+
elif request.method == "POST":
1147+
try:
1148+
value = data["value"]
1149+
assert isinstance(value, str), "Invalid value"
1150+
policy.set_config_value(
1151+
config_key=key, value=value, params=params, user=user
1152+
)
1153+
audit_log.record(
1154+
user or "",
1155+
AuditLogAction.ALLOCATION_POLICY_UPDATE,
1156+
{
1157+
"storage": strategy, # todo: make audit log handle strategy in addition to storage
1158+
"policy": policy.config_key(),
1159+
"key": key,
1160+
"value": value,
1161+
"params": str(params),
1162+
},
1163+
notify=True,
1164+
)
1165+
return Response("", 200)
1166+
except (KeyError, AssertionError) as exc:
1167+
return Response(
1168+
json.dumps({"error": f"Invalid config: {str(exc)}"}),
1169+
400,
1170+
{"Content-Type": "application/json"},
1171+
)
1172+
else:
1173+
return Response(
1174+
json.dumps({"error": "Method not allowed"}),
1175+
405,
1176+
{"Content-Type": "application/json"},
1177+
)
1178+
1179+
1180+
@application.route("/allocation_policy_config_for_storage", methods=["POST", "DELETE"])
10911181
@check_tool_perms(tools=[AdminTools.CAPACITY_MANAGEMENT])
1092-
def set_allocation_policy_config() -> Response:
1182+
def set_allocation_policy_config_for_storage() -> Response:
10931183
data = json.loads(request.data)
10941184
user = request.headers.get(USER_HEADER_KEY)
10951185

@@ -1115,15 +1205,16 @@ def set_allocation_policy_config() -> Response:
11151205
assert policy is not None, "Policy not found on storage"
11161206

11171207
except (KeyError, AssertionError) as exc:
1118-
print("4444444")
11191208
return Response(
11201209
json.dumps({"error": f"Invalid config: {str(exc)}"}),
11211210
400,
11221211
{"Content-Type": "application/json"},
11231212
)
11241213

11251214
if request.method == "DELETE":
1126-
policy.delete_config_value(config_key=key, hash=CAPMAN_HASH, params=params, user=user)
1215+
policy.delete_config_value(
1216+
config_key=key, hash=CAPMAN_HASH, params=params, user=user
1217+
)
11271218
audit_log.record(
11281219
user or "",
11291220
AuditLogAction.ALLOCATION_POLICY_DELETE,
@@ -1152,7 +1243,6 @@ def set_allocation_policy_config() -> Response:
11521243
)
11531244
return Response("", 200)
11541245
except (KeyError, AssertionError) as exc:
1155-
print("5555555")
11561246
return Response(
11571247
json.dumps({"error": f"Invalid config: {str(exc)}"}),
11581248
400,

0 commit comments

Comments
 (0)