{"action":"created","comment":{"url":"https://api.github.com/repos/wagn/wikirate/pulls/comments/43799624","id":43799624,"diff_hunk":"@@ -0,0 +1,231 @@\n+include Card::CachedCount\n+include Type::SearchType\n+\n+def virtual?; true end\n+\n+def raw_content\n+  %{\n+    {\n+      \"left\":{\n+        \"type\":\"metric_value\",\n+        \"left\":{\n+          \"left\":\"_left\"\n+        }\n+      },\n+      \"right\":\"value\",\n+      \"limit\":0\n+    }\n+  }\n+end\n+\n+def get_params key, default\n+  if (value = Env.params[key])\n+    value.to_i\n+  else\n+    default\n+  end\n+end\n+\n+def query params={}\n+  default_query = params.delete :default_query\n+  @query = super params\n+  if !default_query\n+    @query[:limit] = params[:default_limit] || 20\n+    @query[:offset] = get_params(\"offset\", 0)\n+  end\n+  @query\n+end\n+\n+def get_cached_result\n+  @cached_metric_values ||= begin\n+    cached_json = fetch(trait: :cached_count, new: {}).format.render_core\n+    JSON.parse(cached_json)\n+  end\n+end\n+\n+def count params={}\n+  get_cached_result.size\n+end\n+\n+format do\n+  include Type::SearchType::Format\n+\n+  def sort_value_asc cached_metric_values\n+    cached_metric_values.sort do |x,y|\n+      strcmp x[1].sort_by {|value| value[\"year\"]}.reverse[0][\"value\"],\n+        y[1].sort_by {|value| value[\"value\"]}.reverse[0][\"value\"]\n+    end\n+  end\n+\n+  def sort_name_asc cached_metric_values\n+    cached_metric_values.sort do |x,y|\n+      x[0].downcase <=> y[0].downcase\n+    end\n+  end\n+\n+  def get_sorted_result cached_metric_values, sort_by, order\n+    case sort_by\n+    when \"company_name\"\n+      if order == \"asc\"\n+        sort_name_asc cached_metric_values\n+      else\n+        sort_name_asc(cached_metric_values).reverse\n+      end\n+    when \"value\"\n+      if order == \"asc\"\n+        sort_value_asc cached_metric_values\n+      else\n+        sort_value_asc(cached_metric_values).reverse\n+      end\n+    end\n+  end\n+\n+  def search_results args={}\n+    @search_results ||= begin\n+      sort_by = Env.params[\"sort_by\"] || \"company_name\"\n+      sort_order = Env.params[\"sort_order\"] || \"asc\"\n+      offset = card.get_params(\"offset\", 0)\n+      limit = card.query(search_params)[:limit]\n+      cached_result = card.get_cached_result\n+      all_results = get_sorted_result(cached_result, sort_by, sort_order)\n+      all_results[offset, limit]\n+    end\n+  end\n+\n+end\n+format :html do\n+  include Type::SearchType::HtmlFormat\n+  def get_sort_icon_by_state state\n+    order = state.empty? ? '' : \"-#{state}\"\n+    %{<i class=\"fa fa-sort#{order}\"></i>}\n+  end\n+\n+  def toggle_sort_order order\n+    order == 'asc' ? 'desc' : 'asc'\n+  end\n+\n+  def get_sort_order sort_by, sort_order\n+    if sort_by == 'company_name' \n+      [toggle_sort_order(sort_order), 'asc']\n+    else\n+      [get_sort_icon(''), get_sort_icon(sort_order)]\n+    end\n+  end\n+\n+  def get_sort_icon sort_by, sort_order\n+    if sort_by == 'company_name' \n+      [get_sort_icon_by_state(sort_order), get_sort_icon_by_state('')]\n+    else\n+      [get_sort_icon_by_state(''), get_sort_icon_by_state(sort_order)]\n+    end\n+  end\n+\n+  view :card_list_header do |args|\n+    sort_by = Env.params['sort_by'] || 'company_name'\n+    sort_order = Env.params['sort_order'] || 'asc'\n+    offset = card.get_params('offset', 0)\n+    limit = card.query(search_params)[:limit]\n+    company_sort_order, value_sort_order = get_sort_order sort_by, sort_order\n+    company_sort_icon, value_sort_icon = get_sort_icon sort_by, sort_order\n+     \n+    url_template = \"/#{card.cardname.url_key}?item=content&offset=#{offset}\"\\\n+                   \"&limit=#{limit}&sort_order=%s&sort_by=%s\"\n+    %{\n+      <div class='yinyang-row column-header'>\n+        <div class='company-item value-item'>\n+          <a class='header metric-list-header slotter' data-remote='true' \n+            href='#{sprintf(url_template, company_sort_order, 'company_name')}'>\n+            Companies #{company_sort_icon}\n+          </a>\n+          <a class='data metric-list-header slotter' data-remote='true' \n+            href='#{sprintf(url_template, value_sort_order, 'value')}'>\n+            Values #{value_sort_icon}\n+          </a>\n+        </div>\n+      </div>\n+    }\n+  end\n+  # compare lenght first and then normal string comparison\n+  def strcmp str1, str2\n+    if (length_diff = str1.length - str2.length) == 0\n+      str1 <=> str2\n+    else\n+      length_diff\n+    end\n+  end\n+\n+  view :card_list_item do |args|\n+    c = args[:item_card]\n+    item_view = inclusion_defaults(c)[:view]\n+    %{\n+      <div class=\"search-result-item item-#{ item_view }\">\n+        #{nest(c, size: args[:size], view: item_view)}\n+      </div>\n+    }\n+  end\n+\n+  view :card_list_items do |args|\n+    results =\n+      search_results.map do |row|\n+        c = Card[\"#{card.cardname.left}+#{row[0]}\"]\n+        render :card_list_item, args.clone.merge(item_card: c)\n+      end.join \"\\n\"\n+  end\n+\n+  view :card_list do |args|\n+    paging = _optional_render :paging, args.merge({cached_query: true})\n+    if search_results.empty?\n+      render_no_search_results(args)\n+    else\n+      results = render :card_list_items, args\n+      header = render :card_list_header, args\n+      %{\n+        #{paging}\n+        #{header}\n+        <div class=\"search-result-list\">\n+          #{results}\n+        </div>\n+        #{paging if search_results.length > 10}\n+      }\n+    end\n+  end\n+end\n+\n+def cached_count\n+  cached_count_card.content\n+end\n+\n+expired_cached_count_cards do |changed_card|\n+  case changed_card.type_id\n+  when MetricValueID\n+    changed_card = changed_card.left.left.fetch(trait: :all_values)\n+    changed_card.update_cached_count if changed_card\n+  when WikirateCompanyID\n+    metrics = Card.search type_id: MetricID, right_plus: changed_card.name\n+    metrics.each do |metric|\n+      changed_card = metric.fetch(trait: :all_values)\n+      changed_card.update_cached_count if changed_card  \n+    end\n+  when MetricID\n+    changed_card = changed_card.fetch(trait: :all_values)\n+    changed_card.update_cached_count if changed_card  \n+  end\n+\n+end\n+\n+# get all metric values\n+def calculate_count\n+  result = {}\n+  item_cards(default_query: true).each do |card|\n+    company_name = card.left.left.cardname.right\n+    year = card.cardname.parts[-2]\n+    value = card.content\n+    unless result.has_key?company_name\n+      result[company_name] = []\n+    end\n+    result[company_name].push({ year: year, value: value})\n+  end\n+  result.to_json","path":"mod/wikirate/set/type_plus_right/metric/all_values.rb","position":228,"original_position":228,"commit_id":"b340cd1807ee1869f3227765ba07e187d18959dd","original_commit_id":"42230a785eb7313c31b4c4d75f4dfd0e0e8fbb59","user":{"login":"ethn","id":21904,"avatar_url":"https://avatars.githubusercontent.com/u/21904?v=3","gravatar_id":"","url":"https://api.github.com/users/ethn","html_url":"https://github.com/ethn","followers_url":"https://api.github.com/users/ethn/followers","following_url":"https://api.github.com/users/ethn/following{/other_user}","gists_url":"https://api.github.com/users/ethn/gists{/gist_id}","starred_url":"https://api.github.com/users/ethn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ethn/subscriptions","organizations_url":"https://api.github.com/users/ethn/orgs","repos_url":"https://api.github.com/users/ethn/repos","events_url":"https://api.github.com/users/ethn/events{/privacy}","received_events_url":"https://api.github.com/users/ethn/received_events","type":"User","site_admin":false},"body":"It's clever to try to reuse this code, but we can't do this without serious renaming and/or refactoring.  We can't have a method called \"calculate_count\" return json with a bunch of values.  it's just too hacky.","created_at":"2015-11-03T20:03:54Z","updated_at":"2015-11-03T20:03:54Z","html_url":"https://github.com/wagn/wikirate/pull/266#discussion_r43799624","pull_request_url":"https://api.github.com/repos/wagn/wikirate/pulls/266","_links":{"self":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/comments/43799624"},"html":{"href":"https://github.com/wagn/wikirate/pull/266#discussion_r43799624"},"pull_request":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/266"}}},"pull_request":{"url":"https://api.github.com/repos/wagn/wikirate/pulls/266","id":48950761,"html_url":"https://github.com/wagn/wikirate/pull/266","diff_url":"https://github.com/wagn/wikirate/pull/266.diff","patch_url":"https://github.com/wagn/wikirate/pull/266.patch","issue_url":"https://api.github.com/repos/wagn/wikirate/issues/266","number":266,"state":"open","locked":false,"title":"Cached metric value","user":{"login":"chuenlok","id":1924561,"avatar_url":"https://avatars.githubusercontent.com/u/1924561?v=3","gravatar_id":"","url":"https://api.github.com/users/chuenlok","html_url":"https://github.com/chuenlok","followers_url":"https://api.github.com/users/chuenlok/followers","following_url":"https://api.github.com/users/chuenlok/following{/other_user}","gists_url":"https://api.github.com/users/chuenlok/gists{/gist_id}","starred_url":"https://api.github.com/users/chuenlok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chuenlok/subscriptions","organizations_url":"https://api.github.com/users/chuenlok/orgs","repos_url":"https://api.github.com/users/chuenlok/repos","events_url":"https://api.github.com/users/chuenlok/events{/privacy}","received_events_url":"https://api.github.com/users/chuenlok/received_events","type":"User","site_admin":false},"body":"This bases on subcard fix branch.","created_at":"2015-10-28T00:21:43Z","updated_at":"2015-11-03T20:03:54Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d6a8fd525b3e5ccf1b76bd785e3b4de64e2b6f29","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/wagn/wikirate/pulls/266/commits","review_comments_url":"https://api.github.com/repos/wagn/wikirate/pulls/266/comments","review_comment_url":"https://api.github.com/repos/wagn/wikirate/pulls/comments{/number}","comments_url":"https://api.github.com/repos/wagn/wikirate/issues/266/comments","statuses_url":"https://api.github.com/repos/wagn/wikirate/statuses/b340cd1807ee1869f3227765ba07e187d18959dd","head":{"label":"chuenlok:cached_metric_value","ref":"cached_metric_value","sha":"b340cd1807ee1869f3227765ba07e187d18959dd","user":{"login":"chuenlok","id":1924561,"avatar_url":"https://avatars.githubusercontent.com/u/1924561?v=3","gravatar_id":"","url":"https://api.github.com/users/chuenlok","html_url":"https://github.com/chuenlok","followers_url":"https://api.github.com/users/chuenlok/followers","following_url":"https://api.github.com/users/chuenlok/following{/other_user}","gists_url":"https://api.github.com/users/chuenlok/gists{/gist_id}","starred_url":"https://api.github.com/users/chuenlok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chuenlok/subscriptions","organizations_url":"https://api.github.com/users/chuenlok/orgs","repos_url":"https://api.github.com/users/chuenlok/repos","events_url":"https://api.github.com/users/chuenlok/events{/privacy}","received_events_url":"https://api.github.com/users/chuenlok/received_events","type":"User","site_admin":false},"repo":{"id":21124075,"name":"wikirate","full_name":"chuenlok/wikirate","owner":{"login":"chuenlok","id":1924561,"avatar_url":"https://avatars.githubusercontent.com/u/1924561?v=3","gravatar_id":"","url":"https://api.github.com/users/chuenlok","html_url":"https://github.com/chuenlok","followers_url":"https://api.github.com/users/chuenlok/followers","following_url":"https://api.github.com/users/chuenlok/following{/other_user}","gists_url":"https://api.github.com/users/chuenlok/gists{/gist_id}","starred_url":"https://api.github.com/users/chuenlok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chuenlok/subscriptions","organizations_url":"https://api.github.com/users/chuenlok/orgs","repos_url":"https://api.github.com/users/chuenlok/repos","events_url":"https://api.github.com/users/chuenlok/events{/privacy}","received_events_url":"https://api.github.com/users/chuenlok/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/chuenlok/wikirate","description":"Wikirate.org is a new independent wiki platform with the mission to promote corporate transparency.","fork":true,"url":"https://api.github.com/repos/chuenlok/wikirate","forks_url":"https://api.github.com/repos/chuenlok/wikirate/forks","keys_url":"https://api.github.com/repos/chuenlok/wikirate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chuenlok/wikirate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chuenlok/wikirate/teams","hooks_url":"https://api.github.com/repos/chuenlok/wikirate/hooks","issue_events_url":"https://api.github.com/repos/chuenlok/wikirate/issues/events{/number}","events_url":"https://api.github.com/repos/chuenlok/wikirate/events","assignees_url":"https://api.github.com/repos/chuenlok/wikirate/assignees{/user}","branches_url":"https://api.github.com/repos/chuenlok/wikirate/branches{/branch}","tags_url":"https://api.github.com/repos/chuenlok/wikirate/tags","blobs_url":"https://api.github.com/repos/chuenlok/wikirate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chuenlok/wikirate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chuenlok/wikirate/git/refs{/sha}","trees_url":"https://api.github.com/repos/chuenlok/wikirate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chuenlok/wikirate/statuses/{sha}","languages_url":"https://api.github.com/repos/chuenlok/wikirate/languages","stargazers_url":"https://api.github.com/repos/chuenlok/wikirate/stargazers","contributors_url":"https://api.github.com/repos/chuenlok/wikirate/contributors","subscribers_url":"https://api.github.com/repos/chuenlok/wikirate/subscribers","subscription_url":"https://api.github.com/repos/chuenlok/wikirate/subscription","commits_url":"https://api.github.com/repos/chuenlok/wikirate/commits{/sha}","git_commits_url":"https://api.github.com/repos/chuenlok/wikirate/git/commits{/sha}","comments_url":"https://api.github.com/repos/chuenlok/wikirate/comments{/number}","issue_comment_url":"https://api.github.com/repos/chuenlok/wikirate/issues/comments{/number}","contents_url":"https://api.github.com/repos/chuenlok/wikirate/contents/{+path}","compare_url":"https://api.github.com/repos/chuenlok/wikirate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chuenlok/wikirate/merges","archive_url":"https://api.github.com/repos/chuenlok/wikirate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chuenlok/wikirate/downloads","issues_url":"https://api.github.com/repos/chuenlok/wikirate/issues{/number}","pulls_url":"https://api.github.com/repos/chuenlok/wikirate/pulls{/number}","milestones_url":"https://api.github.com/repos/chuenlok/wikirate/milestones{/number}","notifications_url":"https://api.github.com/repos/chuenlok/wikirate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chuenlok/wikirate/labels{/name}","releases_url":"https://api.github.com/repos/chuenlok/wikirate/releases{/id}","created_at":"2014-06-23T11:37:42Z","updated_at":"2015-05-08T17:22:34Z","pushed_at":"2015-11-03T19:57:42Z","git_url":"git://github.com/chuenlok/wikirate.git","ssh_url":"git@github.com:chuenlok/wikirate.git","clone_url":"https://github.com/chuenlok/wikirate.git","svn_url":"https://github.com/chuenlok/wikirate","homepage":"http://wikirate.org","size":86399,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"wagn:master","ref":"master","sha":"3c76a8612d6ef59d8319f985386f89cf9bbd2eb2","user":{"login":"wagn","id":20722,"avatar_url":"https://avatars.githubusercontent.com/u/20722?v=3","gravatar_id":"","url":"https://api.github.com/users/wagn","html_url":"https://github.com/wagn","followers_url":"https://api.github.com/users/wagn/followers","following_url":"https://api.github.com/users/wagn/following{/other_user}","gists_url":"https://api.github.com/users/wagn/gists{/gist_id}","starred_url":"https://api.github.com/users/wagn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wagn/subscriptions","organizations_url":"https://api.github.com/users/wagn/orgs","repos_url":"https://api.github.com/users/wagn/repos","events_url":"https://api.github.com/users/wagn/events{/privacy}","received_events_url":"https://api.github.com/users/wagn/received_events","type":"User","site_admin":false},"repo":{"id":15884166,"name":"wikirate","full_name":"wagn/wikirate","owner":{"login":"wagn","id":20722,"avatar_url":"https://avatars.githubusercontent.com/u/20722?v=3","gravatar_id":"","url":"https://api.github.com/users/wagn","html_url":"https://github.com/wagn","followers_url":"https://api.github.com/users/wagn/followers","following_url":"https://api.github.com/users/wagn/following{/other_user}","gists_url":"https://api.github.com/users/wagn/gists{/gist_id}","starred_url":"https://api.github.com/users/wagn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wagn/subscriptions","organizations_url":"https://api.github.com/users/wagn/orgs","repos_url":"https://api.github.com/users/wagn/repos","events_url":"https://api.github.com/users/wagn/events{/privacy}","received_events_url":"https://api.github.com/users/wagn/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/wagn/wikirate","description":"","fork":false,"url":"https://api.github.com/repos/wagn/wikirate","forks_url":"https://api.github.com/repos/wagn/wikirate/forks","keys_url":"https://api.github.com/repos/wagn/wikirate/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wagn/wikirate/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wagn/wikirate/teams","hooks_url":"https://api.github.com/repos/wagn/wikirate/hooks","issue_events_url":"https://api.github.com/repos/wagn/wikirate/issues/events{/number}","events_url":"https://api.github.com/repos/wagn/wikirate/events","assignees_url":"https://api.github.com/repos/wagn/wikirate/assignees{/user}","branches_url":"https://api.github.com/repos/wagn/wikirate/branches{/branch}","tags_url":"https://api.github.com/repos/wagn/wikirate/tags","blobs_url":"https://api.github.com/repos/wagn/wikirate/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wagn/wikirate/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wagn/wikirate/git/refs{/sha}","trees_url":"https://api.github.com/repos/wagn/wikirate/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wagn/wikirate/statuses/{sha}","languages_url":"https://api.github.com/repos/wagn/wikirate/languages","stargazers_url":"https://api.github.com/repos/wagn/wikirate/stargazers","contributors_url":"https://api.github.com/repos/wagn/wikirate/contributors","subscribers_url":"https://api.github.com/repos/wagn/wikirate/subscribers","subscription_url":"https://api.github.com/repos/wagn/wikirate/subscription","commits_url":"https://api.github.com/repos/wagn/wikirate/commits{/sha}","git_commits_url":"https://api.github.com/repos/wagn/wikirate/git/commits{/sha}","comments_url":"https://api.github.com/repos/wagn/wikirate/comments{/number}","issue_comment_url":"https://api.github.com/repos/wagn/wikirate/issues/comments{/number}","contents_url":"https://api.github.com/repos/wagn/wikirate/contents/{+path}","compare_url":"https://api.github.com/repos/wagn/wikirate/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wagn/wikirate/merges","archive_url":"https://api.github.com/repos/wagn/wikirate/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wagn/wikirate/downloads","issues_url":"https://api.github.com/repos/wagn/wikirate/issues{/number}","pulls_url":"https://api.github.com/repos/wagn/wikirate/pulls{/number}","milestones_url":"https://api.github.com/repos/wagn/wikirate/milestones{/number}","notifications_url":"https://api.github.com/repos/wagn/wikirate/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wagn/wikirate/labels{/name}","releases_url":"https://api.github.com/repos/wagn/wikirate/releases{/id}","created_at":"2014-01-13T22:29:39Z","updated_at":"2015-05-08T17:01:43Z","pushed_at":"2015-11-03T19:57:43Z","git_url":"git://github.com/wagn/wikirate.git","ssh_url":"git@github.com:wagn/wikirate.git","clone_url":"https://github.com/wagn/wikirate.git","svn_url":"https://github.com/wagn/wikirate","homepage":null,"size":87136,"stargazers_count":2,"watchers_count":2,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":1,"forks":5,"open_issues":1,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/266"},"html":{"href":"https://github.com/wagn/wikirate/pull/266"},"issue":{"href":"https://api.github.com/repos/wagn/wikirate/issues/266"},"comments":{"href":"https://api.github.com/repos/wagn/wikirate/issues/266/comments"},"review_comments":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/266/comments"},"review_comment":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/wagn/wikirate/pulls/266/commits"},"statuses":{"href":"https://api.github.com/repos/wagn/wikirate/statuses/b340cd1807ee1869f3227765ba07e187d18959dd"}}}}