<commit_before>require 'json'
require 'date'
require 'sequel'
require 'sequel/extensions'
require 'digest/sha1'
require 'gitpeer/controller'
require 'gitpeer/controller/json_representation'

module GitPeer::API
  class Issues < GitPeer::Controller
    include GitPeer::Controller::JSONRepresentation

    uri :issues,       '/{?state}'
    uri :issue,        '/{id}'

    Issue = Struct.new(:id, :name, :body, :state, :created, :updated, :tags)
    Issues = Struct.new(:issues, :stats, :state)

    get :issues do
      state = param :state, default: 'opened'
      issues = db[:issues].where(:state => state).reverse(:updated).as(Issue)
      stats = db[:issues].group_and_count(:state).to_hash(:state, :count)
      json Issues.new(issues, stats, state)
    end

    post :issues do
      now = DateTime.now
      issue = body_as Issue
      issue.state = 'opened'
      issue.created = now
      issue.updated = now
      issue.id = db.transaction do
        issue_id = db[:issues].insert_from issue
        if issue.tags
          tags = issue.tags.map { |t| {tag: t, issue_id: issue_id} }
          db[:issue_tags].multi_insert tags
        end
        issue_id
      end
      json(issue)
    end

    get :issue do
      id = captures[:id]
      issue = db[:issues].where(id: id).as(Issue).first
      not_found unless issue
      issue.tags = db[:issue_tags].where(issue_id: id).single_row(:tag)
      json issue
    end

    patch :issue do
      id = captures[:id]
      issue = db[:issues].where(id: id).as(Issue).first
      not_found unless issue
      values = body.select { |k, v| [:name, :body, :state].include? k }
      unless values.empty?
        values[:updated] = DateTime.now
        db[:issues].where(id: id).update(**values)
        values.each { |k, v| issue[k] = v }
      end
      json issue
    end
<commit_msg>issues: GET /issues/tags to get stats on tags used<commit_after>require 'json'
require 'date'
require 'sequel'
require 'sequel/extensions'
require 'digest/sha1'
require 'gitpeer/controller'
require 'gitpeer/controller/json_representation'

module GitPeer::API
  class Issues < GitPeer::Controller
    include GitPeer::Controller::JSONRepresentation

    uri :issues,       '/{?state}'
    uri :issues_tags,  '/tags'
    uri :issue,        '/{id}'

    Issue = Struct.new(:id, :name, :body, :state, :created, :updated, :tags)
    Issues = Struct.new(:issues, :stats, :state)

    get :issues do
      state = param :state, default: 'opened'
      issues = db[:issues].where(:state => state).reverse(:updated).as(Issue)
      stats = db[:issues].group_and_count(:state).to_hash(:state, :count)
      json Issues.new(issues, stats, state)
    end

    post :issues do
      now = DateTime.now
      issue = body_as Issue
      issue.state = 'opened'
      issue.created = now
      issue.updated = now
      issue.id = db.transaction do
        issue_id = db[:issues].insert_from issue
        if issue.tags
          tags = issue.tags.map { |t| {tag: t, issue_id: issue_id} }
          db[:issue_tags].multi_insert tags
        end
        issue_id
      end
      json(issue)
    end

    get :issues_tags do
      json db[:issue_tags].group_and_count(:tag).to_hash(:tag, :count)
    end

    get :issue do
      id = captures[:id]
      issue = db[:issues].where(id: id).as(Issue).first
      not_found unless issue
      issue.tags = db[:issue_tags].where(issue_id: id).single_row(:tag)
      json issue
    end

    patch :issue do
      id = captures[:id]
      issue = db[:issues].where(id: id).as(Issue).first
      not_found unless issue
      values = body.select { |k, v| [:name, :body, :state].include? k }
      unless values.empty?
        values[:updated] = DateTime.now
        db[:issues].where(id: id).update(**values)
        values.each { |k, v| issue[k] = v }
      end
      json issue
    end

{'fraction_non_alphanumeric': 0.13284898806434872, 'fraction_numerical': 0.0005189413596263622, 'mean_word_length': 2.6231203007518795, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5363436', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1203, 'n_words': 308}