{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":729,"path":"api-reference/server-api-reference/ClientScopedAPI.md","title":"Client - Scoped, Global","bundle":null,"doc_type":null,"release":"australia","body":"\n# Client- Scoped, Global\n\nThe Client API provides methods to add data to the MetricBase database, to execute transforms on the MetricBase database, and to receive the results of the transforms.\n\nYou can use the Client class in both scoped and global server scripts. This class is part of the MetricBase application and must run within the `sn_clotho` namespace.\n\n**Parent Topic:**[Server API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/api-server.md)\n\n## Client - Client\\(\\)\n\nCreate an instance of the client class to access the MetricBase database.\n\n|Name|Type|Description|\n|----|----|-----------|\n|None| | |\n\n```\nvar client = new sn_clotho.Client();\n```\n\n## Client - accumulate\\(GlideRecord now\\_GR, String metric\\)\n\nAccumulates metric values at specified timestamp and saves the result to the database rather than overwriting the value.\n\nUse this method to handle metrics that can be summed for an accumulation, such as kilowatt-hours \\(kWhs\\) of electricity. Accumulate makes a call for each metric at the provided timestamp. For example, collected kilowatts for a heater, electric kettle, and washing machine would result in three calls to accumulate.\n\n<table id=\"table_zsz_5jd_1db\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nnow\\_GR\n\n</td><td>\n\nGlideRecord\n\n</td><td>\n\nName of the series GlideRecord from which to obtain the accumulated value.See also: [getSeries\\(\\)](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/ClientScopedAPI.md)\n\n</td></tr><tr><td>\n\ndefault\\_value\n\n</td><td>\n\nNumber\n\n</td><td>\n\nOptional. Default value for accumulation at a given timestamp. Used only during the first call to accumulate if a value is unavailable for a given timestamp. A use case could be accumulating a watts metric for a total\\_power. You want to accumulate watts for a router connected to an outlet without a power meter to measure it. If you know the consumption value and it is constant\\), you can use the constant value as a default value to accumulate total\\_power. For example, you would use 20 if the router is constantly plugged in and consumes 20 Watts.\n\n The timestamp value can be provided using the [DataBuilder](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DataBuilderScopedAPI.md) API.\n\n Default: 0\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to accumulate the `total_power` metric value in a record called `building_a`.\n\n```\nvar time = new GlideDateTime();\n\n// total_power is the name of a metric within building_a which stores accumulated values\n\nvar dataBuilder1 = new sn_clotho.DataBuilder(building_a, \"total_power\");\n// values to be accumulated\ndataBuilder1.add(time, 3.5);\nnew sn_clotho.Client().accumulate(dataBuilder1, 0);\n\nvar dataBuilder2 = new sn_clotho.DataBuilder(building_a, \"total_power\");\n// values to be accumulated\ndataBuilder2.add(time, 12);\nnew sn_clotho.Client().accumulate(dataBuilder2, 0);\n\nvar dataBuilder3 = new sn_clotho.DataBuilder(building_a, \"total_power\");\n// values to be accumulated\ndataBuilder3.add(time, 4.3);\nnew sn_clotho.Client().accumulate(dataBuilder3, 0);\n\n// As a result, client saves value of 3.5+12+4.3+0(default value) at the timestamp 'time'\n```\n\n## Client - deleteMetric\\(String tableName, String metricName\\)\n\nRemove a specified metric from a specified table in the MetricBase database\n\n**Note:** This method deletes data from the MetricBase database. There is no recovery mechanism.\n\n|Name|Type|Description|\n|----|----|-----------|\n|tableName|String|The name of the table whose specified metric is to be deleted.|\n|metricName|String|The name of the metric.|\n\n|Type|Description|\n|----|-----------|\n|None| |\n\nThe following example shows how to remove a metric from a table.\n\n```\nvar client = new sn_clotho.Client();\n\n// delete metric (Speed) from the Drones table\nclient.deleteMetric(\"mb_demo_drone\", \"mb_demo_mt_speed\");\n```\n\n## Client - deleteSeries\\(GlideRecord now\\_GR, String metric\\)\n\nRemove the data in the MetricBase database associated with the specified metric in the specified GlideRecord. Use this method for removing test data.\n\n**Note:** This method deletes data from the MetricBase database. There is no recovery mechanism.\n\n|Name|Type|Description|\n|----|----|-----------|\n|now\\_GR|GlideRecord|The records whose time series data for the specified metric is to be deleted.|\n|metric|String|The name of the metric.|\n\n|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar client = new sn_clotho.Client();\n//query drones of a specific model\nvar drones = new GlideRecord(\"mb_demo_drone\");\ndrones.addQuery(\"model\", \"Kingfisher Phantom\");\ndrones.query();\n\nclient.deleteSeries(drones, 'mb_demo_mt_speed');\n```\n\n## Client - getSeries\\(GlideRecord now\\_GR, String metricName, GlideDateTime lastUpdateBefore\\)\n\nGet all series from a specific dimension.\n\n|Name|Type|Description|\n|----|----|-----------|\n|now\\_GR|[GlideRecord](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideRecordScopedAPI.md)|The record from which to obtain the series.|\n|metric|String|The name of the metric.|\n|lastUpdateBefore|[GlideDateTime](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/c_GlideDateTimeScoped.md)|Optional. Date in the future representing the end of the period to be evaluated.|\n\n|Type|Description|\n|----|-----------|\n|Array|List of string sys\\_ids representing the series containing data for the specified metric. If the **lastUpdateBefore** parameter is provided, returns series that have no data more recent than the lastUpdateBefore date \\(non-inclusive\\).|\n\nThe following example shows how to get the complete list of speed values listed in the Drones \\[mb\\_demo\\_drone\\] table.\n\n```\nvar client = new sn_clotho.Client();\n\n// query subject records\nvar grDrone = new GlideRecord('mb_demo_drone');\ngrDrone.query();\n\nvar series = client.getSeries(grDrone,'mb_demo_mt_speed');\n```\n\n## Client - put\\(Object metricData\\)\n\nSaves metric data to the MetricBase database.\n\n<table id=\"table_vxt_wvz_31b\" class=\"parameters\"><thead><tr><th>\n\nName\n\n</th><th>\n\nType\n\n</th><th>\n\nDescription\n\n</th></tr></thead><tbody><tr><td>\n\nmetricData\n\n</td><td>\n\nObject\n\n</td><td>\n\nOne of the following:-   [DataBuilder](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/api-reference/server-api-reference/DataBuilderScopedAPI.md) object containing metric data.\n-   Array of DataBuilder objects containing metric data.\n\n</td></tr></tbody>\n</table>|Type|Description|\n|----|-----------|\n|void| |\n\n```\nvar time = new GlideDateTime();\n​\n// two different GlideRecord instances and metrics\nvar dataBuilder = new sn_clotho.DataBuilder(now_GR, 'cpu_percentage');\ndataBuilder.add(time, 0.6);\n​\nvar dataBuilder2 = new sn_clotho.DataBuilder(gr2, 'disk_free_percentage');\ndataBuilder2.add(time, 0.2);\n​\nnew sn_clotho.Client().put([dataBuilder,dataBuilder2]);\n```\n\n"}}