File size: 1,008 Bytes
7e5cb25
 
 
 
 
 
 
 
 
 
 
 
 
 
97826e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e5cb25
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import postgres from "postgres"
import { cache } from "react"

const sql = postgres(process.env.POSTGRES_URL) // will use psql environment variables

export const getModels = cache(async () => {
  const models = await sql`
    SELECT models.*, SUM(results.score) as total_score
    FROM models
    LEFT JOIN results ON models.id = results.model
    GROUP BY models.id
    ORDER BY total_score DESC;
  `

  console.log("models", models)

  const sorted = models.sort((a, b) => b.total_score - a.total_score)

  // set the rank, so that if two models have the same score, they have the same rank
  for (let i = 0; i < sorted.length; i++) {
    const model = sorted[i]
    const previousModel = sorted[i - 1]

    if (previousModel && previousModel.total_score === model.total_score) {
      model.rank = previousModel.rank
    } else {
      model.rank = previousModel ? previousModel.rank + 1 : 1
    }

    model.slug = model.api_id.split("/").pop().toLowerCase()
  }

  return sorted
})

export default sql