遇到 TypeScript 多层次键映射对的问题

huangapple go评论67阅读模式
英文:

Facing issues with typescript multi hierarchy key map pair

问题

我要的是这段代码在我尝试像这样做时能够平稳运行:dependencyMetric.MyService.failure或dependencyMetric.MyService.time,目前得到了很多对象和其他东西,不知道如何将其变成一个层次结构映射。

英文:

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

答案1

得分: 0

你目前的结构将 MyService 视为对象数组的键。这就是为什么在尝试访问 failuretime 时会得到意外的结果。

从我的理解,你希望 MyService 成为一个包含 failuretime 指标的对象键,而不是一个数组。

尝试这样做:

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'
            }
        }
    }
};

你的 `MetricInfo` 类型将保持不变

编辑:

首先,要将更多的服务添加到你的 dependencyMetrics 对象中,你只需要在对象中包含它们:

const dependencyMetrics : {[dependencyName : string] :  {[name : string] : MetricInfo }} = {
    MyService1 : {
        // ...MyService1 的指标
    },
    MyService2 : {
        // ...MyService2 的指标
    },
    // 添加所需的服务
};

然后,要动态访问特定服务的指标,你可以使用方括号表示法来获取与键关联的值:

let serviceName = 'MyService1';  // 更改为你想要的服务名称
let metricName = 'failure';  // 更改为你想要的指标

let specificMetric = dependencyMetrics[serviceName][metricName];
英文:

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];

huangapple
  • 本文由 发表于 2023年7月14日 01:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定