0

I am trying to write a dashboard in Angular for metrics generated by DropWizard. The json (partial snnipet) returned by metrics is

[ {
  'gauges': {
      'TaskPoolService.converter': {
          'value': {        
              'poolSize': 4,
              'completedTaskCount': 153,
              'taskCount': 234
          }                 
      },                
      'TaskPoolService.loader': {
          'value': {        
              'poolSize': 4,
              'completedTaskCount': 473,
              'taskCount': 474
          }

       ....  (many more) .....               

  } ]

I want to convert it into model for use in template

class Guage { 
      metricName: string;
      poolSize: number;
      completedTaskCount: number;
      taskCount: number;
}

The above json should converted into following array

[
    {
        metricName: 'TaskPoolService.converter',
        poolSize: 4,
        completedTaskCount: 153,
        taskCount: 234
    },
    {
        metricName: 'TaskPoolService.loader',
        poolSize: 4,
        completedTaskCount: 473,
        taskCount: 474
    }];

I have shown only the partial listing of metrics in json and there may be many more metrics. And also, the new metrics may get added dynamically. In other words, metric name is not constant.

Is it possible to convert the property name (which is dynamic) such as TaskPoolService.converter into metricName: 'TaskPoolService.converter'

if not possible, whether json array can be referred in template directly as key, value pair in ngFor loop so that {{ key }} can output as TaskPoolService.converter

1 Answer 1

1

This code create your json output mentioned in question. I hope it helps you.

var x = [{
  gauges: {
    "TaskPoolService.converter": {
      value: {
        poolSize: 4,
        completedTaskCount: 153,
        taskCount: 234
      }
    },
    "TaskPoolService.loader": {
      value: {
        poolSize: 4,
        completedTaskCount: 473,
        taskCount: 474
      }
    }
  }
}];

var output = [];

x.forEach(function(itm) {
  Object.entries(itm).forEach(function(ii) {
    Object.entries(ii[1]).forEach(function(tt) {
      output.push({
        metricName: tt[0],
        poolSize: tt[1].value.poolSize,
        completedTaskCount: tt[1].value.completedTaskCount,
        taskCount: tt[1].value.taskCount
      })
    })
  })
});

console.log(output);

Sign up to request clarification or add additional context in comments.

1 Comment

perfect and clean solution... Thanks a ton

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.