{"id":19316753,"url":"https://github.com/maxim/cx_leaderboard","last_synced_at":"2025-12-12T00:13:57.730Z","repository":{"id":57488035,"uuid":"125664604","full_name":"maxim/cx_leaderboard","owner":"maxim","description":"Elixir library for fast, customizable leaderboards","archived":false,"fork":false,"pushed_at":"2019-06-08T19:14:41.000Z","size":91,"stargazers_count":22,"open_issues_count":1,"forks_count":3,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-07-11T07:44:24.553Z","etag":null,"topics":["database","elixir","ets","games","leaderboard"],"latest_commit_sha":null,"homepage":"https://hexdocs.pm/cx_leaderboard/CxLeaderboard.Leaderboard.html","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maxim.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-17T20:00:31.000Z","updated_at":"2025-02-10T04:42:46.000Z","dependencies_parsed_at":"2022-08-29T13:31:17.158Z","dependency_job_id":null,"html_url":"https://github.com/maxim/cx_leaderboard","commit_stats":null,"previous_names":["crossfield/cx_leaderboard"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/maxim/cx_leaderboard","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fcx_leaderboard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fcx_leaderboard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fcx_leaderboard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fcx_leaderboard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxim","download_url":"https://codeload.github.com/maxim/cx_leaderboard/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxim%2Fcx_leaderboard/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267289605,"owners_count":24064730,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","elixir","ets","games","leaderboard"],"created_at":"2024-11-10T01:12:35.516Z","updated_at":"2025-10-16T18:19:38.682Z","avatar_url":"https://github.com/maxim.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CxLeaderboard\n\n[![Travis](https://img.shields.io/travis/maxim/cx_leaderboard.svg?style=flat-square)](https://travis-ci.org/maxim/cx_leaderboard)\n[![Hex.pm](https://img.shields.io/hexpm/v/cx_leaderboard.svg?style=flat-square)](https://hex.pm/packages/cx_leaderboard)\n\nA featureful, fast leaderboard based on ets store. Can carry payloads, calculate custom stats, provide nearby entries around any entry, and do many other fun things.\n\n```elixir\nalias CxLeaderboard.Leaderboard\n\nboard =\n  Leaderboard.create!(name: :global_lb)\n  |\u003e Leaderboard.populate!([\n    {{-23, :id1}, :user1},\n    {{-65, :id2}, :user2},\n    {{-24, :id3}, :user3},\n    {{-23, :id4}, :user4},\n    {{-34, :id5}, :user5}\n  ])\n\nrecords =\n  board\n  |\u003e Leaderboard.top()\n  |\u003e Enum.to_list()\n\n# Returned records (explained):\n#   {{score, id}, payload, {index, {rank, percentile}}}\n# [ {{-65, :id2}, :user2,  {0,     {1,    99.0}}},\n#   {{-65, :id3}, :user3,  {1,     {1,    99.0}}},\n#   {{-34, :id5}, :user5,  {2,     {3,    59.8}}},\n#   {{-23, :id1}, :user1,  {3,     {4,    40.2}}},\n#   {{-23, :id4}, :user4,  {4,     {4,    40.2}}} ]\n```\n\n## Features\n\n* Ranks, percentiles, any custom stats of your choice\n* Concurrent reads, sequential writes\n* Stream API access to records from the top and the bottom\n* O(1) querying of any record by id\n* Auto-populating data on leaderboard startup\n* Adding, updating, removing, upserting of individual entries in live leaderboard\n* Fetching a range of records around a given id (contextual leaderboard)\n* Pluggable data stores: `EtsStore` for big boards, `TermStore` for dynamic mini boards\n* Atomic full repopulation in O(2n log n) time\n* Multi-node support\n* Extensibility for storage engines (`CxLeaderboard.Storage` behaviour)\n\n## Installation\n\nThe package can be installed by adding `cx_leaderboard` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:cx_leaderboard, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n## Documentation\n\nhttps://hexdocs.pm/cx_leaderboard/CxLeaderboard.Leaderboard.html\n\n## Global Leaderboards\n\nIf you want to have a global leaderboard starting at the same time as your application, and running alongside it, all you need to do is declare a \nas follows:\n\n```elixir\ndefmodule Foo.Application do\n  use Application\n\n  def start(_type, _args) do\n    import Supervisor.Spec\n\n    children = [\n      # This is where you provide a data enumerable (e.g. a stream of paginated \n      # Postgres results) for leaderboard to auto-populate itself on startup.\n      # It's best if this is implemented as a Stream to avoid consuming more\n      # RAM than necessary.\n      worker(CxLeaderboard.Leaderboard, [:global, [data: Foo.MyData.load()]])\n    ]\n\n    opts = [strategy: :one_for_one, name: Foo.Supervisor]\n    Supervisor.start_link(children, opts)\n  end\nend\n```\n\nThen you can interact with it anywhere in your app like this:\n\n```elixir\nalias CxLeaderboard.Leaderboard\n\nglobal_lb = Leaderboard.client_for(:global)\nglobal_lb\n|\u003e Leaderboard.top()\n|\u003e Enum.take(10)\n```\n\n## Fetching ranges\n\nIf you want to get a record and its context (nearby records), you can use a range.\n\n```elixir\nLeaderboard.get(board, :id3, -1..1)\n# [\n#   {{-34, :id5}, :user5, {1, {2, 79.4}}},\n#   {{-24, :id3}, :user3, {2, {3, 59.8}}},\n#   {{-23, :id1}, :user1, {3, {4, 40.2}}}\n# ]\n```\n\n## Different ranking flavors\n\nTo use different ranking you can just create your own indexer. Here's an example of the above leaderboard only in this case we want sequential ranks.\n\n```elixir\nalias CxLeaderboard.{Leaderboard, Indexer}\n\nmy_indexer = Indexer.new(on_rank:\n  \u0026Indexer.Stats.sequential_rank_1_99_less_or_equal_percentile/1)\n\nboard =\n  Leaderboard.create!(name: :global_lb, indexer: my_indexer)\n  |\u003e Leaderboard.populate!([\n    {{-23, :id1}, :user1},\n    {{-65, :id2}, :user2},\n    {{-65, :id3}, :user3},\n    {{-23, :id4}, :user4},\n    {{-34, :id5}, :user5}\n  ])\n\nrecords =\n  board\n  |\u003e Leaderboard.top()\n  |\u003e Enum.to_list()\n\n# Returned records (explained):\n# [ {{-65, :id2}, :user2, {0, {1, 99.0}}},\n#   {{-65, :id3}, :user3, {1, {1, 99.0}}},\n#   {{-34, :id5}, :user5, {2, {2, 59.8}}},\n#   {{-23, :id1}, :user1, {3, {3, 40.2}}},\n#   {{-23, :id4}, :user4, {4, {3, 40.2}}} ]\n```\n\nNotice how the resulting ranks are not offset like 1,1,3,4,4 but are sequential like 1,1,2,3,3.\n\nSee docs for `CxLeaderboard.Indexer.Stats` for various pre-packaged functions you can plug into the indexer, or write your own.\n\n## Mini-leaderboards\n\nSometimes all you need is to render a quick one-off leaderboard with just a few entries in it. For this you don't have to run a persistent ets, instead you can use `TermStore`.\n\n```elixir\nminiboard =\n  Leaderboard.create!(store: CxLeaderboard.TermStore)\n  |\u003e Leaderboard.populate!(\n    [\n      {23, 1},\n      {65, 2},\n      {24, 3},\n      {23, 4},\n      {34, 5}\n    ]\n  )\n\nminiboard\n|\u003e Leaderboard.top()\n|\u003e Enum.take(3)\n# [\n#   {{23, 1}, 1, {0, {1, 99.0}}},\n#   {{23, 4}, 4, {1, {1, 99.0}}},\n#   {{24, 3}, 3, {2, {3, 59.8}}}\n# ]\n```\n\nThis would produce a complete full-featured leaderboard that's entirely stored in the `miniboard` variable. All the same API functions work on it.\n\nNote: It is not recommended to use `TermStore` for big leaderboards (as evident from the benchmarks below). A typical use case for it would be to dynamically render a single-page leaderboard among a small group of users.\n\n## Benchmark\n\nThese benchmarks use 1 million randomly generated records, however, the same set of records is used for both ets and term leaderboard within each benchmark.\n\n```\nOperating System: macOS\nCPU Information: Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz\nNumber of Available Cores: 8\nAvailable memory: 16 GB\nElixir 1.6.2\nErlang 20.2.4\nBenchmark suite executing with the following configuration:\nwarmup: 2 s\ntime: 5 s\nparallel: 1\n```\n\n### Populating the leaderboard with 1mil entries\n\nScript: [benchmark/populate.exs](benchmark/populate.exs)\n\n```\nName           ips        average  deviation         median         99th %\nets           0.21         4.76 s     ±0.95%         4.76 s         4.81 s\nterm         0.169         5.91 s     ±0.00%         5.91 s         5.91 s\n\nComparison:\nets           0.21\nterm         0.169 - 1.24x slower\n```\n\nSummary:\n\n  - It takes ~4.76s to populate ets leaderboard with 1 million random scores.\n  - It takes ~5.91s to populate term leaderboard with 1 million random scores (but you shouldn't).\n\nThe leaderboard is fully sorted and indexed at the end.\n\n### Adding an entry to 1mil leaderboard\n\nScript: [benchmark/add_entry.exs](benchmark/add_entry.exs)\n\n```\nName           ips        average  deviation         median         99th %\nets       148.95 K      0.00001 s    ±88.34%      0.00001 s      0.00002 s\nterm     0.00034 K         2.92 s     ±0.56%         2.92 s         2.94 s\n\nComparison:\nets       148.95 K\nterm     0.00034 K - 435227.97x slower\n```\n\nAs you can see, you should not create a `TermStore` leaderboard with a million entries.\n\n### Getting a -10..10 range from 1mil leaderboard\n\nScript: [benchmark/range.exs](benchmark/range.exs)\n\n```\nName           ips        average  deviation         median         99th %\nets        17.84 K      0.0560 ms    ±20.66%      0.0530 ms       0.101 ms\nterm     0.00290 K      345.13 ms     ±3.83%      345.04 ms      374.28 ms\n\nComparison:\nets        17.84 K\nterm     0.00290 K - 6158.09x slower\n```\n\nAnother example of how the `TermStore` is not intended for big number of entries.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fcx_leaderboard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxim%2Fcx_leaderboard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxim%2Fcx_leaderboard/lists"}