# All files / dist/indexer-management/resolvers indexer-status.js

12.66% Statements 10/79
23.68% Branches 9/38
3.03% Functions 1/33
14.29% Lines 10/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180

    3x

    3x
    6x

    3x
    3x
    3x
    3x
    3x

    3x

    3x

     "use strict";
    /* eslint-disable @typescript-eslint/ban-types */
    var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
        function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
        return new (P || (P = Promise))(function (resolve, reject) {
            function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
            function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
            function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
            step((generator = generator.apply(thisArg, _arguments || [])).next());
        });
    };
    var __importDefault = (this && this.__importDefault) || function (mod) {
        return (mod && mod.__esModule) ? mod : { "default": mod };
    };
    Object.defineProperty(exports, "__esModule", { value: true });
    const ngeohash_1 = __importDefault(require("ngeohash"));
    const graphql_tag_1 = __importDefault(require("graphql-tag"));
    const common_ts_1 = require("@graphprotocol/common-ts");
    const testURL = (url, tests) => __awaiter(void 0, void 0, void 0, function* () {
        const results = [];
        for (const test of tests) {
            const cmd = test.test(url);
            try {
                yield test.run(url);
                results.push({
                    test: cmd,
                    error: null,
                    possibleActions: [],
                });
            }
            catch (e) {
                results.push({
                    test: cmd,
                    error: e.message,
                    possibleActions: test.possibleActions(url),
                });
            }
        }
        return { url, tests: results, ok: !results.find((result) => result.error !== null) };
    });
    const URL_VALIDATION_TEST = {
        test: () => `URL validation`,
        run: (url) => __awaiter(void 0, void 0, void 0, function* () {
            new URL(url);
        }),
        possibleActions: (url) => [`Make sure ${url} is a valid URL`],
    };
    exports.default = {
        indexerRegistration: (_, { address, contracts }) => __awaiter(void 0, void 0, void 0, function* () {
            const registered = yield contracts.serviceRegistry.isRegistered(address);
            if (registered) {
                const service = yield contracts.serviceRegistry.services(address);
                return {
                    address,
                    url: service.url,
                    location: ngeohash_1.default.decode(service.geohash),
                    registered,
                    __typename: 'IndexerRegistration',
                };
            }
            else {
                return {
                    address,
                    url: null,
                    registered,
                    location: null,
                    __typename: 'IndexerRegistration',
                };
            }
        }),
        indexerDeployments: (_, { indexingStatusResolver }) => __awaiter(void 0, void 0, void 0, function* () {
            const result = yield indexingStatusResolver.indexingStatus([]);
            return result.map((status) => (Object.assign(Object.assign({}, status), { subgraphDeployment: status.subgraphDeployment.ipfsHash })));
        }),
        indexerAllocations: (_, { address, networkSubgraph }) => __awaiter(void 0, void 0, void 0, function* () {
            const result = yield networkSubgraph.query(graphql_tag_1.default `
            query allocations($indexer: String!) {
              indexer(id: $indexer) {
                allocations(where: { status: Active }, orderDirection: desc) {
                  id
                  allocatedTokens
                  createdAtBlockHash
                  createdAtEpoch
                  closedAtEpoch
                  subgraphDeployment {
                    id
                    stakedTokens
                    signalAmount
                  }
                }
              }
            }
          `, { indexer: address.toLowerCase() });
            if (result.error) {
                throw new Error(`Falied to query allocations: ${result.error}`);
            }
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            return result.data.indexer.allocations.map((allocation) => (Object.assign(Object.assign({}, allocation), { subgraphDeployment: new common_ts_1.SubgraphDeploymentID(allocation.subgraphDeployment.id)
                    .ipfsHash, signalAmount: allocation.subgraphDeployment.signalAmount, stakedTokens: allocation.subgraphDeployment.stakedTokens })));
        }),
        indexerEndpoints: (_, { address, contracts, logger }) => __awaiter(void 0, void 0, void 0, function* () {
            const endpoints = {
                service: {
                    url: null,
                    healthy: false,
                    tests: [],
                },
                status: {
                    url: null,
                    healthy: false,
                    tests: [],
                },
            };
            try {
                const service = yield contracts.serviceRegistry.services(address);
                if (service) {
                    {
                        const { url, tests, ok } = yield testURL(service.url, [
                            URL_VALIDATION_TEST,
                            {
                                test: (url) => `http get ${url}`,
                                run: (url) => __awaiter(void 0, void 0, void 0, function* () {
                                    const response = yield fetch(url);
                                    if (!response.ok) {
                                        throw new Error(`Returned status ${response.status}: ${response.body ? response.body.toString() : 'No data returned'}`);
                                    }
                                }),
                                possibleActions: (url) => [
                                    `Make sure ${url} can be resolved and reached from this machine`,
                                    `Make sure the port of ${url} is set up correctly`,
                                    `Make sure the test command returns an HTTP status code < 400`,
                                ],
                            },
                        ]);
                        endpoints.service.url = url;
                        endpoints.service.healthy = ok;
                        endpoints.service.tests = tests;
                    }
                    {
                        const statusURL = endpoints.service.url.endsWith('/')
                            ? endpoints.service.url.substring(0, endpoints.service.url.length - 1) +
                                '/status'
                            : endpoints.service.url + '/status';
                        const { url, tests, ok } = yield testURL(statusURL, [
                            URL_VALIDATION_TEST,
                            {
                                test: (url) => `http post ${url} query="{ indexingStatuses { subgraph } }"`,
                                run: (url) => __awaiter(void 0, void 0, void 0, function* () {
                                    const response = yield fetch(url, {
                                        method: 'POST',
                                        headers: { 'content-type': 'application/json' },
                                        body: JSON.stringify({ query: '{ indexingStatuses { subgraph } }' }),
                                    });
                                    if (!response.ok) {
                                        throw new Error(`Returned status ${response.status}: ${response.body ? response.body.toString() : 'No data returned'}`);
                                    }
                                }),
                                possibleActions: (url) => [
                                    `Make sure ${url} can be reached from this machine`,
                                    `Make sure the port of ${url} is set up correctly`,
                                    `Make sure ${url} is the /status endpoint of indexer-service`,
                                    `Make sure the test command returns an HTTP status code < 400`,
                                    `Make sure the test command returns a valid GraphQL response`,
                                ],
                            },
                        ]);
                        endpoints.status.url = url;
                        endpoints.status.healthy = ok;
                        endpoints.status.tests = tests;
                    }
                }
            }
            catch (err) {
                // Return empty endpoints
                logger === null || logger === void 0 ? void 0 : logger.warn(`Failed to detect service endpoints`, { err });
            }
            return endpoints;
        }),
    };
    //# sourceMappingURL=indexer-status.js.map