Skip to content

Commit

Permalink
Extract TimeProxy as TS
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed May 16, 2024
1 parent 1163bba commit 4b951c5
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 91 deletions.
13 changes: 13 additions & 0 deletions app/scripts/korp-api/base-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,19 @@ export default abstract class BaseProxy {
}
}

/** A Korp response is either successful or has error info. */
export type KorpResponse<T> = (T | KorpErrorResponse) & {
/** Execution time in seconds */
time?: number
}

export type KorpErrorResponse = {
ERROR: {
type: string
value: string
}
}

type ProgressResponse = {
progress?: string | { corpus: string }
progress_corpora?: any
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/korp-api/kwic-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @format */
import _ from "lodash"
import settings from "@/settings"
import BaseProxy, { type AjaxSettings } from "@/korp-api/base-proxy"
import BaseProxy, { KorpResponse, type AjaxSettings } from "@/korp-api/base-proxy"
import { angularLocationSearch, httpConfAddMethod } from "@/util"

export default class KwicProxy extends BaseProxy {
Expand Down Expand Up @@ -160,7 +160,7 @@ type MakeRequestOptions = {
type Interval = { start: number; end: number }

/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Concordance/paths/~1query/get */
type KorpQueryResponse = {
type KorpQueryResponse = KorpResponse<{
/** Search hits */
kwic: ApiKwic[]
/** Total number of hits */
Expand All @@ -171,7 +171,7 @@ type KorpQueryResponse = {
time: number
/** A hash of this query */
query_data: string
}
}>

/** Search hits */
type ApiKwic = {
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/korp-api/lemgram-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */
import settings from "@/settings"
import BaseProxy, { AjaxSettings } from "@/korp-api/base-proxy"
import BaseProxy, { AjaxSettings, KorpResponse } from "@/korp-api/base-proxy"
import { httpConfAddMethod } from "@/util"

export default class LemgramProxy extends BaseProxy {
Expand Down Expand Up @@ -61,11 +61,11 @@ type KorpRelationsParams = {
incremental?: boolean
}

type KorpRelationsResponse = {
type KorpRelationsResponse = KorpResponse<{
relations: ApiRelation[]
/** Execution time in seconds */
time: number
}
}>

type ApiRelation = {
dep: string
Expand Down
6 changes: 3 additions & 3 deletions app/scripts/korp-api/stats-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @format */
import _ from "lodash"
import settings from "@/settings"
import BaseProxy, { AjaxSettings } from "@/korp-api/base-proxy"
import BaseProxy, { AjaxSettings, KorpResponse } from "@/korp-api/base-proxy"
import { angularLocationSearch, httpConfAddMethod } from "@/util"
import { statisticsService } from "@/statistics"

Expand Down Expand Up @@ -170,7 +170,7 @@ type KorpStatsParams = {
}

/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Statistics/paths/~1count/get */
type KorpStatsResponse = {
type KorpStatsResponse = KorpResponse<{
corpora: {
[name: string]: StatsColumn
}
Expand All @@ -179,7 +179,7 @@ type KorpStatsResponse = {
count: number
/** Execution time in seconds */
time: number
}
}>

type StatsColumn = {
sums: AbsRelTuple
Expand Down
111 changes: 111 additions & 0 deletions app/scripts/korp-api/time-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/** @format */
import _ from "lodash"
import settings from "@/settings"
import BaseProxy, { AjaxSettings, KorpResponse } from "@/korp-api/base-proxy"
import { httpConfAddMethod } from "@/util"

export default class TimeProxy extends BaseProxy {
makeRequest() {
const data: KorpTimespanParams = {
granularity: "y",
corpus: settings.corpusListing.stringifyAll(),
}

const dfd = $.Deferred()
const ajaxSettings: AjaxSettings = {
url: settings["korp_backend_url"] + "/timespan",
data,
}
const xhr = $.ajax(httpConfAddMethod(ajaxSettings)) as JQuery.jqXHR<KorpTimespanResponse>

xhr.done((data) => {
if ("ERROR" in data) {
console.error("timespan error", data.ERROR)
dfd.reject(data.ERROR)
return
}

const rest = data.combined[""]
delete data.combined[""]

this.expandTimeStruct(data.combined)
const combined = this.compilePlotArray(data.combined)

if (_.keys(data).length < 2) {
dfd.reject()
return
}

return dfd.resolve([data.corpora, combined, rest])
})

xhr.fail(function () {
console.log("timeProxy.makeRequest failed", arguments)
return dfd.reject()
})

return dfd
}

compilePlotArray(dataStruct: Histogram) {
let output = []
$.each(dataStruct, function (key, val) {
if (!key || !val) {
return
}
return output.push([parseInt(key), val])
})

output = output.sort((a, b) => a[0] - b[0])
return output
}

expandTimeStruct(struct: Histogram) {
const years = _.map(_.toPairs(_.omit(struct, "")), (item) => Number(item[0]))
if (!years.length) {
return
}
const minYear = _.min(years)
const maxYear = _.max(years)

if (_.isNaN(maxYear) || _.isNaN(minYear)) {
console.log("expandTimestruct broken, years:", years)
return
}

let prevVal = null
for (let y of _.range(minYear, maxYear + 1)) {
let thisVal = struct[y]
if (typeof thisVal == "undefined") {
struct[y] = prevVal
} else {
prevVal = thisVal
}
}
}
}

/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Statistics/paths/~1timespan/get */
type KorpTimespanParams = {
corpus: string
granularity?: "y" | "m" | "d" | "h" | "n" | "s"
from?: `${number}`
to?: `${number}`
strategy?: 1 | 2 | 3
per_corpus?: boolean
combined?: boolean
incremental?: boolean
}

/** @see https://ws.spraakbanken.gu.se/docs/korp#tag/Statistics/paths/~1timespan/get */
type KorpTimespanResponse = KorpResponse<{
/** An object with corpus names as keys and time statistics objects as values */
corpora: Record<string, Histogram>
/** Number of tokens per time period */
combined: Histogram
/** Execution time in seconds */
time: number
}>

type NumericString = `${number}`
type Histogram = Record<NumericString | "", number>
86 changes: 4 additions & 82 deletions app/scripts/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import settings from "@/settings"
import { httpConfAddMethod } from "@/util"
import BaseProxy from "@/korp-api/base-proxy"
import KwicProxy from "@/korp-api/kwic-proxy"
import LemgramProxy from "./korp-api/lemgram-proxy"
import StatsProxy from "./korp-api/stats-proxy"
import LemgramProxy from "@/korp-api/lemgram-proxy"
import StatsProxy from "@/korp-api/stats-proxy"
import TimeProxy from "@/korp-api/time-proxy"

const model = {}
export default model
Expand All @@ -17,86 +18,7 @@ model.LemgramProxy = LemgramProxy

model.StatsProxy = StatsProxy

model.TimeProxy = class TimeProxy extends BaseProxy {
makeRequest() {
const dfd = $.Deferred()

const xhr = $.ajax(
httpConfAddMethod({
url: settings["korp_backend_url"] + "/timespan",
data: {
granularity: "y",
corpus: settings.corpusListing.stringifyAll(),
},
})
)

xhr.done((data) => {
if (data.ERROR) {
c.error("timespan error", data.ERROR)
dfd.reject(data.ERROR)
return
}

const rest = data.combined[""]
delete data.combined[""]

this.expandTimeStruct(data.combined)
const combined = this.compilePlotArray(data.combined)

if (_.keys(data).length < 2 || data.ERROR) {
dfd.reject()
return
}

return dfd.resolve([data.corpora, combined, rest])
})

xhr.fail(function () {
c.log("timeProxy.makeRequest failed", arguments)
return dfd.reject()
})

return dfd
}

compilePlotArray(dataStruct) {
let output = []
$.each(dataStruct, function (key, val) {
if (!key || !val) {
return
}
return output.push([parseInt(key), val])
})

output = output.sort((a, b) => a[0] - b[0])
return output
}

expandTimeStruct(struct) {
const years = _.map(_.toPairs(_.omit(struct, "")), (item) => Number(item[0]))
if (!years.length) {
return
}
const minYear = _.min(years)
const maxYear = _.max(years)

if (_.isNaN(maxYear) || _.isNaN(minYear)) {
c.log("expandTimestruct broken, years:", years)
return
}

let prevVal = null
for (let y of _.range(minYear, maxYear + 1)) {
let thisVal = struct[y]
if (typeof thisVal == "undefined") {
struct[y] = prevVal
} else {
prevVal = thisVal
}
}
}
}
model.TimeProxy = TimeProxy

model.GraphProxy = class GraphProxy extends BaseProxy {
constructor() {
Expand Down

0 comments on commit 4b951c5

Please sign in to comment.