dhall-grafana icon indicating copy to clipboard operation
dhall-grafana copied to clipboard

Add FieldConfig Custom and Mapping

Open TristanCacqueray opened this issue 1 year ago • 3 comments

TristanCacqueray avatar Jan 16 '24 21:01 TristanCacqueray

Unfortunately that conflicts with the current panels definition. I got these types by clicking migrate on the UI, and these settings only seem to work when most of the existing panel configuration is disabled.

I think it's a bit complicated to provide a generic dhall-grafana binding, I had better results by generating the config using json-to-dhall directly. Here is what I came up with:

-- | To generate a new panel, copy the panel JSON from the UI.
-- Use `json-to-dhall | dhall type` to generate the type and add it to the 'PType' union.
-- Then use `json-to-dhall` to create a mkHelper, moving the common values as function parameters.
--

let -- | The default graph width
    w =
      24

let Separator =
      { collapsed : Bool
      , gridPos : { h : Natural, w : Natural, x : Natural, y : Natural }
      , id : Natural
      , title : Text
      , type : Text
      }

let Lucene =
      { datasource : { type : Text, uid : Text }
      , ...
      }

let PType = < Sep : Separator | Lucene : Lucene >

let mkDashboard =
      \(title : Text) ->
      \(panels : List PType) ->
        { panels
        , title
        , editable = False
        , refresh = False
        , time = { from = "now-2d", to = "now" }
        , timepicker =
          { collapse = False
          , enable = True
          , notice = False
          , now = True
          , refresh_intervals =
            [ "5s", "10s", "30s", "1m", "5m", "15m", "30m", "1h", "2h", "1d" ]
          , status = "Stable"
          , time_options = [ "5m", "1h", "6h", "12h", "24h", "2d", "7d", "30d" ]
          , type = "timepicker"
          }
        , timezone = "utc"
        }

let mkSep =
      \(title : Text) ->
        PType.Sep
          { collapsed = False
          , gridPos = { h = 0, w, x = 0, y = 0 }
          , id = 1
          , title
          , type = "row"
          }

let mkLucene =
      \(title : Text) ->
      \(query : Text) ->
        PType.Lucene
          {...}

in  { mkSep, mkLucene, mkDashboard }

Moreover, it seems like the latest grafana handles id and gridPos.y sets to 0 as expected.

Perhaps it would be more useful to share such custom made config instead of trying to address every use-case? I think what we are missing is an open sum type for PType, or we could simply combine the panel using JSON once https://github.com/dhall-lang/dhall-lang/issues/1027 is available.

TristanCacqueray avatar Jan 16 '24 21:01 TristanCacqueray

Yeah, it's a bit tricky and nowadays Grafana has a pretty solid schema written in Cue here: https://github.com/grafana/grafana/tree/main/kinds . If I had free time I'd try to figure out a way to go from those Cue schema definitions to Dhall, and focus on adding sugar on top where needed :(.

weeezes avatar Jan 24 '24 18:01 weeezes

Ha, that's good to hear. But even there, the #Target is still undefined: https://github.com/grafana/grafana/blob/a81d3b1d221b2ed405246ab2b4bf1eef209fe922/kinds/dashboard/dashboard_kind.cue#L474-L482

For what it's worth, here is the sugar I put together: Panels.dhall, and how it is used: Dashboard.dhall

TristanCacqueray avatar Jan 24 '24 19:01 TristanCacqueray