<commit_before>require 'stax/aws/dynamodb'

module Stax
  module DynamoDB
    def self.included(thor)
      thor.desc(:dynamodb, 'Dynamo subcommands')
      thor.subcommand(:dynamodb, Cmd::DynamoDB)
    end
  end

  module Cmd
    class DynamoDB < SubCommand

      COLORS = {
        CREATING: :yellow,
        UPDATING: :yellow,
        DELETING: :red,
        ACTIVE:   :green,
      }

      no_commands do
        def stack_tables
          Aws::Cfn.resources_by_type(my.stack_name, 'AWS::DynamoDB::Table')
        end
      end

      desc 'tables', 'list tables for stack'
      def tables
        print_table stack_tables.map { |r|
          t = Aws::DynamoDB.table(r.physical_resource_id)
          [ t.table_name, color(t.table_status, COLORS), t.item_count, t.table_size_bytes, t.creation_date_time ]
        }
      end

      desc 'gsi ID', 'list global secondary indexes for table with ID'
      def gsi(id)
        print_table Aws::DynamoDB.gsi(my.resource(id)).map { |i|
          hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
          range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
          [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
        }.sort
      end

      desc 'lsi ID', 'list local secondary indexes for table with ID'
      def lsi(id)
        print_table Aws::DynamoDB.lsi(my.resource(id)).map { |i|
          hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
          range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
          [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
        }.sort
      end

      desc 'keys ID', 'get hash and range keys of table with ID'
      def keys(id)
        print_table Aws::DynamoDB.table(my.resource(id)).key_schema.each_with_object({}) { |schema, h|
          h[schema.key_type.downcase.to_sym] = schema.attribute_name
        }
      end

      desc 'scan ID', 'scan table with given logical id from this stack'
      def scan(id)
        Aws::DynamoDB.scan(table_name: id)
      end

      desc 'count ID', 'count items in table with given id'
      def count(id)
        puts Aws::DynamoDB.count(table_name: id)
      end

    end
  end
end<commit_msg>fix id in scan and count<commit_after>require 'stax/aws/dynamodb'

module Stax
  module DynamoDB
    def self.included(thor)
      thor.desc(:dynamodb, 'Dynamo subcommands')
      thor.subcommand(:dynamodb, Cmd::DynamoDB)
    end
  end

  module Cmd
    class DynamoDB < SubCommand

      COLORS = {
        CREATING: :yellow,
        UPDATING: :yellow,
        DELETING: :red,
        ACTIVE:   :green,
      }

      no_commands do
        def stack_tables
          Aws::Cfn.resources_by_type(my.stack_name, 'AWS::DynamoDB::Table')
        end
      end

      desc 'tables', 'list tables for stack'
      def tables
        print_table stack_tables.map { |r|
          t = Aws::DynamoDB.table(r.physical_resource_id)
          [ t.table_name, color(t.table_status, COLORS), t.item_count, t.table_size_bytes, t.creation_date_time ]
        }
      end

      desc 'gsi ID', 'list global secondary indexes for table with ID'
      def gsi(id)
        print_table Aws::DynamoDB.gsi(my.resource(id)).map { |i|
          hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
          range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
          [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
        }.sort
      end

      desc 'lsi ID', 'list local secondary indexes for table with ID'
      def lsi(id)
        print_table Aws::DynamoDB.lsi(my.resource(id)).map { |i|
          hash  = i.key_schema.find{ |k| k.key_type == 'HASH' }&.attribute_name
          range = i.key_schema.find{ |k| k.key_type == 'RANGE' }&.attribute_name
          [i.index_name, hash, range, i.projection.projection_type, i.index_size_bytes, i.item_count]
        }.sort
      end

      desc 'keys ID', 'get hash and range keys of table with ID'
      def keys(id)
        print_table Aws::DynamoDB.table(my.resource(id)).key_schema.each_with_object({}) { |schema, h|
          h[schema.key_type.downcase.to_sym] = schema.attribute_name
        }
      end

      desc 'scan ID', 'scan table with given logical id from this stack'
      def scan(id)
        Aws::DynamoDB.scan(table_name: my.resource(id))
      end

      desc 'count ID', 'count items in table with given id'
      def count(id)
        puts Aws::DynamoDB.count(table_name: my.resource(id))
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.11509555507837664, 'fraction_numerical': 0.0, 'mean_word_length': 2.419970631424376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2820571', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1613, 'n_words': 376}