@fonoster/apiserver 0.22.5 → 0.22.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,15 +34,17 @@ const influxdb_client_1 = require("@influxdata/influxdb-client");
34
34
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
35
35
  function createFetchSingleCall(influxdb) {
36
36
  return (accessKeyId, ref) => __awaiter(this, void 0, void 0, function* () {
37
+ // "ref" is a tag, so this filters before pivot runs and only ever
38
+ // touches the one matching series instead of every call in the range.
37
39
  const query = (0, influxdb_client_1.flux) `from(bucket: "${common_1.INFLUXDB_CALLS_BUCKET}")
38
- |> range(start: -365d)
40
+ |> range(start: -30d)
41
+ |> filter(fn: (r) => r._measurement == "${common_1.CALL_DETAIL_RECORD_MEASUREMENT}" and r.ref == "${ref}")
39
42
  |> pivot(rowKey: ["callId"], columnKey: ["_field"], valueColumn: "_value")
40
43
  |> map(fn: (r) => ({
41
44
  r with
42
45
  duration: int(v: r.endedAt) - int(v: r.startedAt)
43
46
  }))
44
- |> filter(fn: (r) => r._measurement == "${common_1.CALL_DETAIL_RECORD_MEASUREMENT}")
45
- |> filter(fn: (r) => r.ref == ${ref} and r.accessKeyId == "${accessKeyId}")
47
+ |> filter(fn: (r) => r.accessKeyId == "${accessKeyId}")
46
48
  |> sort(columns: ["_time"], desc: true)
47
49
  |> limit(n: 1)`;
48
50
  logger.verbose("fetch single call request", { accessKeyId, ref });
@@ -21,17 +21,34 @@ exports.createInfluxDbPub = createInfluxDbPub;
21
21
  */
22
22
  const logger_1 = require("@fonoster/logger");
23
23
  const influxdb_client_1 = require("@influxdata/influxdb-client");
24
+ const createPerCallCache_1 = require("./createPerCallCache");
24
25
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
25
26
  function createInfluxDbPub(config) {
26
27
  const { url, token, org, bucket } = config;
27
28
  logger.info("creating influxdb client", { url, org, bucket });
28
29
  const client = new influxdb_client_1.InfluxDB({ url, token });
29
30
  const writeClient = client.getWriteApi(org, bucket, "ns");
31
+ // A call's fields arrive across multiple events over time, but not every
32
+ // event's payload carries "ref" (e.g. an update with just endedAt/status).
33
+ // Every point for a call must share the same tag set for pivot to merge
34
+ // them into one record, so we remember each call's ref and reapply it to
35
+ // every subsequent write.
36
+ const refByCallId = (0, createPerCallCache_1.createPerCallCache)(24 * 60 * 60 * 1000);
30
37
  return (event) => {
31
38
  logger.verbose("writing event to InfluxDB", event);
32
39
  const point = new influxdb_client_1.Point(event.name).tag("callId", event.tag);
40
+ const ref = event.data.ref
41
+ ? String(event.data.ref)
42
+ : refByCallId.get(event.tag);
43
+ if (ref) {
44
+ point.tag("ref", ref);
45
+ refByCallId.set(event.tag, ref);
46
+ }
33
47
  Object.entries(event.data).forEach(([key, value]) => {
34
- if (typeof value === "number") {
48
+ if (key === "ref") {
49
+ return;
50
+ }
51
+ else if (typeof value === "number") {
35
52
  point.intField(key, value); // Or floatField for floating-point numbers
36
53
  }
37
54
  else if (typeof value === "boolean") {
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ /**
20
+ * Keeps a value stable across a call's lifetime (keyed by callId), pruned by
21
+ * age rather than by guessing which event is "final" from its fields.
22
+ */
23
+ declare function createPerCallCache<T>(ttlMs: number): {
24
+ get(key: string): T | undefined;
25
+ set(key: string, value: T): void;
26
+ };
27
+ export { createPerCallCache };
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
4
+ * http://github.com/fonoster/fonoster
5
+ *
6
+ * This file is part of Fonoster
7
+ *
8
+ * Licensed under the MIT License (the "License");
9
+ * you may not use this file except in compliance with
10
+ * the License. You may obtain a copy of the License at
11
+ *
12
+ * https://opensource.org/licenses/MIT
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.createPerCallCache = createPerCallCache;
22
+ /**
23
+ * Keeps a value stable across a call's lifetime (keyed by callId), pruned by
24
+ * age rather than by guessing which event is "final" from its fields.
25
+ */
26
+ function createPerCallCache(ttlMs) {
27
+ const store = new Map();
28
+ const pruneInterval = setInterval(() => {
29
+ const cutoff = Date.now() - ttlMs;
30
+ store.forEach(({ lastSeenAt }, key) => {
31
+ if (lastSeenAt < cutoff) {
32
+ store.delete(key);
33
+ }
34
+ });
35
+ }, Math.min(ttlMs, 60 * 60 * 1000));
36
+ pruneInterval.unref();
37
+ return {
38
+ get(key) {
39
+ var _a;
40
+ return (_a = store.get(key)) === null || _a === void 0 ? void 0 : _a.value;
41
+ },
42
+ set(key, value) {
43
+ store.set(key, { value, lastSeenAt: Date.now() });
44
+ }
45
+ };
46
+ }
@@ -21,12 +21,17 @@ exports.transformEvent = transformEvent;
21
21
  */
22
22
  const types_1 = require("@fonoster/types");
23
23
  const uuid_1 = require("uuid");
24
+ const createPerCallCache_1 = require("./createPerCallCache");
24
25
  const mapCallDirectionToEnum_1 = require("./mapCallDirectionToEnum");
25
26
  const ACCESS_KEY_ID_HEADER = "X-Access-Key-Id";
26
27
  const CALL_REF_HEADER = "X-Call-Ref";
27
28
  const CALL_DIRECTION_HEADER = "X-Call-Direction";
28
29
  const DOD_NUMBER_HEADER = "X-Dod-Number";
29
30
  const API_ORIGINATED_TYPE_HEADER = "X-Is-Api-Originated-Type";
31
+ // SIP-originated calls don't carry a ref header, so we mint one. It must
32
+ // stay the same across every event for the call, not just be unique per
33
+ // event, since it's now used to tag and look up the call's record.
34
+ const syntheticRefByCallId = (0, createPerCallCache_1.createPerCallCache)(24 * 60 * 60 * 1000);
30
35
  function transformEvent(event) {
31
36
  const transformedEvent = Object.assign({}, event);
32
37
  if (event.startTime) {
@@ -57,8 +62,13 @@ function transformEvent(event) {
57
62
  transformedEvent.ref = extraHeaders[CALL_REF_HEADER];
58
63
  }
59
64
  else {
60
- // SIP originated calls don't have a ref so we need to create one
61
- transformedEvent.ref = (0, uuid_1.v4)();
65
+ // SIP originated calls don't have a ref so we need to create one,
66
+ // reused across every event for this call rather than minted fresh
67
+ // each time.
68
+ const callId = event.callId;
69
+ const ref = syntheticRefByCallId.get(callId) || (0, uuid_1.v4)();
70
+ syntheticRefByCallId.set(callId, ref);
71
+ transformedEvent.ref = ref;
62
72
  }
63
73
  if (extraHeaders[DOD_NUMBER_HEADER]) {
64
74
  transformedEvent.from = extraHeaders[DOD_NUMBER_HEADER];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/apiserver",
3
- "version": "0.22.5",
3
+ "version": "0.22.7",
4
4
  "description": "APIServer for Fonoster",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -22,14 +22,14 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@deepgram/sdk": "^3.5.1",
25
- "@fonoster/authz": "^0.22.4",
26
- "@fonoster/autopilot": "^0.22.5",
25
+ "@fonoster/authz": "^0.22.7",
26
+ "@fonoster/autopilot": "^0.22.7",
27
27
  "@fonoster/common": "^0.22.3",
28
- "@fonoster/identity": "^0.22.4",
28
+ "@fonoster/identity": "^0.22.7",
29
29
  "@fonoster/logger": "^0.22.0",
30
- "@fonoster/sipnet": "^0.22.4",
30
+ "@fonoster/sipnet": "^0.22.7",
31
31
  "@fonoster/streams": "^0.22.0",
32
- "@fonoster/types": "^0.22.0",
32
+ "@fonoster/types": "^0.22.7",
33
33
  "@google-cloud/speech": "^6.6.0",
34
34
  "@google-cloud/text-to-speech": "^5.3.0",
35
35
  "@grpc/grpc-js": "~1.10.11",
@@ -76,5 +76,5 @@
76
76
  "@types/uuid": "^10.0.0",
77
77
  "@types/validator": "^13.15.10"
78
78
  },
79
- "gitHead": "52d26a06a2e7dfec57e7352948eef1b6007285ce"
79
+ "gitHead": "af2404e51a423e45e59515282495ecdec27e0eed"
80
80
  }