How do I change this structure so that I can get the values of this object like a map this is my const object
const dependencyMetrics : {[dependancyName : string] : {[name : string] : MetricInfo }[]} = {
MyService : [{
failure : {
metric: new Metric({
namespace: 'namespace',
metricName: 'ABC'
}),
metricProperties: {
period: Duration.seconds(60),
statistic: 'Average'
}
},
time : {
metric: new Metric({
namespace: 'namespace',
metricName: 'age'
}),
metricProperties: {
period: Duration.seconds(60),
statistic: 'Average'
}
}
}]};
//I am using this also as a type :
type MetricInfo = {
metric : Metric;
metricProperties : MetricOptions;
}
What I want is this code to behave smoothly when I try some thing like : dependencyMetric.MyService.failure or dependencyMetric.MyService.time
currently getting a lot of Objects and stuff don't know how to make this a hierarchy map
Your current structure treats MyService
as a key for an array of objects. Which is why your getting unexpected results when trying to access failure
or time
From what i understood, you want MyService
to be a key for an object containing failure
and time
metrics, not an array.
Try this:
const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
MyService : {
failure : {
metric: new Metric({
namespace: 'namespace',
metricName: 'ABC'
}),
metricProperties: {
period: Duration.seconds(60),
statistic: 'Average'
}
},
time : {
metric: new Metric({
namespace: 'namespace',
metricName: 'age'
}),
metricProperties: {
period: Duration.seconds(60),
statistic: 'Average'
}
}
}
};
Your MetricInfo
type will stay the same
Edit:
First, to add more services to your dependencyMetrics
object, you can just include them in the object:
const dependencyMetrics : {[dependencyName : string] : {[name : string] : MetricInfo }} = {
MyService1 : {
// ...metrics for MyService1
},
MyService2 : {
// ...metrics for MyService2
},
// add as many services as you need
};
Then, to access the metrics of a specific service dynamically, you can use the bracket notation to get the value associated with a key:
let serviceName = 'MyService1'; // change this to the service name you want
let metricName = 'failure'; // change this to the metric you want
let specificMetric = dependencyMetrics[serviceName][metricName];