<commit_before>return {
  config: {
    connections: { },
    layout: {
      isDraggable: true,
      isResizable: true,
      rowHeight: 30,
      // This turns off compaction so you can place items wherever.
      verticalCompact: false,
      cols: {lg: 12, md: 10, sm: 6, xs: 4, xxs: 2},
      breakpoints: {lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}
    }
  },
  dataSources: [
    {
      id: 'timespan',
      type: 'Constant',
      params: {
        values: ['24 hours', '1 week', '1 month', '3 months'],
        selectedValue: '1 month'
      },
      calculated: (state, dependencies) => {
        var queryTimespan = 
              state.selectedValue === '24 hours' ? 'PT24H' : 
              state.selectedValue === '1 week' ? 'P7D' : 
              state.selectedValue === '1 month' ? 'P30D' : 
              'P90D';
        var granularity = 
              state.selectedValue === '24 hours' ? '5m' : 
              state.selectedValue === '1 week' ? '1d' : '1d';

        return { queryTimespan, granularity };
      }
    },
    {
      id: 'conversions',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` +
          ` | extend successful=customDimensions.successful` +
          ` | where name startswith 'message.convert'` +
          ` | summarize event_count=count() by name, tostring(successful)`,
        mappings: [
          { key: 'name' },
          { key: 'successful', val: (val) => val === 'true' },
          { key: 'event_count', def: 0 }
        ]
      },
      calculated: (state) => {
        var { values } = state;

        var total = _.find(values, { name: 'message.convert.start' });
        var successful = _.find(values, { name: 'message.convert.end', successful: true }) || { event_count: 0 };

        if (!total) {
          return null;
        }

        var displayValues = [
          { label: 'Successful', count: successful.event_count },
          { label: 'Failed', count: total.event_count - successful.event_count + 5 },
        ];

        return { displayValues };
      }
    },
    {
      id: 'timeline',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan', granularity: 'timespan:granularity' },
      params: {
        query: (dependencies) => {
          var { granularity } = dependencies;
          return ` customEvents` +
                   ` | where name == 'Activity'` + 
                   ` | summarize event_count=count() by bin(timestamp, ${granularity}), name, tostring(customDimensions.channel)` +
                   ` | order by timestamp asc `
        },
        mappings: [
          { key: 'time' },
          { key: '__' },
          { key: 'channel', def: 'unknown' },
          { key: 'count', def: 0 }
        ]
      },
      calculated: (state) => {
        var _timeline = {};
        var _channels = {};
        var values = state.values || [];
        var timespan = state.timespan;

        values.forEach(row => {
          var { channel, time, count } = row;
          var timeValue = (new Date(time)).getTime();
          
          if (!_timeline[timeValue]) _timeline[timeValue] = { time: (new Date(time)).toUTCString() };
          if (!_channels[channel]) _channels[channel] = { name: channel, value: 0 };

          _timeline[timeValue][channel] = count;
          _channels[channel].value += count;
        });

        var channels = Object.keys(_channels);
        var channelUsage = _.values(_channels);
        var timeline = _.map(_timeline, value => {
          channels.forEach(channel => {
            if (!value[channel]) value[channel] = 0;
          });
          return value;
        });

        return { graphData: timeline, channelUsage, channels, timeFormat: (timespan === "24 hours" ? 'hour' : 'date') };
      }
    },
    {
      id: 'errors',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` exceptions` +
          ` | summarize count_error=count() by handledAt, innermostMessage` +
          ` | order by count_error desc `,
        mappings: [
          { key: 'handledAt', def: 'Unknown' },
          { key: 'message', def: '' }, 
          { key: 'count', def: '' }
        ]
      },
      calculated: (state) => {
        var { values } = state;

        var errors = values;
        var handlers = {};
        var handledAtTotal = 0;
        var handledAtUncaught = 0;
        errors.forEach(error => {
          if (!handlers[error.handledAt]) handlers[error.handledAt] = {
            name: error.handledAt,
            count: 0
          };
          handlers[error.handledAt].count += error.count;
          handledAtTotal += error.count;
          handledAtUncaught += (error.handledAt !== 'UserCode' ? error.count : 0);
        });

        return {
          errors,
          handlers: _.values(handlers),
          handledAtTotal,
          handledAtTotal_icon: handledAtTotal > 0 ? 'bug_report' : 'done',
          handledAtTotal_class: handledAtTotal > 0 ? 'dash-error' : 'dash-success',
          handledAtUncaught
        };
      }
    },
    {
      id: 'intents',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                   ` | extend cslen = customDimensions.callstack_length, intent=customDimensions.intent` +
                   ` | where name startswith "message.intent" and (cslen == 0 or strlen(cslen) == 0) and strlen(intent) > 0` +
                   ` | summarize event_count=count() by tostring(intent)`,
        mappings: [
          { key: 'intent', def: 'Unknown' },
          { key: 'count', def: 0 }
        ]
      },
      calculated: (state) => {
        return {
          bars: [ 'count' ]
        };
      }
    },
    {
      id: 'users',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                ` | summarize totalUsers=count() by user_Id`,
      },
      calculated: (state) => {
        var value = _.values(state);
        return {
          value: value,
          icon: 'face'
        };
      }
    },
    {
      id: 'channelActivity',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                ` | where name == 'Activity'` + 
                ` | extend channel=customDimensions.channel` + <commit_msg>total users card<commit_after>return {
  config: {
    connections: { },
    layout: {
      isDraggable: true,
      isResizable: true,
      rowHeight: 30,
      // This turns off compaction so you can place items wherever.
      verticalCompact: false,
      cols: {lg: 12, md: 10, sm: 6, xs: 4, xxs: 2},
      breakpoints: {lg: 1200, md: 996, sm: 768, xs: 480, xxs: 0}
    }
  },
  dataSources: [
    {
      id: 'timespan',
      type: 'Constant',
      params: {
        values: ['24 hours', '1 week', '1 month', '3 months'],
        selectedValue: '1 month'
      },
      calculated: (state, dependencies) => {
        var queryTimespan = 
              state.selectedValue === '24 hours' ? 'PT24H' : 
              state.selectedValue === '1 week' ? 'P7D' : 
              state.selectedValue === '1 month' ? 'P30D' : 
              'P90D';
        var granularity = 
              state.selectedValue === '24 hours' ? '5m' : 
              state.selectedValue === '1 week' ? '1d' : '1d';

        return { queryTimespan, granularity };
      }
    },
    {
      id: 'conversions',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` +
          ` | extend successful=customDimensions.successful` +
          ` | where name startswith 'message.convert'` +
          ` | summarize event_count=count() by name, tostring(successful)`,
        mappings: [
          { key: 'name' },
          { key: 'successful', val: (val) => val === 'true' },
          { key: 'event_count', def: 0 }
        ]
      },
      calculated: (state) => {
        var { values } = state;

        var total = _.find(values, { name: 'message.convert.start' });
        var successful = _.find(values, { name: 'message.convert.end', successful: true }) || { event_count: 0 };

        if (!total) {
          return null;
        }

        var displayValues = [
          { label: 'Successful', count: successful.event_count },
          { label: 'Failed', count: total.event_count - successful.event_count + 5 },
        ];

        return { displayValues };
      }
    },
    {
      id: 'timeline',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan', granularity: 'timespan:granularity' },
      params: {
        query: (dependencies) => {
          var { granularity } = dependencies;
          return ` customEvents` +
                   ` | where name == 'Activity'` + 
                   ` | summarize event_count=count() by bin(timestamp, ${granularity}), name, tostring(customDimensions.channel)` +
                   ` | order by timestamp asc `
        },
        mappings: [
          { key: 'time' },
          { key: '__' },
          { key: 'channel', def: 'unknown' },
          { key: 'count', def: 0 }
        ]
      },
      calculated: (state) => {
        var _timeline = {};
        var _channels = {};
        var values = state.values || [];
        var timespan = state.timespan;

        values.forEach(row => {
          var { channel, time, count } = row;
          var timeValue = (new Date(time)).getTime();
          
          if (!_timeline[timeValue]) _timeline[timeValue] = { time: (new Date(time)).toUTCString() };
          if (!_channels[channel]) _channels[channel] = { name: channel, value: 0 };

          _timeline[timeValue][channel] = count;
          _channels[channel].value += count;
        });

        var channels = Object.keys(_channels);
        var channelUsage = _.values(_channels);
        var timeline = _.map(_timeline, value => {
          channels.forEach(channel => {
            if (!value[channel]) value[channel] = 0;
          });
          return value;
        });

        return { graphData: timeline, channelUsage, channels, timeFormat: (timespan === "24 hours" ? 'hour' : 'date') };
      }
    },
    {
      id: 'errors',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` exceptions` +
          ` | summarize count_error=count() by handledAt, innermostMessage` +
          ` | order by count_error desc `,
        mappings: [
          { key: 'handledAt', def: 'Unknown' },
          { key: 'message', def: '' }, 
          { key: 'count', def: '' }
        ]
      },
      calculated: (state) => {
        var { values } = state;

        var errors = values;
        var handlers = {};
        var handledAtTotal = 0;
        var handledAtUncaught = 0;
        errors.forEach(error => {
          if (!handlers[error.handledAt]) handlers[error.handledAt] = {
            name: error.handledAt,
            count: 0
          };
          handlers[error.handledAt].count += error.count;
          handledAtTotal += error.count;
          handledAtUncaught += (error.handledAt !== 'UserCode' ? error.count : 0);
        });

        return {
          errors,
          handlers: _.values(handlers),
          handledAtTotal,
          handledAtTotal_icon: handledAtTotal > 0 ? 'bug_report' : 'done',
          handledAtTotal_class: handledAtTotal > 0 ? 'dash-error' : 'dash-success',
          handledAtUncaught
        };
      }
    },
    {
      id: 'intents',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                   ` | extend cslen = customDimensions.callstack_length, intent=customDimensions.intent` +
                   ` | where name startswith "message.intent" and (cslen == 0 or strlen(cslen) == 0) and strlen(intent) > 0` +
                   ` | summarize event_count=count() by tostring(intent)`,
        mappings: [
          { key: 'intent', def: 'Unknown' },
          { key: 'count', def: 0 }
        ]
      },
      calculated: (state) => {
        return {
          bars: [ 'count' ]
        };
      }
    },
    {
      id: 'users',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                ` | summarize totalUsers=count() by user_Id`,
      },
      calculated: (state) => {
        var value = _.values(state);
        return {
          value: value,
          icon: 'account_circle'
        };
      }
    },
    {
      id: 'channelActivity',
      type: 'ApplicationInsights/Query',
      dependencies: { timespan: 'timespan', queryTimespan: 'timespan:queryTimespan' },
      params: {
        query: ` customEvents` + 
                ` | where name == 'Activity'` + 
                ` | extend channel=customDimensions.channel` + 
{'fraction_non_alphanumeric': 0.14175541681968418, 'fraction_numerical': 0.009401395519647448, 'mean_word_length': 1.8841347172209277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4816484', 'n_tokens_mistral': 4090, 'n_tokens_neox': 3931, 'n_words': 902}