santiment / sanpy

Santiment API Python Client
MIT License
94 stars 29 forks source link

age_distribution not working #142

Closed lhcsnelm closed 2 years ago

lhcsnelm commented 2 years ago

san.error.SanError: GraphQL error occured running query { query_0: getMetric(metric: "age_distribution"){ timeseriesData( slug: "santiment"

        from: "utc_now-30d"
        to: "utc_now"
        interval: "1d"
        aggregation: null
        includeIncompleteData: false
    ){
    datetime value
    }
}
} 

errors: [{'locations': [{'column': 9, 'line': 3}], 'message': '[68531562-8a4e-4147-b5b3-7578a95eb4a7] Can\'t fetch age_distribution for project with slug santiment, Reason: "The metric \'age_distribution\' is not supported or is mistyped. Did you mean the histogram metric \'age_distribution\'?"', 'path': ['query_0', 'timeseriesData']}]

IvanIvanoff commented 2 years ago

I guess you try to execute this: san.get("age_distribution/santiment"), which turns this into the following graphql:

{
  getMetric(metric: "age_distribution") {
    timeseriesData(
      slug: "santiment"
      from: "utc_now-30d"
      to: "utc_now"
      interval: "1d"
      aggregation: null
      includeIncompleteData: false) {
        datetime
        value
    }
  }
}

The important part of the error is this one Did you mean the histogram metric 'age_distribution'?. Most of the metrics are defined as timeseries - a list of datetime and value like : [(2022-05-01, 100.5),(2022-05-02, 101.92)] There are other metrics which response cannot be fit into this format and they return something else. This is the case with age_distribution - it is a histogram metric. Its response is in the format price_range - value where the price range is represented as a list of the min and max prices of these ranges. The GraphQL request for it looks like this (note the histogramData instead of timeseriesData used):

{
  getMetric(metric: "age_distribution") {
    histogramData(
      slug: "santiment"
      from: "2020-01-01T00:00:00Z"
      to: "2020-02-20T00:00:00Z") {
        values{
          ... on DatetimeRangeFloatValueList{
            data{
              range
              value
            }
          }
        }
    }
  }
}

And the response looks like this:

{
  "data": {
    "getMetric": {
      "histogramData": {
        "values": {
          "data": [
            {
              "range": [
                "2017-12-11T00:00:00Z",
                "2017-12-12T00:00:00Z"
              ],
              "value": 49590.09950020001
            },
            {
              "range": [
                "2019-01-22T00:00:00Z",
                "2019-01-23T00:00:00Z"
              ],
              "value": 30816.730561136355
            }
            ...

You can execute this in sanpy in the following way and you need to look at the response and extract the important parts:


import san
from san.graphql import execute_gq
metrics = execute_gql("""
{
  getMetric(metric: "age_distribution") {
    histogramData(
      slug: "santiment"
      from: "2020-01-01T00:00:00Z"
      to: "2020-02-20T00:00:00Z") {
        values{
          ... on DatetimeRangeFloatValueList{
            data{
              range
              value
            }
          }
        }
    }
  }
}
""")
lhcsnelm commented 2 years ago

Thanks. I will have a try.

jinsyu commented 2 years ago

thx for kind explanations. Please let me know how to graphql query with variables? """ { getMetric(metric: "{}") { histogramData( slug: "santiment" from: "2020-01-01T00:00:00Z" to: "2020-02-20T00:00:00Z") { values{ ... on DatetimeRangeFloatValueList{ data{ range value } } } } } } """.format(xx)

such string interpolation things not work.

spiderjako commented 2 years ago

Hey @jinsyu

from san.graphql import execute_gql

metric = 'age_distribution'
slug = 'santiment'

res = execute_gql("""
{{
getMetric(metric: "{}") {{
histogramData(
slug: "{}"
from: "2020-01-01T00:00:00Z"
to: "2020-02-20T00:00:00Z") {{
values{{
... on DatetimeRangeFloatValueList{{
data{{range
value
}}
}}
}}
}}
}}
}}""".format(metric, slug))

The following code snippet will give you the result you need. In essence, the curly braces should be doubled when they're not used for showing an argument for formatting