0

I'm Working on AngularJS. In this part of the project my goal is to obtain a JSON structure after filling a form with some particulars values.

Here's the fiddle of my simple form: Fiddle

With the form I will do a query to KairosDB, that is my NoSql Database, I will query data from it by a JSON object. The form is structured in this way:

  • a Name
  • a certain Number of Tags, with Tag Id ("ch" for example) and tag value ("932" for example)
  • a certain Number of Aggregators to manipulate data coming from DB
  • Start Timestamp and End Timestamp (now they are static and only included in the final JSON Object)

After filling this form, with my code I'll obtain for example this JSON object:

{
    "metrics": [
    {
      "tags": [
        {
          "id": "ch",
          "value": "932"
        },
        {
          "id": "ch",
          "value": "931"
        }
      ],
      "aggregators": {
        "name": "sum",
        "sampling": [
          {
            "value": "1",
            "unit": "milliseconds",
            "type": "SUM"
          }
        ]
      }
    }
  ],
  "cache_time": 0,
  "start_absolute": 123,
  "end_absolute": 1234

}

Unfortunately, KairosDB accepts a different structure, and as you could see, Tag id "ch" doesn't hase an "id" string before, or for example, Tag values coming from the same tag id are grouped together

{
  "metrics": [
    {
      "tags": {
        "ch": [
          "932",
          "931"
        ]
      },
      "name": "AIENR",
      "aggregators": [
        {
          "name": "sum",
          "sampling": {
            "value": "1",
            "unit": "milliseconds"
          }
        }
      ]
    }
  ],
  "cache_time": 0,
  "start_absolute": 1367359200000,
  "end_absolute": 1386025200000
}

My question is: Is there a way to obtain the JSON structure like the one accepted by Kairos DB with an Angular JS form?. Thanks to everyone. I've seen this topic as the one more similar to mine but it isn't in AngularJS.

1 Answer 1

1

Personally, I'd do the refactoring work in the backend - Have what ever server interfaces sends and receives data do the manipulation - Otherwise you'll end up needing to refactor your data inside Angular anywhere you want to use that dataset.

Where as doing it in the backend would put it in a single access point.

Of course, you could do it in Angular, just replace userString in the submitData method with a copy of the array and replace the tags section with data in the new format, and likewise refactor the returned result to the correct format when you get a reply.

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

1 Comment

Consider this fiddle for example link . Is there a way to dynamically assign a different name to "Options" field in the Questions variable? I want to obtain a similar object but with some of the field dynamics. Thanks

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.