Hi,
I want to post data from my task end using HTTP(Custom). But im unable to do that.
Can anyone help please.
My task looks like -
import "influxdata/influxdb/monitor"
import "slack"
import "influxdata/influxdb/secrets"
import "experimental"
import "array"
import "json"
import "http"
option task = {name: "Test TA", every: 2m, offset: 1m}
httpendpoint = http.endpoint(url: "http://ec2-xx-xx-xx-xx.ap-south-1.compute.amazonaws.com/xx/hooks/xx/qq/10714484948879987")
initialData = from(bucket: "DATA_DB")
|> range(start: -1d)
assetIDs1 = initialData
|> filter(fn: (r) =>
(r["_measurement"] == "1071448494555_P"))
|> filter(fn: (r) =>
(r["_field"] == "latitute"))
|> group(columns: ["_field"])
|> map(fn: (r) =>
({r with "assetId": r["assetId"]}))
|> findColumn(fn: (key) =>
(true), column: "assetId")
initialData
|> filter(fn: (r) =>
(r["_measurement"] == "1071448494555_P"))
|> filter(fn: (r) =>
(contains(set: assetIDs1, value: r["assetId"])))
|> filter(fn: (r) =>
(r["_field"] == "latitute" or r["_field"] == "longitude"))
|> aggregateWindow(
every: 15m,
column: "_value",
fn: mean,
createEmpty: true,
)
|> fill(usePrevious: true, column: "_value")
|> drop(columns: ["prop"])
|> group(columns: ["_field", "assetId"])
|> group(columns: ["_field", "assetId"])
|> filter(fn: (r) =>
(r["_field"] == "latitute" and r["_value"] > 80 or r["_field"] == "longitude"))
|> count()
|> filter(fn: (r) =>
(r["_value"] > 5))
|> limit(n: 1000000)
|> pivot(columnKey: ["_field"], rowKey: ["assetId"], valueColumn: "_value")
|> group(columns: ["_measurement"])
|> map(fn: (r) =>
({r with "assetId": r["assetId"]}))
|> findColumn(fn: (key) =>
(true), column: "assetId")
snapshotData = from(bucket: "DATA_DB")
|> range(start: 0)
snapshotData
|> filter(fn: (r) =>
(r["_measurement"] == "1071448494555_P"))
|> last()
|> drop(columns: ["prop", "asset_name"])
|> pivot(columnKey: ["_field"], rowKey: ["assetId"], valueColumn: "_value")
|> map(fn: (r) =>
({r with jsonStr: string(v: json.encode(v: {"latitute": r.latitute, "longitude": r.longitude, "assetId": r.assetId}))}))
|> monitor["notify"](data: {}, endpoint: httpendpoint(mapFn: (r) =>
({channel: "", text: r["jsonStr"], color: "danger"})))
On run Error is like -
I need to send data as well from the task endpoint. Can i do that ? if yes the how.
Any help will be appreciated
Hello @debnath ,
I’m a little confused by your task.
It doesn’t look like you’re doing anything with the data in the following lines:
initialData
|> filter(fn: (r) =>
(r["_measurement"] == "1071448494555_P"))
...
|> findColumn(fn: (key) =>
(true), column: "assetId")
I think you need to define the endpoint like this:
// ## Parameters
// - url: URL to send the POST reqeust to.
//
// ## Examples
//
// ### Send an HTTP POST request for each row
// ```no_run
// import "http"
// import "sampledata"
//
// endpoint = http.endpoint(url: "http://example.com/")(
// mapfn: (r) => ({
// headers: {header1: "example1", header2: "example2"},
// data: bytes(v: "The value is ${r._value}"),
// }),
// )
//
// sampledata.int()
// |> endpoint()
// ```
//
I would also consider using the http.post() function instead. http.post() function | Flux 0.x Documentation
And not using the monitor.notify() function unless you need that metadata.
Here’s an example of what I mean:
Query/Alert for Difference in Value - #2 by Anaisdg
As an aside here’s an example of using reduce() function to create a json so that a user can send it in a an http.post() function.
import "json"
import "http"
import "array"
lastReported = from(bucket: "engineering's Bucket") |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "CBBandwidthAlert")
|> filter(fn: (r) => r["_field"] == "code")
|> group()
|> map(fn: (r) => ({ r with index: 1 }))
|> cumulativeSum(columns: ["index"])
|> map(fn: (r) => ({ r with gindex: r.index / 250 }))
|> group(columns: ["gindex"])
|> reduce(
fn: (r, accumulator) => ({ _value:accumulator._value + "\"" + r.host + "\","}),
identity: {_value: "{hosts:["}
)
|> map(fn: (r) => ({ r with _value: r._value + "]}" }))
|> yield()
Does any of this help? If not please let me know. Also bravo for getting this far with Flux.
@Anaisdg Thank you very much for your help. It helped me a lot. I am trying to do more with Flux task query.
1 Like
@debnath do you mind sharing your solution in the end? So ppl can benefit off of your knowledge? No pressure but I’m sure others will want to follow in your footsteps.
@Anaisdg This is a great idea. I also want share my knowledge and grow together. But i need to take NOC from my company to showcase the solution. It is quite complex, big and interesting. So let me propose them and let you know.
Thank you for your suggestion.
@Anaisdg So where can we showcase the solution that i got. Any git repo or something available in influx community or we can share our public git link somewhere.
Can you suggest?
One more person is also interested with your suggestion. So we both are coming together.
@debnath I’ve messaged you