Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 55A51200C28 for ; Mon, 13 Mar 2017 20:33:21 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 54570160B8B; Mon, 13 Mar 2017 19:33:21 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 76A3C160B5D for ; Mon, 13 Mar 2017 20:33:20 +0100 (CET) Received: (qmail 15343 invoked by uid 500); 13 Mar 2017 19:33:18 -0000 Mailing-List: contact dev-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list dev@couchdb.apache.org Received: (qmail 15016 invoked by uid 99); 13 Mar 2017 19:33:18 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Mar 2017 19:33:18 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 77600DF9F1; Mon, 13 Mar 2017 19:33:18 +0000 (UTC) From: millayr To: dev@couchdb.apache.org Reply-To: dev@couchdb.apache.org References: In-Reply-To: Subject: [GitHub] couchdb-fauxton pull request #864: Update replication to work with scheduler... Content-Type: text/plain Message-Id: <20170313193318.77600DF9F1@git1-us-west.apache.org> Date: Mon, 13 Mar 2017 19:33:18 +0000 (UTC) archived-at: Mon, 13 Mar 2017 19:33:21 -0000 Github user millayr commented on a diff in the pull request: https://github.com/apache/couchdb-fauxton/pull/864#discussion_r105711712 --- Diff: app/addons/replication/api.js --- @@ -228,38 +255,176 @@ export const parseReplicationDocs = (rows) => { status: doc._replication_state, errorMsg: doc._replication_state_reason ? doc._replication_state_reason : '', statusTime: new Date(doc._replication_state_time), - url: `#/database/_replicator/${app.utils.getSafeIdForDoc(doc._id)}`, + startTime: new Date(doc._replication_start_time), + url: `#/database/_replicator/${encodeURIComponent(doc._id)}`, raw: doc }; }); }; +export const convertState = (state) => { + if (state.toLowerCase() === 'error' || state.toLowerCase() === 'crashing') { + return 'retrying'; + } + + return state; +}; + +export const combineDocsAndScheduler = (docs, schedulerDocs) => { + return docs.map(doc => { + const schedule = schedulerDocs.find(s => s.doc_id === doc._id); + if (!schedule) { + return doc; + } + + doc.status = convertState(schedule.state); + if (schedule.start_time) { + doc.startTime = new Date(schedule.start_time); + } + + if (schedule.last_updated) { + doc.stateTime = new Date(schedule.last_updated); + } + + return doc; + }); +}; + export const fetchReplicationDocs = () => { - return $.ajax({ - type: 'GET', - url: '/_replicator/_all_docs?include_docs=true&limit=100', - contentType: 'application/json; charset=utf-8', - dataType: 'json', - }).then((res) => { - return parseReplicationDocs(res.rows.filter(row => row.id.indexOf("_design/_replicator") === -1)); + return supportNewApi() + .then(newApi => { + const docsPromise = fetch('/_replicator/_all_docs?include_docs=true&limit=100', { + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8', + } + }) + .then(res => res.json()) + .then((res) => { + if (res.error) { + return []; + } + + return parseReplicationDocs(res.rows.filter(row => row.id.indexOf("_design/_replicator") === -1)); + }); + + if (!newApi) { + return docsPromise; + } + const schedulerPromise = fetchSchedulerDocs(); + return FauxtonAPI.Promise.join(docsPromise, schedulerPromise, (docs, schedulerDocs) => { + return combineDocsAndScheduler(docs, schedulerDocs); + }) + .catch(() => { + return []; + }); + }); +}; + +export const fetchSchedulerDocs = () => { + return fetch('/_scheduler/docs?include_docs=true', { + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8', + } + }) + .then(res => res.json()) + .then((res) => { + if (res.error) { + return []; + } + + return res.docs; }); }; export const checkReplicationDocID = (docId) => { const promise = FauxtonAPI.Deferred(); - $.ajax({ - type: 'GET', - url: `/_replicator/${docId}`, - contentType: 'application/json; charset=utf-8', - dataType: 'json', - }).then(() => { - promise.resolve(true); - }, function (xhr) { - if (xhr.statusText === "Object Not Found") { + fetch(`/_replicator/${docId}`, { + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8' + }, + }).then(resp => { + if (resp.statusText === "Object Not Found") { promise.resolve(false); return; } promise.resolve(true); }); return promise; }; + +export const parseReplicateInfo = (resp) => { + return resp.jobs.filter(job => job.database === null).map(job => { + return { + _id: job.id, + source: getDocUrl(job.source.slice(0, job.source.length - 1)), + target: getDocUrl(job.target.slice(0, job.target.length - 1)), + startTime: new Date(job.start_time), + statusTime: new Date(job.last_updated), + //making an asumption here that the first element is the latest + status: convertState(job.history[0].type), + errorMsg: '', + selected: false, + continuous: /continuous/.test(job.id), + raw: job + }; + }); +}; + +export const fetchReplicateInfo = () => { + return supportNewApi() + .then(newApi => { + if (!newApi) { + return []; + } + + return fetch('/_scheduler/jobs', { + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8' + }, + }) + .then(resp => resp.json()) + .then(resp => { + return parseReplicateInfo(resp); + }); + }); +}; + +export const deleteReplicatesApi = (replicates) => { + const promises = replicates.map(replicate => { + const data = { + replication_id: replicate._id, + cancel: true + }; + + return fetch('/_replicate', { + method: 'POST', + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8', + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }) + .then(resp => resp.json()); + }); + + return FauxtonAPI.Promise.all(promises); +}; + +export const createReplicatorDB = () => { + return fetch('/_replicator', { + method: 'PUT', + credentials: 'include', + headers: { + 'Accept': 'application/json; charset=utf-8', + } + }) + .then(res => res.json()) + .then(() => { + return true; --- End diff -- This looks like it could just fail silently if the PUT fails. I'd recommend being a little more defensive here if it's not too difficult. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---