Manipulate JSON data into cachet.io - json

I have just set up a Cachet status page but i am struggling to push updates to the components via it's API.
I am looking to take an existing JSON feed from a partner site and use this to update the status on my own page.
Here is a sample of the JSON data I need to pull:
{
"state":"online",
"message":"",
"description":"",
"append":"false",
"status":true,
"time":"Sat 23 Apr 2016 10:51:23 AM UTC +0000"
}
and below is the format Cachet uses in it's API.
{
"data": {
"id": 1,
"name": "Component Name",
"description": "Description",
"link": "",
"status": 1,
"order": 0,
"group_id": 0,
"created_at": "2015-08-01 12:00:00",
"updated_at": "2015-08-01 12:00:00",
"deleted_at": null,
"status_name": "Operational"
}
}
Never dealt with JSON stuff before, but I guess i need a script that i can run every X mins to grab the original data and do the following:
Convert the "state" from the original feed into Cachet ones.
Update the "updated_at" time with the time the script was last run.
Any help or tutorial's would be really appreciated.
Thanks!

I'm the Lead Developer of Cachet, thanks for trying it out!
All you need to do is update the Component status. Cachet will take care of the updated_at timestamp for you.
I'm unable to write the script for you, but you'd do something like this:
// This will be a lookup of the states from the service you're watching to Cachet.
serviceStates = [
'online' => 1,
'issues' => 2,
'offline' => 4,
]
// The id of the component we're updating
componentId = 1
// The state that is coming back from the service you're watching
state = 'online'
request({url: 'demo.cachethq.io/api/v1/components/'+componentId, data: { status: serviceStates[state] }})
Pseudo code, but you should be able to work from that.

Related

Parsing a very complex json response in dart

I'm trying to load a json file from a server response and parsing it in flutter, the model i create is working for all the other fields but i'm in trouble with this class
this is a part of the JSON response:
"episodes": {
"1": [
{
"id": "63",
"episode_num": 1,
"title": "Some Name",
"container_extension": "mp4",
"info": {
"director": "",
"plot": "",
"cast": "",
"rating": "",
"releasedate": "",
"movie_image": "",
"genre": "",
"duration_secs": 6495,
"duration": "01:48:15"
}
}
]
}
in this case the entry under episodes is just one but this will represents a season and all the episode inside it, so under episodes many of this entry (undefined number during coding) can be present
At this time, using online json to dart converter tools i can be able to retrive just this one entry but if a response have more than 1 season i can't see it.
There is any way to handle this?
EDIT:
Solved using a for cicle with max value = (json['episodes'].length + 1).
For the info stored inside each 'episodes' value i can use
json['episodes']['$i']
Valid JSON is always convertible to a Dart data structure. But what you may be asking is "can I get nested objects from this?", and that just depends on how hard you want to work. Some JSON-to-Dart tools are better than others and some JSON values are impossible for any automated tool to make sense of. Only real answer is: "it depends".

Couchbaselite change Changes returns all objects even though only one document was changed

I am working on a couchbase lite driven app and trying to do live query based on this help from couchbase mobile lite.
While it works, I am confused on the number of documents that reported as changed. This is only in my laptop so I uploaded json file to couchbase server via cbimport. Then sync gateway did sync all the data succesfully to my android app.
Now, I changed one document in couchbase server but all 27 documents are returned as changed in the live query. I was expecting only the document I have changed to be returned as changed since the last sync time.
Looking at the meta information of each document, the document I have changed have the following:
{
"meta": {
"id": "Group_2404_159_5053",
"rev": "15-16148876737400000000000002000006",
"expiration": 0,
"flags": 33554438,
"type": "json"
},
"xattrs": {
"_sync": {
"rev": "7-ad618346393fa2490359555e9c889876",
"sequence": 2951,
"recent_sequences": [
2910,
2946,
2947,
2948,
2949,
2950,
2951
],
"history": {
"revs": [
"3-89bb125a9bb1f5e8108a6570ffb31821",
"4-71480618242841447402418fa1831968",
"5-4c4d990af34fa3f53237c3faafa85843",
"1-4fbb4708f69d8a6cda4f9c38a1aa9570",
"6-f43462023f82a12170f31aed879aecb2",
"7-ad618346393fa2490359555e9c889876",
"2-cf80ca212a3279e4fc01ef6ab6084bc9"
],
"parents": [
6,
0,
1,
-1,
2,
4,
3
],
"channels": [
null,
null,
null,
null,
null,
null,
null
]
},
"cas": "0x0000747376881416",
"value_crc32c": "0x8c664755",
"time_saved": "2020-06-01T14:23:30.669338-07:00"
}
}
}
while the rest 26 documents are similar to this one:
{
"meta": {
"id": "Group_2404_159_5087",
"rev": "2-161344efd90c00000000000002000006",
"expiration": 0,
"flags": 33554438,
"type": "json"
},
"xattrs": {
"_sync": {
"rev": "1-577011ccb4ce61c69507ba44985ca038",
"sequence": 2934,
"recent_sequences": [
2934
],
"history": {
"revs": [
"1-577011ccb4ce61c69507ba44985ca038"
],
"parents": [
-1
],
"channels": [
null
]
},
"cas": "0x00000cd9ef441316",
"value_crc32c": "0xc37bb792",
"time_saved": "2020-05-28T11:34:50.3200745-07:00"
}
}
}
Is that the expected behavior or there is something I can do about it?
That behavior is as expected. The live query re-runs the query every time there is a database change that impacts the results of the query. So in your case, since it's a query that fetches ALL documents in your database, the query re-runs when any document in database changes and it returns all documents (which is what the query is for).
Live queries are best suited if you have a filter predicate on your query. For instance, if the app wants to be notified if the status field in documents of type "foo" changes. in that case, you will only be notified if the status field changes in document of type "foo".
In your case, if you just care about changes if any of the document in your database changes, you should just use a Database Change Listener

Populating data after check from JSON data

I'm kind of confused and after spending a lot of time on it, I have not found any good solution to my problem. So without even wasting my time I'm explaining my question.
I have a JSON data, with a data[] having different objects which looks like this :
{
"data": [
{
"id": 1,
"name": "XYZ",
"course": {
"id": 25,
"name": "XYZ",
"description": "",
"teacher": {
"id": 4,
"name": "",
"email": "",
"role": "",
"token": "",
"about": "Blah blah blah ",
"phone": "2222222222",
"image_url": "",
"payment_information": {}
},
"image": "",
"cover_image": "",
"course_type": "",
"ongoing": true,
"price": 10,
"students_count": 4,
"lesson_price": 2.5,
"drop_in_price": 12,
"lessons_data": {
"first_lesson_at": "",
"last_lesson_at": "",
"next_lesson_at": ""
},
"data": {
"number_of_classes": "",
"start_time": "06:45",
"day_of_week": "Tuesday",
"duration": "-300",
"start_date": "2017-11-07T06:45:04.000-0800",
"end_time": "06:50"
}
},
"start_time": "2018-01-23T14:45:00.000Z",
"end_time": "2018-01-23T14:50:00.000Z",
"zoom_url": "http://zoom.us/j/5124648907"
}
]
}
This is only one object which I just showed it to you but there are many other objects present inside the data array.
I have a widget which has a block and accepts the title and the , we will look at the later. So for this first I want to loop through the data object and check if the timestamps' date is matching with the other one or not and then get the particular amount of that widget after using *ngFor in my html
For now I have 7 object inside the JSON data and after getting the result from the timestamps I have 4 objects which has similar start_time (2 same and other 2 same).
More pieces of the information :
JSON data
1 with Wednesday from Timestamp
2 objects with Thursday from Timestamp
2 Objects with Friday from Timestamp
1 with Saturday from Timestamp
So according to the above data we have to get only 4 blocks, but I have tried finding some solution to it but failed.
I have tried doing this
<div *ngFor="les lesson of lessons">
<widget-app-block [title]="lesson.start_time | date: 'EEEE, MMMM d'"></widget-app-block>
</div>
But it prints all the 7 blocks and prints the Time for all the 7 blocks. But what we need here to get only 4 blocks which would be sorted after checking.
POSSIBLE ATTEMPTS
I have read about using *ngIF-else condition from HERE, so what I have done is I have tried getting the results from my TYPESCRIPT file and tried matching it with the lesson variable from the foreach loop in my html and then pass the title else just print the timestamp for individual objects but didn't work out.
TYPESCRIPT
ngAfterViewInit() {
this.http.get("/lessons").subscribe(data => {
this.lessons = castCollection(data['data'], Lesson)
for(let time of this.lessons){
this.timeTitle = time.start_time
}
})
}
and then comparing it to the lesson variable in my html
HTML
<div *ngFor="let lesson of lessons">
<div *ngIf="lesson.start_time === timeTitle; else elseTimeBlock">
<widget-app-block [title]="timeTitle | date: 'EEEE, MMMM d'"></widget-app-block>
</div>
<ng-template #elseTimeBlock>
<widget-app-block [title]="lesson_start_time | date: 'EEEE, MMMM d'"></widget-app-block>
</ng-template>
</div>
But no luck. Since my data fetching is working fine so, now I have to just focus on the data itself. Could any one please help me with this. I'd be grateful to learn new thing. Since this is very tricky.
It sounds like what you need is to filter your list to match a particular timestamp. I have a blog post here on filtering data in Angular: https://blogs.msmvps.com/deborahk/filtering-in-angular/
Something like this:
performFilter(filterBy: string): IProduct[] {
filterBy = filterBy.toLocaleLowerCase();
return this.products.filter((product: IProduct) =>
product.productName.toLocaleLowerCase().indexOf(filterBy) !== -1);
}
I've filtering on a string, but you could change this to filter on a timestamp instead.
See the blog post for the full code.

Python 2.7: Generate JSON file with multiple query results in nested dict

What started as my personal initiative, ended up being a quiet interesting ( may I say, challenging to some degree) project. My company decided to phase out one product and replace it with new one, which instead of storing data in mdb files, uses JSON files. So I took the initiative to create a converter that will read already created mdb files and convert them into the new format JSON.
However, now I'm at wits-ends with this one:
I can read mdb files, run query to extract specific data.
By placing the targetobj inside the FOR LOOP, I managed to extract data for each row and fed into a dict(targetobj)
for val in rows:
targetobj={"connection_props": {"port": 7800, "service": "", "host": val.Hostname, "pwd": "", "username": ""},
"group_list": val.Groups, "cpu_core_cnt": 2, "target_name": "somename", "target_type": "somethingsamething",
"os": val.OS, "rule_list": [], "user_list": val.Users}
if I print targetobj to console, I can clearly get all extracted values for each row.
Now, my quest is to have the obtained results ( for each row), inserted into the main_dict under the key targets:[]. ( Please see sample of JSON file for illustration)
main_dict = {"changed_time": 0, "year": 0, "description": 'blahblahblah', 'targets':[RESULTS FROM TARGETOBJ SHOULD BE ADDED HERE],"enabled": False}
so for example my Json file should have structure such as:
{"changed_time":1234556,
"year":0,
"description":"blahblahblah",
"targets":[
{"group_list":["QA"],
"cpu_core_cnt":1,
"target_name":"NewTarget",
"os":"unix",
"target_type":"",
"rule_list":[],
"user_list":[""],"connection_props":"port":someport,"service":"","host":"host1","pwd":"","username":""}
},
{"group_list":[],
"cpu_core_cnt":2,
"target_name":"",
"os":"unix",
"target_type":"",
"rule_list":[],
"user_list":["Web2user"],
"connection_props":{"port":anotherport,"service":"","host":"host2","pwd":"","username":""}}
],
"enabled":false}
So far I've been tweaking here and there, to have the results written as intended, however each time,I'm getting only the last row values written.
ie.: putting the targetobj as a variable inside the targets:[]
{"changed_time": 0, "year": 0, "description": 'ConvertedConfigFile', 'targets':[targetobj],
I know I'm missing something, I just need to find what and where.
Any help would be highly appreciated.
thank you
Just create your main_dict first and append to it in your loop, i.e.:
main_dict = {"changed_time": 0,
"year": 0,
"description": "blahblahblah",
"targets": [], # a new list for the target objects
"enabled": False}
for val in rows:
main_dict["targets"].append({ # append this dict to the targets list of main_dict
"connection_props": {
"port": 7800,
"service": "",
"host": val.Hostname,
"pwd": "",
"username": ""},
"group_list": val.Groups,
"cpu_core_cnt": 2,
"target_name": "somename",
"target_type": "somethingsamething",
"os": val.OS,
"rule_list": [],
"user_list": val.Users
})

Extract data from a paginated API with tRESTClient

I need to crawl a paginated REST API with offset and limit parameters using Talend.
The API gives me a list of the resources I am interested in.
For instance, the response to the initial request with offset=0 and limit=2 is:
{
"meta": {
"limit": 2,
"next": "/api/v1/request/?offset=2&limit=2",
"offset": 0,
"previous": null,
"total_count": 4300
},
"objects": [
{
"id": 1,
"name": "foo"
},
{
"id": 2,
"name": "bar"
}
]
}
As you can see, the response object contains an objects key, i.e. some of the desired resources and a meta key which indicates the next URL to query: next. So far I am able to perform the initial request with tRESTClient. However, I don't know how to proceed from here and request the remaining pages using the clue given by next.
How can I perform multiple requests to that API so that I iterate over the whole list until next equals null (=list is exhausted)?
I tried to figure out how tSetGlobalVar and tLoop could help me, but so far with no success. But then again, I am a Talend newbie.
Current job
This is what my job currently looks like: