{
  "title": "Lambda Operations & Performance",
  "description": "Lambda cold starts, memory optimization, cost analysis, and performance metrics",
  "timezone": "utc",
  "editable": true,
  "graphTooltip": 1,
  "time": {
    "from": "now-7d",
    "to": "now"
  },
  "refresh": "5m",
  "tags": [
    "lambda",
    "operations",
    "performance",
    "cost"
  ],
  "templating": {
    "list": [
      {
        "name": "function",
        "type": "query",
        "label": "Lambda Function",
        "datasource": {
          "type": "grafana-clickhouse-datasource"
        },
        "query": "SELECT DISTINCT function_name FROM lambda_logs ORDER BY function_name",
        "refresh": 1,
        "includeAll": false,
        "multi": false,
        "current": {
          "selected": false,
          "text": "",
          "value": ""
        }
      }
    ]
  },
  "panels": [
    {
      "id": 1,
      "type": "stat",
      "title": "Total Invocations",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 0,
        "y": 0
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT count() AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "background"
      },
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "value": 0,
                "color": "blue"
              },
              {
                "value": 1000,
                "color": "green"
              },
              {
                "value": 10000,
                "color": "yellow"
              }
            ]
          }
        }
      }
    },
    {
      "id": 2,
      "type": "stat",
      "title": "Cold Start Rate",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 6,
        "y": 0
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT round(countIf(init_duration_ms > 0) / count() * 100, 2) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "background"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "percent",
          "color": {
            "mode": "thresholds"
          },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "value": 0,
                "color": "green"
              },
              {
                "value": 10,
                "color": "yellow"
              },
              {
                "value": 25,
                "color": "red"
              }
            ]
          }
        }
      }
    },
    {
      "id": 3,
      "type": "stat",
      "title": "Average Duration",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 12,
        "y": 0
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT round(avg(duration_ms), 2) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "value"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "ms",
          "decimals": 2
        }
      }
    },
    {
      "id": 4,
      "type": "stat",
      "title": "Estimated Period Cost",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 18,
        "y": 0
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT sum((memory_size_mb / 1024) * (billed_duration_ms / 1000) * 0.0000133334) + (count() * 0.0000002) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "background"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "color": {
            "mode": "thresholds"
          },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "value": 0,
                "color": "green"
              },
              {
                "value": 10,
                "color": "yellow"
              },
              {
                "value": 100,
                "color": "red"
              }
            ]
          }
        }
      }
    },
    {
      "id": 5,
      "type": "timeseries",
      "title": "Cold Start Rate Over Time",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 4
      },
      "targets": [
        {
          "refId": "Cold Start Rate",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfHour(timestamp) AS time, round(countIf(init_duration_ms > 0) / count() * 100, 2) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "percent",
          "custom": {
            "drawStyle": "line",
            "lineWidth": 2,
            "fillOpacity": 20,
            "showPoints": "auto"
          }
        }
      }
    },
    {
      "id": 6,
      "type": "timeseries",
      "title": "Cold vs Warm Start Duration",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 4
      },
      "targets": [
        {
          "refId": "Cold Starts",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfHour(timestamp) AS time, avg(duration_ms) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' AND init_duration_ms > 0 GROUP BY time ORDER BY time"
        },
        {
          "refId": "Warm Starts",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfHour(timestamp) AS time, avg(duration_ms) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' AND init_duration_ms = 0 GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "ms",
          "custom": {
            "drawStyle": "line",
            "lineWidth": 2,
            "fillOpacity": 10,
            "showPoints": "auto"
          }
        }
      }
    },
    {
      "id": 7,
      "type": "table",
      "title": "Duration Percentiles",
      "gridPos": {
        "h": 6,
        "w": 12,
        "x": 0,
        "y": 12
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT 'p50' AS Percentile, round(quantile(0.50)(duration_ms), 2) AS \"Duration (ms)\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' UNION ALL SELECT 'p95', round(quantile(0.95)(duration_ms), 2) FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' UNION ALL SELECT 'p99', round(quantile(0.99)(duration_ms), 2) FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Duration (ms)"
            },
            "properties": [
              {
                "id": "custom.width",
                "value": 150
              }
            ]
          }
        ]
      }
    },
    {
      "id": 8,
      "type": "gauge",
      "title": "Memory Utilization",
      "gridPos": {
        "h": 6,
        "w": 12,
        "x": 12,
        "y": 12
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT round(avg(max_memory_used_mb / memory_size_mb * 100), 2) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "options": {
        "showThresholdLabels": true,
        "showThresholdMarkers": true
      },
      "fieldConfig": {
        "defaults": {
          "unit": "percent",
          "min": 0,
          "max": 100,
          "color": {
            "mode": "thresholds"
          },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "value": 0,
                "color": "red"
              },
              {
                "value": 50,
                "color": "yellow"
              },
              {
                "value": 70,
                "color": "green"
              }
            ]
          }
        }
      }
    },
    {
      "id": 9,
      "type": "timeseries",
      "title": "Cost Over Time",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 18
      },
      "targets": [
        {
          "refId": "Daily Cost",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfDay(timestamp) AS time, sum((memory_size_mb / 1024) * (billed_duration_ms / 1000) * 0.0000133334) + (count() * 0.0000002) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "custom": {
            "drawStyle": "bars",
            "lineWidth": 1,
            "fillOpacity": 80,
            "showPoints": "never"
          }
        }
      }
    },
    {
      "id": 10,
      "type": "stat",
      "title": "Monthly Cost Projection",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 18
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT (sum((memory_size_mb / 1024) * (billed_duration_ms / 1000) * 0.0000133334) + (count() * 0.0000002)) * (30 / dateDiff('day', min(timestamp), max(timestamp))) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "background",
        "textMode": "value_and_name"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "decimals": 2,
          "color": {
            "mode": "thresholds"
          },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "value": 0,
                "color": "green"
              },
              {
                "value": 50,
                "color": "yellow"
              },
              {
                "value": 200,
                "color": "red"
              }
            ]
          }
        }
      }
    },
    {
      "id": 11,
      "type": "table",
      "title": "Slowest Invocations",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 26
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT timestamp AS Time, request_id::String AS \"Request ID\", duration_ms AS \"Duration (ms)\", max_memory_used_mb AS \"Memory Used (MB)\", billed_duration_ms AS \"Billed (ms)\", init_duration_ms AS \"Init (ms)\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' ORDER BY duration_ms DESC LIMIT 50"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Duration (ms)"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "value": 0,
                      "color": "green"
                    },
                    {
                      "value": 1000,
                      "color": "yellow"
                    },
                    {
                      "value": 5000,
                      "color": "red"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    },
    {
      "id": 12,
      "type": "table",
      "title": "Cost Breakdown by Day",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 26
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfDay(timestamp) AS Day, count() AS Invocations, round(sum((memory_size_mb / 1024) * (billed_duration_ms / 1000) * 0.0000133334), 4) AS \"Compute Cost\", round(count() * 0.0000002, 4) AS \"Request Cost\", round(sum((memory_size_mb / 1024) * (billed_duration_ms / 1000) * 0.0000133334) + (count() * 0.0000002), 4) AS \"Total Cost\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' AND log_type = 'REPORT' GROUP BY Day ORDER BY Day DESC"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byRegexp",
              "options": ".*Cost"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          }
        ]
      }
    },
    {
      "id": 13,
      "type": "heatmap",
      "title": "Invocation Heatmap (Day/Hour)",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 34
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfHour(timestamp) AS time, concat('Day ', toString(toDayOfWeek(timestamp))) AS day, count() AS count FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' GROUP BY time, day ORDER BY time"
        }
      ],
      "options": {
        "calculate": false,
        "color": {
          "mode": "scheme",
          "scheme": "Spectral"
        }
      }
    },
    {
      "id": 14,
      "type": "timeseries",
      "title": "Invocations Over Time",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 34
      },
      "targets": [
        {
          "refId": "Invocations",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfHour(timestamp) AS time, count() AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND function_name = '$function' GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "custom": {
            "drawStyle": "line",
            "lineWidth": 2,
            "fillOpacity": 20,
            "showPoints": "auto"
          }
        }
      }
    }
  ],
  "schemaVersion": 39
}