An open API service indexing awesome lists of open source software.

https://github.com/vtfk/azf-statistics

Azure function for handling stats
https://github.com/vtfk/azf-statistics

Last synced: 27 days ago
JSON representation

Azure function for handling stats

Awesome Lists containing this project

README

        

# azf-statistics
Azure function for handling stats

# Usage
## POST /Stats or POST /Stats/{system}
Creates a statistics document in {system}-collection in statistics database

See the example below for required fields. You can add as many optional fields as you like in addition to the required fields.

### POST /Stats
Example payload
```js
const payload = {
"system": "Acos", // Required when not using system in param (url). System name. New system creates a new collection
"engine": "azf-acos-interact v1", // Required. e.g. from package json
"county": "VTFK", // Optional. If missing autogenerated from DEFAULT_COUNTY environment variable
"company": "SMM", // Required. Sector
"department": "Graveteam", // Optional. If missing, company will be set here.
"description": "Arkivering av gravesøknad og opprettelse av et listeelement i SP. Oppdaterer også kontaktobjektet i 360", // Required. A description of what the statistic element represents
"projectId": "Prosjekt ID (Hvis det er en kobling til innovasjonsløypa)", // Optional. If not set, will be set to "ingen prosjekttilknytning"
"type": "SMM - Gravesoknad", // Required. A short searchable type-name that distinguishes the statistic element
"externalId": "refId fra Acos", // Optional. ID in the external {system}
// optional fields:
"optionalField": "23/45678-3", // Optional. anything you like
"optionalField2": 4567895, // Optional. anything you like
// ...
"anotherLastOptionalkField": true // Optional. anything you like
}
```

### POST /Stats/{system}
Example payload - system from url will be used as "system" property and collection in stats db
```js
const payload = {
"engine": "azf-acos-interact v1", // Required. e.g. from package json
"county": "VTFK", // Optional. If missing autogenerated from DEFAULT_COUNTY environment variable
"company": "SMM", // Required. Sector
"department": "Graveteam", // Optional. If missing, company will be set here.
"description": "Arkivering av gravesøknad og opprettelse av et listeelement i SP. Oppdaterer også kontaktobjektet i 360", // Required. A description of what the statistic element represents
"projectId": "Prosjekt ID (Hvis det er en kobling til innovasjonsløypa)", // Optional. If not set, will be set to "ingen prosjekttilknytning"
"type": "SMM - Gravesoknad", // Required. A short searchable type-name that distinguishes the statistic element
"externalId": "refId fra Acos", // Optional. ID in the external {system}
// optional fields:
"optionalField": "23/45678-3", // Optional. anything you like
"optionalField2": 4567895, // Optional. anything you like
// ...
"anotherLastOptionalkField": true // Optional. anything you like
}
```

## GET /Systems
Returns a list of all system names (collections) in the statistics db

Example request
`GET https://{statsurl}/api/Systems`

Example return value
```json
[
"publish-teams-document",
"vigo-isi-arkiv",
"import-files-p360",
"tullball",
"Masseutsendelse",
"littasystem",
"IOP",
"onbaording",
"Digitroll",
"MinElev",
"hei med mellomrom",
"Acos skjema",
"onboarding",
"dos-arkivaros"
]
```

## GET /Stats/{system}
Returns stats for {system}

Example request
`GET https://{statsurl}/api/Stats/Acos%20skjema`

Example return value
```json
[
{
"_id": "655f398962f1958a50d4eec5",
"system": "Acos skjema",
"engine": "azf-acos-interact 1.9.1",
"createdTimestamp": "2023-11-23T11:37:45.025Z",
"county": "VFK",
"company": "Opplæring",
"department": "EKSAMEN",
"description": "Sender til elevmappe",
"projectId": "ingen prosjekttilknytning",
"externalId": "1287555",
"type": "Søknad om tilrettelegging på fag, svenne eller kompetanseprøve"
},
{
"_id": "655f511e62f1958a50d4eec6",
"system": "Acos skjema",
"engine": "azf-acos-interact 1.9.1",
"createdTimestamp": "2023-11-23T13:18:22.648Z",
"county": "VFK",
"company": "HRMU",
"department": "Mestring og utvikling",
"description": "Arkivering av henvendelse til mobbeombud. Skal opprettes en ny sak pr skjema",
"projectId": "ingen prosjekttilknytning",
"externalId": "1287567",
"type": "Henvendelse til mobbeombud"
}
]
```

### Query params
#### count
?count=true only returns the count of returned documents

Example request
`GET https://{statsurl}/api/Stats/Acos%20skjema?count=true`

Example response
`156`

#### filter
?filter={filter} only returns documents that match the filter

Supported logical operators:
- and
- or
- nor

Supported comparison operators:
- eq
- gt
- gte
- lt
- lte
- ne
(Not implemented until we need it: in, nin)

**When using several logical operators on one level - you must use parenthesis**

Comparisons must be on the format

Examples
- `GET https://{statsurl}/api/Stats/Acos%20skjema?filter=createdTimestamp gt 2024-01-01 and createdTimestamp lt 2025-01-01`
- `GET https://{statsurl}/api/Stats/Acos%20skjema?filter=createdTimestamp gt 2024-01-01 and createdTimestamp lt 2025-01-01 and (type eq 'type 1' or type ne type2)`
- `GET https://{statsurl}/api/Stats/Acos%20skjema?filter=createdTimestamp gt 2024-01-01 and createdTimestamp lt 2025-01-01 and (type eq 'type 1' or type ne type2)&count=true` can be combined with query param "count"

The filter is parsed by the horrific function in [parse-query.js](./lib/parse-query.js)

#### select
?select=property1,property2 only returns the selected properties

if omitted, uses default select (in backend): `select=_id,system,engine,createdTimestamp,county,company,department,description,projectId,externalId,type`

Example
`GET https://{statsurl}/api/Stats/Acos%20skjema?select=createdTimestamp,type`

Returns
```json
[
{
"_id": "655f398962f1958a50d4eec5",
"createdTimestamp": "2023-11-23T11:37:45.025Z",
"type": "Søknad om tilrettelegging på fag, svenne eller kompetanseprøve"
},
{
"_id": "655f511e62f1958a50d4eec6",
"createdTimestamp": "2023-11-23T13:18:22.648Z",
"type": "Henvendelse til mobbeombud"
}
]
```

Example for getting all properties ('select=all') (cannot use '*' due to azure WAF)
`GET https://{statsurl}/api/Stats/Acos%20skjema?select=all`

Returns
```json
[
{
"_id": "655f398962f1958a50d4eec5",
"system": "Acos skjema",
"engine": "azf-acos-interact 1.9.1",
"createdTimestamp": "2023-11-23T11:37:45.025Z",
"county": "VFK",
"company": "Opplæring",
"department": "EKSAMEN",
"description": "Sender til elevmappe",
"projectId": "ingen prosjekttilknytning",
"externalId": "1287555",
"type": "Søknad om tilrettelegging på fag, svenne eller kompetanseprøve",
"documentNumber": "24/00001-12" // Additional property is also returned
},
{
"_id": "655f511e62f1958a50d4eec6",
"system": "Acos skjema",
"engine": "azf-acos-interact 1.9.1",
"createdTimestamp": "2023-11-23T13:18:22.648Z",
"county": "VFK",
"company": "HRMU",
"department": "Mestring og utvikling",
"description": "Arkivering av henvendelse til mobbeombud. Skal opprettes en ny sak pr skjema",
"projectId": "ingen prosjekttilknytning",
"externalId": "1287567",
"type": "Henvendelse til mobbeombud",
"documentNumber": "25/00098-3", // Additional property is also returned
"skole": "Tullball barneskole" // Additional property is also returned
}
]
```

## GET `/Grafana?start=\${__from}&end=\${__to}` or `/Grafana/all?start=\${__from}&end=\${__to}` or `/Grafana/{system}?start=\${__from}&end=\${__to}`

`__from` and `__to` is generated ISO DateTime UTC stamps from Grafana

### GET `/Grafana?start=\${__from}&end=\${__to}`

Returns Grafana table stat for all systems in a timeframe

Example request `GET https://{statusurl}/api/Grafana?start=2025-03-21T10:20:00Z&end=2025-03-21T10:25:00Z`

```json
[
{
"system": "MinElev",
"count": 42
},
{
"system": "Acos skjema",
"count": 13
}
]
```

### GET `/Grafana/all?start=${__from}&end=${__to}`

Returns Grafana stats for all systems in given timeframe

Request `GET https://{statusurl}/api/Grafana/all?start=2025-03-21T10:20:00Z&end=2025-03-21T10:25:00Z`

```json
[
{
"timestamp": "2024-08-26T09:31:00.000Z",
"MinElev": 418
},
{
"timestamp": "2024-08-26T09:31:00.000Z",
"VikarApp": 404
}
]
```

### GET `/Grafana/{system}?start=${__from}&end=${__to}`

Returns Grafana stats for {system} in given timeframe

Example request `GET https://{statusurl}/api/Grafana/MinElev?start=2025-03-21T10:20:00Z&end=2025-03-21T10:25:00Z`

```json
[
{
"timestamp": "2024-08-26T09:31:00.000Z",
"varsel-fag": 1
},
{
"timestamp": "2024-08-26T09:31:00.000Z",
"notat-notat": 1
}
]
```

# Development

- Clone the repo
- Make sure you have azure function core tools installed

```bash
npm i
```

- Create a local.settings.json file
```json
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"MONGO_STATISTICS_CONNECTION_STRING": "connectionstring",
"MONGO_DB_STATISTICS_DATABASE": "statistics db name",
"DEFAULT_COUNTY": "county name",
"BETTERSTACK_URL": "URL to betterstack",
"BETTERSTACK_TOKEN": "token for betterstack",
"NODE_ENV": "development"
}
}
```

### Test the function
```bash
func start
```