Skip to content

Commit

Permalink
Group the requests by preprint language
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Jun 20, 2024
1 parent e715883 commit 1baad9f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"@observablehq/framework": "^1.9.0",
"d3-dsv": "^3.0.1",
"d3-time-format": "^4.1.0",
"effect": "^3.3.5"
"effect": "^3.3.5",
"iso-639-1": "^3.1.2"
},
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
Expand Down
8 changes: 7 additions & 1 deletion src/data/requests.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { HttpClient, Terminal } from '@effect/platform'
import { NodeTerminal } from '@effect/platform-node'
import { Schema } from '@effect/schema'
import { Effect } from 'effect'
import * as LanguageCode from '../lib/LanguageCode.js'
import * as Temporal from '../lib/Temporal.js'

const Requests = Schema.Array(Schema.Struct({ timestamp: Temporal.InstantFromStringSchema }))
const Requests = Schema.Array(
Schema.Struct({
timestamp: Temporal.InstantFromStringSchema,
language: Schema.optional(LanguageCode.LanguageCodeSchema, { nullable: true }),
}),
)

const program = Effect.gen(function* () {
const terminal = yield* Terminal.Terminal
Expand Down
10 changes: 10 additions & 0 deletions src/lib/LanguageCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Schema } from '@effect/schema'
import type { Predicate } from 'effect'
import iso6391, { type LanguageCode } from 'iso-639-1'

export { type LanguageCode } from 'iso-639-1'

export const isLanguageCode: Predicate.Refinement<unknown, LanguageCode> = (u): u is LanguageCode =>
typeof u === 'string' && iso6391.validate(u)

export const LanguageCodeSchema: Schema.Schema<LanguageCode, string> = Schema.String.pipe(Schema.filter(isLanguageCode))
16 changes: 14 additions & 2 deletions src/requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ toc: false

```js
const parseTimestamp = d3.utcParse('%Y-%m-%dT%H:%M:%S.%LZ')
const languageNames = new Intl.DisplayNames(['en-US'], { type: 'language' })

const requests = FileAttachment('./data/requests.json')
.json()
Expand All @@ -27,13 +28,24 @@ function requestsTimeline({ width } = {}) {
title: 'Requests per week',
width: Math.max(width, 600),
height: 400,
color: {},
color: {
legend: true,
tickFormat: d => (d ? languageNames.of(d) : 'Not yet detected'),
},
y: { grid: true, label: 'Requests' },
x: { label: '' },
marks: [
Plot.rectY(
requests,
Plot.binX({ y: 'count' }, { x: 'timestamp', interval: d3.utcWeek, fill: 'var(--theme-foreground-focus)' }),
Plot.binX(
{ y: 'count' },
{
x: 'timestamp',
interval: d3.utcWeek,
fill: 'language',
order: ['en', 'es', 'pt'],
},
),
),
Plot.ruleY([0]),
],
Expand Down

0 comments on commit 1baad9f

Please sign in to comment.