{
  "title": "ROI Calculator - CRM & Sales Intelligence",
  "description": "Advanced CRM analytics: lead scoring, segmentation, pipeline forecasting, and opportunity tracking",
  "timezone": "utc",
  "editable": true,
  "graphTooltip": 1,
  "time": {
    "from": "now-30d",
    "to": "now"
  },
  "refresh": "5m",
  "tags": [
    "lambda",
    "crm",
    "sales",
    "leads",
    "pipeline"
  ],
  "panels": [
    {
      "id": 1,
      "type": "table",
      "title": "HOT LEADS - Last Hour (>$250K spend)",
      "gridPos": {
        "h": 6,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT timestamp AS \"Time\", json_data.name::String AS \"Name\", json_data.email::String AS \"Email\", json_data.observabilitySpend::Float64 AS \"Current Spend\", json_data.savings3Year::Float64 AS \"3Y Savings\", json_data.savingsPercentage::Float64 AS \"Savings %\", json_data.engagementMonths::Int64 AS \"Months\" FROM lambda_logs WHERE timestamp > now() - INTERVAL 1 HOUR AND json_data.event::String = 'roi_generated' AND json_data.observabilitySpend::Float64 > 250000 ORDER BY json_data.observabilitySpend::Float64 DESC"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Current Spend"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "value": 0,
                      "color": "yellow"
                    },
                    {
                      "value": 500000,
                      "color": "orange"
                    },
                    {
                      "value": 1000000,
                      "color": "red"
                    }
                  ]
                }
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "3Y Savings"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          }
        ]
      }
    },
    {
      "id": 2,
      "type": "stat",
      "title": "Total Pipeline Value",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 0,
        "y": 6
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT sum(json_data.savings3Year::Float64) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated'"
        }
      ],
      "options": {
        "graphMode": "area",
        "colorMode": "background"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "color": {
            "mode": "fixed",
            "fixedColor": "green"
          }
        }
      }
    },
    {
      "id": 3,
      "type": "stat",
      "title": "Projected Revenue (20% close, 15% fee)",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 6,
        "y": 6
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT sum(json_data.savings3Year::Float64) * 0.20 * 0.15 AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated'"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "color": {
            "mode": "fixed",
            "fixedColor": "blue"
          }
        }
      }
    },
    {
      "id": 4,
      "type": "stat",
      "title": "Average Deal Size",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 12,
        "y": 6
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT round(avg(json_data.observabilitySpend::Float64)) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated'"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD"
        }
      }
    },
    {
      "id": 5,
      "type": "stat",
      "title": "Avg Hours to Convert",
      "gridPos": {
        "h": 4,
        "w": 6,
        "x": 18,
        "y": 6
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "WITH conversions AS (SELECT json_data.ipAddress::String AS ip, minIf(timestamp, json_data.event::String = 'calculation_tracked') AS first_calc, minIf(timestamp, json_data.event::String = 'roi_generated') AS first_lead FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String IN ('calculation_tracked', 'roi_generated') GROUP BY ip HAVING first_lead > first_calc) SELECT round(avg(dateDiff('hour', first_calc, first_lead)), 1) AS value FROM conversions"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "hours"
        }
      }
    },
    {
      "id": 6,
      "type": "table",
      "title": "Lead Scoring & Prioritization (Top 30)",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 10
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT json_data.email::String AS Email, json_data.name::String AS Name, json_data.observabilitySpend::Float64 AS Spend, json_data.savings3Year::Float64 AS Savings, json_data.engagementMonths::Int64 AS Months, (CASE WHEN json_data.observabilitySpend::Float64 > 500000 THEN 40 WHEN json_data.observabilitySpend::Float64 > 100000 THEN 25 ELSE 10 END + CASE WHEN json_data.engagementMonths::Int64 >= 12 THEN 30 WHEN json_data.engagementMonths::Int64 >= 6 THEN 20 ELSE 10 END + CASE WHEN json_data.savingsPercentage::Float64 >= 40 THEN 30 WHEN json_data.savingsPercentage::Float64 >= 20 THEN 20 ELSE 10 END) AS Score FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' ORDER BY Score DESC LIMIT 30"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Spend"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Savings"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Score"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "value": 0,
                      "color": "blue"
                    },
                    {
                      "value": 50,
                      "color": "yellow"
                    },
                    {
                      "value": 70,
                      "color": "orange"
                    },
                    {
                      "value": 85,
                      "color": "red"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    },
    {
      "id": 7,
      "type": "piechart",
      "title": "Market Segmentation by Spend",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 10
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT CASE WHEN json_data.observabilitySpend::Float64 >= 1000000 THEN 'Enterprise ($1M+)' WHEN json_data.observabilitySpend::Float64 >= 500000 THEN 'Large ($500K-$1M)' WHEN json_data.observabilitySpend::Float64 >= 100000 THEN 'Mid-Market ($100K-$500K)' ELSE 'SMB (<$100K)' END AS segment, count() AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' GROUP BY segment"
        }
      ],
      "options": {
        "legend": {
          "displayMode": "table",
          "placement": "right",
          "values": [
            "value",
            "percent"
          ]
        },
        "pieType": "pie"
      }
    },
    {
      "id": 8,
      "type": "table",
      "title": "Market Segment Analysis",
      "gridPos": {
        "h": 6,
        "w": 24,
        "x": 0,
        "y": 18
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT CASE WHEN json_data.observabilitySpend::Float64 >= 1000000 THEN 'Enterprise ($1M+)' WHEN json_data.observabilitySpend::Float64 >= 500000 THEN 'Large ($500K-$1M)' WHEN json_data.observabilitySpend::Float64 >= 100000 THEN 'Mid-Market ($100K-$500K)' ELSE 'SMB (<$100K)' END AS Segment, count() AS Leads, round(avg(json_data.savings3Year::Float64)) AS \"Avg Savings\", round(avg(json_data.savingsPercentage::Float64), 1) AS \"Avg Savings %\", sum(json_data.savings3Year::Float64) AS \"Total Value\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' GROUP BY Segment ORDER BY \"Total Value\" DESC"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Avg Savings"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Total Value"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              }
            ]
          }
        ]
      }
    },
    {
      "id": 9,
      "type": "timeseries",
      "title": "Weekly Lead Velocity & Growth",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 24
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toMonday(timestamp) AS time, count() AS \"Leads This Week\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "custom": {
            "drawStyle": "bars",
            "fillOpacity": 50,
            "showPoints": "never"
          }
        }
      }
    },
    {
      "id": 10,
      "type": "heatmap",
      "title": "Conversion Heatmap (Day/Hour)",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 24
      },
      "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 json_data.event::String = 'roi_generated' GROUP BY time, day ORDER BY time"
        }
      ]
    },
    {
      "id": 11,
      "type": "table",
      "title": "Lost Opportunities - High-Value Anonymous Users (>$100K)",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 32
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "WITH last_calc_per_ip AS (SELECT json_data.ipAddress::String AS ip, argMax(json_data.observabilitySpend::Float64, timestamp) AS last_spend, argMax(timestamp, timestamp) AS last_seen, count() AS total_calcs, countIf(json_data.event::String = 'roi_generated') AS conversions FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String IN ('calculation_tracked', 'roi_generated') GROUP BY ip) SELECT ip AS \"IP Address\", last_spend AS \"Potential Spend\", total_calcs AS \"Engagement\", last_seen AS \"Last Seen\", dateDiff('day', last_seen, now()) AS \"Days Ago\" FROM last_calc_per_ip WHERE conversions = 0 AND last_spend > 100000 ORDER BY last_spend DESC LIMIT 50"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Potential Spend"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              },
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "value": 100000,
                      "color": "yellow"
                    },
                    {
                      "value": 250000,
                      "color": "orange"
                    },
                    {
                      "value": 500000,
                      "color": "red"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    },
    {
      "id": 12,
      "type": "table",
      "title": "Engagement Journey - Converted Users",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 32
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "WITH user_journey AS (SELECT json_data.ipAddress::String AS ip, min(timestamp) AS first_touch, max(timestamp) AS last_touch, count() AS total_interactions, countIf(json_data.event::String = 'calculation_tracked') AS anonymous_calcs, countIf(json_data.event::String = 'roi_generated') AS conversions, argMaxIf(json_data.email::String, timestamp, json_data.event::String = 'roi_generated') AS email, argMaxIf(json_data.observabilitySpend::Float64, timestamp, json_data.event::String = 'roi_generated') AS final_spend FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String IN ('calculation_tracked', 'roi_generated') GROUP BY ip) SELECT email AS Email, first_touch AS \"First Touch\", last_touch AS \"Conversion\", dateDiff('hour', first_touch, last_touch) AS \"Hours to Convert\", anonymous_calcs AS \"Experiments\", final_spend AS \"Spend\" FROM user_journey WHERE conversions > 0 ORDER BY \"Hours to Convert\" ASC LIMIT 50"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Spend"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          }
        ]
      }
    },
    {
      "id": 13,
      "type": "table",
      "title": "Repeat Users (Deduplicated by IP - Last Event)",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 0,
        "y": 40
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "WITH last_events AS (SELECT json_data.ipAddress::String AS ip, argMax(json_data.event::String, timestamp) AS last_event, argMax(json_data.email::String, timestamp) AS email, argMax(json_data.observabilitySpend::Float64, timestamp) AS last_spend, count() AS total_events, max(timestamp) AS last_seen FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String IN ('calculation_tracked', 'roi_generated') GROUP BY ip) SELECT ip AS \"IP\", last_event AS \"Last Event\", email AS \"Email\", last_spend AS \"Last Spend\", total_events AS \"Events\", last_seen AS \"Last Seen\" FROM last_events WHERE total_events > 1 ORDER BY total_events DESC, last_spend DESC LIMIT 50"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byName",
              "options": "Last Spend"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          },
          {
            "matcher": {
              "id": "byName",
              "options": "Events"
            },
            "properties": [
              {
                "id": "custom.cellOptions",
                "value": {
                  "type": "color-background"
                }
              },
              {
                "id": "color",
                "value": {
                  "mode": "thresholds"
                }
              },
              {
                "id": "thresholds",
                "value": {
                  "mode": "absolute",
                  "steps": [
                    {
                      "value": 2,
                      "color": "blue"
                    },
                    {
                      "value": 5,
                      "color": "yellow"
                    },
                    {
                      "value": 10,
                      "color": "orange"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    },
    {
      "id": 14,
      "type": "bargauge",
      "title": "Pipeline Forecast Scenarios",
      "gridPos": {
        "h": 8,
        "w": 12,
        "x": 12,
        "y": 40
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT 'Total Pipeline' AS scenario, sum(json_data.savings3Year::Float64) AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' UNION ALL SELECT 'Conservative (10% close)' AS scenario, sum(json_data.savings3Year::Float64) * 0.10 AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' UNION ALL SELECT 'Moderate (20% close)' AS scenario, sum(json_data.savings3Year::Float64) * 0.20 AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' UNION ALL SELECT 'Optimistic (30% close)' AS scenario, sum(json_data.savings3Year::Float64) * 0.30 AS value FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated'"
        }
      ],
      "options": {
        "orientation": "horizontal",
        "displayMode": "gradient"
      },
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "color": {
            "mode": "continuous-GrYlRd"
          }
        }
      }
    },
    {
      "id": 15,
      "type": "table",
      "title": "Revenue Forecast (15% Fee on Savings)",
      "gridPos": {
        "h": 6,
        "w": 24,
        "x": 0,
        "y": 48
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 1,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT count() AS \"Total Leads\", sum(json_data.savings3Year::Float64) AS \"Total Pipeline\", sum(json_data.savings3Year::Float64) * 0.10 AS \"Conservative 10%\", sum(json_data.savings3Year::Float64) * 0.20 AS \"Moderate 20%\", sum(json_data.savings3Year::Float64) * 0.30 AS \"Optimistic 30%\", sum(json_data.savings3Year::Float64) * 0.10 * 0.15 AS \"Revenue (10% \u00d7 15% fee)\", sum(json_data.savings3Year::Float64) * 0.20 * 0.15 AS \"Revenue (20% \u00d7 15% fee)\", sum(json_data.savings3Year::Float64) * 0.30 * 0.15 AS \"Revenue (30% \u00d7 15% fee)\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated'"
        }
      ],
      "fieldConfig": {
        "overrides": [
          {
            "matcher": {
              "id": "byRegexp",
              "options": ".*Pipeline|.*10%|.*20%|.*30%|.*Revenue.*"
            },
            "properties": [
              {
                "id": "unit",
                "value": "currencyUSD"
              }
            ]
          }
        ]
      }
    },
    {
      "id": 16,
      "type": "timeseries",
      "title": "Cumulative Pipeline Value",
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 54
      },
      "targets": [
        {
          "refId": "A",
          "datasource": {
            "type": "grafana-clickhouse-datasource"
          },
          "editorType": "sql",
          "format": 2,
          "pluginVersion": "4.12.0",
          "queryType": "table",
          "rawSql": "SELECT toStartOfDay(timestamp) AS time, sum(json_data.savings3Year::Float64) OVER (ORDER BY toStartOfDay(timestamp)) AS \"Cumulative Pipeline Value\" FROM lambda_logs WHERE $__timeFilter(timestamp) AND json_data.event::String = 'roi_generated' GROUP BY time ORDER BY time"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "currencyUSD",
          "custom": {
            "drawStyle": "line",
            "lineWidth": 2,
            "fillOpacity": 20,
            "gradientMode": "opacity"
          }
        }
      }
    }
  ],
  "schemaVersion": 39
}